Files
huangjingfen/pro_v3.5.1_副本/app/services/hjf/QueuePoolServices.php
apple 434aa8c69d feat(fsgx): 完成全部24项开发任务 Phase1-7
Phase1 后端核心:
- 新增 fsgx_v1.sql 迁移脚本(is_queue_goods/frozen_points/available_points/no_assess)
- SystemConfigServices 返佣设置扩展(周期人数/分档比例/范围/时机)
- StoreOrderCreateServices 周期循环佣金计算
- StoreOrderTakeServices 佣金发放后同步冻结积分
- StoreProductServices/StoreProduct 保存 is_queue_goods

Phase2 后端接口:
- GET /api/hjf/brokerage/progress 佣金周期进度
- GET /api/hjf/assets/overview 资产总览
- HjfPointsServices 每日 frozen_points 0.4‰ 释放定时任务
- PUT /adminapi/hjf/member/{uid}/no_assess 不考核接口
- GET /adminapi/hjf/points/release_log 积分日志接口

Phase3 前端清理:
- hjfCustom.js 路由精简(仅保留 points/log)
- hjfQueue.js/hjfMember.js API 清理/重定向至 CRMEB 原生接口
- pages.json 公排→推荐佣金/佣金记录/佣金规则

Phase4-5 前端改造:
- queue/status.vue 推荐佣金进度页整体重写
- 商品详情/订单确认/支付结果页文案与逻辑改造
- 个人中心/资产页/引导页/规则页文案改造
- HjfQueueProgress/HjfRefundNotice/HjfAssetCard 组件改造
- 推广中心嵌入佣金进度摘要
- hjfMockData.js 全量更新(公排字段→佣金字段)

Phase6 Admin 增强:
- 用户列表新增 frozen_points/available_points 列及不考核操作按钮
- hjfPoints.js USE_MOCK=false 对接真实积分日志接口

Phase7 配置文档:
- docs/fsgx-phase7-config-checklist.md 后台配置与全链路验收清单

Made-with: Cursor
2026-03-23 22:32:19 +08:00

194 lines
5.8 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
declare(strict_types=1);
namespace app\services\hjf;
use app\dao\hjf\QueuePoolDao;
use app\jobs\hjf\QueueRefundJob;
use app\services\BaseServices;
use app\services\user\UserServices;
use crmeb\services\CacheService;
use crmeb\services\SystemConfigService;
use think\annotation\Inject;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
/**
* 公排池服务
*
* 负责入队enqueue+ 退款触发条件判断 + 统计信息查询。
* 退款的实际执行委托给 QueueRefundJob异步以避免支付回调阻塞。
*
* Class QueuePoolServices
* @package app\services\hjf
* @mixin QueuePoolDao
*/
class QueuePoolServices extends BaseServices
{
#[Inject]
protected QueuePoolDao $dao;
/** Redis 分布式锁 Key */
const LOCK_KEY = 'hjf:queue:enqueue_lock';
/** 锁超时(秒) */
const LOCK_TTL = 10;
/**
* 报单商品订单入队
*
* 使用 Redis SET NX EX 分布式锁保证同一时刻只有一个入队+触发检测操作执行。
*
* @param int $uid 用户 ID
* @param string $orderId 原始订单号
* @param float $amount 金额(默认 3600.00
* @return array 新入队记录数组
* @throws ValidateException
*/
public function enqueue(int $uid, string $orderId, float $amount = 3600.00): array
{
$lockKey = self::LOCK_KEY;
$lockValue = uniqid('', true);
// 获取 Redis 实例
/** @var \Redis $redis */
$redis = CacheService::getRedis();
// SET NX EX 原子锁
$acquired = $redis->set($lockKey, $lockValue, ['NX', 'EX' => self::LOCK_TTL]);
if (!$acquired) {
throw new ValidateException('公排入队繁忙,请稍后重试');
}
try {
return Db::transaction(function () use ($uid, $orderId, $amount, $redis, $lockKey, $lockValue) {
$queueNo = $this->dao->nextQueueNo();
$record = $this->dao->save([
'uid' => $uid,
'order_id' => $orderId,
'amount' => $amount,
'queue_no' => $queueNo,
'status' => 0,
'refund_time' => 0,
'trigger_batch' => 0,
]);
$data = $record->toArray();
// 检查是否触发退款条件
$this->checkAndTriggerRefund();
return $data;
});
} finally {
// 释放锁Lua 原子删除,防止误删他人的锁)
$script = <<<'LUA'
if redis.call("GET", KEYS[1]) == ARGV[1] then
return redis.call("DEL", KEYS[1])
else
return 0
end
LUA;
$redis->eval($script, [$lockKey, $lockValue], 1);
}
}
/**
* 检查是否达到退款触发条件,若是则派发异步退款 Job
*
* 触发条件:当前排队中总单数 ≥ triggerMultiple默认4
* 即每进入4单就对最早的1单触发退款。
*/
public function checkAndTriggerRefund(): void
{
$multiple = (int)SystemConfigService::get('hjf_trigger_multiple', 4);
$pending = $this->dao->countPending();
if ($pending < $multiple) {
return;
}
$earliest = $this->dao->getEarliestPending();
if (!$earliest) {
return;
}
// 批次号 = 历史已退款总数 + 1
$batchNo = $this->dao->count(['status' => 1]) + 1;
// 派发异步退款 Job
QueueRefundJob::dispatch($earliest['id'], $earliest['uid'], $earliest['amount'], $batchNo);
}
/**
* 获取用户的公排状态摘要(用于状态页)
*/
public function getUserStatus(int $uid): array
{
$multiple = (int)SystemConfigService::get('hjf_trigger_multiple', 4);
$pending = $this->dao->countPending();
$total = $this->dao->countTotal();
// 当前批次已入队单数(本批次进度)
$batchCount = $pending % $multiple;
// 用户自己的订单
$myOrders = $this->dao->getModel()
->where('uid', $uid)
->order('add_time', 'desc')
->select()
->toArray();
foreach ($myOrders as &$item) {
$item['estimated_wait'] = $item['status'] === 1
? '已退款'
: $this->estimateWait((int)$item['queue_no'], $pending, $multiple);
}
unset($item);
return [
'total_orders' => $total,
'my_orders' => $myOrders,
'progress' => [
'current_batch_count' => $batchCount,
'trigger_multiple' => $multiple,
'next_refund_queue_no' => $this->dao->getEarliestPending()['queue_no'] ?? 0,
],
];
}
/**
* 获取用户公排历史(分页,支持按状态筛选)
*/
public function getUserHistory(int $uid, int $status, int $page, int $limit): array
{
$result = $this->dao->getUserList($uid, $status, $page, $limit);
foreach ($result['list'] as &$item) {
$item['time_key'] = date('Y-m-d', (int)$item['add_time']);
}
unset($item);
return $result;
}
/**
* 简单估算等待时间(基于队列位置)
*/
private function estimateWait(int $queueNo, int $pending, int $multiple): string
{
$earliest = $this->dao->getEarliestPending();
if (!$earliest) {
return '--';
}
$positionFromFront = $queueNo - (int)$earliest['queue_no'];
if ($positionFromFront <= 0) {
return '即将退款';
}
$waitCycles = (int)ceil($positionFromFront / $multiple);
return "约等待 {$waitCycles}";
}
}