327 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			327 lines
		
	
	
		
			10 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 _initialize()
 | 
						|
    {
 | 
						|
        parent::_initialize();
 | 
						|
        $id = $this->request->header('Token');
 | 
						|
        if(!$id){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
        $user = Db::name('user')->where('token', $id)->find();
 | 
						|
        if(!$user){
 | 
						|
            return $this->error('用户不存在','',99998);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public function index (){
 | 
						|
        $token = $this->request->header('Token');
 | 
						|
        $type = $this->request->request('type',1);
 | 
						|
        $term = $this->request->request('time',date('Y'));
 | 
						|
        if(!$token){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
        if(!$type){
 | 
						|
            return $this->error('缺少参数');
 | 
						|
        }
 | 
						|
         if(!$term){
 | 
						|
            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)
 | 
						|
                ->where('a.term','like',"%$term%")
 | 
						|
                ->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'));
 | 
						|
        
 | 
						|
        // 新增分页参数
 | 
						|
        $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('月份参数格式错误');
 | 
						|
        }
 | 
						|
        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')
 | 
						|
            ->join('user_group g', 'u.group_id = g.id')
 | 
						|
            ->where('u.group_id', $group_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 ($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[] = [
 | 
						|
                '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(){
 | 
						|
        // 接收参数
 | 
						|
        $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('未查询到相关数据');
 | 
						|
        }
 | 
						|
    }
 | 
						|
    
 | 
						|
}
 |