From e89a66dcfdb0da70e936308e6be7ae2db2755e18 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Tue, 5 Sep 2023 10:54:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=B8=8B=E5=88=86=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 32 +++++++++++++ app/controller/AdminStatistics.php | 76 ++++++++++++++++++++++++++++++ route/app.php | 1 + 3 files changed, 109 insertions(+) diff --git a/app/common.php b/app/common.php index f4b1281..98e65e0 100644 --- a/app/common.php +++ b/app/common.php @@ -191,4 +191,36 @@ function format_people_count($number):string // 小于1万,直接返回人数 return $number . '人'; } +} + +/** + * 获取近几个月数据 + * @param int $start + * @return array + */ +function get_near_month_datetime(int $start = 4):array +{ + $dateArr = []; + + for ($i = $start; $i >= 1; $i--) { + + $last_month = date('m',strtotime("-{$i} month")); + + $start_time = date("Y-{$last_month}-01 00:00:00"); + $end_time = date("Y-m-d 23:59:59",strtotime('+1 month',strtotime($start_time)) - 86400); + + $dateArr[$last_month] = [ + 'start_time' => $start_time, + 'end_time' => $end_time + ]; + } + + $month = date("m"); + + $dateArr[$month] = [ + 'start_time' => date("Y-{$month}-01 00:00:00"), + 'end_time' => date("Y-m-d H:i:s") + ]; + + return $dateArr; } \ No newline at end of file diff --git a/app/controller/AdminStatistics.php b/app/controller/AdminStatistics.php index 40260fc..555355c 100644 --- a/app/controller/AdminStatistics.php +++ b/app/controller/AdminStatistics.php @@ -4,7 +4,11 @@ declare (strict_types = 1); namespace app\controller; use app\BaseController; +use app\model\AdminDownScoresRecords; +use app\model\AdminUpScoresRecords; use app\model\AgentUser; +use think\exception\ValidateException; +use think\facade\Request; use think\facade\Session; use app\model\User as UserModel; @@ -64,4 +68,76 @@ class AdminStatistics extends BaseController ] ]); } + + /** + * 上下分统计数据(柱状图) + * @return array + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function upAndDownStatistics() + { + + $param = Request::param(); + + try { + + validate()->rule(['type|统计类型' => 'require|in:1,2'])->check($param); + + $type = $param['type']; + + $start_time = date("Y-m-d H:i:s",strtotime('-4 month')); + + $date = get_near_month_datetime(); + + $data = []; + + if ($type == 1) { + // 上分统计 + $sumData = AdminUpScoresRecords::field('SUM(balance) as sum_num') + ->where('id','>=',1) + ->find(); + // 统计近4个月数据 + foreach ($date as $month => $datetime) { + + $res = AdminUpScoresRecords::field('SUM(balance) as sum_num') + ->where([ + ['create_time','>=',$datetime['start_time']], + ['create_time','<=',$datetime['end_time']], + ]) + ->find(); + $data['histogram'] = [ + 'date' => $month, + 'value' => $res['sum_num'] + ]; + } + } else { + // 下分统计 + $sumData = AdminDownScoresRecords::field('SUM(withdrawal_amount) as sum_num') + ->where('id','>=',1) + ->find(); + // 统计近4个月数据 + foreach ($date as $month => $datetime) { + $res = AdminDownScoresRecords::field('SUM(withdrawal_amount) as sum_num') + ->where([ + ['create_time','>=',$datetime['start_time']], + ['create_time','<=',$datetime['end_time']], + ]) + ->find(); + $data['histogram'] = [ + 'date' => $month, + 'value' => $res['sum_num'] + ]; + } + } + + $data['sum_value'] = $sumData['sum_num']; + + return $this->renderSuccess('数据返回成功',$data); + } catch (ValidateException $validateException) { + return $this->renderError($validateException->getMessage()); + } + } + } diff --git a/route/app.php b/route/app.php index 31f06c4..6f74949 100644 --- a/route/app.php +++ b/route/app.php @@ -59,6 +59,7 @@ Route::group('agentTeam',function(){ Route::group('adminStatistics',function() { Route::post('needStatistics','adminStatistics/needStatistics')->middleware(CheckAgent::class)->allowCrossDomain(); Route::post('userStatistics','adminStatistics/userStatistics')->middleware(CheckAgent::class)->allowCrossDomain(); + Route::post('upAndDownStatistics','adminStatistics/upAndDownStatistics')->middleware(CheckAgent::class)->allowCrossDomain(); }); Route::group('adminUserTeam',function() {