setName('agreementPay') ->setDescription('招商银行协议扣款'); } protected function execute(Input $input, Output $output) { Sign::where(['sign_status' => Sign::SIGN_STATUS_SUCCESS])->field('*')->chunk(50, function (Collection $signCollection) use (&$output) { foreach ($signCollection as $collection) { // 查询是否已经生成过订单 $order = Order::getPaidOrderByAgreementId($collection->m_agreement_id); if ($order->isEmpty() || empty($collection->agree_recharge_time) || $order->order_status != Order::STATUS_RECHARGE_SUCCESS) { continue; } // 该协议号扣款订单次数 $orderCount = Order::getCountByAgreementId($collection->m_agreement_id); if ($orderCount == 12) { // 1年扣款12次 continue; } $oneYearAgo = strtotime("-1 year", time()); if ($oneYearAgo > strtotime($collection->create_time)) { // 签约时间超过一年 continue; } $currentTimestamp = strtotime(date('Y-m-d')); $agreeRechargeTime = $collection->agree_recharge_time; $day = env('cmb.agree_pay_day'); if ($currentTimestamp < $agreeRechargeTime + $day * 24 * 60 * 60 ) { //未到扣款时间 continue; } $this->count += 1; $res = AgreementService::agreementPay($collection, $order); if ($res['respCode'] == 1000) { $this->successCount += 1; $output->writeln("受理成功,等待支付通知"); } else { $this->failedCount += 1; $output->writeln("用户".$collection->user_id . "扣款失败:" . $res['respMsg']); } } }); Log::info(sprintf("扣款job执行情况:执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount)); $output->writeln(sprintf("执行成功,累计 %s, 受理成功 %s, 失败 %s", $this->count, $this->successCount, $this->failedCount)); } }