'bigint',// 'tradNo' =>'varchar',//资金编号 'logNo' =>'varchar',//资金交易编号 'platform_type' =>'tinyint',//0无属性 1ToB 2ToC 'customerNo' =>'varchar',//客户公司 'companyNo' =>'varchar',//业务公司编号 'trade_time' =>'datetime',//交易时间 'total_fee' =>'decimal',//总额 'remark' =>'varchar',//备注 'tag_type' =>'int',//资金类型 0 认领自己 1平台衍生资金认领 'relaNo' =>'varchar',//关联平台衍生资金编号 'is_del' =>'int',// 'status' =>'varchar',//状态 1待审核2审核通过3 驳回 'apply_name' =>'varchar',// 'apply_id' =>'int',// 'addtime' =>'datetime',// 'updatetime' =>'datetime',// ]; protected $createTime='addtime'; protected $updateTime='updatetime'; public function Trade(){ return $this->belongsTo(Trade::class,"tradNo","tradNo") ->bind(["trade_out","trade_in","trade_bank","companyName","pcompanyNo"=>"companyNo","customerName","pcustomerNo"=>"customerNo","ptotal_fee"=>"total_fee","trade_type","balance","used_fee","poCode","NdCode",'pcCode']); } public function Lib(){ return $this->belongsTo(TradeLib::class,"tradNo","tradNo")->bind(['palt_po','skuCode','goodName','goodNum','platPrice','plat_total','tax','cat_code','cat_name','comPrice','com_total']); } public static function onAfterInsert(Model $model) : void{ if($model->status==2 && $model->tag_type==0){ self::makeChangeOrder($model->logNo,2); } } public static function onAfterUpdate(Model $model) : void{ $changedData = $model->getChangedData(); if(isset($changedData['status'])&& $model->tag_type==0){ self::makeChangeOrder($model->logNo,$changedData['status']); } } //修改衍生数据 public static function makeChangeOrder($logNo,$status){ $info = self::with(["Trade","Lib"])->where("logNo",$logNo)->findOrEmpty(); Log::info("修改衍生数据".json_encode($info)); if($info->isEmpty()) throw new \Exception("数据不存在"); if($info->trade_type==0) return; switch ($status){ case 2: self::createTrade($info); break; case 4: self::returnTrade($info); break; default: throw new \Exception("状态错误"); } } public static function createTrade($info){ $assoc = Assoc::alias('a') ->leftJoin('qrd_info b','a.orderCode = b.sequenceNo') ->where('a.viceCode',$info->logNo) ->where(['a.status'=>2,'a.is_del'=>0,"a.type"=>2]) ->where('b.cxCode','<>','') ->field('a.cancel_fee,a.order_total,b.sequenceNo,b.cxCode') ->findOrEmpty(); if($assoc->isEmpty()) throw new \Exception('数据不存在'); $qrdArr = QrdInfo::where(['cxCode'=>$assoc->cxCode,'is_comon'=>0,'is_del'=>0]) ->field('totalPrice,wpay_fee,apay_fee,platform_type,companyNo,customerNo,sequenceNo,id')->findOrEmpty(); if($qrdArr->isEmpty()) throw new \Exception($assoc->cxCode.'对应关联销售单数据不存在'); Log::info("订单数据".json_encode($qrdArr,JSON_UNESCAPED_UNICODE)); $orderLv= bcdiv($assoc->cancel_fee,$assoc->order_total,8); $trade_fee=round(bcmul($qrdArr->totalPrice,$orderLv,8),2); $temp=['logNo' => makeNo('TRC'), 'tradNo' => $info->tradNo, 'platform_type' => $qrdArr->platform_type, 'companyNo' => $qrdArr->companyNo, 'customerNo' => $qrdArr->customerNo, 'apply_id' =>0, 'apply_name' => 'system', 'trade_time' => $info->trade_time, 'total_fee' => $trade_fee, "tag_type"=>1,//1平台衍生 'relaNo' => $info->logNo, 'status' => 2,//2审核通过 ]; $Asoc=[ 'assocNo' =>makeNo('AS'), 'apply_id' =>0, 'apply_name' =>'system', 'type' => 2, 'orderCode' => $qrdArr->sequenceNo, 'customerNo' => $qrdArr->customerNo, 'viceCode' => $temp['logNo'], 'order_total' =>$qrdArr->totalPrice, 'vice_total' => $info->plat_total, 'cancel_fee' =>$trade_fee, 'status' => 2,//认领通过 ]; if($qrdArr->wpay_fee<$trade_fee) throw new \Exception("{$qrdArr->sequenceNo}待支付金额不足,主单号:{$assoc->cxCode}"); $qrdArr->wpay_fee-= $trade_fee; $qrdArr->apay_fee+= $trade_fee; $qrdArr->pay_status= ($qrdArr->wpay_fee==0 &&$qrdArr->pay_fee==0) ?3:2; $qrdArr->status=1; $up=$qrdArr->save(); if(!$up) throw new \Exception($qrdArr->sequenceNo.'订单数据修改失败,主单号:'.$assoc->cxCode); $asse= Assoc::create($Asoc); if($asse->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'关联数据创建失败,主单号:'.$assoc->cxCode); $trade= self::create($temp); if($trade->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'资金数据创建失败,主单号:'.$assoc->cxCode); $info->relaNo=$trade->logNo; $info->save(); Log::info('订单数据'.self::getLastSql()); } public static function returnTrade($info){ $return = TradeReturn::where(["logNo"=>$info->logNo,"status"=>1])->findOrEmpty(); Log::info('订单数据'.json_encode($return->toArray(),JSON_UNESCAPED_UNICODE)); if($return->isEmpty()) throw new \Exception("{$info->logNo}退款记录未找到"); $trade = self::where(["relaNo"=>$info->logNo,"tag_type"=>1,"status"=>2,"tradNo"=>$info->tradNo,"is_del"=>0])->findOrEmpty(); Log::info('订单数据'.json_encode($trade->toArray(),JSON_UNESCAPED_UNICODE)); if($trade->isEmpty()) throw new \Exception("未找到关联资金数据"); $assoc = Assoc::alias('a') ->leftJoin('qrd_info b','a.orderCode = b.sequenceNo') ->where('a.viceCode',$trade->logNo) ->where(['a.status'=>2,'a.is_del'=>0,'a.type'=>2]) ->where('b.cxCode','<>','') ->field('a.cancel_fee,a.orderCode,a.viceCode,a.id,a.status,b.cxCode') ->findOrEmpty(); if($assoc->isEmpty()) throw new \Exception($trade->logNo.'关联订单数据不存在'); $qrdArr = QrdInfo::where(['sequenceNo'=>$assoc->orderCode,'is_comon'=>0,'is_del'=>0]) ->field('totalPrice,wpay_fee,pay_fee,apay_fee,inv_status,platform_type,companyNo,customerNo,sequenceNo,id')->findOrEmpty(); if($qrdArr->isEmpty()) throw new \Exception($qrdArr->sequenceNo.'订单数据不存在,主单号:'.$assoc->cxCode); Assoc::CheckTrad( $assoc->viceCode,4); $returns=[ 'returnCode'=>makeNo('RTA'), 'logNo'=>$trade->logNo, 'companyNo'=>$trade->companyNo, 'tradNo'=>$trade->tradNo, 'return_img'=>'', 'type'=>2, 'apply_id'=>'0', 'apply_name'=>'system', 'return_reason'=>$return->return_reason, 'remark'=>$return->remark, 'status'=>1]; $trade->status=4; $up=$trade->save(); if(!$up) throw new \Exception('资金数据修改失败'); $TradeReturn= TradeReturn::create($returns); if($TradeReturn->isEmpty()) throw new \Exception('退款记录创建失败'); } }