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

120 lines
4.1 KiB
PHP

<?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 TaobaoAfterSalesOrderGet extends Command
{
protected function configure()
{
// $this->setName('xQueue:Book');
// $this->setDescription('导入章节内容');
$this->setName('xQueue:TaobaoAfterSalesOrderGet')->setDescription('[ 淘宝直充 - 自动同步 ] 主动获取售后订单');
}
/**
* @param Input $input
* @param Output $output
* @throws \think\admin\Exception
*/
protected function execute(Input $input, Output $output)
{
ini_set('memory_limit', '1024M');
$request = [];
#other_param,merchant_type =8;access_token,access_token_time
$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){
$other_param = json_decode($merchant['other_param'],true);
$mid =$merchant['id'];
// $request['agentId'] = '34615444';
// $request['agentkey'] = '2e59a0c00b4fafd9ae46bedced6eccc3';
$request['agentId'] = $other_param['reapi_appkey'];
$request['agentkey'] = $other_param['reapi_appsecret'];
$request['status'] = 'WAIT_SELLER_AGREE';
$request['access_token'] = $merchant['access_token'];
$response = Coretb::taobaosdk('refundsList', $request);
$json = json_encode($response);
// $this->setQueueError($json);
$array = json_decode($json, true);
if (!is_array($array) || !is_array($array['refunds']) || !isset($array['refunds']['refund'])){
$this->setQueueError("没有获取到售后退款订单!");
}
$getAfterSalesList = $array['refunds']['refund'];
$total = isset($getAfterSalesList[0])?count($getAfterSalesList):1;
$total_sum+=$total;
$count = 0;
if($total==1){
$getAfterSalesList=[$getAfterSalesList];
}
foreach ($getAfterSalesList as $k => $vo) {
$aftersalesservice = AfterSalesOrderService::instance();
$info = $aftersalesservice->db()->where(['refund_id' => $vo['refund_id']])->find();
if (!$info) {
$count++;
$count_sum++;
$update = [];
$update['listdata'] = json_encode($vo);
$update['mid'] = $mid;
$update['status'] = '1';
$update['merchant_order_id'] = $vo['tid'];
$update['refund_id'] = $vo['refund_id'];
$update['refund_fee'] = $vo['refund_fee'];
$update['dispute_type'] = $vo['dispute_type'];
$update['refund_desc'] = $vo['desc'];
$update['create_at'] = $vo['created'];
$update['buyer_nick'] = $vo['buyer_nick'];
$update['buyer_open_uid'] = $vo['buyer_open_uid'];
$update['refund_phase'] = $vo['refund_phase'];
$up = $aftersalesservice->up(['refund_id' => $vo['refund_id']], $update);
$this->setQueueProgress("获取到新的售后退款订单 {$vo['refund_id']} ", $count / $total * 100);
}
}
}
// $mid = '1';
// $merchant = MerchantService::instance()->get($mid);
$this->setQueueSuccess("共处理 {$merchant_num} 个商家,共获取到个 {$total_sum} 售后退款订单,新增 {$count_sum} 个售后订单!");
}
}