setName('xQueue:TaobaoBlacklistRules')->setDescription('[ 淘宝直充 - 黑名单 ] 自动拉黑规则'); } /** * @param Input $input * @param Output $output * @throws \think\admin\Exception */ protected function execute(Input $input, Output $output) { ini_set('memory_limit', '1024M'); $where[] = [ 'other_param','like','%_black_%' ]; $merchants=MerchantService::instance()->db()->where(['merchant_type'=>8])->whereNotNull('access_token')->where($where)->select()->toArray(); if(!$merchants) $this->setQueueSuccess("未找到需要处理的天猫商家"); $merchant_num = count($merchants); $count_sum = 0; $total_sum=0; $afterSalesOrderService = AfterSalesOrderService::instance(); foreach($merchants as $merchant) { $request = []; $other_param = json_decode($merchant['other_param'], true); if(isset($other_param['refund_black_num'])){ $where = [ 'status' => 5, 'mid'=>$merchant['id'] ]; list($count, $total) = [0, $afterSalesOrderService->db() ->whereNotNull('order_id') ->where($where) ->field('order_id,buyer_open_uid, count(*) as count') ->group('buyer_open_uid') ->having('count > '.$other_param['refund_black_num']) ->count()]; $total_sum+=$total; $data = $afterSalesOrderService->db() ->whereNotNull('order_id') ->where($where) ->field('order_id,buyer_open_uid, count(*) as count') ->group('buyer_open_uid') ->having('count > '.$other_param['refund_black_num']) ->select(); foreach ($data->toArray() as $vo) { $order = $this->getOrderInfo($vo['order_id']); if ($order) { $param = json_decode($order['param'], true); $count++; $count_sum++; if (isset($param['By_OpenUid']) && $param['By_OpenUid'] != 'error') { $value = $param['By_OpenUid']; $desc = '该买家于' . date('Y-m-d H-i-s') . '因为退款'.$vo['count'].'次,超过退款限制自动加入黑名单,关联订单号之一:' . $vo['order_id']; $upStatus = $this->upBlack(1, $value, $desc); if ($upStatus) { $this->setQueueProgress("{$param['By_OpenUid']} 已经自动拉黑", $count / $total * 100); } else { $this->setQueueProgress("{$param['By_OpenUid']} 之前被拉黑不再处理", $count / $total * 100); } } } else { $this->setQueueProgress(" {$vo['order_id']} 暂未找到需要拉黑订单", $count / $total * 100); } } } } $this->setQueueSuccess("共找到 {$merchant_num} 个商户需要处理,总共找到 {$total_sum} 个,共拉黑 {$count_sum} 个订单完成!"); } public function getOrderInfo($order_id) { $orderService = OrderService::instance(); $order = $orderService->db()->field('param')->where(['order_id' => $order_id])->find(); if (!$order) { $orderLastweekHistoryService = OrderLastweekHistoryService::instance(); $order = $orderLastweekHistoryService->db()->field('param')->where(['order_id' => $order_id])->find(); } if (!$order) { $orderLastHistoryService = OrderLastHistoryService::instance(); $order = $orderLastHistoryService->db()->field('param')->where(['order_id' => $order_id])->find(); } if (!$order) { $orderTwoHistoryService = OrderTwoHistoryService::instance(); $order = $orderTwoHistoryService->db()->field('param')->where(['order_id' => $order_id])->find(); } if (!$order) { $orderHistoryService = OrderHistoryService::instance(); $order = $orderHistoryService->db()->field('param')->where(['order_id' => $order_id])->find(); } return $order; } public function upBlack($type, $value, $desc = '') { $BlackCardService = BlackCardService::instance(); $where['type'] = $type; $where['value'] = $value; $info = $BlackCardService->db()->where($where)->find(); if($info){ return false; }else{ $update['type'] = $type; $update['value'] = $value; $update['desc'] = $desc; $update['status'] = 1; $BlackCardService->db()->insert($update); return true; } } #判断响应是否成功 }