setName('OrderTake') ->setDescription('OrderTake start'); } /** * @param Input $input * @param Output $output * @return int|void|null * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException * @throws \think\Exception */ protected function execute(Input $input, Output $output) { \think\Log::custom_log("接单超时定时任务", "-------------- start --------------", "order_take"); $now_time = time(); // 订单接单超时时间(分钟) $max_timeout_limit = (config("site.order_take_of_timeout") ?? 5) * 60; $orderModel = new Order(); $thirdPayService = new ThirdPayService(); $userWalletService = new WalletService(); $orders = $orderModel->byOrderTakeExpired($now_time - $max_timeout_limit); $logs = []; foreach ($orders as $order) { $orderLock = new RedLock(); $oLock = $orderLock->lock(Order::OKey($order["id"])); if (!is_array($oLock)) continue; if (\E_ORDER_STATUS::Purchase !== $order->status) continue; \db("cancel_order_log")->insert([ "order_no" => $order["no"], "order_status_enum" => \E_ORDER_STATUS::AutoCancel, "createtime" => time() ]); Db::startTrans(); try { // 退款 switch ($order["payment_type"]) { case \E_ORDER_PAY_TYPE::Wechat: $res = $thirdPayService->refundByWx( $order["pay_platform"], $order["no"], "助教超时未接单退款", $order["total_real_amount"] + $order["membership_amount"], $order["total_real_amount"]); break; case \E_ORDER_PAY_TYPE::ALi: $res = $thirdPayService->refundByAli($order); break; default: $res = $userWalletService->refundByBalance($order, \E_ORDER_STATUS::AutoCancel); } if (0 == $res->code()) throw new Exception($res->msg()); $orderModel->update([ "refund_amount" => $order["total_real_amount"], "is_refund_trip" => 1 ], ["id" => $order["id"]]); Message::sendSystemMessage( \E_IDENTITY_TYPE::User, ["to_user_id" => $order->user_id], "取消订单通知", "您的订单【{$order->no}】因助教未及时接单,订单金额将原路返回您的账户,请重新选择助教下单!" ); Db::commit(); array_push($logs, ["order_id" => $order["id"], "s_res" => $res->code()]); } catch (Exception $e) { Db::rollback(); array_push($logs, ["order_id" => $order["id"], "s_res" => $e->getMessage()]); } finally { $orderLock->unlock($oLock); } } \think\Log::custom_log("接单超时定时任务", $logs, "order_take"); \think\Log::custom_log("接单超时定时任务", "--------------- over --------------", "order_take"); } }