CheckTrade.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\cxinv\command;
  4. use app\cxinv\model\Assoc;use app\cxinv\model\QrdInfo;use app\cxinv\model\Trade;use app\cxinv\model\TradePool;use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;use think\facade\Cache;
  9. class CheckTrade extends Command
  10. {
  11. protected function configure()
  12. {
  13. // 指令配置
  14. $this->setName('checktrade')
  15. ->setDescription('the checktrade command');
  16. }
  17. protected function execute(Input $input, Output $output)
  18. {
  19. if(Cache::get('checkTradePlus')==1)return;
  20. $list = $this->getTrade();
  21. while ($list->valid()){
  22. Cache::set('checkTradePlus',1,100);
  23. $item = $list->current();
  24. $this->checkTrade($item);
  25. $list->next();
  26. }
  27. }
  28. public function getTrade(){
  29. $list = Trade::where([['status','=',1],["poCode","<>",""],['is_del','=',0],['updatetime','<=',date('Y-m-d H:i:s')]])->cursor();
  30. foreach ($list as $item){
  31. yield $item;
  32. }
  33. }
  34. public function checkTrade($item){
  35. if($item['customerNo'] == ''||$item['companyNo'] == ''){
  36. echo "订单{$item['tradNo']}信息不全".PHP_EOL;
  37. return;
  38. }
  39. if($item['poCode']==''){
  40. echo "订单{$item['tradNo']}无PO订单号".PHP_EOL;
  41. return;
  42. }
  43. $qrd = QrdInfo::where([['customerNo','=',$item['customerNo']],['poCode','=',$item['poCode']],['companyNo','=',$item['companyNo']],['pay_status','=',1],['wpay_fee','=',$item['balance']]])->findOrEmpty();
  44. if($qrd->isEmpty()){
  45. return;
  46. }
  47. QrdInfo::startTrans();
  48. try{
  49. $qrd->pay_status = 3;
  50. $qrd->apay_fee = $item['balance'];
  51. $qrd->wpay_fee = 0;
  52. $result= $qrd->save();
  53. if($result==false) throw new \Exception("订单{$qrd->sequenceNo}更新失败");
  54. $tradup= Trade::where([['status','=',1],["is_del","=",0],["id","=",$item['id']]])->update(['status'=>3,"balance"=>0,"used_fee"=>$item['balance']]);
  55. if($tradup==false) throw new \Exception("交易流水{$item['tradNo']}更新失败");
  56. $this->makeAssoc($item,$qrd);
  57. QrdInfo::commit();
  58. }catch (\Exception $e){
  59. QrdInfo::rollback();
  60. echo $e->getMessage().PHP_EOL;
  61. return;
  62. }
  63. echo "交易流水{$item['tradNo']}关联顶动感{$qrd->sequenceNo}更新成功".PHP_EOL;
  64. }
  65. public function makeAssoc($item,$qrd){
  66. $TradeLog=[
  67. 'logNo'=>makeNo("TRC"),
  68. 'tradNo'=>$item['tradNo'],
  69. 'platform_type'=>$qrd['platform_type']??0,
  70. 'companyNo'=>$item['companyNo'],
  71. 'customerNo'=>$item['customerNo'],
  72. 'apply_id'=>0,
  73. 'apply_name'=>'system',
  74. 'trade_time'=>$item['trade_time'],
  75. 'total_fee'=>$item['balance'],
  76. 'status'=>2,
  77. 'addtime'=>date('Y-m-d H:i:s'),
  78. 'updatetime'=>date('Y-m-d H:i:s')
  79. ];
  80. $result= TradePool::create($TradeLog);
  81. if($result==false) throw new \Exception("交易流水关联记录{$TradeLog['logNo']}插入失败");
  82. $Assoc=[
  83. 'assocNo'=>makeNo('AS'),
  84. 'apply_id'=>0,
  85. 'apply_name'=>'system',
  86. 'type'=>2,
  87. 'orderCode'=>$qrd['sequenceNo'],
  88. 'customerNo'=>$qrd['customerNo'],
  89. 'viceCode'=>$TradeLog['logNo'],
  90. 'order_total'=>$qrd['totalPrice'],
  91. 'vice_total'=>$item['balance'],
  92. 'cancel_fee'=>$item['balance'],
  93. 'status'=>2,
  94. "assoc_time"=>date('Y-m-d H:i:s'),
  95. 'addtime'=>date('Y-m-d H:i:s'),
  96. 'updatetime'=>date('Y-m-d H:i:s')
  97. ];
  98. $as= Assoc::create($Assoc);
  99. if($as==false) throw new \Exception("订单关联记录{$Assoc['assocNo']}插入失败");
  100. }
  101. }