REAPI/app/queue/command/taobao/TaobaoAfterSalesOrderUpStatus.php

142 lines
4.9 KiB
PHP
Raw Permalink Normal View History

2024-09-29 15:43:18 +08:00
<?php
namespace app\queue\command\taobao;
use app\merchant\service\MerchantService;
use app\openapi\controller\Coretb;
use app\order\service\AfterSalesOrderService;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
use think\Collection;
/**
* 主动更新退款订单详情
* Class KamiOrderStatusSync
* @package app\data\command
*/
class TaobaoAfterSalesOrderUpStatus extends Command
{
protected function configure()
{
$this->setName('xQueue:TaobaoAfterSalesOrderUpStatus')->setDescription('[ 淘宝直充 - 自动同步 ] 主动更新订单状态');
}
/**
* @param Input $input
* @param Output $output
* @throws \think\admin\Exception
*/
protected function execute(Input $input, Output $output)
{
ini_set('memory_limit', '1024M');
$merchants=MerchantService::instance()->db()->where(['merchant_type'=>8,['access_token_time','>=',time()]])->whereNotNull('access_token')->select()->toArray();
if(!$merchants) $this->setQueueSuccess("未找到需要处理的天猫商家");
$merchant_num = count($merchants);
$count_sum = 0;
$total_sum=0;
foreach($merchants as $merchant){
$request = [];
$other_param = json_decode($merchant['other_param'],true);
$request['agentId'] = $other_param['reapi_appkey'];
$request['agentkey'] = $other_param['reapi_appsecret'];
$mid =$merchant['id'];
$request['access_token'] = $merchant['access_token'];
$aftersalesservice = AfterSalesOrderService::instance();
$needstatus = [1,2,3,5];
$currentTime = time();
// 设置凌晨3点的时间戳
$start = strtotime('today 23:40');
$end = strtotime('today 23:50');
// 如果当前时间早于凌晨3点则执行代码
if ($start< $currentTime && $currentTime < $end) {
$needstatus = [1,2,3];
}
list($count, $total) = [0, $aftersalesservice->db()->whereNotIn('status', $needstatus)->where(['mid'=>$mid])->count()];
$total_sum+=$total;
$aftersalesservice->db()->whereNotIn('status', $needstatus)->where(['mid'=>$mid])->chunk(100, function (Collection $data) use (&$count, $total,$aftersalesservice,$request,&$count_sum) {
// var_dump($orderlist);die;
foreach ($data->toArray() as $vo) {
$request['tid'] = $vo['merchant_order_id'];
$response = Coretb::taobaosdk('getRefundStatus', $request);
$json = json_encode($response);
$array = json_decode($json,true);
if (!isset($array['result_package']['result_list'])||empty($array['result_package']['result_list'])){
$this->setQueueError("没有获取到售后退款订单!");
}
if(isset($array['result_package']['result_list']['query_refund_status_response'])) {
$array = $array['result_package']['result_list']['query_refund_status_response'];
$update = [];
$up = false;
switch ($array['status']) {
case "SUCCESS" :
$update['status'] = '2';
if (isset($array['end_time'])) {
$update['end_time'] = $array['modified'];
}
$up = true;
break;
case "CLOSED" :
$update['status'] = '3';
if (isset($array['end_time'])) {
$update['end_time'] = $array['modified'];
}
$up = true;
break;
case "SELLER_REFUSE_BUYER" :
// $update['status'] = json_encode($array);
$update['status'] = '5';
$up = true;
break;
}
if ($up) {
$count++;
$count_sum++;
$this->setQueueProgress("更新售后订单 {$vo['refund_id']} 状态", $count / $total * 100);
$upRefund = $aftersalesservice->up(['refund_id' => $vo['refund_id']], $update);
}
}
$this->setQueueProgress("处理完订单状态 {$vo['order_id']} ", $count / $total * 100);
}
});
}
$this->setQueueSuccess("共处理 {$merchant_num} 个商家,共获取到个 {$total_sum} 售后退款订单,更新了 {$count_sum} 个售后订单信息!");
}
}