admin-api/app/jobs/store/platformCommission/refundOrderHandle/HandleIntegralJob.php

84 lines
3.5 KiB
PHP

<?php
namespace app\jobs\store\platformCommission\refundOrderHandle;
use app\common\model\store\order\StoreOrderProduct;
use app\common\model\store\platformCommission\LegumesLog;
use app\common\repositories\store\platformCommission\LegumesLogRepository;
use app\common\repositories\user\UserBillRepository;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
class HandleIntegralJob implements JobInterface{
public function fire($job,$data){
try{
// Log::info("订单退款成功 - 抵扣积分退回 - 开始处理: ".var_export($data,1));
// 获取订单商品信息
$orderProductInfo = StoreOrderProduct::field('uid,order_id,use_legumes_integral')->where('order_product_id',$data['order_product_id'])->find();
$useLegumesIntegral = $orderProductInfo->use_legumes_integral ?? 0;
$uid = $orderProductInfo->uid ?? 0;
if($useLegumesIntegral > 0 && $uid > 0){
// 计算退回积分
$returnIntegral = (float)sprintf("%.2f",$useLegumesIntegral * $data['refund_rate'] / 100);
// 获取退回的记录列表
$useLegumesLogList = app()->make(LegumesLogRepository::class)->getUsedList((int)$uid,(float)$returnIntegral);
// 循环处理
$returnComputeIntegral = $returnIntegral;// 计算用 退回积分
$updateData = [];
foreach($useLegumesLogList as $logInfo){
// 当前分配信息可退回积分 剩余可退回积分
$reduce = $returnComputeIntegral < $logInfo['use_integral'] ? $returnComputeIntegral : $logInfo['use_integral'];
$returnComputeIntegral = (float)sprintf("%.2f",$returnComputeIntegral - $reduce);
$updateData[] = [
'id' => $logInfo['id'],
'use_integral' => (float)sprintf("%.2f",$logInfo['use_integral'] - $reduce)
];
if($returnComputeIntegral <= 0) break;
}
if($updateData){
LegumesLog::batchUpdate(array_values($updateData));
// 获取用户可用豆豆积分
$hold_legumes_integral = app()->make(LegumesLogRepository::class)->getHoldLegumesIntegral((int)$uid);
// 添加用户账单信息变更记录
$mark = '订单退款 - 退回抵扣积分:'.$returnIntegral;
$bills[] = [
'uid' => (int)$uid,
'link_id' => $orderProductInfo->order_id ?? 0,
'pm' => 1,
'title' => '订单退款 - 退回积分',
'category' => 'integral',
'type' => 'deduction',
'number' => (float)$returnIntegral,
'balance' => $hold_legumes_integral,
'mark' => $mark,
'mer_id' => 0,
'status' => 1
];
app()->make(UserBillRepository::class)->insertAll($bills);
}
}
}
catch(\Exception $e){
$data['error_msg'] = $e->getMessage();
Log::info('订单退款成功 - 抵扣积分退回 - 失败: '.var_export($data,1));
}
$job->delete();
}
public function failed($data){
Log::info('订单退款成功 - 抵扣积分退回 - 失败(failed): '.var_export($data,1));
}
}