From 30bf091369233d9f6173300ca887513472445022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Mon, 15 Mar 2021 16:35:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=A5=96=E5=8A=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/service/OrderService.php | 10 ++--- app/data/service/RebateCurrentService.php | 30 +++++++++++++-- app/data/service/UserUpgradeService.php | 45 ++++++++++++++++------- app/data/view/user_rebate/config.html | 21 ++++++++--- 4 files changed, 79 insertions(+), 27 deletions(-) diff --git a/app/data/service/OrderService.php b/app/data/service/OrderService.php index bd48867e5..db71dc6f1 100644 --- a/app/data/service/OrderService.php +++ b/app/data/service/OrderService.php @@ -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]; } diff --git a/app/data/service/RebateCurrentService.php b/app/data/service/RebateCurrentService.php index 16620f632..b019a5b3c 100644 --- a/app/data/service/RebateCurrentService.php +++ b/app/data/service/RebateCurrentService.php @@ -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; } /** diff --git a/app/data/service/UserUpgradeService.php b/app/data/service/UserUpgradeService.php index 3939e03f8..27b838e53 100644 --- a/app/data/service/UserUpgradeService.php +++ b/app/data/service/UserUpgradeService.php @@ -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; diff --git a/app/data/view/user_rebate/config.html b/app/data/view/user_rebate/config.html index c670cc690..462108af0 100644 --- a/app/data/view/user_rebate/config.html +++ b/app/data/view/user_rebate/config.html @@ -170,7 +170,7 @@
-
+
{php} $key = 'direct_state_vip_'.$level['number']; {/php} @@ -192,7 +192,7 @@
-
+
{php} $key = 'indirect_state_vip_'.$level['number']; {/php} @@ -214,7 +214,9 @@
-
+
+
+
{php} $key = 'upgrade_state_vip_'.$level['number']; {/php} @@ -227,6 +229,15 @@ {/if}
+ {php} $key = 'upgrade_type_vip_'.$level['number'].'_'.$l['number']; {/php} + + {php} $data[$key] = $data[$key] ?? 1; {/php} + {foreach [1 => '奖励固定金额 / 每人',2 => '按返利金额交易金额比例'] as $k => $v} + {if isset($data[$key]) and $data[$key] eq $k} + + {else} + + {/if}{/foreach} {php} $key = 'upgrade_value_vip_'.$level['number']; {/php}
-
+
{php} $key = 'manage_state_vip_'.$level['number']; {/php} @@ -273,11 +284,9 @@
- {if auth('save')}
- {/if}