修改奖励

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] * @return array|null [USER, ORDER, ENTRY]
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @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(); $order = $this->app->db->name('ShopOrder')->where($map)->find();
if (empty($order)) return null; if (empty($order)) return null;
// 订单用户数据 // 订单用户数据
@ -85,12 +85,12 @@ class OrderService extends Service
// 重置用户信息并绑定订单 // 重置用户信息并绑定订单
$user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find(); $user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find();
if ($user['pid1'] > 0) { 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'], 'puid1' => $user['pid1'], 'puid2' => $user['pid2'],
]); ]);
} }
// 重新计算用户等级 // 重新计算用户等级
UserUpgradeService::instance()->syncLevel($user['id']); UserUpgradeService::instance()->syncLevel($user['id'], $orderNo);
return [$user, $order, $entry]; 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 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 * @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')); $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')); $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)) { if (empty($nots)) {
$this->app->db->name('DataUser')->where(['id' => $uuid])->update([ $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]);
'balance_total' => $total, 'balance_used' => $count, 'balance_lock' => $locks,
]);
} else { } else {
$count -= $this->app->db->name('DataUserBalance')->whereRaw("uid={$uuid}")->whereIn('code', $nots)->sum('amount'); $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 integer $uid 指定用户UID
* @param boolean $parent 同步计算上级 * @param boolean $parent 同步计算上级
* @param ?string $orderNo 升级触发订单
* @return boolean * @return boolean
* @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException * @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException * @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(); $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find();
if (empty($user)) return true; if (empty($user)) return true;
// 刷新用户返利
$this->syncRebate($uid);
// 开始处理等级
[$vipName, $vipCode] = ['普通用户', 0]; [$vipName, $vipCode] = ['普通用户', 0];
// 统计历史数据 // 统计历史数据
$teamsDirect = $this->app->db->name('DataUser')->where(['pid1' => $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])->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])->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'); $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) { 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]; $map = ['status' => 1, 'number' => $tmpNumber];
$upgrade = $this->app->db->name('DataUserUpgrade')->where($map)->find(); $upgrade = $this->app->db->name('DataUserUpgrade')->where($map)->find();
if (!empty($upgrade)) [$vipName, $vipCode] = [$upgrade['name'], $upgrade['number']]; 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'); $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, 'teams_amount_indirect' => $teamsAmountIndirect,
'order_amount_total' => $orderAmountTotal, 'order_amount_total' => $orderAmountTotal,
]; ];
if ($data['vip_code'] !== $user['vip_code']) { if (!empty($orderNo)) $data['vip_order'] = $orderNo;
$data['vip_datetime'] = date('Y-m-d H:i:s'); 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); $this->app->db->name('DataUser')->where(['id' => $uid])->update($data);
if ($user['vip_code'] < $vipCode) { if ($user['vip_code'] < $vipCode) {
// 用户升级事件 // 用户升级事件
$this->app->event->trigger('UserUpgradeLevel', [ $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; return ($parent && $user['pid2'] > 0) ? $this->syncLevel($user['pid2'], false) : true;

View File

@ -170,7 +170,7 @@
</table> </table>
<div class="padding-20"> <div class="padding-20">
<div class="layui-row layui-col-space20"> <div class="layui-row layui-col-space20">
<div class="layui-col-xs4"> <div class="layui-col-xs6">
<fieldset class="block"> <fieldset class="block">
<legend> <legend>
{php} $key = 'direct_state_vip_'.$level['number']; {/php} {php} $key = 'direct_state_vip_'.$level['number']; {/php}
@ -192,7 +192,7 @@
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div class="layui-col-xs4"> <div class="layui-col-xs6">
<fieldset class="block"> <fieldset class="block">
<legend> <legend>
{php} $key = 'indirect_state_vip_'.$level['number']; {/php} {php} $key = 'indirect_state_vip_'.$level['number']; {/php}
@ -214,7 +214,9 @@
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div class="layui-col-xs4"> </div>
<div class="layui-row layui-col-space20">
<div class="layui-col-xs6">
<fieldset class="block"> <fieldset class="block">
<legend> <legend>
{php} $key = 'upgrade_state_vip_'.$level['number']; {/php} {php} $key = 'upgrade_state_vip_'.$level['number']; {/php}
@ -227,6 +229,15 @@
{/if} {/if}
</legend> </legend>
<div class="layui-form-item nowrap"> <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} {php} $key = 'upgrade_value_vip_'.$level['number']; {/php}
<input class="layui-input" type="hidden" name="upgrade_type_vip_{$level.number}" value="2"> <input class="layui-input" type="hidden" name="upgrade_type_vip_{$level.number}" value="2">
<label class="block"> <label class="block">
@ -236,7 +247,7 @@
</div> </div>
</fieldset> </fieldset>
</div> </div>
<div class="layui-col-xs4"> <div class="layui-col-xs6">
<fieldset class="block"> <fieldset class="block">
<legend> <legend>
{php} $key = 'manage_state_vip_'.$level['number']; {/php} {php} $key = 'manage_state_vip_'.$level['number']; {/php}
@ -273,11 +284,9 @@
</div> </div>
</div> </div>
{if auth('save')}
<div class="layui-form-item text-center margin-top-30"> <div class="layui-form-item text-center margin-top-30">
<button class="layui-btn" type='submit'>保存配置</button> <button class="layui-btn" type='submit'>保存配置</button>
</div> </div>
{/if}
</div> </div>
</form> </form>