刮刮后端接口
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.

200 lines
6.4 KiB

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\middleware\CheckAdmin;
use app\model\AdminDownScoresRecords;
use app\model\AdminUpScoresRecords;
use app\model\AgentDownScoresRecords;
use app\model\AgentUpScoresRecords;
use app\model\AwardsRecords;
use think\exception\ValidateException;
use think\facade\Request;
use app\model\User as UserModel;
class AdminStatistics extends BaseController
{
protected $middleware = [CheckAdmin::class];
/**
* 待办
* @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();
$user_quota = $userData->quota ?? 0;
return $this->renderSuccess('数据返回成功',[
'update_time' => date("Y/m/d H:i",time()),
'down_scores_quota' => [
'title' => '待下分额度',
'quota' => $user_quota
]
]);
}
/**
* 用户统计
* @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)->where('identity',3)->field('count(id) as num')->find();
$agentData = UserModel::where('status',1)->where('identity',2)->field('count(id) as num')->find();
$user_num = $userData->num ?? 0;
$agent_num = $agentData->num ?? 0;
$sum_num = $user_num + $agent_num;
return $this->renderSuccess('数据返回成功',[
'agent' => [
'num' => $agent_num,
'rate' => empty($agent_num) ? '0%' :round($agent_num / $sum_num * 100,2) . '%'
],
'user' => [
'num' => $user_num,
'rate' => empty($user_num) ? '0%' : 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();
$dateKey = array_keys($date);
$data = ['sum_value' => 0];
// type = 1 上分 type = 2 下分 (统计)
$field = 'balance';
$adminModel = AdminUpScoresRecords::class;
$agentModel = AgentUpScoresRecords::class;
if ($type == 2) {
$field = 'withdrawal_amount';
$adminModel = AdminDownScoresRecords::class;
$agentModel = AgentDownScoresRecords::class;
}
// 总数统计
$adminData = $this->adminOrAgentStatistics($adminModel,$date,$field);
$data['sum_value'] += $adminData['sum_value'];
$agentData = $this->adminOrAgentStatistics($agentModel,$date,$field);
$data['sum_value'] += $agentData['sum_value'];
// 月份统计
foreach ($dateKey as $dateValue) {
$data['histogram'][] = [
'date' => $dateValue,
'value' => $adminData[$dateValue] + $agentData[$dateValue]
];
}
return $this->renderSuccess('数据返回成功',$data);
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
}
}
/**
* 统计 管理|代理 上分、下分数据 及 月份数据
* @param $model
* @param $date
* @param $field
* @return array
*/
protected function adminOrAgentStatistics($model,$date,$field)
{
$data = [];
$sumData = $model::field("SUM({$field}) as sum_num")
->where('id','>=',1)
->find();
$data['sum_value'] = $sumData['sum_num'] ?? 0;
// 统计近4个月数据
foreach ($date as $month => $datetime) {
$res = $model::field("SUM({$field}) as sum_num")
->where([
['create_time','>=',$datetime['start_time']],
['create_time','<=',$datetime['end_time']],
])
->find();
$data[$month] = $res['sum_num'] ?? 0;
}
return $data;
}
/**
* 中奖记录
* @return array
* @throws \think\db\exception\DbException
*/
public function awardRecords()
{
$data = Request::param();
$limit = $data['limit'] ?? 10;
$where = [['id', '>=', '1']];
if (isset($data['date'])) {
$date = $data['date'] ?? date("Y-m-d",time());
$start_time = $date . ' 00:00:00';
$end_time = $date . ' 23:59:59';
$where = [['create_time','>=',$start_time],['create_time', '<=', $end_time]];
}
$records = new AwardsRecords();
$list = $records->field('user_id,awards_amount,create_time')
->where($where)
->order('create_time desc')
->paginate($limit);
$data = $list->toArray()['data'];
$sumRes = $records->field('SUM(awards_amount) as sum_amount')->where($where)->find();
foreach ($data as &$item) {
$user = UserModel::withTrashedUserInfo($item['user_id']);
$item['phone'] = $user['phone'];
$item['awards_amount'] *= 1;
$item['datetime'] = date("Y-m-d H:i:s",strtotime($item['create_time']));
unset($item['create_time']);
}
if (!$sumRes) $sumRes['sum_amount'] = 0;
return $this->renderSuccess('数据返回成功',[
'list' => $data,
'total' => $list->total(),
'sum_amount' => $sumRes['sum_amount'] * 1,
'unit' => '元'
]);
}
}