初始化仓库
This commit is contained in:
103
admin/models/forms/drug/GranularForm.php
Normal file
103
admin/models/forms/drug/GranularForm.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace admin\models\forms\drug;
|
||||
|
||||
use common\core\BaseModel;
|
||||
use common\models\Drug;
|
||||
use common\models\DrugStoreDrug;
|
||||
use yii\db\Exception;
|
||||
|
||||
//颗粒配方form
|
||||
class GranularForm extends BaseModel
|
||||
{
|
||||
public $id;
|
||||
public $drug_name;
|
||||
public $drug_number;
|
||||
public $drug_alias;
|
||||
public $unit_id;
|
||||
|
||||
public $place;
|
||||
public $source;
|
||||
public $instruction;
|
||||
public $type;
|
||||
public $drugstore;
|
||||
|
||||
public $pinyin_simple;
|
||||
|
||||
public $is_otc;
|
||||
public $status;
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['id','type','is_otc'],'integer'],
|
||||
[['drug_name','drug_number','drug_alias','unit_id','pinyin_simple','status'],'required'],
|
||||
[['place','source','instruction','drugstore','pinyin_simple'],'string'],
|
||||
['type','default','value'=>'3'],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributeLabels()
|
||||
{
|
||||
return [
|
||||
'drug_name'=>'药名',
|
||||
'drug_number'=>'药品编号',
|
||||
'drug_alias'=>'药品别名',
|
||||
'unit_id'=>'单位',
|
||||
'pinyin_simple'=>'拼音首拼',
|
||||
'status'=>'状态 1草稿 2下架 3上架',
|
||||
];
|
||||
}
|
||||
public function save()
|
||||
{
|
||||
if (!$this->validate()){
|
||||
throw new Exception($this->getErrorMsg());
|
||||
}
|
||||
|
||||
$t = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
$drug=new Drug();
|
||||
$drug->attributes=$this->attributes;
|
||||
$drug->created_at=time();
|
||||
$drug->saveOrFail();
|
||||
|
||||
$t->commit();
|
||||
return ['新增成功'];
|
||||
}catch (\Exception $e){
|
||||
$t->rollBack();
|
||||
throw new Exception($e->getMessage());
|
||||
}
|
||||
}
|
||||
public function update()
|
||||
{
|
||||
$drug=Drug::find()->where([
|
||||
'id'=>$this->id,
|
||||
'type'=>3,
|
||||
])->one();
|
||||
if (!$drug) throw new Exception('该药品不存在');
|
||||
$drug->pinyin_simple=$this->pinyin_simple??$drug->pinyin_simple;
|
||||
$drug->status=$this->status??$drug->status;
|
||||
$drug->drug_name=$this->drug_name??$drug->drug_name;
|
||||
$drug->drug_number=$this->drug_number??$drug->drug_number;
|
||||
$drug->drug_alias=$this->drug_alias??$drug->drug_alias;
|
||||
$drug->unit_id=$this->unit_id??$drug->unit_id;
|
||||
$drug->place=$this->place??$drug->place;
|
||||
$drug->source=$this->source??$drug->source;
|
||||
$drug->instruction=$this->instruction??$drug->instruction;
|
||||
|
||||
$drug->saveOrFail();
|
||||
return ['编辑成功'];
|
||||
}
|
||||
|
||||
|
||||
public function info()
|
||||
{
|
||||
$drug= Drug::find()->where([
|
||||
'id'=>$this->id,
|
||||
'type'=>3,
|
||||
])->one();
|
||||
if (!$drug) throw new Exception('该药品不存在');
|
||||
return $drug;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user