REAPI/app/queue/command/maSuDa/MaSuDaAfterSalesOrdeRelated.php
b3f70a8d30 feat(queue): 新增临时锁卡任务
- 添加 LinSuoKa 类实现临时锁卡功能
- 优化卡密订单关联逻辑,处理状态为 4 的订单- 新增 redis 记录订单详情,用于限单和退款处理
- 修改 Qiling 类中的签名生成逻辑
- 更新 Test 控制器中的 test_ts 方法,增加对不同订单表的处理
2025-06-03 18:55:03 +08:00

421 lines
17 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\queue\command\maSuDa;
use app\channel\service\ChannelService;
use app\channel\service\KamiService;
use app\kami\service\LockCardService;
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\merchant\service\OrderTwoHistoryService;
use app\openapi\controller\Coretb;
use app\order\service\AfterSalesOrderService;
use dever\Log;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
use think\Collection;
/**
* 关联订单并进行是否锁卡操作
* Class TaobaoAfterSalesOrdeRelated
* @package app\data\command
*/
class MaSuDaAfterSalesOrdeRelated extends Command
{
protected function configure()
{
$this->setName('xQueue:MaSuDaAfterSalesOrdeRelated')->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(['other_status' => 1])->whereLike('other_param', '%"maSuDa_afterSales_status":"1"%')->select()->toArray();
if (!$merchants) $this->setQueueSuccess("未找到需要处理的商家");
$merchant_num = count($merchants);
$count_sum = 0;
$total_sum = 0;
$afterSalesOrderService = AfterSalesOrderService::instance();
$channelService = ChannelService::instance();
$LockCardService = LockCardService::instance();
foreach ($merchants as $merchant) {
$request = [];
$other_param = json_decode($merchant['other_param'], true);
$mid = $merchant['id'];
if (!is_array($other_param)) {
$this->setQueueProgress(" 该商家配置错误,请检查", 0);
continue;
}
if (!$other_param['autoCardSoftware'] || $other_param['autoCardSoftware'] != 2) {
$this->setQueueProgress(" 该商家暂不支持码速达,请检查", 0);
continue;
}
$where = [
'status' => 1,
'order_id' => null,
'mid' => $mid
];
list($count, $total) = [0, $afterSalesOrderService->db()->where($where)->whereNull('infodata')->count()];
$total_sum += $total;
// var_dump($total);die;
#1.同步更新订单如果没找到关联订单则更新status为6,
$afterSalesOrderService->db()->where($where)->whereNull('infodata')->chunk(100, function (Collection $data) use (&$count, &$count_sum, $total, $afterSalesOrderService, $request, $merchant, $channelService, $LockCardService, $other_param) {
foreach ($data->toArray() as $vo) {
// var_dump($vo);die;
$count++;
$count_sum++;
$order = $this->getOrderInfo('', $vo['merchant_order_id'], $vo['mid']);
if (!$order) {
$up_data = [
'status' => '6'
];
$field = ['merchant_order_id' => $vo['merchant_order_id']];
$afterSalesOrderService->db()->where($field)->update($up_data);
// if($other_param[''])
// $up_notes = $this->tradeUpdate($vo,$request,3); #备注
$this->setQueueProgress(" {$vo['merchant_order_id']} 不存在我们系统中", $count / $total * 100);
} else {
$channelInfo = $channelService->get($order['cid']);
$up_data = [];
$up_data['order_id'] = $order['order_id'];
$up_data['pid'] = $order['pid'];
$up_data['cid'] = $order['cid'];
$up_data['product_key'] = $order['product_key'];
$response = json_decode($order['response'], true);
if ($channelInfo['suoka_status'] == 1) {
if (isset($response['kami'])) {
$suoka = $channelService->call('lockCard', $order['cid'], $order);
if ($suoka !== 'ok' && $suoka !== 'old_ok') {
$suoka = $channelService->call('lockCard', $order['cid'], $order);
}
if ($suoka == 'ok' || $suoka == 'old_ok') {
$spRefund = $this->refundsp($vo, $request);
if ($spRefund == 'true') {
$request['sub_code'] = $merchant['sub_code'];
$caozuo_refunf = $this->refundtk($vo, $request);
if ($caozuo_refunf != 'error') {
$this->setQueueProgress("售后订单 {$vo['refund_id']} 已经自动退款处理", $count / $total * 100);
} else {
// Log::write('queue', 'taobao_sh', $caozuo_refunf);#写入到日志里
$this->setQueueProgress("售后订单 {$vo['refund_id']} 已经完成审批,自动退款失败", $count / $total * 100);
}
} else {
// Log::write('queue', 'taobao_sh', $spRefund);#写入到日志里
$this->setQueueProgress("售后订单 {$vo['refund_id']} 已经自动锁卡,审批失败", $count / $total * 100);
}
} else {
$up_data['status'] = '8';
Log::write('queue', 'taobao_sk', $order['order_id'] . '锁卡失败');#写入到日志里
$this->setQueueProgress("售后订单 {$vo['refund_id']} 锁卡失败", $count / $total * 100);
}
}
} elseif ($channelInfo['suoka_status'] == 2) {
if (isset($response['kami'])) {
$cardno = $response['kami']['cardno'];
$cardpwd = $response['kami']['cardpwd'];
$expire_time = $response['kami']['expired'];
$suoka = $LockCardService->call('suoka', $order['cid'], $cardno, $cardpwd, $order['pid'], $order['cash']);
if ($suoka == 'ok' || $suoka == 'old_ok') {
// $KamiService = KamiService::instance();
// $submitKami = $KamiService->upKami($cardno, $cardpwd, $order['cash'],$expire_time);
// if(!$submitKami){
// Log::write('queue', 'submitkami', $order['order_id'].'提交数据库失败');#写入到日志里
// }
$spRefund = $this->refundsp($vo, $request);
if ($spRefund == 'error') {
$spRefund = $this->refundsp($vo, $request);
if ($spRefund == 'error') {
$spRefund = $this->refundsp($vo, $request);
}
}
if ($spRefund == 'true') {
$request['sub_code'] = $merchant['sub_code'];
$caozuo_refunf = $this->refundtk($vo, $request, 0);
if ($caozuo_refunf != 'error') {
$up_data['status'] = '4';
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 已经自动退款处理", $count / $total * 100);
} else {
// Log::write('queue', 'taobao_sh', $caozuo_refunf);#写入到日志里
$up_data['status'] = '4';
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 已经完成审批,自动退款失败", $count / $total * 100);
}
} else {
$up_data['status'] = '7';
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 已经自动锁卡,审批失败", $count / $total * 100);
}
} elseif ($suoka == 'use_error') {
//
// $up_notes = $this->tradeUpdate($vo,$request,1);
// if($up_notes == 'ok'){
// $up_data['status'] = '7';
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 已使用,已修改备注", $count / $total * 100);
// }else{
$up_data['status'] = '8';
Log::write('queue', 'taobao_bz', $order['order_id'] . '已使用');#写入到日志里
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 已使用,修改备注失败", $count / $total * 100);
// }
} else {
// $up_notes = $this->tradeUpdate($vo,$request,2);
// if($up_notes == 'ok'){
// $up_data['status'] = '8';
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 锁卡失败,已修改备注", $count / $total * 100);
// }else{
$up_data['status'] = '8';
Log::write('queue', 'taobao_bz', $order['order_id'] . '已使用');#写入到日志里
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 锁卡失败,修改备注失败", $count / $total * 100);
// }
Log::write('queue', 'taobao_sk', $order['order_id'] . '锁卡失败');#写入到日志里
// $this->setQueueProgress("售后订单 {$vo['refund_id']} 锁卡失败", $count / $total * 100);
}
}
} else {
$up_data['status'] = '8';
}
$field = ['merchant_order_id' => $vo['merchant_order_id']];
$afterSalesOrderService->db()->where($field)->update($up_data);
}
}
});
}
$this->setQueueSuccess("共找到 {$merchant_num} 个商户需要处理,总共找到 {$total_sum} 个,同步关联 {$count_sum} 个订单完成!");
}
public function getOrderInfo($order_id,$merchant_order_id,$mid)
{
$orderService = OrderService::instance();
$order = $orderService->get($order_id, $merchant_order_id, $mid);
if (!$order) {
$orderLastweekHistoryService = OrderLastweekHistoryService::instance();
$order = $orderLastweekHistoryService->get($order_id, $merchant_order_id, $mid);
}
if (!$order) {
$orderLastHistoryService = OrderLastHistoryService::instance();
$order = $orderLastHistoryService->get($order_id, $merchant_order_id, $mid);
}
if (!$order) {
$orderTwoHistoryService = OrderTwoHistoryService::instance();
$order = $orderTwoHistoryService->get($order_id, $merchant_order_id, $mid);
}
if (!$order) {
$orderHistoryService = OrderHistoryService::instance();
$order = $orderHistoryService->get($order_id, $merchant_order_id, $mid);
}
return $order;
}
public function tradeUpdate($vo,$request,$beizhu = 1)
{
$request['merchant_order_id'] = $vo['merchant_order_id'];
if($beizhu == 1){
$request['beizhu'] = "卡密已使用,请操作拒绝退款,并上传退款凭证--Reapi系统自动化";
}elseif($beizhu == 2){
$request['beizhu'] = "卡密锁卡失败,请人工介入操作,核实拒绝退款,并上传拒绝退款凭证--Reapi系统自动化";
}elseif ($beizhu == 3){
$request['beizhu'] = "此订单不存在中控系统中,可能属于早期订单,请人工核实处理--Reapi系统自动化";
}
$request['flag'] = "1";
$request['reset'] = "false";
// $request['access_token'] = $merchant['access_token'];
$response = Coretb::taobaosdk('tradeUpdate', $request);
$json = json_encode($response);
$array = json_decode($json, true);
if (isset($array['Trade']['tid']) && $array['Trade']['tid'] == $vo['merchant_order_id']) {
return 'ok';
} else {
return 'error';
}
}
public function refundsp($vo,$request)
{
$request['caozuo_name'] = '系统自动处理';
$request['refundId'] = $vo['refund_id'];
$request['phase'] = $vo['refund_phase'];
$RefundVersion = json_decode($vo['infodata'],true);
$RefundVersion = $RefundVersion['refund_version'];
$request['RefundVersion'] = $RefundVersion;
$request['RefundMsg'] = '未使用,同意退款';#默认同意
$request['RefundStatus'] = 'true';
// $request['access_token'] = $merchant['access_token'];
$response = Coretb::taobaosdk('refundSp', $request);
$json = json_encode($response);
$array = json_decode($json, true);
if($array['is_success'] == 'true'){
return 'true';
}else{
Log::write('queue', 'taobao_sp', $array);#写入到日志里
return 'error';
}
#判断响应是否成功
}
public function refundtk($vo,$request,$num = 0)
{
$RefundVersion = $this->refundget($vo,$request);
if($RefundVersion != 'error'){
$cash = $vo['refund_fee'] * 100;
$request['refund_infos'] = $vo['refund_id'].'|'.$cash.'|'.$RefundVersion .'|'.$vo['refund_phase'];
$response = Coretb::taobaosdk('refundAgree', $request);
$json = json_encode($response);
$array = json_decode($json, true);
if(isset($array['sub_msg']) ){
if(str_contains($array['sub_msg'], "未审核状态")){
while ($num<1) {
$num++;
sleep(3);
return $this->refundtk($vo,$request,$num,);
}
}
}
if (isset($array['succ'])) {
if(isset($array['message']) && $array['message']=='操作成功'){
return 'ok';
}else{
Log::write('queue', 'taobao_tk', $array);#写入到日志里
return 'error';
}
} else {
Log::write('queue', 'taobao_tk', $array);#写入到日志里
return 'error';
}
}else {
Log::write('queue', 'taobao_tk', $RefundVersion);#写入到日志里
return 'error';
}
}
public function refundget($vo,$request)
{
if(isset($request['sub_code'])){
unset($request['sub_code']);
}
$request['refundId'] = $vo['refund_id'];
// $request['access_token'] = $merchant['access_token'];
$response = Coretb::taobaosdk('refundInfo', $request);
$json = json_encode($response);
$array = json_decode($json, true);
if (isset($array['refund'])) {
$array = $array['refund'];
$RefundVersion = $array['refund_version'];
return $RefundVersion;
} else {
return 'error';
}
}
#判断响应是否成功
// #以下为成功,自动切换子账号审批退款
// $request1['agentId'] = '34615444';
// $request1['agentkey'] = '2e59a0c00b4fafd9ae46bedced6eccc3';
// $request1['sub_code'] = $merchant['sub_code'];
// $request['refund_infos'] = $vo['refund_id'].'|'.$cash,
//
//// $req->setCode($request['sub_code']);
//// $req->setRefundInfos("退款订单号|按分金额|退款时间戳|退款状态");
// $req->setRefundInfos($request['refund_infos']);
// $req->setIgnoreCode("true");
// return $c->execute($req, $request['sub_code']);
// $response1 = Coretb::taobaosdk('refundAgree', $request);
#尝试发起锁卡,如果锁卡成功自动退款
#锁卡失败不处理等待rpa机器人自动化处理。
}