setName('xQueue:OrderCancel')->setDescription('[ 订单列表 ] 主动拦截取消订单'); } /** * @param Input $input * @param Output $output * @throws \think\admin\Exception */ protected function execute(Input $input, Output $output) { ini_set('memory_limit', '1024M'); $channelService = ChannelService::instance(); $channelList = $channelService->db()->where(['cancel_status'=>1,'status'=>1])->select()->toArray(); if(!$channelList) $this->setQueueSuccess("未找到支持取消的渠道"); $merchantService = MerchantService::instance(); $merchantList = $merchantService->db()->where(['cancel_support'=>1,'status'=>1])->select()->toArray(); if(!$merchantList) $this->setQueueSuccess("未找到支持取消的商家"); $merchantList_num = count($merchantList); $count_sum = 0; $total_sum=0; // $channelList_num = count($channelList); // $count_sum = 0; // $total_sum=0; $class = OrderService::instance(); foreach ($merchantList as $merchant) { $mid = $merchant['id']; $other_param = $merchant['other_param']; $array = json_decode($other_param,true); $timeout_cancel = 3600; if(isset($array['timeout_cancel'])){ $timeout_cancel = $array['timeout_cancel']; } foreach($channelList as $channel) { // $where['cid'] = $channel['id']; // $where['mid'] = $mid; // $where['status'] = 4; $where = [['status','=',4],['mid','=',$mid],['cid','=',$channel['id']],['expire_time','<=',date('Y-m-d H:i:s',time()+$timeout_cancel)]]; list($count, $total) = [0, $class->db()->where($where)->order('id asc')->count()]; $total_sum += $total; // $data = $class->db()->where($where)->limit(100)->select()->toArray(); $class->db()->where($where)->order('id asc')->chunk(100, function (Collection $data) use (&$count, $total,$channel,$channelService,&$count_sum) { $array = $data->toArray(); foreach ($array as $k => $vo) { if(isset($vo['num']) && $vo['num']>0) { $vo['order_id'] = $vo['order_id'].'_'.$vo['num']; } $result = $channelService->call('cancel', $vo['cid'], $vo); if ($result) { $count_sum++; $count++; $this->setQueueProgress("拦截 {$vo['order_id']} 订单成功", $count / $total * 100); } } }); } } $this->setQueueSuccess("共处理 {$merchantList_num} 个商家,共有 {$total_sum} 个订单处理查询中, 完成{$count_sum} 个订单查询新状态更新!"); } }