setName('xQueue:Book'); // $this->setDescription('导入章节内容'); $this->setName('xQueue:OrderFinish')->setDescription('[ 订单列表 ] 完成订单迁移'); } /** * @param Input $input * @param Output $output * @throws \think\admin\Exception */ protected function execute(Input $input, Output $output) { ini_set('memory_limit', '1024M'); $page = 1; while ($this->finishOne_lastWeek('', $page)) { $page++; } $page = 1; while ($this->finishOne_last('', $page)) { $page++; } $page = 1; while ($this->finishOne_two('', $page)) { $page++; } $page = 1; while ($this->finishOne_history('', $page)) { $page++; } $this->setQueueSuccess("完成订单迁移!"); } #上周 public function finishOne_lastWeek($day, $page) { $orderService = OrderService::instance(); $orderLastWeekHistoryService = OrderLastweekHistoryService::instance(); $data = $this->getFinishData($day, $page, $orderService,5); list($count, $total) = [0, $data->count()]; if($total==0){ return false; } return $data->chunk(1000, function (Collection $data) use (&$count, $total, $orderService, $orderLastWeekHistoryService) { foreach ($data->toArray() as $vo) { if ($vo) { # 先插入一条历史数据 $id = $vo['id']; unset($vo['id']); $state = $orderLastWeekHistoryService->up($vo); if ($state) { $orderService->del($id); $count++; $this->setQueueProgress("迁移最近一周订单 {$vo['order_id']} ", $count / $total * 100); } }else{ return false; } } return true; }); } #上个月 public function finishOne_last($day, $page) { $orderLastHistoryService = OrderLastHistoryService::instance(); $orderLastWeekHistoryService = OrderLastweekHistoryService::instance(); $data = $this->getFinishData($day, $page, $orderLastWeekHistoryService,2); list($count, $total) = [0, $data->count()]; if($total==0){ return false; } return $data->chunk(1000, function (Collection $data) use (&$count, $total, $orderLastHistoryService, $orderLastWeekHistoryService) { foreach ($data->toArray() as $vo) { if ($vo) { # 先插入一条历史数据 $id = $vo['id']; unset($vo['id']); $state = $orderLastHistoryService->up($vo); if ($state) { $orderLastWeekHistoryService->del($id); $count++; $this->setQueueProgress("迁移最近一个月订单 {$vo['order_id']} ", $count / $total * 100); } }else{ return false; } } return true; }); } #上2-3个月 public function finishOne_two($day, $page) { $orderLastHistoryService = OrderLastHistoryService::instance(); $orderTwoHistoryService = OrderTwoHistoryService::instance(); $data = $this->getFinishData($day, $page, $orderLastHistoryService,3); list($count, $total) = [0, $data->count()]; if($total==0){ return false; } return $data->chunk(1000, function (Collection $data) use (&$count, $total, $orderLastHistoryService, $orderTwoHistoryService) { foreach ($data->toArray() as $vo) { if ($vo) { # 先插入一条历史数据 $id = $vo['id']; unset($vo['id']); $state = $orderTwoHistoryService->up($vo); if ($state) { $orderLastHistoryService->del($id); $count++; $this->setQueueProgress("迁移最近2-3个月订单 {$vo['order_id']} ", $count / $total * 100); } }else{ return false; } } return true; }); } #历史 public function finishOne_history($day, $page) { $orderHistoryService = OrderHistoryService::instance(); $orderTwoHistoryService = OrderTwoHistoryService::instance(); $data = $this->getFinishData($day, $page, $orderTwoHistoryService,4); list($count, $total) = [0, $data->count()]; if($total==0){ return false; } return $data->chunk(1000, function (Collection $data) use (&$count, $total, $orderHistoryService, $orderTwoHistoryService) { foreach ($data->toArray() as $vo) { if ($vo) { # 先插入一条历史数据 $id = $vo['id']; unset($vo['id']); $state = $orderHistoryService->up($vo); if ($state) { $orderTwoHistoryService->del($id); $count++; $this->setQueueProgress("迁移超三月历史订单 {$vo['order_id']} ", $count / $total * 100); } }else{ return false; } } return true; }); } // return true; public function getFinishData($day, $page, $orderService ,$type = 1) { if (!$day) { $start = date('Y-m-d', strtotime('-360 day')); $day = date('Y-m-d', strtotime('-1 day')); if($type == 5){ $start = date('Y-m-d', strtotime('-360 day')); $day = date('Y-m-d', strtotime('-1 day')); } if($type == 2){ $start = date('Y-m-d', strtotime('-360 day')); $day = date('Y-m-d', strtotime('-8 day')); } if($type == 3){ $start = date('Y-m-d', strtotime('-360 day')); $day = date('Y-m-d', strtotime('-31 day')); } if($type == 4){ $start = date('Y-m-d', strtotime('-360 day')); $day = date('Y-m-d', strtotime('-91 day')); } } else { $start = $day; } $begin = $start . ' 00:00:00'; $after = $day . ' 23:59:59'; $time = [$begin, $after]; $db = $orderService->db()->whereRaw('status in(2,3) '); $db = $db->whereBetween('create_at', $time)->order('id asc'); if ($page) { $limit = 1000; $page = $page - 1; $page = $page * $limit; $db->limit($page, $limit); } return $db; } }