diff --git a/app/data/command/UserLevel.php b/app/data/command/UserLevel.php new file mode 100644 index 000000000..63ec30a81 --- /dev/null +++ b/app/data/command/UserLevel.php @@ -0,0 +1,13 @@ +title = '用户等级管理'; + $this->_query($this->table)->order('number asc')->page(); + } + + /** + * 数据列表处理 + * @param array $data + */ + protected function _page_filter(array &$data) + { + foreach ($data as &$vo) { + $vo['rebate_rule'] = str2arr($vo['rebate_rule']); + } + } + + /** + * 添加用户等级 + * @auth true + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function add() + { + $this->_form($this->table, 'form'); + } + + /** + * 编辑用户等级 + * @auth true + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit() + { + $this->_form($this->table, 'form'); + } + + /** + * 表单数据处理 + * @param array $vo + */ + protected function _form_filter(array &$vo) + { + if ($this->request->isGet()) { + $this->rules = ['首推奖利', '复购奖利', '直属团队', '间接团队', '差额奖励']; + $vo['rebate_rule'] = str2arr($vo['rebate_rule'] ?? ''); + } else { + $vo['utime'] = time(); + $vo['rebate_rule'] = arr2str($vo['rebate_rule'] ?? []); + $vo['goods_vip_status'] = isset($vo['goods_vip_status']) ? 1 : 0; + $vo['order_amount_status'] = isset($vo['order_amount_status']) ? 1 : 0; + $vo['teams_total_status'] = isset($vo['teams_total_status']) ? 1 : 0; + $vo['teams_direct_status'] = isset($vo['teams_direct_status']) ? 1 : 0; + $vo['teams_indirect_status'] = isset($vo['teams_indirect_status']) ? 1 : 0; + // 根据数量判断状态 + $vo['order_amount_status'] = intval($vo['order_amount_status'] && $vo['order_amount_number'] > 0); + $vo['teams_total_status'] = intval($vo['teams_total_status'] && $vo['teams_total_number'] > 0); + $vo['teams_direct_status'] = intval($vo['teams_direct_status'] && $vo['teams_direct_number'] > 0); + $vo['teams_indirect_status'] = intval($vo['teams_indirect_status'] && $vo['teams_indirect_number'] > 0); + $state = 0; + foreach ($vo as $k => $v) if (stripos($k, '_status') !== false) $state += $v; + if (empty($state)) $this->error('升级条件不能为空!'); + } + } + + /** + * 表单结果处理 + * @param boolean $state + * @throws \think\db\exception\DbException + */ + public function _form_result(bool $state) + { + if ($state) { + $order = 'number asc,utime desc'; + if (input('old_level', 100) < input('level', '0')) $order = 'number asc,utime asc'; + foreach ($this->app->db->name($this->table)->order($order)->cursor() as $k => $vo) { + $this->app->db->name($this->table)->where(['id' => $vo['id']])->update(['number' => $k + 1]); + } + } + } + + /** + * 修改等级状态 + * @auth true + * @throws \think\db\exception\DbException + */ + public function state() + { + $this->_save($this->table); + } + + /** + * 删除用户等级 + * @auth true + * @throws \think\db\exception\DbException + */ + public function remove() + { + $this->_delete($this->table); + } + + /** + * 状态变更处理 + * @auth true + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function _save_result() + { + $this->_form_result(true); + } + + /** + * 删除结果处理 + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + protected function _delete_result() + { + $this->_form_result(true); + } + +} \ No newline at end of file diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 4d53c1e56..074c06c25 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -209,6 +209,78 @@ class UserService extends Service } } + /** + * 同步计算会员级别 + * @param integer $uid 指定会员uid + * @param boolean $parent 同步计算上级 + * @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 + { + $user = $this->app->db->name('DataUser')->where(['id' => $uid])->find(); + if (empty($user)) return true; + [$level, $title] = [0, '普通用户']; + // 统计历史数据 + $teamsDirect = $this->app->db->name('DataUser')->where(['pid1' => $uid])->count(); + $teamsIndirect = $this->app->db->name('DataUser')->where(['pid2' => $uid])->count(); + $amountTotal = $this->app->db->name('ShopOrder')->where(['uid' => $uid])->whereIn('status', [3, 4, 5])->sum('amount_total'); + // 计算会员级别 + foreach ($this->app->db->name('DataUserLevel')->where(['status' => 1])->order('number desc')->select()->toArray() as $item) { + $l1 = empty($item['goods_vip_status']) || $user['vip_auth'] > 0; + $l2 = empty($item['teams_user_status']) || $item['teams_user_number'] <= $teamsDirect + $teamsIndirect; + $l3 = empty($item['teams_direct_status']) || $item['teams_direct_number'] <= $teamsDirect; + $l4 = empty($item['teams_indirect_status']) || $item['teams_indirect_number'] <= $teamsIndirect; + $l5 = empty($item['order_amount_status']) || $item['order_amount_number'] <= $amountTotal; + if ( + ($item['upgrade_type'] == 0 && ($l1 || $l2 || $l3 || $l4 || $l5)) /* 满足任何条件可以等级 */ + || + ($item['upgrade_type'] == 1 && ($l1 && $l2 && $l3 && $l4 && $l5)) /* 满足所有条件可以等级 */ + ) { + [$level, $title] = [$item['number'], $item['name']]; + break; + } + } + // 购买商品升级 + $query = $this->app->db->name('ShopOrderItem')->alias('b')->rightJoin('store_order a', 'b.order_no=a.order_no'); + $tempLevel = $query->whereRaw("a.uid={$uid} and a.pay_status=1 and a.status in (3,4,5) and b.vip_mod=1")->max('b.vip_level'); + if ($tempLevel > $level) { + $tempLevelInfo = $this->app->db->name('DataUserLevel')->where(['number' => $tempLevel, 'status' => 1])->find(); + if (!empty($tempLevelInfo)) [$level, $title] = [$tempLevelInfo['number'], $tempLevelInfo['name']]; + } + // 统计当日数据 +// $where = ['pay_date_frist' => date('Y-m-d')]; +// $dailyTeamsDirect = $this->app->db->name('DataUser')->where($where)->where(['pid1' => $uid])->count(); +// $dailyTeamsIndirect = $this->app->db->name('DataUser')->where($where)->where(['pid2' => $uid])->count(); + // 统计团队业绩 +// $teamsPerformanceDirect = $this->app->db->name('ShopOrder')->whereRaw("from_uid={$uid} and status in (3,4,5)")->sum('price_discount'); +// $mysql2 = $this->app->db->name('DataUser')->field('id')->where(['pid1' => $uid])->buildSql(); +// $teamsPerformanceIndirect = $this->app->db->name('ShopOrder')->whereRaw("from_uid in {$mysql2} and status in (3,4,5)")->sum('price_discount'); + // 更新会员数据 + $data = [ + 'vip_name' => $title, + 'vip_level' => $level, +// 'teams_total' => $teamsDirect + $teamsIndirect, +// 'teams_direct' => $teamsDirect, +// 'teams_indirect' => $teamsIndirect, +// 'amount_order_total' => $amountTotal, +// 'teams_performance_total' => $teamsPerformanceDirect + $teamsPerformanceIndirect, +// 'teams_performance_direct' => $teamsPerformanceDirect, +// 'teams_performance_indirect' => $teamsPerformanceIndirect, +// 'amount_profit_total' => $this->app->db->name('StoreProfitRecord')->where(['uid' => $uid])->sum('profit_price'), +// 'amount_profit_used' => $this->app->db->name('StoreProfitUsed')->where(['uid' => $uid])->whereIn('status', [1, 2, 3])->sum('pay_price'), +// 'amount_profit_lock' => $this->app->db->name('StoreProfitRecord')->where(['uid' => $uid, 'profit_status' => '0'])->sum('profit_price'), +// 'daily_teams_total' => $dailyTeamsDirect + $dailyTeamsIndirect, +// 'daily_teams_direct' => $dailyTeamsDirect, +// 'daily_teams_indirect' => $dailyTeamsIndirect, + ]; + if ($data['vip_level'] !== $user['vip_level']) $data['vip_date'] = date('Y-m-d H:i:s'); + $this->app->db->name('DataUser')->where(['id' => $uid])->update($data); + return ($parent && $user['pid2'] > 0) ? $this->syncLevel($user['pid2'], false) : true; + } + /** * 获取令牌的认证值 * @return string diff --git a/app/data/view/user_level/form.html b/app/data/view/user_level/form.html new file mode 100644 index 000000000..20548d7a7 --- /dev/null +++ b/app/data/view/user_level/form.html @@ -0,0 +1,125 @@ +
diff --git a/app/data/view/user_level/index.html b/app/data/view/user_level/index.html new file mode 100644 index 000000000..47c0f636e --- /dev/null +++ b/app/data/view/user_level/index.html @@ -0,0 +1,76 @@ +{extend name="../../admin/view/main"} + +{block name="button"} + + + + + + + +{/block} + +{block name='content'} ++ + | +用户级别 | +升级规则 | +入会礼包 | +团队总数 | +直属团队 | +间接团队 | +订单金额 | +奖利规则 | +使用状态 | ++ |
---|---|---|---|---|---|---|---|---|---|---|
+ + | +[ {$vo.number} ] {$vo.name|default=''} | +{if $vo.upgrade_type eq 1}全部完成{else}任何条件{/if} | +{if $vo.goods_vip_status>0} {else} - {/if} | +{if $vo.teams_user_status>0} {$vo.teams_user_number} 人 {else} - {/if} | +{if $vo.teams_direct_status>0} {$vo.teams_direct_number} 人 {else} - {/if} | +{if $vo.teams_indirect_status>0} {$vo.teams_indirect_number} 人 {else} - {/if} | +{if $vo.order_amount_status>0} {$vo.order_amount_number+0} 元 {else} - {/if} | +{:join(', ',$vo.rebate_rule)} | ++ {if $vo.status eq 0}已禁用{elseif $vo.status eq 1}使用中{/if} + | ++ + 编 辑 + + + + 禁 用 + + 启 用 + + + + 删 除 + + | +