jh-admin/addon/commission/job/ComputeIntegralJob.php

93 lines
3.9 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
/**
* SaaSMall商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 成都SAAS云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.gobuysaas.com
* =========================================================
*/
namespace addon\commission\job;
use addon\commission\model\Setting;
use app\model\NewBaseModel;
use think\Exception;
use think\facade\Db;
use think\facade\Queue;
use think\queue\Job;
class ComputeIntegralJob{
public function fire(Job $job,$data){
Db::startTrans();
try{
// trace($data, '平台抽成 - 全平台豆豆转积分 - 开始处理');
// 判断当前data不为数组时 强制为数组
if(!is_array($data)) $data = [];
// 并且开启处理
$page = $data['page'] ?? 1;
$ids = $data['ids'] ?? [];
$limit = 1000;
$legumesPrice = (new Setting())->getLegumesPrice();
// 获取当前页数据 有效记录、已获取积分低于订单金额
$where = [
['status', 'in', [0,1]]
];
if(count($ids) > 0) $where[] = ['id', 'in', $ids];
else $where[] = ['', 'exp', Db::raw('get_integral < order_money')];
$field = 'id,(order_money - refund_order_money) as order_money,(get_legumes - refund_get_legumes) as get_legumes,get_integral,member_id';
$result = model('commission_legumes_log')->pageList($where,$field,'',$page,$limit);
$count = $result['count'] ?? 0;
$list = $result['list'] ?? [];
if(count($list) <= 0) throw new \Exception('无处理数据!');
// 循环处理
$updateData = [];
$insertData = [];
foreach($list as $item){
// 计算最新本订单商品 总释放积分;总释放积分不能超过订单金额
$getIntegral = (float)sprintf("%.2f",$item['get_legumes'] * $legumesPrice);
$resultGetIntegral = $getIntegral >= $item['order_money'] ? $item['order_money'] : $getIntegral;
// 修改信息记录
$updateData[] = [
'id' => $item['id'],
'get_integral' => $resultGetIntegral
];
// 增加释放记录 仅存在变更数量时记录
$changeQuantity = (float)sprintf("%.2f",$resultGetIntegral - $item['get_integral']);
if($changeQuantity > 0){
$insertData[] = [
'member_id' => $item['member_id'],
'legumes_log_id' => $item['id'],
'change_front' => $item['get_integral'],
'change_quantity' => (float)sprintf("%.2f",$resultGetIntegral - $item['get_integral']),
'change_after' => $resultGetIntegral,
];
}
}
// 修改
$newBaseModel = (new NewBaseModel(['table_name' => 'commission_legumes_log', 'pk' => 'id']));
$newBaseModel->saveAll($updateData);
// 增加记录
if(count($insertData) > 0) model('commission_legumes_release_log')->addList($insertData);
// 判断:是否存在下一页
$currentLimit = $page * $limit;
if($currentLimit < $count){
$data['page'] = $page + 1;
Queue::push(ComputeIntegralJob::class,$data);
}
Db::commit();
}
catch(\Exception $e){
Db::rollback();
trace($e->getMessage(), '平台抽成 - 全平台豆豆转积分 - 失败');
}
$job->delete();
}
public function failed($data){
trace($data, '平台抽成 - 全平台豆豆转积分 - 失败(failed)');
}
}