REAPI/app/queue/command/order/OrderFinish.php
2024-09-29 15:43:18 +08:00

211 lines
6.4 KiB
PHP

<?php
namespace app\queue\command\order;
use app\merchant\service\MerchantService;
use app\merchant\service\OrderHistoryService;
use app\merchant\service\OrderLastHistoryService;
use app\merchant\service\OrderLastweekHistoryService;
use app\merchant\service\OrderService;
//use app\order\service\Kami91OrderService;
use app\merchant\service\OrderTwoHistoryService;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
use think\Collection;
/**
* 完成订单迁移
* Class KamiOrderTimeoutRefund
* @package app\data\command
*/
class OrderFinish extends Command
{
protected function configure()
{
// $this->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');
$day = input('day');
// var_dump($day);die;
MerchantService::instance()->init($day);
/*
if (!$day) {
$day = date('Y-m-d', strtotime('-1 day'));
}
*/
$page = 1;
while ($this->finishOne($day, $page)) {
$page++;
}
}
public function finishOne($day, $page)
{
$orderService = OrderService::instance();
$orderLastweekHistoryService = OrderLastweekHistoryService::instance();
$data = $this->getFinishData($day, $page, $orderService,5);
//??//
list($count, $total) = [0, $data->count()];
$data->chunk(1000, function (Collection $data) use (&$count, $total, $orderService, $orderLastweekHistoryService) {
// var_dump($orderlist);die;
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);
}
}
}
});
$orderLastHistoryService = OrderLastHistoryService::instance();
$data = $this->getFinishData($day, $page, $orderLastweekHistoryService,2);
$data->chunk(1000, function (Collection $data) use (&$count, $total, $orderLastweekHistoryService, $orderLastHistoryService) {
// var_dump($orderlist);die;
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);
}
}
}
});
$orderTwoHistoryService = OrderTwoHistoryService::instance();
$data = $this->getFinishData($day, $page, $orderLastHistoryService,3);
$data->chunk(1000, function (Collection $data) use (&$count, $total, $orderTwoHistoryService, $orderLastHistoryService) {
// var_dump($orderlist);die;
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("迁移订单 {$vo['order_id']} ", $count / $total * 100);
}
}
}
});
$orderHistoryService = OrderHistoryService::instance();
$data = $this->getFinishData($day, $page, $orderTwoHistoryService,4);
$data->chunk(1000, function (Collection $data) use (&$count, $total, $orderTwoHistoryService, $orderHistoryService) {
// var_dump($orderlist);die;
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);
}
}
}
});
$this->setQueueSuccess("完成 {$count} 个订单迁移!");
}
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('-7 day'));
$day = date('Y-m-d', strtotime('-1 day'));
}
if($type == 2){
$start = date('Y-m-d', strtotime('-30 day'));
$day = date('Y-m-d', strtotime('-8 day'));
}
if($type == 3){
$start = date('Y-m-d', strtotime('-90 day'));
$day = date('Y-m-d', strtotime('-31 day'));
}
if($type == 4){
$start = date('Y-m-d', strtotime('-365 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 = 10000;
$page = $page - 1;
$page = $page * $limit;
$db->limit($page, $limit);
}
return $db;
}
}