'bigint',// 'code' =>'varchar',// 订单编号 'type' =>'tinyint',// 修改类型 1 类目修改 "companyNo"=>'varchar',// 业务公司编号 "goodNo"=>'varchar',//商品编号 'order_type' =>'tinyint',// 修改类型 1 销售单 2 采购单 'before' =>'text',// 元数据类型 'after' =>'text',// 修改后数据类型 'remark' =>'varchar',// 附件截图备注 'apply_id' =>'int',// 'apply_name' =>'varchar',// 'createTime' =>'datetime',// ]; public static $typeMap=[ 1=>'类目修改', ]; public static $orderTypeMap=[ 1=>'销售单', 2=>'采购单', ]; public function getAfterAttr($value){ return json_decode($value,true); } public function getBeforeAttr($value){ return json_decode($value,true); } public function setAfterAttr($value){ return json_encode($value,JSON_UNESCAPED_UNICODE); } public function setBeforeAttr($value){ return json_encode($value,JSON_UNESCAPED_UNICODE); } public function Good(){ return $this->hasOne("Good","spuCode","goodNo")->bind(["goodName"=>"good_name"]); } public function Business(){ return $this->hasOne(Business::class,"companyNo","companyNo")->bind(["companyName"=>"company"]); } public static function GetBefore($code,$type,$order_type){ switch ($type){ case 1: $info = OrderCategory::with(['good'])->where(['code'=>$code,'order_type'=>$order_type])->select()->visible(['spuCode','merge_code','short_name','cat_name','tax','goodName'])->toArray(); break; default: $info=[]; break; } return $info; } public static function diffContent(&$before,&$after,$type=1){ switch ($type){ case 1: self::CategoryDiff($before,$after); break; default: break; } } public static function CategoryDiff(&$before,&$after){ if (empty($before)) return; if (empty($after))return; $beforeSpuc=array_column($before,"spuCode"); foreach ($after as $k=>$v){ if (isset($beforeSpuc[$v['spuCode']])){ $key = array_search($v['spuCode'],$beforeSpuc); if(($before[$key ]['merge_code']==$v['merge_code']&& $before[$key]['tax']==$v['tax'])){ unset($after[$k]); unset($before[$key]); } } } } public static function onAfterInsert(Model $model) : void{ $type= $model['type']; $order_type= $model['order_type']; $mod = $order_type==1 ? new QrdInfo() : new CgdInfo(); $info = $mod->where("sequenceNo",$model['code'])->findOrEmpty(); switch ($type){ case 1: self::updateCategory($model->after,$model->code); if(!$info->isEmpty() && $info->is_comon==1){ self::updateComon($model->after,$model->code,$order_type); } break; default: break; } } public static function updateCategory($after,$code){ if (empty($after)) return; $data=[]; foreach ($after as $v){ $id = OrderCategory::where("code",$code)->where("spuCode",$v['spuCode'])->value("id",''); if($id=='') continue; $data[]=[ "id"=>$id, "spuCode"=>$v['spuCode'], "merge_code"=>$v['merge_code'], "short_name"=>$v['short_name'], "cat_name"=>$v['cat_name'], "cat_code"=>TaxCategory::where("merge_code",$v['merge_code'])->value("cat_code"), "tax"=>$v['tax'], ]; } Log::info(json_encode($data)); if(!empty($data))(new OrderCategory())->saveAll($data); } public static function updateComon($after,$code,$order_type){ if($order_type==1){ $where=["orderCode"=>$code]; $field="cgdNo"; }else{ $where=['cgdNo'=>$code]; $field='orderCode'; } $comonCode= ComonOrder::where($where)->value($field,""); if($comonCode=="")return; foreach ($after as $v){ $id = OrderCategory::where('code',$comonCode)->where('spuCode',$v['spuCode'])->value('id',''); if($id=='') continue; $data[]=[ 'id'=>$id, 'spuCode'=>$v['spuCode'], 'merge_code'=>$v['merge_code'], 'short_name'=>$v['short_name'], 'cat_name'=>$v['cat_name'], 'cat_code'=>TaxCategory::where('merge_code',$v['merge_code'])->value('cat_code'), 'tax'=>$v['tax'], ]; } if(!empty($data))(new OrderCategory())->saveAll($data); } }