Initial commit
This commit is contained in:
parent
418bf4c1e6
commit
9b599993af
@ -174,46 +174,80 @@ class Evaluate extends Api
|
||||
$group_id = $this->request->param('group_id', 0, 'intval');
|
||||
$searchMonth = $this->request->param('month', date('Y-m'));
|
||||
|
||||
// 验证月份格式
|
||||
// 新增分页参数
|
||||
$page = $this->request->param('page', 1, 'intval');
|
||||
$limit = $this->request->param('limit', 10, 'intval');
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
// 参数验证
|
||||
if ($group_id <= 0) {
|
||||
$this->error('无效的group_id');
|
||||
}
|
||||
if (!preg_match('/^\d{4}-\d{2}$/', $searchMonth)) {
|
||||
$this->error('月份参数格式错误');
|
||||
}
|
||||
|
||||
// 计算月末日期
|
||||
$firstDay = $searchMonth . '-01';
|
||||
$lastDay = date('Y-m-d', strtotime("last day of $searchMonth"));
|
||||
|
||||
$result = Db::name('user')
|
||||
if ($page < 1 || $limit < 1) {
|
||||
$this->error('分页参数错误');
|
||||
}
|
||||
|
||||
// 计算时间范围
|
||||
$firstDay = $searchMonth . '-01 00:00:00';
|
||||
$lastDay = date('Y-m-d 23:59:59', strtotime("last day of $searchMonth"));
|
||||
|
||||
// 获取用户总数(用于分页)
|
||||
$totalUsers = Db::name('user')
|
||||
->alias('u')
|
||||
->field([
|
||||
'u.id',
|
||||
'u.nickname AS username',
|
||||
'u.group_id',
|
||||
'g.name AS group_name',
|
||||
'SUM(CASE WHEN r.assessment_type = 1 THEN r.score_value ELSE 0 END) AS total_addition',
|
||||
'SUM(CASE WHEN r.assessment_type = 2 THEN r.score_value ELSE 0 END) AS total_subtraction'
|
||||
])
|
||||
->join('addition_and_subtraction_records r', 'u.id = r.user_id')
|
||||
->join('user_group g', 'u.group_id = g.id')
|
||||
->where('u.group_id', $group_id)
|
||||
->whereTime('r.fsdate', 'between', [$firstDay, $lastDay]) // 修正后的时间查询
|
||||
->group('u.id')
|
||||
->count();
|
||||
|
||||
// 分页查询用户
|
||||
$users = Db::name('user')
|
||||
->alias('u')
|
||||
->field(['u.id', 'u.nickname AS username', 'g.name AS group_name'])
|
||||
->join('user_group g', 'u.group_id = g.id')
|
||||
->where('u.group_id', $group_id)
|
||||
->limit($offset, $limit)
|
||||
->select();
|
||||
|
||||
|
||||
// 统计分数(使用子查询优化)
|
||||
$scores = Db::name('addition_and_subtraction_records')
|
||||
->field([
|
||||
'user_id',
|
||||
'SUM(CASE WHEN assessment_type = 1 THEN score_value ELSE 0 END) AS total_addition',
|
||||
'SUM(CASE WHEN assessment_type = 2 THEN score_value ELSE 0 END) AS total_subtraction'
|
||||
])
|
||||
->whereTime('fsdate', 'between', [$firstDay, $lastDay])
|
||||
->group('user_id')
|
||||
->select();
|
||||
|
||||
// 构建返回数据
|
||||
$returnData = [];
|
||||
foreach ($result as $item) {
|
||||
foreach ($users as $user) {
|
||||
$scoreArray = array_column($scores, null, 'user_id');
|
||||
$total_addition = $scoreArray[$user['id']]['total_addition'] ?? 0;
|
||||
$total_subtraction = $scoreArray[$user['id']]['total_subtraction'] ?? 0;
|
||||
|
||||
$returnData[] = [
|
||||
'username' => $item['username'],
|
||||
'group_name' => $item['group_name'], // 需要根据实际科室表字段替换
|
||||
'month' => date('Y-m'),
|
||||
'total_addition' => $item['total_addition'],
|
||||
'total_subtraction' => $item['total_subtraction'],
|
||||
'user_id' => $user['id'],
|
||||
'username' => $user['username'],
|
||||
'group_name' => $user['group_name'],
|
||||
'month' => $searchMonth,
|
||||
'total_addition' => floatval($total_addition),
|
||||
'total_subtraction' => floatval($total_subtraction)
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
// 分页信息
|
||||
$totalPages = ceil($totalUsers / $limit);
|
||||
$returnData = [
|
||||
'data' => $returnData,
|
||||
'count' => $totalUsers,
|
||||
];
|
||||
|
||||
return $this->success('评价成功', $returnData);
|
||||
}
|
||||
|
||||
|
||||
public function groupIndexrecordsList(){
|
||||
// 接收参数
|
||||
|
Loading…
x
Reference in New Issue
Block a user