202 行
7.6 KiB
PHP
202 行
7.6 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\service\partner;
|
|
|
|
use think\Db;
|
|
use addons\shopro\service\Wallet;
|
|
use app\admin\model\shopro\partner\DailyReport;
|
|
use app\admin\model\shopro\partner\Reward as RewardModel;
|
|
use app\admin\model\shopro\commission\Agent as AgentModel;
|
|
|
|
/**
|
|
* 日结:马豆 / 馆县均分 / 券释放
|
|
*/
|
|
class DailySettleService
|
|
{
|
|
public function run($date = null, $rerun = false)
|
|
{
|
|
$settleDate = $date ?: date('Y-m-d', strtotime('-1 day'));
|
|
$lockDir = RUNTIME_PATH . 'lock';
|
|
if (!is_dir($lockDir)) {
|
|
@mkdir($lockDir, 0755, true);
|
|
}
|
|
$lockFile = $lockDir . '/partner_settle_' . date('Ymd', strtotime($settleDate)) . '.lock';
|
|
$fp = fopen($lockFile, 'c');
|
|
if (!$fp || !flock($fp, LOCK_EX | LOCK_NB)) {
|
|
throw new \Exception('日结正在执行,请稍后再试');
|
|
}
|
|
|
|
try {
|
|
$exist = DailyReport::where('settle_date', $settleDate)->where('status', 1)->find();
|
|
if ($exist && !$rerun) {
|
|
return $exist;
|
|
}
|
|
if ($rerun) {
|
|
$this->clearDaily($settleDate);
|
|
}
|
|
|
|
Db::transaction(function () use ($settleDate) {
|
|
$this->settle($settleDate);
|
|
});
|
|
|
|
return DailyReport::where('settle_date', $settleDate)->find();
|
|
} catch (\Exception $e) {
|
|
DailyReport::create([
|
|
'settle_date' => $settleDate,
|
|
'status' => 0,
|
|
'error_msg' => mb_substr($e->getMessage(), 0, 500),
|
|
'createtime' => time(),
|
|
'updatetime' => time(),
|
|
]);
|
|
throw $e;
|
|
} finally {
|
|
flock($fp, LOCK_UN);
|
|
fclose($fp);
|
|
}
|
|
}
|
|
|
|
protected function settle($settleDate)
|
|
{
|
|
$start = strtotime($settleDate . ' 00:00:00');
|
|
$end = strtotime($settleDate . ' 23:59:59');
|
|
$rateService = new RateService();
|
|
$active = new ActiveService();
|
|
|
|
$orders = $rateService->qualifiedOrderQuery($start, $end)->field('o.id,o.pay_fee')->select();
|
|
$orderCount = count($orders);
|
|
$gmv = 0;
|
|
foreach ($orders as $order) {
|
|
$gmv = bcadd((string) $gmv, (string) $order['pay_fee'], 2);
|
|
}
|
|
|
|
$windowStart = strtotime($settleDate . ' 00:00:00') - (PartnerConfig::activeDays() - 1) * 86400;
|
|
$tasterIds = $active->activeUserIds($windowStart, $end);
|
|
$venueIds = $active->activeUserIds($windowStart, $end, ['is_venue' => 1]);
|
|
$countyIds = $active->activeUserIds($windowStart, $end, ['is_county' => 1]);
|
|
|
|
$madow = (new MadowService())->share($settleDate, $gmv, $tasterIds);
|
|
|
|
$venuePer = (float) PartnerConfig::get('partner_venue_share_per_order', 100);
|
|
$countyPer = (float) PartnerConfig::get('partner_county_share_per_order', 50);
|
|
$venuePool = round($orderCount * $venuePer, 2);
|
|
$countyPool = round($orderCount * $countyPer, 2);
|
|
|
|
$this->cashShare($settleDate, $venueIds, $venuePool, 'venue_share', 'venue', '酒道馆');
|
|
$this->cashShare($settleDate, $countyIds, $countyPool, 'county_share', 'county', '区县');
|
|
|
|
$voucherTotal = (new VoucherService())->releaseByDate($settleDate);
|
|
|
|
$rateLog = $rateService->snapshot($settleDate, $gmv, [
|
|
'madow_pool' => $madow['pool'],
|
|
'share_user_count' => $madow['count'],
|
|
'venue_pool' => $venuePool,
|
|
'county_pool' => $countyPool,
|
|
]);
|
|
|
|
DailyReport::create([
|
|
'settle_date' => $settleDate,
|
|
'order_count' => $orderCount,
|
|
'order_count_n' => $rateLog->order_count_n ?? $rateService->countN(),
|
|
'gmv' => $gmv,
|
|
'rate' => $rateLog->rate ?? 0,
|
|
'madow_pool' => $madow['pool'],
|
|
'share_user_count' => $madow['count'],
|
|
'venue_pool' => $venuePool,
|
|
'venue_user_count' => count($venueIds),
|
|
'county_pool' => $countyPool,
|
|
'county_user_count' => count($countyIds),
|
|
'voucher_release_total' => $voucherTotal,
|
|
'status' => 1,
|
|
'error_msg' => '',
|
|
'createtime' => time(),
|
|
'updatetime' => time(),
|
|
]);
|
|
}
|
|
|
|
protected function cashShare($settleDate, $userIds, $pool, $type, $role, $label)
|
|
{
|
|
$count = count($userIds);
|
|
$orderId = PartnerRewardService::dailyOrderId($settleDate);
|
|
if ($pool <= 0) {
|
|
return;
|
|
}
|
|
if ($count <= 0) {
|
|
PartnerRewardService::write([
|
|
'order_id' => $orderId,
|
|
'user_id' => 0,
|
|
'role' => 'system',
|
|
'reward_type' => $type,
|
|
'amount' => $pool,
|
|
'base_amount' => $pool,
|
|
'settle_date' => $settleDate,
|
|
'ext' => ['formula' => "{$label}分母为0,奖池 {$pool} 进平台"],
|
|
'status' => 1,
|
|
]);
|
|
return;
|
|
}
|
|
$each = function_exists('bcdiv') ? bcdiv((string) $pool, (string) $count, 2) : number_format($pool / $count, 2, '.', '');
|
|
$distributed = 0;
|
|
foreach ($userIds as $userId) {
|
|
PartnerRewardService::write([
|
|
'order_id' => $orderId,
|
|
'user_id' => $userId,
|
|
'role' => $role,
|
|
'reward_type' => $type,
|
|
'amount' => $each,
|
|
'base_amount' => $pool,
|
|
'settle_date' => $settleDate,
|
|
'ext' => ['formula' => "{$label}奖池 {$pool} / {$count} = {$each}", 'share_user_count' => $count],
|
|
'status' => 1,
|
|
]);
|
|
if ((float) $each > 0) {
|
|
Wallet::change($userId, 'commission', $each, $type, [
|
|
'settle_date' => $settleDate,
|
|
]);
|
|
AgentModel::where('user_id', $userId)->setInc('total_income', $each);
|
|
}
|
|
$distributed = bcadd((string) $distributed, (string) $each, 2);
|
|
}
|
|
$tail = bcsub((string) $pool, (string) $distributed, 2);
|
|
if ((float) $tail > 0) {
|
|
PartnerRewardService::write([
|
|
'order_id' => $orderId,
|
|
'user_id' => 0,
|
|
'role' => 'system',
|
|
'reward_type' => $type,
|
|
'amount' => $tail,
|
|
'base_amount' => $pool,
|
|
'settle_date' => $settleDate,
|
|
'ext' => ['formula' => "{$label}尾差 {$tail} 进平台"],
|
|
'status' => 1,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function clearDaily($settleDate)
|
|
{
|
|
(new VoucherService())->rollbackRelease($settleDate);
|
|
(new MadowService())->rollback($settleDate);
|
|
|
|
$cashRows = RewardModel::where('settle_date', $settleDate)
|
|
->whereIn('reward_type', ['venue_share', 'county_share'])
|
|
->where('status', 1)
|
|
->where('user_id', '>', 0)
|
|
->select();
|
|
foreach ($cashRows as $row) {
|
|
if ((float) $row->amount > 0) {
|
|
Wallet::change($row->user_id, 'commission', -$row->amount, 'reward_back', [
|
|
'settle_date' => $settleDate,
|
|
'rerun' => 1,
|
|
], '日结重跑扣回');
|
|
AgentModel::where('user_id', $row->user_id)->setDec('total_income', $row->amount);
|
|
}
|
|
}
|
|
|
|
RewardModel::where('settle_date', $settleDate)
|
|
->whereIn('reward_type', PartnerConfig::dailyTypes())
|
|
->delete();
|
|
DailyReport::where('settle_date', $settleDate)->delete();
|
|
\app\admin\model\shopro\partner\RateLog::where('settle_date', $settleDate)->delete();
|
|
}
|
|
}
|