setName('demand:deal')->setDescription('bidding deal command!'); } protected function execute(Input $input, Output $output) { $output->writeln("bidding:deal:"); $this->confirmReceiptTimeoutDeal($input, $output); $this->demandProfitUnfreezeDeal($input, $output); } /** * 确认收货超时处理 * @param Input $input * @param Output $output * @return void */ protected function confirmReceiptTimeoutDeal(Input $input, Output $output) { $confirmReceiptTimeoutDay = intval(config('site.confirm_receipt_timeout_day') ?? 7); $where = [ 'status' => DemandService::STATUS_CONFIRM_RECEIPT, 'accept_status' => DemandService::$Common::SUCCESS, ]; DemandService::where($where)->chunk(100, function ($demandList) use ($confirmReceiptTimeoutDay) { foreach ($demandList as $item) { try { Db::transaction(function () use ($item, $confirmReceiptTimeoutDay) { //如果七天未确认收货,则自动确认收货 if (time() > strtotime("+$confirmReceiptTimeoutDay day", $item->accept_time)) { $item->status = DemandService::STATUS_FINISHED; $item->settle_status = DemandService::$Common::ONGOING; $saveRes = $item->save(); if (!$saveRes) { exception('更新结算状态失败'); } //订单状态改为已完成 $upRes = OrderService::where(['parent_order_no' => $item->parent_order_no])->update(['order_status' => OrderService::ORDER_STATUS_FINISHED]); if (!$upRes) { exception('更新订单状态失败'); } } }); } catch (\Exception $e) { Log::info('确认收货超时处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE)); } } }); } /** * 订单收益解冻处理 * @param Input $input * @param Output $output * @return void */ protected function demandProfitUnfreezeDeal(Input $input, Output $output) { $where = [ 'status' => DemandService::STATUS_FINISHED, 'settle_status' => DemandService::$Common::ONGOING, ]; DemandService::where($where)->with(['orders', 'winBidding'])->chunk(100, function ($demandList) { foreach ($demandList as $item) { try { Db::transaction(function () use ($item) { //根据订单列表查找冻结金额日志,根据冻结日志解冻冻结金额到余额 $userId = $item->winBidding->user_id; $settleTotalAmount = 0; foreach ($item->orders as $order) { $logWhere = [ 'user_id' => $userId, 'account_type' => AccountLogService::ACCOUNT_TYPE_FREEZE, 'type' => AccountLogService::TYPE_FREEZE, 'inc_or_exp' => AccountLogService::INC_EXP_INCREASE, 'refer_id' => $order->id, 'status' => AccountLogService::$Common::STATUS_OFF, ]; $log = AccountLogService::where($logWhere)->find(); if ($log) { $amount = $log->amount; $settleTotalAmount += $amount; //写入余额日志 $title = OrderService::ORDER_TYPE_MAP[$order->order_type] ?? '需求收益'; $log_res = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_FREEZE, $userId, $amount, $title . '解冻-冻结金额扣除', AccountLogService::TYPE_UNFREEZE, 0, AccountLogService::INC_EXP_EXPEND, $order->id, $order->total_amount); $log_res2 = AccountLogService::addLog(AccountLogService::ACCOUNT_TYPE_AMOUNT, $userId, $amount, $title . '解冻-余额增加', AccountLogService::TYPE_PROFIT, 0, AccountLogService::INC_EXP_INCREASE, $order->id, $order->total_amount); //减去冻结,加到余额里面 AccountService::updateData($userId, $amount, -$amount, 0, 0, $log_res->id); //更新日志状态 $log->status = AccountLogService::$Common::STATUS_ON; $saveRes = $log->save(); if (!$saveRes) { exception('更新余额日志状态失败'); } } } //更新结算状态 $item->settle_status = DemandService::$Common::SUCCESS; $saveRes2 = $item->save(); if (!$saveRes2) { exception('更新需求结算状态失败'); } //发送结算完成通知 $msgData = [ 'values' => [ $item->parent_order_no, round($settleTotalAmount, 2), SubscribeMessageService::MSG_MAP[SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE]['remark'], ] ]; (new EasyWeChatService)->sendMsg($userId, SubscribeMessageService::MSG_TEMP_DEMAND_SETTLE, $msgData); }); } catch (\Exception $e) { Log::info('订单收益解冻处理异常:' . json_encode(['file' => $e->getFile(), 'line' => $e->getLine(), 'error' => $e->getMessage(), 'item' => $item], JSON_UNESCAPED_UNICODE)); } } }); } }