OrderTake.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace app\command;
  3. use app\api\model\order\Order;
  4. use app\api\model\system\Message;
  5. use app\api\service\ThirdPayService;
  6. use app\api\service\WalletService;
  7. use redis\RedLock;
  8. use think\console\Command;
  9. use think\console\Input;
  10. use think\console\Output;
  11. use think\Db;
  12. use think\Exception;
  13. // 接单超时
  14. class OrderTake extends Command
  15. {
  16. // 配置定时器的信息
  17. protected function configure()
  18. {
  19. $this->setName('OrderTake')
  20. ->setDescription('OrderTake start');
  21. }
  22. /**
  23. * @param Input $input
  24. * @param Output $output
  25. * @return int|void|null
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. * @throws \think\Exception
  30. */
  31. protected function execute(Input $input, Output $output)
  32. {
  33. \think\Log::custom_log("接单超时定时任务", "-------------- start --------------", "order_take");
  34. $now_time = time();
  35. // 订单接单超时时间(分钟)
  36. $max_timeout_limit = (config("site.order_take_of_timeout") ?? 5) * 60;
  37. $orderModel = new Order();
  38. $thirdPayService = new ThirdPayService();
  39. $userWalletService = new WalletService();
  40. $orders = $orderModel->byOrderTakeExpired($now_time - $max_timeout_limit);
  41. $logs = [];
  42. foreach ($orders as $order) {
  43. $orderLock = new RedLock();
  44. $oLock = $orderLock->lock(Order::OKey($order["id"]));
  45. if (!is_array($oLock))
  46. continue;
  47. if (\E_ORDER_STATUS::Purchase !== $order->status)
  48. continue;
  49. \db("cancel_order_log")->insert([
  50. "order_no" => $order["no"],
  51. "order_status_enum" => \E_ORDER_STATUS::AutoCancel,
  52. "createtime" => time()
  53. ]);
  54. Db::startTrans();
  55. try {
  56. // 退款
  57. switch ($order["payment_type"]) {
  58. case \E_ORDER_PAY_TYPE::Wechat:
  59. $res = $thirdPayService->refundByWx(
  60. $order["pay_platform"],
  61. $order["no"],
  62. "助教超时未接单退款",
  63. $order["total_real_amount"] + $order["membership_amount"],
  64. $order["total_real_amount"]);
  65. break;
  66. case \E_ORDER_PAY_TYPE::ALi:
  67. $res = $thirdPayService->refundByAli($order);
  68. break;
  69. default:
  70. $res = $userWalletService->refundByBalance($order, \E_ORDER_STATUS::AutoCancel);
  71. }
  72. if (0 == $res->code())
  73. throw new Exception($res->msg());
  74. $orderModel->update([
  75. "refund_amount" => $order["total_real_amount"],
  76. "is_refund_trip" => 1
  77. ], ["id" => $order["id"]]);
  78. Message::sendSystemMessage(
  79. \E_IDENTITY_TYPE::User,
  80. ["to_user_id" => $order->user_id],
  81. "取消订单通知",
  82. "您的订单【{$order->no}】因助教未及时接单,订单金额将原路返回您的账户,请重新选择助教下单!"
  83. );
  84. Db::commit();
  85. array_push($logs, ["order_id" => $order["id"], "s_res" => $res->code()]);
  86. } catch (Exception $e) {
  87. Db::rollback();
  88. array_push($logs, ["order_id" => $order["id"], "s_res" => $e->getMessage()]);
  89. } finally {
  90. $orderLock->unlock($oLock);
  91. }
  92. }
  93. \think\Log::custom_log("接单超时定时任务", $logs, "order_take");
  94. \think\Log::custom_log("接单超时定时任务", "--------------- over --------------", "order_take");
  95. }
  96. }