diff --git a/app/common/repositories/store/platformCommission/LegumesLogRepository.php b/app/common/repositories/store/platformCommission/LegumesLogRepository.php index 1f24d22..f0263f1 100644 --- a/app/common/repositories/store/platformCommission/LegumesLogRepository.php +++ b/app/common/repositories/store/platformCommission/LegumesLogRepository.php @@ -48,7 +48,37 @@ class LegumesLogRepository extends BaseRepository{ return compact('count','list'); } + /** + * Common: 查询需要使用的分配信息 尽可能仅查询需要使用的信息 + * Author: wu-hui + * Time: 2023/12/29 18:31 + * @param int $uid + * @param float $useLegumesIntegral + * @param int $limit + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getUseList(int $uid,float $useLegumesIntegral,int $limit = 5){ + $model = $this->dao->getSearch([]) + ->field('id,get_integral,use_integral,(get_integral - use_integral) as surplus_integral') + ->where('uid', (int)$uid) + ->where('get_integral > use_integral') + ->where('status', 1) + ->order('id ASC'); + $count = $model->count(); + $list = $model->limit($limit)->select()->toArray(); + if($count > $limit){ + $totalSurplusIntegral = array_sum(array_column($list,'surplus_integral')); + if($useLegumesIntegral > $totalSurplusIntegral) { + $limit = $limit + 5; + return $this->getUseList($uid,$useLegumesIntegral,$limit); + } + } + return $list; + } } diff --git a/app/jobs/store/platformCommission/UseLegumesIntegralJob.php b/app/jobs/store/platformCommission/UseLegumesIntegralJob.php index 52987cc..1206e9d 100644 --- a/app/jobs/store/platformCommission/UseLegumesIntegralJob.php +++ b/app/jobs/store/platformCommission/UseLegumesIntegralJob.php @@ -3,6 +3,10 @@ namespace app\jobs\store\platformCommission; +use app\common\model\store\platformCommission\LegumesLog; +use app\common\repositories\store\order\StoreOrderRepository; +use app\common\repositories\store\platformCommission\LegumesLogRepository; +use app\common\repositories\user\UserBillRepository; use crmeb\interfaces\JobInterface; use think\facade\Log; @@ -18,13 +22,51 @@ class UseLegumesIntegralJob implements JobInterface{ public function fire($job,$data){ try{ Log::info('豆豆积分使用 - 开始处理: '.var_export($data,1)); - - - - - - - + // 获取订单信息 + $orderInfo = app()->make(StoreOrderRepository::class) + ->getSearch([]) + ->field('order_id,uid,use_legumes_integral,use_legumes_integral_price') + ->where('order_id',$data['order_id']) + ->find(); + if(!$orderInfo) throw new \Exception('信息不存在!'); + $orderInfo = $orderInfo->toArray(); + $useLegumesIntegral = (float)$orderInfo['use_legumes_integral']; + // 查询需要使用的分配记录 + $useLegumesLogList = app()->make(LegumesLogRepository::class)->getUseList((int)$orderInfo['uid'],(float)$orderInfo['use_legumes_integral']); + // 循环处理 + $updateData = []; + foreach($useLegumesLogList as $logInfo){ + $useIntegral = $useLegumesIntegral >= $logInfo['surplus_integral'] ? (float)$logInfo['surplus_integral'] : (float)$useLegumesIntegral; + $useLegumesIntegral = (float)sprintf("%.2f",$useLegumesIntegral - $useIntegral); + $updateData[] = [ + 'id' => $logInfo['id'], + 'use_integral' => (float)sprintf("%.2f",$logInfo['use_integral'] + $useIntegral) + ]; + if($useLegumesIntegral <= 0) break; + } + // 修改成功信息 + if($updateData){ + LegumesLog::batchUpdate(array_values($updateData)); + // 添加用户账单信息变更记录 + $legumesModel = (new LegumesLog())->where('uid', (int)$orderInfo['uid'])->where('status', 1); + $totalGetIntegral = $legumesModel->sum('get_integral');// 总获取积分 + $totalUseIntegral = $legumesModel->sum('use_integral');// 总已使用积分 + $hold_legumes_integral = (float)sprintf("%.2f",$totalGetIntegral - $totalUseIntegral);// 持有可使用积分 + $bills[] = [ + 'uid' => $orderInfo['uid'], + 'link_id' => $orderInfo['order_id'], + 'pm' => 0, + 'title' => '购买商品', + 'category' => 'integral', + 'type' => 'deduction', + 'number' => (float)$orderInfo['use_legumes_integral'], + 'balance' => $hold_legumes_integral, + 'mark' => '购买商品使用'.$orderInfo['use_legumes_integral'].'积分抵扣'.floatval($orderInfo['use_legumes_integral_price']).'元', + 'mer_id' => 0, + 'status' => 1 + ]; + app()->make(UserBillRepository::class)->insertAll($bills); + } } catch(\Exception $e){ $data['error_msg'] = $e->getMessage();