DataChange.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\cxinv\model;
  3. use app\user\model\Business;use app\user\model\TaxCategory;use think\facade\Log;use think\Model;
  4. class DataChange extends Base{
  5. protected $createTime="createTime";
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'code' =>'varchar',// 订单编号
  9. 'type' =>'tinyint',// 修改类型 1 类目修改
  10. "companyNo"=>'varchar',// 业务公司编号
  11. "goodNo"=>'varchar',//商品编号
  12. 'order_type' =>'tinyint',// 修改类型 1 销售单 2 采购单
  13. 'before' =>'text',// 元数据类型
  14. 'after' =>'text',// 修改后数据类型
  15. 'remark' =>'varchar',// 附件截图备注
  16. 'apply_id' =>'int',//
  17. 'apply_name' =>'varchar',//
  18. 'createTime' =>'datetime',//
  19. ];
  20. public static $typeMap=[
  21. 1=>'类目修改',
  22. ];
  23. public static $orderTypeMap=[
  24. 1=>'销售单',
  25. 2=>'采购单',
  26. ];
  27. public function getAfterAttr($value){
  28. return json_decode($value,true);
  29. }
  30. public function getBeforeAttr($value){
  31. return json_decode($value,true);
  32. }
  33. public function setAfterAttr($value){
  34. return json_encode($value,JSON_UNESCAPED_UNICODE);
  35. }
  36. public function setBeforeAttr($value){
  37. return json_encode($value,JSON_UNESCAPED_UNICODE);
  38. }
  39. public function Good(){
  40. return $this->hasOne("Good","spuCode","goodNo")->bind(["goodName"=>"good_name"]);
  41. }
  42. public function Business(){
  43. return $this->hasOne(Business::class,"companyNo","companyNo")->bind(["companyName"=>"company"]);
  44. }
  45. public static function GetBefore($code,$type,$order_type){
  46. switch ($type){
  47. case 1:
  48. $info = OrderCategory::with(['good'])->where(['code'=>$code,'order_type'=>$order_type])->select()->visible(['spuCode','merge_code','short_name','cat_name','tax','goodName'])->toArray();
  49. break;
  50. default:
  51. $info=[];
  52. break;
  53. }
  54. return $info;
  55. }
  56. public static function diffContent(&$before,&$after,$type=1){
  57. switch ($type){
  58. case 1:
  59. self::CategoryDiff($before,$after);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. public static function CategoryDiff(&$before,&$after){
  66. if (empty($before)) return;
  67. if (empty($after))return;
  68. $beforeSpuc=array_column($before,"spuCode");
  69. foreach ($after as $k=>$v){
  70. if (isset($beforeSpuc[$v['spuCode']])){
  71. $key = array_search($v['spuCode'],$beforeSpuc);
  72. if(($before[$key ]['merge_code']==$v['merge_code']&& $before[$key]['tax']==$v['tax'])){
  73. unset($after[$k]);
  74. unset($before[$key]);
  75. }
  76. }
  77. }
  78. }
  79. public static function onAfterInsert(Model $model) : void{
  80. $type= $model['type'];
  81. $order_type= $model['order_type'];
  82. $mod = $order_type==1 ? new QrdInfo() : new CgdInfo();
  83. $info = $mod->where("sequenceNo",$model['code'])->findOrEmpty();
  84. switch ($type){
  85. case 1:
  86. self::updateCategory($model->after,$model->code);
  87. if(!$info->isEmpty() && $info->is_comon==1){
  88. self::updateComon($model->after,$model->code,$order_type);
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. public static function updateCategory($after,$code){
  96. if (empty($after)) return;
  97. $data=[];
  98. foreach ($after as $v){
  99. $id = OrderCategory::where("code",$code)->where("spuCode",$v['spuCode'])->value("id",'');
  100. if($id=='') continue;
  101. $data[]=[
  102. "id"=>$id,
  103. "spuCode"=>$v['spuCode'],
  104. "merge_code"=>$v['merge_code'],
  105. "short_name"=>$v['short_name'],
  106. "cat_name"=>$v['cat_name'],
  107. "cat_code"=>TaxCategory::where("merge_code",$v['merge_code'])->value("cat_code"),
  108. "tax"=>$v['tax'],
  109. ];
  110. }
  111. Log::info(json_encode($data));
  112. if(!empty($data))(new OrderCategory())->saveAll($data);
  113. }
  114. public static function updateComon($after,$code,$order_type){
  115. if($order_type==1){
  116. $where=["orderCode"=>$code];
  117. $field="cgdNo";
  118. }else{
  119. $where=['cgdNo'=>$code];
  120. $field='orderCode';
  121. }
  122. $comonCode= ComonOrder::where($where)->value($field,"");
  123. if($comonCode=="")return;
  124. foreach ($after as $v){
  125. $id = OrderCategory::where('code',$comonCode)->where('spuCode',$v['spuCode'])->value('id','');
  126. if($id=='') continue;
  127. $data[]=[
  128. 'id'=>$id,
  129. 'spuCode'=>$v['spuCode'],
  130. 'merge_code'=>$v['merge_code'],
  131. 'short_name'=>$v['short_name'],
  132. 'cat_name'=>$v['cat_name'],
  133. 'cat_code'=>TaxCategory::where('merge_code',$v['merge_code'])->value('cat_code'),
  134. 'tax'=>$v['tax'],
  135. ];
  136. }
  137. if(!empty($data))(new OrderCategory())->saveAll($data);
  138. }
  139. }