修改奖励

This commit is contained in:
邹景立 2021-03-15 16:35:22 +08:00
parent 2e87a347b2
commit 30bf091369
4 changed files with 79 additions and 27 deletions

View File

@ -60,16 +60,16 @@ class OrderService extends Service
/**
* 根据订单更新用户等级
* @param string $order_no
* @param string $orderNo
* @return array|null [USER, ORDER, ENTRY]
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncUserLevel(string $order_no): ?array
public function syncUserLevel(string $orderNo): ?array
{
// 目标订单数据
$map = [['order_no', '=', $order_no], ['status', '>=', 4]];
$map = [['order_no', '=', $orderNo], ['status', '>=', 4]];
$order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) return null;
// 订单用户数据
@ -85,12 +85,12 @@ class OrderService extends Service
// 重置用户信息并绑定订单
$user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find();
if ($user['pid1'] > 0) {
$this->app->db->name('ShopOrder')->where(['order_no' => $order_no])->update([
$this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update([
'puid1' => $user['pid1'], 'puid2' => $user['pid2'],
]);
}
// 重新计算用户等级
UserUpgradeService::instance()->syncLevel($user['id']);
UserUpgradeService::instance()->syncLevel($user['id'], $orderNo);
return [$user, $order, $entry];
}

View File

@ -396,11 +396,35 @@ class RebateCurrentService extends Service
}
/**
* 升级奖励发放
* 用户升级奖励发放
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
private function _prize07()
private function _prize07(): bool
{
if ($this->order['order_no'] !== $this->user['vip_order']) return false;
if (!$this->checkLevelPrize(self::PRIZE_07, $this->from1['vip_code'])) return false;
// 创建返利奖励记录
$key = "{$this->user['id']}";
$map = ['type' => self::PRIZE_07, 'order_no' => $this->order['order_no'], 'order_uid' => $this->order['uid']];
if ($this->config("upgrade_state_vip_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) {
$value = $this->config("upgrade_value_vip_{$key}");
if ($this->config("upgrade_type_vip_{$key}") == 1) {
$amount = $value ?: '0.00';
$name = "{$this->name(self::PRIZE_07)},每人 {$amount}";
} else {
$amount = $value * $this->order['rebate_amount'] / 100;
$name = "{$this->name(self::PRIZE_07)},订单 {$value}%";
}
$this->app->db->name($this->table)->insert(array_merge($map, [
'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'status' => $this->status, 'order_amount' => $this->order['amount_total'],
]));
// 更新用户奖利金额
UserUpgradeService::instance()->syncLevel($this->from1['id']);
}
return true;
}
/**

View File

@ -12,6 +12,23 @@ use think\admin\Service;
class UserUpgradeService extends Service
{
/**
* 同步刷新用户返利
* @param integer $uuid
* @return array [total, count, lock]
* @throws \think\db\exception\DbException
*/
public function syncRebate(int $uuid): array
{
$total = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount'));
$count = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount'));
$lockd = abs($this->app->db->name('DataUserRebate')->where("uid='{$uuid}' and status=0 and amount<0 and deleted=0")->sum('amount'));
$this->app->db->name('DataUser')->where(['id' => $uuid])->update([
'rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $lockd,
]);
return [$total, $count, $lockd];
}
/**
* 同步刷新用户余额
* @param int $uuid 用户UID
@ -23,15 +40,12 @@ class UserUpgradeService extends Service
{
$total = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount>0 and deleted=0")->sum('amount'));
$count = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=1 and amount<0 and deleted=0")->sum('amount'));
$locks = abs($this->app->db->name('DataUserBalance')->where("uid='{$uuid}' and status=0 and amount>0 and deleted=0")->sum('amount'));
if (empty($nots)) {
$this->app->db->name('DataUser')->where(['id' => $uuid])->update([
'balance_total' => $total, 'balance_used' => $count, 'balance_lock' => $locks,
]);
$this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
} else {
$count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount');
}
return [$total, $count, $locks];
return [$total, $count];
}
/**
@ -87,20 +101,24 @@ class UserUpgradeService extends Service
* 同步计算用户等级
* @param integer $uid 指定用户UID
* @param boolean $parent 同步计算上级
* @param ?string $orderNo 升级触发订单
* @return boolean
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function syncLevel(int $uid, bool $parent = true): bool
public function syncLevel(int $uid, bool $parent = true, ?string $orderNo = null): bool
{
$user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) return true;
// 刷新用户返利
$this->syncRebate($uid);
// 开始处理等级
[$vipName, $vipCode] = ['普通用户', 0];
// 统计历史数据
$teamsDirect = $this->app->db->name('DataUser')->where(['pid1' => $uid])->count();
$teamsIndirect = $this->app->db->name('DataUser')->where(['pid2' => $uid])->count();
$teamsUsers = $this->app->db->name('DataUser')->where(['pid1|pid2' => $uid])->count();
$teamsDirect = $this->app->db->name('DataUser')->where(['pid1' => $uid])->where('vip_code>0')->count();
$teamsIndirect = $this->app->db->name('DataUser')->where(['pid2' => $uid])->where('vip_code>0')->count();
$teamsUsers = $this->app->db->name('DataUser')->where(['pid1|pid2' => $uid])->where('vip_code>0')->count();
$orderAmount = $this->app->db->name('ShopOrder')->where("uid={$uid} and status>=4")->sum('amount_total');
// 计算用户等级
foreach ($this->app->db->name('DataUserUpgrade')->where(['status' => 1])->order('number desc')->cursor() as $item) {
@ -125,6 +143,8 @@ class UserUpgradeService extends Service
$map = ['status' => 1, 'number' => $tmpNumber];
$upgrade = $this->app->db->name('DataUserUpgrade')->where($map)->find();
if (!empty($upgrade)) [$vipName, $vipCode] = [$upgrade['name'], $upgrade['number']];
} else {
$orderNo = null;
}
// 统计订单金额
$orderAmountTotal = $this->app->db->name('ShopOrder')->whereRaw("uid={$uid} and status>=4")->sum('amount_goods');
@ -142,14 +162,13 @@ class UserUpgradeService extends Service
'teams_amount_indirect' => $teamsAmountIndirect,
'order_amount_total' => $orderAmountTotal,
];
if ($data['vip_code'] !== $user['vip_code']) {
$data['vip_datetime'] = date('Y-m-d H:i:s');
}
if (!empty($orderNo)) $data['vip_order'] = $orderNo;
if ($data['vip_code'] !== $user['vip_code']) $data['vip_datetime'] = date('Y-m-d H:i:s');
$this->app->db->name('DataUser')->where(['id' => $uid])->update($data);
if ($user['vip_code'] < $vipCode) {
// 用户升级事件
$this->app->event->trigger('UserUpgradeLevel', [
'uid' => $user['uid'], 'vip_code_old' => $user['vip_code'], 'vip_code_new' => $vipCode,
'uid' => $user['uid'], 'order_no' => $orderNo, 'vip_code_old' => $user['vip_code'], 'vip_code_new' => $vipCode,
]);
}
return ($parent && $user['pid2'] > 0) ? $this->syncLevel($user['pid2'], false) : true;

View File

@ -170,7 +170,7 @@
</table>
<div class="padding-20">
<div class="layui-row layui-col-space20">
<div class="layui-col-xs4">
<div class="layui-col-xs6">
<fieldset class="block">
<legend>
{php} $key = 'direct_state_vip_'.$level['number']; {/php}
@ -192,7 +192,7 @@
</div>
</fieldset>
</div>
<div class="layui-col-xs4">
<div class="layui-col-xs6">
<fieldset class="block">
<legend>
{php} $key = 'indirect_state_vip_'.$level['number']; {/php}
@ -214,7 +214,9 @@
</div>
</fieldset>
</div>
<div class="layui-col-xs4">
</div>
<div class="layui-row layui-col-space20">
<div class="layui-col-xs6">
<fieldset class="block">
<legend>
{php} $key = 'upgrade_state_vip_'.$level['number']; {/php}
@ -227,6 +229,15 @@
{/if}
</legend>
<div class="layui-form-item nowrap">
{php} $key = 'upgrade_type_vip_'.$level['number'].'_'.$l['number']; {/php}
<input type="hidden" name="{$key}" value="1">
{php} $data[$key] = $data[$key] ?? 1; {/php}
{foreach [1 => '奖励固定金额 / 每人',2 => '按返利金额交易金额比例'] as $k => $v}
{if isset($data[$key]) and $data[$key] eq $k}
<input checked class="layui-input" type="radio" name="{$key}" value="{$k}" title="{$v}">
{else}
<input class="layui-input" type="radio" name="{$key}" value="{$k}" title="{$v}">
{/if}{/foreach}
{php} $key = 'upgrade_value_vip_'.$level['number']; {/php}
<input class="layui-input" type="hidden" name="upgrade_type_vip_{$level.number}" value="2">
<label class="block">
@ -236,7 +247,7 @@
</div>
</fieldset>
</div>
<div class="layui-col-xs4">
<div class="layui-col-xs6">
<fieldset class="block">
<legend>
{php} $key = 'manage_state_vip_'.$level['number']; {/php}
@ -273,11 +284,9 @@
</div>
</div>
{if auth('save')}
<div class="layui-form-item text-center margin-top-30">
<button class="layui-btn" type='submit'>保存配置</button>
</div>
{/if}
</div>
</form>