DemandDeal.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\common\command;
  3. use app\common\service\AccountLogService;
  4. use app\common\service\AccountService;
  5. use app\common\service\EasyWeChatService;
  6. use app\common\service\OrderService;
  7. use app\common\service\SubscribeMessageService;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\Output;
  11. use app\common\service\DemandService;
  12. use think\Log;
  13. use think\Db;
  14. class DemandDeal extends Command
  15. {
  16. protected function configure()
  17. {
  18. $this->setName('demand:deal')->setDescription('bidding deal command!');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $output->writeln("bidding:deal:");
  23. $this->confirmReceiptTimeoutDeal($input, $output);
  24. $this->demandProfitUnfreezeDeal($input, $output);
  25. }
  26. /**
  27. * 确认收货超时处理
  28. * @param Input $input
  29. * @param Output $output
  30. * @return void
  31. */
  32. protected function confirmReceiptTimeoutDeal(Input $input, Output $output)
  33. {
  34. $confirmReceiptTimeoutDay = intval(config('site.confirm_receipt_timeout_day') ?? 7);
  35. $where = [
  36. 'status' => DemandService::STATUS_CONFIRM_RECEIPT,
  37. 'accept_status' => DemandService::$Common::SUCCESS,
  38. ];
  39. DemandService::where($where)->chunk(100, function ($demandList) use ($confirmReceiptTimeoutDay) {
  40. foreach ($demandList as $item) {
  41. try {
  42. Db::transaction(function () use ($item, $confirmReceiptTimeoutDay) {
  43. //如果七天未确认收货,则自动确认收货
  44. if (time() > strtotime("+$confirmReceiptTimeoutDay day", $item->accept_time)) {
  45. $item->status = DemandService::STATUS_FINISHED;
  46. $item->settle_status = DemandService::$Common::ONGOING;
  47. $saveRes = $item->save();
  48. if (!$saveRes) {
  49. exception('更新结算状态失败');
  50. }
  51. //订单状态改为已完成
  52. $upRes = OrderService::where(['parent_order_no' => $item->parent_order_no])->update(['order_status' => OrderService::ORDER_STATUS_FINISHED]);
  53. if (!$upRes) {
  54. exception('更新订单状态失败');
  55. }
  56. }
  57. });
  58. } catch (\Exception $e) {
  59. Log::info('确认收货超时处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE));
  60. }
  61. }
  62. });
  63. }
  64. /**
  65. * 订单收益解冻处理
  66. * @param Input $input
  67. * @param Output $output
  68. * @return void
  69. */
  70. protected function demandProfitUnfreezeDeal(Input $input, Output $output)
  71. {
  72. $where = [
  73. 'status' => DemandService::STATUS_FINISHED,
  74. 'settle_status' => DemandService::$Common::ONGOING,
  75. ];
  76. DemandService::where($where)->with(['orders', 'winBidding'])->chunk(100, function ($demandList) {
  77. foreach ($demandList as $item) {
  78. try {
  79. Db::transaction(function () use ($item) {
  80. //根据订单列表查找冻结金额日志,根据冻结日志解冻冻结金额到余额
  81. $userId = $item->winBidding->user_id;
  82. $settleTotalAmount = 0;
  83. foreach ($item->orders as $order) {
  84. $logWhere = [
  85. 'user_id' => $userId,
  86. 'account_type' => AccountLogService::ACCOUNT_TYPE_FREEZE,
  87. 'type' => AccountLogService::TYPE_FREEZE,
  88. 'inc_or_exp' => AccountLogService::INC_EXP_INCREASE,
  89. 'refer_id' => $order->id,
  90. 'status' => AccountLogService::$Common::STATUS_OFF,
  91. ];
  92. $log = AccountLogService::where($logWhere)->find();
  93. if ($log) {
  94. $amount = $log->amount;
  95. $settleTotalAmount += $amount;
  96. //写入余额日志
  97. $title = OrderService::ORDER_TYPE_MAP[$order->order_type] ?? '需求收益';
  98. $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '解冻-冻结金额扣除', AccountLogService::TYPE_UNFREEZE, 0, AccountLogService::INC_EXP_EXPEND, $order->id, $order->total_amount);
  99. $log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '解冻-余额增加', AccountLogService::TYPE_PROFIT, 0, AccountLogService::INC_EXP_INCREASE, $order->id, $order->total_amount);
  100. //减去冻结,加到余额里面
  101. AccountService::updateData($userId, $amount, -$amount, 0, 0, $log_res->id);
  102. //更新日志状态
  103. $log->status = AccountLogService::$Common::STATUS_ON;
  104. $saveRes = $log->save();
  105. if (!$saveRes) {
  106. exception('更新余额日志状态失败');
  107. }
  108. }
  109. }
  110. //更新结算状态
  111. $item->settle_status = DemandService::$Common::SUCCESS;
  112. $saveRes2 = $item->save();
  113. if (!$saveRes2) {
  114. exception('更新需求结算状态失败');
  115. }
  116. //发送结算完成通知
  117. $msgData = [
  118. 'values' => [
  119. $item->parent_order_no,
  120. $settleTotalAmount,
  121. SubscribeMessageService::MSG_MAP[SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE]['remark'],
  122. ]
  123. ];
  124. (new EasyWeChatService)->sendMsg($userId, SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE, $msgData);
  125. });
  126. } catch (\Exception $e) {
  127. Log::info('订单收益解冻处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE));
  128. }
  129. }
  130. });
  131. }
  132. }