刮刮后端接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.8 KiB

<?php
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;
class AdminStatistics extends BaseController
{
/**
* 待办
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function needStatistics()
{
$userData = UserModel::where('status',1)->field('SUM(withdrawal_balance) as quota')->find();
$agentData = AgentUser::where('status',1)->field('SUM(withdrawal_balance) as quota')->find();
return $this->renderSuccess('数据返回成功',[
'update_time' => date("Y/m/d H:i",time()),
'down_scores_quota' => [
'title' => '待下分额度',
'quota' => bcadd($userData->quota,$agentData->quota,2)
]
]);
}
/**
* 用户统计
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function userStatistics()
{
$userData = UserModel::where('status',1)->field('count(id) as num')->find();
$agentData = AgentUser::where('status',1)->field('count(id) as num')->find();
$user_num = $userData->num;
$agent_num = $agentData->num;
$sum_num = $user_num + $agent_num;
return $this->renderSuccess('数据返回成功',[
'agent' => [
'num' => $agent_num,
'rate' => round($agent_num / $sum_num * 100,2) . '%'
],
'user' => [
'num' => $user_num,
'rate' => round($user_num / $sum_num * 100,2) . '%'
],
'sum' => [
'num' => format_people_count($sum_num)
]
]);
}
/**
* 上下分统计数据(柱状图)
* @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'];
$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'] ?? 0
];
}
} 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'] ?? 0
];
}
}
$data['sum_value'] = $sumData['sum_num'] ?: 0;
return $this->renderSuccess('数据返回成功',$data);
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
}
}
}