From c4eb04428e4c26f25d6b9c1b456e1f0cf545f4c1 Mon Sep 17 00:00:00 2001 From: "elf@home" <360197197@qq.com> Date: Wed, 30 Sep 2020 00:41:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/PromoteGradeService.class.php | 99 ++++++++++++---- .../PromoteGradeController.class.php | 106 ++++++++++++++---- .../Home/View/default/PromoteGrade/index.html | 26 ++--- .../View/default/PromoteGrade/setting.html | 46 ++++---- .../View/default/PromoteGrade/settings.html | 50 +++++++-- 5 files changed, 241 insertions(+), 86 deletions(-) diff --git a/Application/Base/Service/PromoteGradeService.class.php b/Application/Base/Service/PromoteGradeService.class.php index 5ef4fbe15..a5005ce93 100644 --- a/Application/Base/Service/PromoteGradeService.class.php +++ b/Application/Base/Service/PromoteGradeService.class.php @@ -6,6 +6,11 @@ use Base\Tool\GameCatClient; class PromoteGradeService { + public static $symbols = [ + 1 => '>=', + 2 => '>', + ]; + /** * @config * reach_level @@ -19,27 +24,39 @@ class PromoteGradeService $setting = null; if ($id > 0) { - $setting = M('promote_grade_setting', 'tab')->where(['id' => $id])->find(); + $setting = M('promote_grade_setting', 'tab_')->where(['id' => $id])->find(); if (is_null($setting)) { throw new \Exception('记录不存在'); } if ($setting['company_id'] != $promote['company_id']) { throw new \Exception('不允许修改其他公司的配置'); } + } else { + $setting = M('promote_grade_setting', 'tab_')->where(['company_id' => $promote['company_id']])->find(); + if ($setting) { + throw new \Exception('您已经设置过了'); + } } $config = []; $config['reach_level'] = $params['level']; $config['grades'] = $params['grades']; + $config['default_grade_name'] = $params['default_grade_name']; $data = []; - $data['company_id'] = $promote['company_id']; $data['status'] = 1; + $data['name'] = $params['name']; $data['config'] = json_encode($config); - $data['create_time'] = time(); - $data['update_time'] = time(); - M('promote_grade_setting', 'tab_')->add($data); - M('promote_grade_setting', 'tab_')->where(['id' => $id])->save($data); + + if ($setting) { + $data['create_time'] = time(); + M('promote_grade_setting', 'tab_')->where(['id' => $id])->save($data); + } else { + $data['company_id'] = $promote['company_id']; + $data['create_time'] = time(); + $data['update_time'] = time(); + M('promote_grade_setting', 'tab_')->add($data); + } } private function checkGrades($grades) @@ -47,26 +64,20 @@ class PromoteGradeService } - public function hadSetting($promote) + public function getCurrentSetting($promote) { - $setting = M('promote_grade_setting', 'tab_')->field(['id'])->where(['status' => 1, 'company_id' => $promote['company_id']])->find(); - if ($setting) { - return true; - } - return false; + return M('promote_grade_setting', 'tab_')->where(['status' => 1, 'company_id' => $promote['company_id']])->find(); } - public function searchGradeByPromotes($promotes, $params) + public function searchGradeByPromotes($promotes, $params, $setting) { - /** - * @todo 通过设置获取 - */ - $settingLevel = 50; + $config = json_decode($setting['config'], true); + $settingLevel = $config['reach_level']; $month = $params['month'] ?? date('Y-m'); - $promoteIds = array_column($promotes, 'id'); $beginTime = strtotime($month . '-01 00:00:00'); - $endTime = strtotime($month . '-01 00:00:00') + 24*3600 - 1; + $endDate = date('Y-m-01 00:00:00', strtotime($month . '-01' . ' +1 month')); + $endTime = strtotime($endDate) - 1; $betweenTime = [$beginTime, $endTime]; @@ -99,18 +110,60 @@ class PromoteGradeService ->select(); $amountItems = index_by_column('promote_id', $amountItems); + $promoteService = new PromoteService(); + $records = []; foreach ($promotes as $promote) { $amountItem = $amountItems[$promote['id']] ?? null; $accountItem = $accountItems[$promote['id']] ?? null; + $amount = $amountItem ? $amountItem['amount'] : 0; + $num = $accountItem ? $accountItem['num'] : 0; + $value = $num == 0 ? 0 : round($amount / $num, 2); $records[] = [ - 'amount' => $amountItem ? $amountItem['amount'] : 0, - 'num' => $accountItem ? $accountItem['num'] : 0, - 'real_name' => $promote['real_name'], + 'id' => $promote['id'], + 'level' => $promote['level'], + 'amount' => $amount, + 'num' => $num, + 'real_name' => hideRealName($promote['real_name']), 'account' => $promote['account'], - 'promote_group' => $promote['promote_group'] + 'promote_group' => $promoteService->getGroupNameByChain($promote['chain'], $promote['id']), + 'value' => $value, + 'grade_name' => $this->getGradeByValue($value, $setting), + 'current_display' => '' ]; } return $records; } + + public function getGradeByValue($value, $setting) + { + $config = json_decode($setting['config'], true); + $grades = $config['grades']; + $gradeName = $config['default_grade_name']; + foreach ($grades as $key => $grade) { + if ($key == 0 && !$this->isBigger($value, $grade)) { + $gradeName = $config['default_grade_name']; + break; + } + $nextGrade = $grade[$key + 1] ?? null; + if ($this->isBigger($value, $grade) && !$this->isBigger($value, $nextGrade)) { + $gradeName = $grade['name']; + break; + } + } + return $gradeName; + } + + private function isBigger($value, $grade) + { + if (is_null($grade)) { + return false; + } + if ($grade['symbol'] == 1 && $value >= $grade['value']) { + return true; + } elseif ($grade['symbol'] == 2 && $value > $grade['value']) { + return true; + } + return false; + } } \ No newline at end of file diff --git a/Application/Home/Controller/PromoteGradeController.class.php b/Application/Home/Controller/PromoteGradeController.class.php index 22f6aaf1d..ae83a6a52 100644 --- a/Application/Home/Controller/PromoteGradeController.class.php +++ b/Application/Home/Controller/PromoteGradeController.class.php @@ -11,22 +11,17 @@ class PromoteGradeController extends BaseController { public function index($p = 1) { + $month = I('month', date('Y-m')); $loginPromote = $this->getLoginPromote(); - /* $promoteGradeService = new PromoteGradeService(); - if (!$promoteGradeService->hadSetting($loginPromote)) { + $promoteGradeService = new PromoteGradeService(); + $setting = $promoteGradeService->getCurrentSetting($loginPromote); + if (is_null($setting)) { return $this->error('未设置评级规则'); - } */ - - $time = I('time', date('Y-m-d')); - if (!empty($time)) { - $defaultTime = $time; - } else { - $defaultTime = date('Y-m-d', time()); } + $parentId = I('parent_id', 0); $promoteId = I('promote_id', 0); - $status = I('status', 0); $searchLevel = 0; $searchLevelName = ''; $currentDisplay = ''; @@ -54,13 +49,21 @@ class PromoteGradeController extends BaseController $query = M('promote', 'tab_')->field(['id', 'account', 'real_name', 'level', 'chain'])->where($map); list($promotes, $pagination, $count) = $this->paginate($query); + if (I('p', 1) == 1) { + array_unshift($promotes, $parent); + } + $records = $promoteGradeService->searchGradeByPromotes($promotes, [ 'month' => $month, - ]); + ], $setting); + + if (I('p', 1) == 1) { + $records[0]['current_display'] = $currentDisplay; + } $this->meta_title = '团队评级'; - $this->assign('records', $records); + $this->assign('month', $month); $this->assign('prevParentId', $prevParentId); $this->assign('searchLevelName', $searchLevelName); $this->assign('subPromotes', $subPromotes); @@ -76,31 +79,90 @@ class PromoteGradeController extends BaseController $loginPromote = $this->getLoginPromote(); $items = M('promote_grade_setting', 'tab_')->where(['company_id' => $loginPromote['company_id']])->select(); + $symbols = PromoteGradeService::$symbols; + $records = []; - foreach ($items as $key => $item) { + foreach ($items as $item) { $i = 0; $config = json_decode($item['config'], true); - $gradeCount = count($config['grades']); - foreach ($config['grades'] as $grade) { - $records['id'] = $item['id']; - $records['grade_count'] = $item['id']; - $records['reach_level'] = $config['reach_level']; - $records['grade_name'] = $grade['name']; - $records['grade_value'] = $grade['symbol'] . $grade['value']; + $gradeCount = count($config['grades']) + 1; + if ($gradeCount == 1) { + $records[] = [ + 'id' => $item['id'], + 'name' => $item['name'], + 'grade_count' => $gradeCount, + 'reach_level' => $config['reach_level'], + 'grade_name' => $config['default_grade_name'], + 'grade_value' => '全部' + ]; + } else { + $firstGrade = $config['grades'][0]; + $records[] = [ + 'id' => $item['id'], + 'name' => $item['name'], + 'grade_count' => $gradeCount, + 'reach_level' => $config['reach_level'], + 'grade_name' => $config['default_grade_name'], + 'grade_value' => ($firstGrade['symbol'] == 1 ? '<' : '<=') . $firstGrade['value'] + ]; + foreach ($config['grades'] as $key => $grade) { + $records[] = [ + 'id' => $item['id'], + 'name' => $item['name'], + 'grade_count' => 0, + 'reach_level' => $config['reach_level'], + 'grade_name' => $grade['name'], + 'grade_value' => $symbols[$grade['symbol']] . $grade['value'] + ]; + } } } - $this->assign('records', $records); $this->display(); } public function setting() { + $id = I('id', 0); + + $setting = null; + if ($id > 0) { + $setting = M('promote_grade_setting', 'tab_')->where(['id' => $id])->find(); + if (is_null($setting)) { + return $this->error('记录不存在'); + } else { + $setting['config'] = json_decode($setting['config'], true); + } + } + $this->assign('setting', $setting); $this->display(); } + public function delete() + { + $id = I('id', 0); + if ($id == 0) { + return $this->error('记录不存在'); + } + $setting = M('promote_grade_setting', 'tab_')->field(['id'])->where(['id' => $id])->find(); + if (is_null($setting)) { + return $this->error('记录不存在'); + } + + M('promote_grade_setting', 'tab_')->where(['id' => $id])->delete(); + return $this->success('删除成功'); + } + public function saveSetting() { - var_dump(I('post.')); + $params = I('post.'); + $loginPromote = $this->getLoginPromote(); + $promoteGradeService = new PromoteGradeService(); + try { + $promoteGradeService->saveSetting($params, $loginPromote); + return $this->success('保存成功'); + } catch (\Exception $e) { + return $this->error($e->getMessage()); + } } } \ No newline at end of file diff --git a/Application/Home/View/default/PromoteGrade/index.html b/Application/Home/View/default/PromoteGrade/index.html index d713d13b0..90cd6a7a6 100644 --- a/Application/Home/View/default/PromoteGrade/index.html +++ b/Application/Home/View/default/PromoteGrade/index.html @@ -26,7 +26,7 @@