123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace app\cxinv\model;
- use app\user\model\Business;use app\user\model\TaxCategory;use think\facade\Log;use think\Model;
- class DataChange extends Base{
- protected $createTime="createTime";
- protected $schema = [
- 'id' =>'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);
- }
- }
|