Files
xk-api-yii/admin/controllers/drug/DrugController.php
2024-12-23 12:33:05 +08:00

473 lines
18 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace admin\controllers\drug;
use admin\components\BaseAdminController;
use admin\models\forms\drug\DrugForm;
use admin\models\forms\drug\ImportForm;
use common\enums\UserRoleEnum;
use common\helpers\FuncHelper;
use common\models\oldDb\Drug;
use common\models\oldDb\DrugUseFrequency;
use common\models\oldDb\DrugUseTime;
use common\models\oldDb\DrugUseType;
use common\models\oldDb\DrugUseWay;
use common\models\oldDb\WestUnit;
use common\services\ExportService;
use moonland\phpexcel\Excel;
use yii\db\Exception;
use yii\web\Response;
use yii\web\UploadedFile;
class DrugController extends BaseAdminController
{
/**
* @doc-name 药品类别列表
*/
public function actionTypeList()
{
return [
['key' => 0, 'value' => '中药'],
['key' => 1, 'value' => '西药'],
['key' => 2, 'value' => '颗粒配方'],
['key' => 3, 'value' => '中成药'],
['key' => 4, 'value' => '产品服务包'],
];
}
/**
* @doc-name 中药的煎熬方式
*/
public function actionUseWay()
{
return DrugUseWay::find()->all();
}
/**
* @doc-name 药使用时间
*/
public function actionUseTime()
{
return DrugUseTime::find()->all();
}
/**
* @doc-name 药的使用频率
*/
public function actionUseFrequency()
{
return DrugUseFrequency::find()->all();
}
/**
* @doc-name 药的使用方式
*/
public function actionUseType()
{
return DrugUseType::find()->all();
}
/**
* @doc-name 西药单位
*/
public function actionWestUnit()
{
return WestUnit::find()->all();
}
/**
* @doc-name 药品基本信息列表
* @doc-param int status 状态类型1中药2西药3颗粒配方5全部列表
* @doc-param string name 搜素药名、编号、条形码、厂家、中西药标签、拼音首拼、别名
* @doc-return mixed @List{id,drug_name-string-药名,drug_number-string-编号,drug_alias-string-别名,bar_code-string-条形码,guozi_no-string-国字准号,category_first-int-一级分类,category_second-int-二级分类,source-string-货源(厂家),function-string-功能主治,decotion-string-煎法,is_otc-int-是否处方药,usage-string-用法,specification-string-规格,instruction-string-说明书图片,image-string-药品图片,time-string-使用时间,use_type-string-使用方法,frequency-string-频率,unit-string-单位,small_info-string-简介,info-string-基础信息,content-string-详情信息,status-int-状态1草稿2下架3上架,is_shalving-int-状态1下架2上架,stock-int-库存store-int-门店,type-int-类别1中药2西药3颗粒配方4中成药} 药品列表
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionList()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
['status', 'required']
]);
if (!empty($get['start_time']) && !empty($get['end_time'])) {
$start_time = strtotime($get['start_time'] . ' ' . '00:00:00');
$end_time = strtotime($get['end_time'] . ' ' . '23:59:59');
}
$query = Drug::find()->alias('d')->orderBy(['id' => SORT_ASC]);
$user = \Yii::$app->user->identity;
if ($user->role == UserRoleEnum::STORE_ADMIN) { //门店
$query->joinWith(['drugStoreRelationes' => function ($drug) use ($user) {
$drug->alias('ds');
$drug->andWhere(['ds.store_id' => $user->store_id,'ds.is_forbid'=>0,'ds.status'=>2]);
}]);
}
switch ($get['status']) {
case 1:
$query->andWhere(['d.type' => 1]);
break;
case 2:
$query->andWhere(['d.type' => 2]);
break;
case 3:
$query->andWhere(['d.type' => 3]);
break;
case 4:
$query->andWhere(['d.type' => 4]);
break;
case 5:
$query->andWhere(['d.type' => [1, 2, 3, 4, 5]]);
break;
default:
throw new Exception('参数错误');
}
if (!empty($get['name'])) {
$query->andWhere([
'or',
['like', 'drug_name', $get['name']],
['like', 'drug_number', $get['name']],
['like', 'bar_code', $get['name']],
['like', 'source', $get['name'] ],
['like', 'pinyin_simple', $get['name'] ],
['like', 'drug_alias', $get['name'] ],
]);
}
if (!empty($start_time) && !empty($end_time)) {
if ($start_time==$end_time){//筛选当天
$time=FuncHelper::getDayBE($get['start_time']);
$end_time=$time;
}
$query->andWhere(['between', 'd.created_at', $start_time,$end_time]);
}
if (!empty($get['type'])) {
$query->andWhere(['type' => $get['type']]);
}
$this->field = [
Drug::class => [
'id', 'pinyin_simple', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second', 'small_info', 'info', 'content', 'usage', 'specification', 'image', 'status', 'drug_number', 'drug_alias', 'bar_code', 'guozi_no', 'place', 'function', 'instruction', 'decotion_id' => 'decotion', 'decotion' => 'drugUseWay.name', 'unit' => 'unit.name',
'store' => 'drugStoreRelationes.store_id',
'price' => 'drugStoreRelationes.price',
'buy_price' => 'drugStoreRelationes.buy_price',
'stock' => 'drugStoreRelationes.drugStoreDrug.stock',
'is_shalving' => 'drugStoreRelationes.status',
'type_id', 'frequency_id', 'unit_id', 'time_id',
'use_frequency' => 'drugUseFrequency.name', 'use_type' => 'drugUseType.name',
'use_time' => 'drugUseTime.name',
'is_forbid'=>'drugStoreRelationes.is_forbid',
],
];
return $this->create($query, $get);
}
/**
* @doc-name 基础商品库列表
* @doc-param string drug_name 药名 / optional
* @doc-param string drug_number 编号 / optional
* @doc-param string bar_code 条形码 / optional
* @doc-param int category_first 一级分类 / optional
* @doc-param int category_second 二级分类 / optional
* @doc-param string source 厂家 / optional
* @doc-param int type 中西药标签 / optional
* @doc-return mixed @List{id,drug_name-string-药名,drug_number-string-编号,drug_alias-string-别名,bar_code-string-条形码,guozi_no-string-国字准号,category_first-int-一级分类,category_second-int-二级分类,source-string-货源(厂家),function-string-功能主治,decotion-string-煎法,is_otc-int-是否处方药,usage-string-用法,specification-string-规格,instruction-string-说明书图片,image-string-药品图片,time-string-使用时间,use_type-string-使用方法,frequency-string-频率,unit-string-单位,small_info-string-简介,info-string-基础信息,content-string-详情信息,status-int-状态1草稿2下架3上架,type-int-类别1中药2西药3颗粒配方4中成药} 药品列表
* @doc-return mixed @Pagination{total-int-总数量,totalPage-int-总页数,pageSize-int-每页条数} 分页数据
*/
public function actionBaseGood()
{
$get = \Yii::$app->request->get();
$query = Drug::find()->alias('d');
if (!empty($get['drug_name'])) {
$query->andWhere(['like', 'drug_name', $get['drug_name']]);
}
if (!empty($get['drug_number'])) {
$query->andWhere(['like', 'drug_number', $get['drug_number']]);
}
if (!empty($get['bar_code'])) {
$query->andWhere(['like', 'bar_code', $get['bar_code']]);
}
if (!empty($get['category_first'])) {
$query->andWhere(['category_first' => $get['category_first']]);
}
if (!empty($get['category_second'])) {
$query->andWhere(['category_second' => $get['category_second']]);
}
if (!empty($get['source'])) {
$query->andWhere(['like', 'source', $get['source']]);
}
if (!empty($get['type'])) {
$query->andWhere(['type' => $get['type']]);
}
$this->field = [
Drug::class => [
'id', 'pinyin_simple', 'drug_name', 'source', 'type', 'is_otc', 'category_first', 'category_second', 'small_info', 'info', 'content', 'usage', 'specification', 'image', 'status', 'drug_number', 'drug_alias', 'bar_code', 'guozi_no', 'place', 'function', 'instruction', 'decotion_id' => 'decotion', 'decotion' => 'drugUseWay.name', 'unit' => 'unit.name', 'time_id', 'type_id', 'frequency_id', 'unit_id',
'use_frequency' => 'drugUseFrequency.name', 'use_type' => 'drugUseType.name',
'use_time' => 'drugUseTime.name',
],
];
return $this->create($query, $get);
}
/**
* @doc-name 导出基础药品
*/
public function actionExportDrug()
{
\Yii::$app->response->format = Response::FORMAT_RAW;
$params=\Yii::$app->request->get();
$parameter['header']=['ID','药名','拼音首拼','货源(厂家)','类型','是否处方药','详情信息','用法','功能主治','规格','药品编号','条形码','国字准号','药品别名','状态'];
$parameter['data']=Drug::inventory($params);
ExportService::ExportByCors($parameter);
}
/**
* @doc-name 新增西药/或中成药
* @doc-param string drug_name 药名
* @doc-param string pinyin_simple 拼音首拼
* @doc-param int is_otc 是否处方药
* @doc-param string drug_number 编号
* @doc-param string bar_code 条形码
* @doc-param string specification 规格
* @doc-param string guozi_no 国字准号
* @doc-param string function 功能主治
* @doc-param string source 厂家
* @doc-param string instruction['1','2'] 说明书图片
* @doc-param json image['1','2'] 药品图片
* @doc-param int type 分类类型1中药2西药3颗粒配方4中成药
* @doc-param int status 状态1草稿2下架3上架
* @doc-param int unit_id 单位 / optional
* @doc-param int frequency_id 频次
* @doc-param int type_id 方法 / optional
* @doc-param int time_id 时间 / optional
* @doc-param string drug_alias 别名 / optional
* @doc-param string usage 用法
*/
public function actionSave()
{
$post = \Yii::$app->request->post();
$DrugForm = new DrugForm();
$DrugForm->attributes = $post;
return $DrugForm->save();
}
/**
* @doc-name 基础药品上下架
* @doc-param int id 药品ID
* @doc-param int type 类型类型1中药2西药3颗粒配方
*/
public function actionIsSale()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
[['id', 'type', 'status'], 'required']
]);
$DrugForm = new DrugForm();
$DrugForm->attributes = $get;
return $DrugForm->IsSale();
}
/**
* @doc-name 基础药品-草稿
* @doc-param int id 药品ID
* @doc-param int type 类型类型1中药2西药3颗粒配方
*/
public function actionDraft()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
[['id', 'type'], 'required']
]);
$DrugForm = new DrugForm();
$DrugForm->attributes = $get;
return $DrugForm->Draft();
}
/**
* @doc-name 修改西药/或中成药
* @doc-param int id 药品ID
* @doc-param string drug_name 药名
* @doc-param int is_otc 是否处方药
* @doc-param string drug_number 编号
* @doc-param string bar_code 条形码
* @doc-param string specification 规格
* @doc-param string guozi_no 国字准号
* @doc-param string function 功能主治
* @doc-param string source 厂家
* @doc-param string place 产地 / optional
* @doc-param int category_first 一级分类
* @doc-param int category_second 二级分类
* @doc-param string instruction['1','2'] 说明书图片
* @doc-param json image['1','2'] 药品图片
* @doc-param int type 分类类型1中药2西药3颗粒配方4中成药
* @doc-param int unit_id 单位
* @doc-param int frequency_id 频次
* @doc-param int type_id 方法
* @doc-param int time_id 时间
* @doc-param string drug_alias 别名 / optional
* @doc-param string usage 用法
*/
public function actionUpdate()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post, [
['id', 'required']
]);
$DrugForm = new DrugForm();
$DrugForm->attributes = $post;
return $DrugForm->update();
}
/**
* @doc-name 查看详情(西药)
* @doc-param int id 药品ID
* @doc-param int type 类型1中药2西药3颗粒配方4中成药
*/
public function actionInfo()
{
$get = \Yii::$app->request->get();
$this->requestValidate($get, [
[['id', 'type'], 'required']
]);
$DrugForm = new DrugForm();
$DrugForm->attributes = $get;
return $DrugForm->info();
}
/**
* @doc-name 导入西药
* @doc-param file file 文件
*/
public function actionImport()
{
$request = \Yii::$app->request;
if ($request->isPost) {
$post = \Yii::$app->request->post();
//设置最大执行时间
ini_set("max_execution_time", "360");
$file = UploadedFile::getInstanceByName('file');
if ($file->size > 5 * 1024 * 1024) {
return ['excel文件不能超过5M!'];
}
// $relative_path = 'excel/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
// $absolute_path = rtrim(\Yii::$app->params['upload_dir'], '/') . '/' . $relative_path;
// if (!file_exists($absolute_path)) {
// try {
// FileHelper::createDirectory($absolute_path, 0777, true);
// } catch (\Exception $e) {
// return ['errno' => 0, 'msg' => '目录创建失败,' . $e->getMessage()];
// }
// }
//文件名
$filename = date('His') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
//保存文件
$file->saveAs($filename);
// $fullFileName = $absolute_path . $filename;
$data = Excel::import($filename, [
'setFirstRecordAsKeys' => true,
'getOnlySheet' => 'Sheet1',
]);
$ImportForm = new ImportForm();
$ImportForm->attributes = $post;
return $ImportForm->WestImport($data);
} else {
throw new Exception('请求方式错误');
}
}
/**
* @doc-name 导入中成药
* @doc-param file file 文件
*/
public function actionImportZhongcheng()
{
$request = \Yii::$app->request;
if ($request->isPost) {
$post = \Yii::$app->request->post();
//设置最大执行时间
ini_set("max_execution_time", "360");
$file = UploadedFile::getInstanceByName('file');
if ($file->size > 5 * 1024 * 1024) {
return ['excel文件不能超过5M!'];
}
//文件名
$filename = date('His') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
//保存文件
$file->saveAs($filename);
// $fullFileName = $absolute_path . $filename;
$data = Excel::import($filename, [
'setFirstRecordAsKeys' => true,
'getOnlySheet' => 'Sheet1',
]);
$ImportForm = new ImportForm();
$ImportForm->attributes = $post;
return $ImportForm->ImportZhongcheng($data);
} else {
throw new Exception('请求方式错误');
}
}
/**
* @doc-name 单位列表
*/
public function actionUnit()
{
return WestUnit::find()->all();
}
/**
* @doc-name 仓库导入药品
*/
public function actionExportReal()
{
try {
$request = \Yii::$app->request;
if ($request->isPost) {
$post = \Yii::$app->request->post();
//设置最大执行时间
ini_set("max_execution_time", "360");
$file = UploadedFile::getInstanceByName('file');
if ($file->size > 5 * 1024 * 1024) {
return ['excel文件不能超过5M!'];
}
//文件名
$filename = date('His') . md5($file->getBaseName()) . mt_rand(1000, 9999) . '.' . $file->getExtension();
//保存文件
$file->saveAs($filename);
// $fullFileName = $absolute_path . $filename;
$data = Excel::import($filename, [
'setFirstRecordAsKeys' => true,
'getOnlySheet' => 'Sheet1',
]);
$ImportForm = new ImportForm();
$ImportForm->attributes = $post;
return $ImportForm->ExportRealDrug($data);
} else {
throw new Exception('请求方式错误');
}
} catch (\Exception $e) {
return ['error' => $e->getMessage()];
} finally {
if (file_exists($filename)) {
unlink($filename);
}
}
}
}