Browse Source

上下分统计数据接口

master
wanghongjun 3 years ago
parent
commit
e89a66dcfd
  1. 32
      app/common.php
  2. 76
      app/controller/AdminStatistics.php
  3. 1
      route/app.php

32
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;
}

76
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());
}
}
}

1
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() {

Loading…
Cancel
Save