272 lines
8.7 KiB
PHP

<?php
namespace app\api\controller\backend;
use app\common\controller\Api;
use think\Db;
/**
* 评价
*/
class Evaluate extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
public function index (){
$token = $this->request->header('Token');
$type = $this->request->request('type',1);
if(!$token){
return $this->error('缺少参数');
}
if(!$type){
return $this->error('缺少参数');
}
$user = Db::name('user')->where('token', $token)->find();
if(!$user){
return $this->error('该用户不存在');
}
$date = Db::name('evaluate')
->field('a.*,u.nickname,g.name as group_name')
->alias('a')
->join('user u','a.user_id = u.id','LEFT')
->join('user_group g','a.group_id = g.id','LEFT')
->where('a.user_id', $user['id'])->where('a.type',$type)->order('a.id', 'desc')->select();
if($date){
return $this->success('查询成功',$date);
}
return $this->error('未查询到相关信息');
}
/**
* 新增记录
*/
public function create()
{
$token = $this->request->header('Token');
if (!$token) {
return $this->error('缺少参数');
}
$user = Db::name('user')->where('token', $token)->find();
if (!$user) {
return $this->error('该用户不存在');
}
$type = $this->request->request('type');
$term = $this->getTermByType($type);
// 检查是否已评价
$existingRecord = Db::name('evaluate')
->where('user_id', $user['id'])
->where('group_id', $user['group_id'])
->where('type', $type)
->where('term', $term)
->find();
if ($existingRecord) {
return $this->success('已评价,请勿重新提交');
}
// 准备插入数据
$data = [
'user_id' => $user['id'],
'group_id' => $user['group_id'],
'self_evaluation' => $this->request->post('self_evaluation'),
'group_evaluation' => $this->request->post('group_evaluation'),
'term' => $term,
'selftime' => date('Y-m-d H:i:s'),
'type' => $type
];
// 插入数据库
$result = Db::name('evaluate')->strict(false)->insert($data);
if ($result) {
return $this->success('添加成功', $result);
} else {
return $this->error('添加失败', $result);
}
}
/**
* 根据类型获取时间周期
* @param int $type
* @return string|null
*/
protected function getTermByType($type)
{
switch ($type) {
case 1:
return date('Y-m');
case 2:
$currentMonth = date('n');
$currentQuarter = ceil($currentMonth / 3);
return date('Y') . '-' . $currentQuarter;
case 3:
return date('Y');
default:
return null;
}
}
public function groupIndex (){
$token = $this->request->header('Token');
$group_id = $this->request->request('group_id');
$type = $this->request->request('type');
if(!$token){
return $this->error('缺少参数');
}
if(!$group_id){
return $this->error('缺少参数');
}
if(!$type){
return $this->error('缺少参数');
}
$user = Db::name('user')->where('token', $token)->find();
if(!$user){
return $this->error('该用户不存在');
}
$date = Db::name('evaluate')
->field('a.*,u.nickname,g.name as group_name')
->alias('a')
->join('user u','a.user_id = u.id','LEFT')
->join('user_group g','a.group_id = g.id','LEFT')
->where('a.group_id', $group_id)->where('a.type',$type)->order('a.id', 'desc')->select();
if($date){
return $this->success('查询成功',$date);
}
return $this->error('未查询到相关信息');
}
public function groupUpdate()
{
$data = $this->request->request();
$token = $this->request->header('Token');
if (!$token) {
return $this->error('缺少参数');
}
$user = Db::name('user')->where('token', $token)->find();
if (!$user) {
return $this->error('该用户不存在');
}
// 准备插入数据
$data['grouptime'] = date('Y-m-d H:i:s');
// 插入数据库
$result = Db::name('evaluate')->where('id',$data['id'])->strict(false)->update($data);
if ($result) {
return $this->success('评价成功', $result);
} else {
return $this->error('评价失败', $result);
}
}
public function groupIndexrecords() {
$group_id = $this->request->param('group_id', 0, 'intval');
$searchMonth = $this->request->param('month', date('Y-m'));
// 验证月份格式
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')
->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')
->select();
// 构建返回数据
$returnData = [];
foreach ($result as $item) {
$returnData[] = [
'username' => $item['username'],
'group_name' => $item['group_name'], // 需要根据实际科室表字段替换
'month' => date('Y-m'),
'total_addition' => $item['total_addition'],
'total_subtraction' => $item['total_subtraction'],
];
}
return $this->success('评价成功', $returnData);
}
public function groupIndexrecordsList(){
// 接收参数
$userId = $this->request->param('user_id', 0, 'intval');
$assessmentType = $this->request->param('assessment_type', 1, 'intval'); // 1加分 2减分
$searchMonth = $this->request->param('month', date('Y-m'));
// 验证参数合法性
if (!in_array($assessmentType, [1, 2])) {
$this->error('考评类型参数错误');
}
// 计算月末日期
$firstDay = $searchMonth . '-01';
$lastDay = date('Y-m-d', strtotime("last day of $searchMonth"));
// 构建关联查询
$records = Db::name('addition_and_subtraction_records')
->alias('a')
->join('plus_minus_scoring s', 'a.assessment_project = s.id')
->field([
's.project_name',
'a.score_value',
'a.fsdate',
'a.notes',
's.single_score_max',
's.single_score_min'
])
->where('a.user_id', $userId)
->where('a.assessment_type', $assessmentType)
->whereTime('a.fsdate', 'between', [$firstDay, $lastDay])
->order('a.fsdate desc')
->select();
// 处理结果
$result = [];
if ($records) {
foreach ($records as $record) {
$result[] = [
'project_name' => $record['project_name'],
'score_value' => (float)$record['score_value'],
'date' => date('Y-m'),
'notes' => $record['notes'],
'max_score' => (float)$record['single_score_max'],
'min_score' => (float)$record['single_score_min']
];
}
$res = ['data' => $result, 'total' => count($result)];
return $this->success('查询到相关数据', $res);
} else {
return $this->error('未查询到相关数据');
}
}
}