处方药审核需要微信比较严格,商品上架要审核的,做一个一键上下架功能方便微信审核。效果如下:
产景说明:如果是新客户没有操作过,新增一条改客户的全部处方药商品记录;如果是旧客户(操作过了)保留一份旧的处方药商品记录;通过判断后台操作的是否显隐藏,只用一个方法,你会如何处理呢?上代码
<?php
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2020/11/3
* Time: 13:52
*/
namespace app\modules\mch\models\prescription;
use app\models\Goods;
use app\models\Inventory;
use app\models\Patient;
use app\models\PrescriptionGoodsStatus;
use app\modules\mch\models\MchModel;
use app\models\User;
use Codeception\Module\Db;
use phpDocumentor\Reflection\DocBlock\Tags\Var_;
use yii\data\Pagination;
class OneKeyUpdateForm extends MchModel
{
public $store_id;
public $status;
public $id;
public function rules()
{
return [
[['store_id','status','id'], 'integer'],
[['status'], 'default', 'value' => 1],
];
}
public function edit()
{
$n =0 ;
if($this->id){
$model = PrescriptionGoodsStatus::findOne($this->id);
if($model->status != $this->status){
//不一致时再更新
$n = 1;
$ids = explode(',',$model->goods_ids);
}
}elseif (!$this->id && !$this->status) {
//没有任何记录并且隐藏新增一条记录
$ids = $this->getPrescriptionGoodsList();
$n = 1;
}
if($n){
//开启事务
$transaction = \Yii::$app->db->beginTransaction();
//新增记录
$model = new PrescriptionGoodsStatus();
$model->store_id = $this->store_id;
$model->goods_ids = implode(',',$ids);
$model->status = $this->status;
$model->add_time = time();
if(!$model->save()){
$transaction->rollBack();
return [
'code' => 1,
'msg' => '更新失败'
];
}
if(!empty($ids)){
$inventory_ids = Inventory::find()->where(['store_id'=>$this->store_id])->andWhere(['in','goods_id',$ids])->select('id')->asArray()->all();
array_column($inventory_ids,'id');
$res = Inventory::updateAll(['status'=>$this->status],['in','id',$inventory_ids]);
if(!$res){
$transaction->rollBack();
return [
'code' => 1,
'msg' => '更新失败'
];
}
}
$transaction->commit();
}
return [
'code' => 0,
'msg' => '更新成功'
];
}
//获取处方商品列表
public function getPrescriptionGoodsList(){
$goods_list = Goods::find()->where(['store_id'=>$this->store_id,'drug_type'=>1])->select('id')->asArray()->all();
return array_column($goods_list,'id');
}
}