feat: 临时调整马豆结算与兑换券释放为1分钟执行一次
这个提交包含在:
@@ -55,6 +55,148 @@ class DailySettleService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时分钟级调度:执行未结算订单的马豆分红,并释放一期兑换券
|
||||
*/
|
||||
public function runMinute()
|
||||
{
|
||||
$lockDir = RUNTIME_PATH . 'lock';
|
||||
if (!is_dir($lockDir)) {
|
||||
@mkdir($lockDir, 0755, true);
|
||||
}
|
||||
$lockFile = $lockDir . '/partner_settle_minute.lock';
|
||||
$fp = fopen($lockFile, 'c');
|
||||
if (!$fp || !flock($fp, LOCK_EX | LOCK_NB)) {
|
||||
return ['status' => 'skipped', 'message' => '上一次分钟任务仍在执行中'];
|
||||
}
|
||||
|
||||
try {
|
||||
$today = date('Y-m-d');
|
||||
$rateService = new RateService();
|
||||
$rewardService = new PartnerRewardService();
|
||||
$activeService = new ActiveService();
|
||||
|
||||
// 1. 兑换券 1 分钟释放一次
|
||||
$voucherTotal = (new VoucherService())->releaseMinute();
|
||||
|
||||
// 2. 马豆 1 分钟结算一次(提取已支付但尚未结算马豆的合格订单)
|
||||
$allQualifiedOrders = $rateService->qualifiedOrderQuery()->field('o.id,o.pay_fee,o.user_id,o.ext,o.paid_time')->select();
|
||||
$unsettledOrders = [];
|
||||
$madowGmv = 0;
|
||||
|
||||
foreach ($allQualifiedOrders as $o) {
|
||||
$ext = $o['ext'] ?? [];
|
||||
if (is_string($ext)) {
|
||||
$ext = json_decode($ext, true) ?: [];
|
||||
}
|
||||
if (empty($ext['madow_settled'])) {
|
||||
$unsettledOrders[] = $o;
|
||||
$qFee = $rewardService->getQualifiedPayFee($o);
|
||||
$madowGmv = bcadd((string) $madowGmv, (string) $qFee, 2);
|
||||
}
|
||||
}
|
||||
|
||||
$madowRes = ['pool' => 0, 'count' => 0, 'each' => 0];
|
||||
$settledOrderIds = [];
|
||||
|
||||
if ((float) $madowGmv > 0 && !empty($unsettledOrders)) {
|
||||
$settledOrderIds = array_column($unsettledOrders, 'id');
|
||||
$windowStart = time() - (PartnerConfig::activeDays() - 1) * 86400;
|
||||
$tasterIds = $activeService->activeUserIds($windowStart, time());
|
||||
|
||||
$madowRes = (new MadowService())->shareMinute($madowGmv, $tasterIds, $settledOrderIds);
|
||||
|
||||
// 标记订单已结算马豆
|
||||
foreach ($unsettledOrders as $o) {
|
||||
$ext = $o['ext'] ?? [];
|
||||
if (is_string($ext)) {
|
||||
$ext = json_decode($ext, true) ?: [];
|
||||
}
|
||||
$ext['madow_settled'] = 1;
|
||||
$ext['madow_settle_time'] = time();
|
||||
\think\Db::name('shopro_order')->where('id', $o['id'])->update([
|
||||
'ext' => json_encode($ext, JSON_UNESCAPED_UNICODE),
|
||||
]);
|
||||
}
|
||||
|
||||
// 清除汇率缓存,联动大盘最新动态汇率
|
||||
RateService::clearCache();
|
||||
}
|
||||
|
||||
// 3. 统计今日最新大盘并同步/创建今日 DailyReport
|
||||
$todayStart = strtotime($today . ' 00:00:00');
|
||||
$todayEnd = strtotime($today . ' 23:59:59');
|
||||
$todayGmv = $rateService->qualifiedGmv($todayStart, $todayEnd);
|
||||
$todayOrderCount = $rateService->qualifiedOrderQuery($todayStart, $todayEnd)->count();
|
||||
$orderCountN = $rateService->countN();
|
||||
$currentRate = $rateService->calcRate($orderCountN);
|
||||
|
||||
// 当日累计发放兑换券总额
|
||||
$todayVoucherReleased = (float) \think\Db::name('shopro_partner_reward')
|
||||
->whereBetween('createtime', [$todayStart, $todayEnd])
|
||||
->where('reward_type', 'voucher_release')
|
||||
->where('status', 1)
|
||||
->sum('amount');
|
||||
|
||||
// 当日累计发放马豆总额
|
||||
$todayMadowPool = (float) \think\Db::name('shopro_partner_reward')
|
||||
->whereBetween('createtime', [$todayStart, $todayEnd])
|
||||
->where('reward_type', 'madow_share')
|
||||
->where('status', 1)
|
||||
->sum('madow');
|
||||
|
||||
$existingTodayReport = DailyReport::where('settle_date', $today)->find();
|
||||
if ($existingTodayReport) {
|
||||
$existingTodayReport->order_count = $todayOrderCount;
|
||||
$existingTodayReport->order_count_n = $orderCountN;
|
||||
$existingTodayReport->gmv = $todayGmv;
|
||||
$existingTodayReport->rate = $currentRate;
|
||||
$existingTodayReport->madow_pool = $todayMadowPool;
|
||||
$existingTodayReport->voucher_release_total = $todayVoucherReleased;
|
||||
$existingTodayReport->updatetime = time();
|
||||
$existingTodayReport->save();
|
||||
} else {
|
||||
DailyReport::create([
|
||||
'settle_date' => $today,
|
||||
'order_count' => $todayOrderCount,
|
||||
'order_count_n' => $orderCountN,
|
||||
'gmv' => $todayGmv,
|
||||
'rate' => $currentRate,
|
||||
'madow_pool' => $todayMadowPool,
|
||||
'share_user_count' => $madowRes['count'] ?? 0,
|
||||
'venue_pool' => 0,
|
||||
'venue_user_count' => 0,
|
||||
'county_pool' => 0,
|
||||
'county_user_count' => 0,
|
||||
'voucher_release_total' => $todayVoucherReleased,
|
||||
'status' => 1,
|
||||
'error_msg' => '',
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
]);
|
||||
}
|
||||
|
||||
// 更新汇率日志
|
||||
$rateService->snapshot($today, $todayGmv, [
|
||||
'madow_pool' => $todayMadowPool,
|
||||
'share_user_count' => $madowRes['count'] ?? 0,
|
||||
]);
|
||||
|
||||
return [
|
||||
'status' => 'success',
|
||||
'time' => date('Y-m-d H:i:s'),
|
||||
'voucher_released' => $voucherTotal,
|
||||
'madow_settled_gmv' => (float) $madowGmv,
|
||||
'madow_pool' => $madowRes['pool'],
|
||||
'madow_each' => $madowRes['each'],
|
||||
'settled_orders' => $settledOrderIds,
|
||||
];
|
||||
} finally {
|
||||
flock($fp, LOCK_UN);
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
||||
|
||||
protected function settle($settleDate)
|
||||
{
|
||||
$start = strtotime($settleDate . ' 00:00:00');
|
||||
|
||||
在新工单中引用
屏蔽一个用户