admin-api/app/jobs/store/platformCommission/ComputeIntegralJob.php

82 lines
2.7 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\jobs\store\platformCommission;
use app\common\model\store\platformCommission\LegumesLog;
use app\common\repositories\store\platformCommission\RecordRepository;
use crmeb\interfaces\JobInterface;
use think\facade\Log;
use think\facade\Queue;
/**
* Common: 全平台豆豆计算
* Author: wu-hui
* Time: 2023/12/29 11:13
* Class ComputeIntegralJob
* @package app\jobs\store\platformCommission
*/
class ComputeIntegralJob implements JobInterface{
public function fire($job,$data){
try{
// 判断当前data不为数组时 强制为数组
if(!is_array($data)) $data = [];
// 记录日志 并且开启处理
// Log::info("全平台豆豆计算 - 开始处理: ".var_export($data,1));
$page = $data['page'] ?? 0;
$ids = $data['ids'] ?? [];
$limit = 1000;
$set = app()->make(RecordRepository::class)->getBaseConfig();
// 获取当前页数据 有效记录、已获取积分低于订单金额
$model = LegumesLog::field('id,(order_money - refund_order_money) as order_money,(get_legumes - refund_get_legumes) as get_legumes,get_integral')
->when(count($ids) > 0,function($query) use ($ids){
$query->whereIn('id',$ids);
},function($query){
$query->where('get_integral < order_money');
})
->whereIn('status',[0,1]);
$count = $model->count();
$list = $model->page($page,$limit)->select()->toArray();
if(count($list) <= 0) throw new \Exception('无处理数据!');
// 循环处理
$updateData = [];
foreach($list as $item){
$getIntegral = (float)sprintf("%.2f",$item['get_legumes'] * $set['legumes_price']);
$updateData[] = [
'id' => $item['id'],
'get_integral' => $getIntegral >= $item['order_money'] ? $item['order_money'] : $getIntegral
];
}
LegumesLog::batchUpdate(array_values($updateData));
// 判断:是否存在下一页
$currentLimit = $page * $limit;
if($currentLimit < $count){
$data['page'] = $page + 1;
Queue::push(ComputeIntegralJob::class,$data);
}
}
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));
}
}