1117 lines
		
	
	
		
			43 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			1117 lines
		
	
	
		
			43 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						||
 | 
						||
namespace app\api\controller\backend;
 | 
						||
 | 
						||
use app\common\controller\Api;
 | 
						||
use app\api\model\Admin as AdminModel;
 | 
						||
use think\Db;
 | 
						||
 | 
						||
/**
 | 
						||
 * 季度控制器
 | 
						||
 */
 | 
						||
class Year 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 getEvaluation()
 | 
						||
    {
 | 
						||
        $id = $this->request->header('Token');
 | 
						||
        if(!$id){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        $group_id = Db::name('user')->where('token', $id)->value('group_id');
 | 
						||
        if(!$group_id){
 | 
						||
            return $this->error('未查询到该用户科室');
 | 
						||
        }
 | 
						||
        $data = Db::name('evaluation_schedule')->where('evaluation_type', 1)->select(); 
 | 
						||
        // 初始化结果数组
 | 
						||
        $result = [];
 | 
						||
        
 | 
						||
        // 遍历获取的数据
 | 
						||
        foreach ($data as $key => $value) {
 | 
						||
            // 获取当前记录的 user_group_id 字段
 | 
						||
            $userGroupId = $value['user_group_id'];
 | 
						||
            
 | 
						||
            // 检查传入的 user_id 是否存在于 user_group_id 中
 | 
						||
            if (in_array($group_id, explode(',', $userGroupId))) {
 | 
						||
                // 如果存在,将该记录添加到结果数组中
 | 
						||
                $result[] = $value;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        if($result){
 | 
						||
            return $this->success('返回成功', $result);
 | 
						||
        }else {
 | 
						||
            return $this->error('未找到相关记录');
 | 
						||
        }
 | 
						||
    }
 | 
						||
    /**
 | 
						||
     * 首页
 | 
						||
     *
 | 
						||
     */
 | 
						||
    public function getAnnualData()
 | 
						||
    {   
 | 
						||
        $token = $this->request->header('Token');
 | 
						||
        $evaluation_id = $this->request->post('id');
 | 
						||
        $time = $this->request->post('time', date('Y'));
 | 
						||
        if(!$token){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        $id = Db::name('user')->where('token', $token)->value('id');
 | 
						||
       
 | 
						||
        // 从数据库中获取计划
 | 
						||
        $data = Db::name('evaluation_schedule')->where('id', $evaluation_id)->find();
 | 
						||
        
 | 
						||
        if (!$data) {
 | 
						||
            return $this->error('暂无数据');
 | 
						||
        }
 | 
						||
 | 
						||
        if($time == date('Y')){
 | 
						||
            $createtime =  $time ;
 | 
						||
            $currentYear = date('Y');
 | 
						||
            $startYear = date('Y', strtotime($createtime));
 | 
						||
        }else{
 | 
						||
            $createtime = $time;
 | 
						||
            $currentYear =  $time;
 | 
						||
            $startYear = $time;
 | 
						||
        }
 | 
						||
 | 
						||
        
 | 
						||
        // 初始化年度数组
 | 
						||
        $periods = [];
 | 
						||
        
 | 
						||
        // return $this->error('暂无数据', $currentYear);
 | 
						||
        while ($startYear <= $currentYear) {
 | 
						||
            $periods[] = $startYear;
 | 
						||
            $startYear++;
 | 
						||
        }
 | 
						||
        
 | 
						||
        // 获取年度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')->where('scoring_period', 1)->where('evaluation_schedule_id',$evaluation_id)->where('user_id', $id)->select();
 | 
						||
        
 | 
						||
        // 整理评分记录,以年度为键
 | 
						||
        $scoringYears = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordYear = date('Y', strtotime($record['term']));
 | 
						||
    
 | 
						||
            if (!isset($scoringYears[$recordYear])) {
 | 
						||
                $scoringYears[$recordYear] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'party_branch_score' => 0,
 | 
						||
                    'overall_party_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'createtime' => null, // 初始化为null
 | 
						||
                ];
 | 
						||
            }
 | 
						||
    
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringYears[$recordYear]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringYears[$recordYear]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
    
 | 
						||
            $scoringYears[$recordYear]['self_score'] += $record['self_score'];
 | 
						||
            $scoringYears[$recordYear]['department_score'] += $record['department_score'];
 | 
						||
            $scoringYears[$recordYear]['party_branch_score'] += $record['party_branch_score'];
 | 
						||
            $scoringYears[$recordYear]['overall_party_score'] += $record['overall_party_score'];
 | 
						||
            $scoringYears[$recordYear]['hospital_score'] += $record['hospital_score'];
 | 
						||
            $scoringYears[$recordYear]['createtime'] = $record['createtime']; // 记录时间
 | 
						||
        }
 | 
						||
    
 | 
						||
        $user = Db::name('user')->where('id', $id)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
 | 
						||
        // 对比并输出结果
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $period) {
 | 
						||
            $currentPeriod = date('Y');
 | 
						||
    
 | 
						||
            if (isset($scoringYears[$period])) {
 | 
						||
                if ($period != $currentPeriod) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                if ($scoringYears[$period]['self_score']) {
 | 
						||
                    $if_period = 1;
 | 
						||
                }
 | 
						||
                $createtime = $scoringYears[$period]['createtime'] ?: null; // 有值显示原值,无值显示0
 | 
						||
            } else {
 | 
						||
                if ($period != $currentPeriod) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                $createtime = null; // 无记录时显示0
 | 
						||
            }
 | 
						||
            $total_score = 0;
 | 
						||
            if (($scoringYears[$period]['hospital_score'] ?? 0) > 0) {
 | 
						||
                 
 | 
						||
                $scoringrecord_status = 4;
 | 
						||
                // 获取该年总加分和减分的分值
 | 
						||
                $zongjiafenfenzhi = Db::name('addition_and_subtraction_records')
 | 
						||
                    ->where('user_id', $id)
 | 
						||
                    ->where('YEAR(createtime)', $period)
 | 
						||
                    ->where('status', 2)
 | 
						||
                    ->where('assessment_type', 1)
 | 
						||
                    ->sum('score_value');
 | 
						||
                
 | 
						||
                $zongjianfenfenzhi = Db::name('addition_and_subtraction_records')
 | 
						||
                    ->where('user_id', $id)
 | 
						||
                    ->where('YEAR(createtime)', $period)
 | 
						||
                    ->where('status', 2)
 | 
						||
                    ->where('assessment_type', 2)
 | 
						||
                    ->sum('score_value');
 | 
						||
                
 | 
						||
                $total_score = $scoringYears[$period]['self_score'] * 0.2 
 | 
						||
                             + $scoringYears[$period]['department_score'] * 0.4 
 | 
						||
                             + $scoringYears[$period]['party_branch_score'] * 0.4 
 | 
						||
                             + $zongjiafenfenzhi 
 | 
						||
                             - $zongjianfenfenzhi;
 | 
						||
            }
 | 
						||
    
 | 
						||
            $result[] = [
 | 
						||
                'year' => $period,
 | 
						||
                'user' => $user['nickname'],
 | 
						||
                'currentYear' => $currentPeriod,
 | 
						||
                'plus_score' => $scoringYears[$period]['plus_score'] ?? '',
 | 
						||
                'minus_score' => $scoringYears[$period]['minus_score'] ?? '',
 | 
						||
                'self_score' => $scoringYears[$period]['self_score'] ?? '',
 | 
						||
                'department_score' => $scoringYears[$period]['department_score'] ?? '',
 | 
						||
                'party_branch_score' => $scoringYears[$period]['party_branch_score'] ?? '',
 | 
						||
                'overall_party_score' => $scoringYears[$period]['overall_party_score'] ?? '',
 | 
						||
                'hospital_score' => $scoringYears[$period]['hospital_score'] ?? '',
 | 
						||
                'if' => isset($scoringYears[$period]) ? 2 : 1,
 | 
						||
                'if_period' => $if_period,
 | 
						||
                'year_range' => "{$period}-01至{$period}-12",
 | 
						||
                'createtime' => $createtime,
 | 
						||
                'total_score' => $total_score,
 | 
						||
            ];
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 返回结果
 | 
						||
        $sortedResult = $this->sortByMonthDescending($result);
 | 
						||
 | 
						||
        return $this->success('请求成功',$sortedResult);
 | 
						||
    }
 | 
						||
    public function sortByMonthDescending($array) {
 | 
						||
        // 使用 usort 函数对数组进行排序
 | 
						||
        usort($array, function ($a, $b) {
 | 
						||
            // 将 month 字段转换为时间戳以便比较
 | 
						||
            $timeA = strtotime($a['year']);
 | 
						||
            $timeB = strtotime($b['year']);
 | 
						||
            
 | 
						||
            // 按时间戳降序排列
 | 
						||
            return $timeB - $timeA;
 | 
						||
        });
 | 
						||
     
 | 
						||
        return $array;
 | 
						||
    }
 | 
						||
    /**
 | 
						||
     *添加数据
 | 
						||
     */
 | 
						||
    /**
 | 
						||
     *添加数据
 | 
						||
     */
 | 
						||
    public function create()
 | 
						||
    {
 | 
						||
        // 获取 JSON 数据(假设通过 POST 请求传入)
 | 
						||
        $jsonData = htmlspecialchars_decode($this->request->post('json'));
 | 
						||
        // $jsonData = htmlspecialchars_decode($_POST['json']);
 | 
						||
        // var_dump($jsonData);die();
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
        $term = $this->request->post('term');
 | 
						||
        // 将 JSON 解码为 PHP 数组
 | 
						||
        $dataArray = json_decode($jsonData, true);
 | 
						||
        if (!$dataArray) {
 | 
						||
            return $this->error('JSON 数据解析失败');
 | 
						||
        }
 | 
						||
        if(!$user_id){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        $user = Db::name('user')->where('id', $user_id)->find();
 | 
						||
        if(!$user){
 | 
						||
            
 | 
						||
        }
 | 
						||
        $if = Db::name('scoringrecord')->where('term', $term)
 | 
						||
                                        ->where('user_id',$user['id'])
 | 
						||
                                        ->where('type', 1)
 | 
						||
                                        ->where('scoring_period', 1)
 | 
						||
                                        ->where('evaluation_schedule_id', $evaluation_schedule_id)
 | 
						||
                                        ->select();
 | 
						||
        if($if){
 | 
						||
            return $this->error('该季度已提交,请勿重复提交');
 | 
						||
        }
 | 
						||
        // 准备要插入的数据
 | 
						||
        $insertData = [];
 | 
						||
        
 | 
						||
        // 遍历父项目
 | 
						||
        foreach ($dataArray as $parent) {
 | 
						||
            // 父项目信息
 | 
						||
            $parentId = $parent['id'];
 | 
						||
            $parentName = $parent['project_name'];
 | 
						||
            $parentBaseScore = $parent['base_score'];
 | 
						||
            
 | 
						||
            // 遍历子项目
 | 
						||
            if (isset($parent['children']) && is_array($parent['children'])) {
 | 
						||
                foreach ($parent['children'] as $child) {
 | 
						||
                   
 | 
						||
                    $insertData[] = [
 | 
						||
                        'user_id' => $user['id'], // 示例:可以根据实际需求分配用户ID
 | 
						||
                        'group_id' => $user['group_id'], // 示例:部门ID
 | 
						||
                        'basic_id' => $child['id'], 
 | 
						||
                        'evaluation_schedule_id' => $evaluation_schedule_id, 
 | 
						||
                        'plus_id' => null, 
 | 
						||
                        'self_score' => $child['content_score'], // 子项目的 content_score
 | 
						||
                        'department_score' => null, // 需要手动评分时可以设为 NULL
 | 
						||
                        'party_branch_score' => null,
 | 
						||
                        'hospital_score' => null,
 | 
						||
                        'scoring_period' => 1, // 评分类型
 | 
						||
                        'term' => $term, // 届数
 | 
						||
                        'status' => 1, // 默认状态
 | 
						||
                        'type' => 1, // 类型:1为自提
 | 
						||
                        'value' => null, // 基础分值
 | 
						||
                        'createtime' => date('Y-m-d H:i:s'), // 当前时间戳
 | 
						||
                    ];
 | 
						||
                }
 | 
						||
            }
 | 
						||
        }
 | 
						||
        // return $this->success('数据插入成功111', $insertData);
 | 
						||
        // 插入数据到数据库
 | 
						||
        if (!empty($insertData)) {
 | 
						||
            $result = Db::name('scoringrecord')->strict(false)->insertAll($insertData);
 | 
						||
            if ($result) {
 | 
						||
                $user = Db::name('user')->where('id', $user_id)->find();
 | 
						||
                $group_id = $user['group_id'];
 | 
						||
                $mail_user = Db::name('user')->where('group_id', $group_id)->where('auth_group_id',4)->find();
 | 
						||
                $mail_user_id = $mail_user['id'];
 | 
						||
                Mail::createMail($mail_user_id,'您的科室下有年度自评待审核人员');
 | 
						||
                return $this->success('数据插入成功', $result);
 | 
						||
            } else {
 | 
						||
                return $this->error('数据插入失败');
 | 
						||
            }
 | 
						||
        } else {
 | 
						||
            return $this->error('没有可插入的数据');
 | 
						||
        }
 | 
						||
    }
 | 
						||
    public function getYear(){
 | 
						||
        $id = $this->request->post('id');
 | 
						||
        if(!$id){
 | 
						||
            return $this->error('参数错误');
 | 
						||
        }
 | 
						||
        $ids =  Db::name('evaluation_schedule')->where('id', $id)->find();
 | 
						||
        if(!$ids){
 | 
						||
            return $this->error('未查询到相关计划');
 | 
						||
        }
 | 
						||
        // 从 schedule 中获取 basic_rating_id 字段
 | 
						||
        $basicRatingIds = $ids['basic_rating_id'];
 | 
						||
        
 | 
						||
        // 将 basic_rating_id 字符串转换为数组
 | 
						||
        $basicRatingIdArray = explode(',', $basicRatingIds);
 | 
						||
        
 | 
						||
        // 查询 basic_rating_table 表中 id 在 basic_rating_id 数组中的记录
 | 
						||
        $data = Db::name('basic_rating_table')
 | 
						||
            ->whereIn('id', $basicRatingIdArray)
 | 
						||
            ->select();
 | 
						||
        //  return $this->success('请求成功',$data);
 | 
						||
        // 构建层级结构
 | 
						||
        $tree = $this->buildTree($data);
 | 
						||
 
 | 
						||
        return $this->success('请求成功',$tree);
 | 
						||
    }
 | 
						||
 | 
						||
    private function buildTree(array $data, $parentId = 0)
 | 
						||
    {
 | 
						||
        $tree = [];
 | 
						||
        foreach ($data as $item) {
 | 
						||
            if ($item['pid'] == 0) {
 | 
						||
                    $item['avg_score'] = 0;
 | 
						||
                }
 | 
						||
             // 添加 content_score 并设置其值与 base_score 相同
 | 
						||
            if (isset($item['base_score'])) {
 | 
						||
                    $item['content_score'] = 0;
 | 
						||
                }
 | 
						||
            if ($item['pid'] == $parentId) {
 | 
						||
                $children = $this->buildTree($data, $item['id']);
 | 
						||
                if ($children) {
 | 
						||
                    $item['children'] = $children;
 | 
						||
                }
 | 
						||
                $tree[] = $item;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        return $tree;
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    public function getYearFind(){
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $month = $this->request->post('year');
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
 | 
						||
        $where = [
 | 
						||
            'user_id' => $user_id,
 | 
						||
            'term' => $month,
 | 
						||
            'scoring_period' => 1,
 | 
						||
            'evaluation_schedule_id' => $evaluation_schedule_id,
 | 
						||
        ];
 | 
						||
        $ids = Db::name('evaluation_schedule')->where('id', $evaluation_schedule_id)->find();
 | 
						||
        if(!$ids){
 | 
						||
            return $this->error('未查询到相关计划');
 | 
						||
        }
 | 
						||
        // 从 schedule 中获取 basic_rating_id 字段
 | 
						||
        $basicRatingIds = $ids['basic_rating_id'];
 | 
						||
        
 | 
						||
        // 将 basic_rating_id 字符串转换为数组
 | 
						||
        $basicRatingIdArray = explode(',', $basicRatingIds);
 | 
						||
        
 | 
						||
        // 查询 basic_rating_table 表中 id 在 basic_rating_id 数组中的记录
 | 
						||
        $data = Db::name('basic_rating_table')
 | 
						||
            ->whereIn('id', $basicRatingIdArray)
 | 
						||
            ->select();
 | 
						||
        $scoringrecord = Db::name('scoringrecord')
 | 
						||
                ->where($where)
 | 
						||
                ->select();
 | 
						||
        $scoringrecordMap = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $scoringrecordMap[$record['basic_id']] = $record;
 | 
						||
        }
 | 
						||
        foreach ($data as &$item) {
 | 
						||
            if (isset($scoringrecordMap[$item['id']])) {
 | 
						||
                $item['self_score'] = $scoringrecordMap[$item['id']]['self_score'];
 | 
						||
                $item['content_score'] = $scoringrecordMap[$item['id']]['self_score'];
 | 
						||
            } else {
 | 
						||
                $item['self_score'] = null; // 或者其他适当的默认值
 | 
						||
            }
 | 
						||
        }
 | 
						||
        // 构建层级结构
 | 
						||
        $tree = $this->buildTreetwo($data);
 | 
						||
        return $this->success('请求成功',$tree);
 | 
						||
    }
 | 
						||
 | 
						||
    private function buildTreetwo(array $data, $parentId = 0)
 | 
						||
    {
 | 
						||
        $tree = [];
 | 
						||
        foreach ($data as $item) {
 | 
						||
            if ($item['pid'] == $parentId) {
 | 
						||
                $children = $this->buildTreetwo($data, $item['id']);
 | 
						||
                if ($children) {
 | 
						||
                    $item['children'] = $children;
 | 
						||
                }
 | 
						||
                
 | 
						||
                $tree[] = $item;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        return $tree;
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
    public function getUserGroup(){
 | 
						||
        $token = $this->request->header('token');
 | 
						||
        if (!$token) {
 | 
						||
            return $this->error('参数错误');
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 查找用户信息
 | 
						||
        $user = Db::name('user')->where('token', $token)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('用户不存在');
 | 
						||
        }
 | 
						||
    
 | 
						||
        $data = [];
 | 
						||
        // 如果用户ID为1,查出所有组别
 | 
						||
        if ($user['id'] == 1) {
 | 
						||
            // 查询所有组别
 | 
						||
            $data = Db::name('user_group')->select();
 | 
						||
        } else {
 | 
						||
            // 查找用户所在组别信息
 | 
						||
            $group = Db::name('user_group')->where('id', $user['group_id'])->find();
 | 
						||
            if (!$group) {
 | 
						||
                return $this->error('用户组别不存在');
 | 
						||
            }
 | 
						||
    
 | 
						||
            // 判断是否为父级
 | 
						||
            if ($group['pid'] == 0) {
 | 
						||
                // 如果是父级,查出父级以及子级
 | 
						||
                $data[] = $group;
 | 
						||
                $subGroups = Db::name('user_group')->where('pid', $group['id'])->select();
 | 
						||
                foreach ($subGroups as $subGroup) {
 | 
						||
                    $data[] = $subGroup;
 | 
						||
                }
 | 
						||
            } else {
 | 
						||
                // 如果是子级,只查出子级
 | 
						||
                // $data[] = $group;
 | 
						||
                return $this->success('查询成功', $group);
 | 
						||
            }
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 构建树状结构
 | 
						||
        $tree = $this->buildTree($data);
 | 
						||
    
 | 
						||
        return $this->success('查询成功', $tree);
 | 
						||
    }
 | 
						||
 | 
						||
    //获取科室有多少考评时间项
 | 
						||
    public function getGroupEvaluation(){
 | 
						||
        // 获取请求中传入的科室ID
 | 
						||
        $groupId = $this->request->post('group_id');
 | 
						||
        
 | 
						||
        // 检查参数是否存在
 | 
						||
        if (!$groupId) {
 | 
						||
            return $this->error('参数错误');
 | 
						||
        }
 | 
						||
 
 | 
						||
        // 查询指定科室的考评时间数据
 | 
						||
        $allResults = Db::name('evaluation_schedule')
 | 
						||
                ->where('evaluation_type', '=', '1')
 | 
						||
                ->select();
 | 
						||
                
 | 
						||
       // 初始化过滤后的结果数组
 | 
						||
       $filteredResults = [];
 | 
						||
 
 | 
						||
       // 循环判断每个记录的 user_group_id
 | 
						||
       foreach ($allResults as $result) {
 | 
						||
           $userGroupId = $result['user_group_id'];
 | 
						||
           
 | 
						||
           // 将 user_group_id 转换为数组
 | 
						||
           $userGroupIdArray = explode(',', $userGroupId);
 | 
						||
           
 | 
						||
           // 使用 in_array 判断是否包含指定的 group_id
 | 
						||
           if (in_array($groupId, $userGroupIdArray)) {
 | 
						||
                array_push($filteredResults, $result);
 | 
						||
            //    $filteredResults[] = $result;
 | 
						||
           }
 | 
						||
       }
 | 
						||
        // 返回查询结果
 | 
						||
        if ($filteredResults) {
 | 
						||
            return $this->success('查询成功', $filteredResults);
 | 
						||
        } else {
 | 
						||
            return $this->error('未找到相关数据');
 | 
						||
        }
 | 
						||
    }   
 | 
						||
    public function getUserList(){
 | 
						||
        $groupId = $this->request->post('group_id');
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $time = $this->request->post('time',date('Y'));
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');  
 | 
						||
        $page = $this->request->post('page',1);
 | 
						||
        $size = $this->request->post('size',10);
 | 
						||
        $scoringrecord_status = $this->request->post('scoringrecord_status');  
 | 
						||
        $where = [];
 | 
						||
        if(!$groupId){
 | 
						||
            return $this->error('参数错误');
 | 
						||
        }
 | 
						||
        
 | 
						||
        if($user_id){
 | 
						||
            $where['id'] = $user_id;
 | 
						||
        }
 | 
						||
        $where['group_id'] = $groupId;
 | 
						||
        
 | 
						||
        $result = Db::name('user')
 | 
						||
                ->where($where)
 | 
						||
                ->page($page,$size)
 | 
						||
                ->select();
 | 
						||
                
 | 
						||
        $count = Db::name('user')
 | 
						||
                ->where($where)
 | 
						||
                ->count();
 | 
						||
        // $time = date('Y-m');
 | 
						||
 | 
						||
        foreach ($result as $key => $value) {
 | 
						||
            $result[$key]['group_name'] = Db::name('user_group')->where('id', $value['group_id'])->value('name');
 | 
						||
            $result[$key]['user_scoringrecord'] = Db::name('scoringrecord')->where('user_id', $value['id'])->where('term', $time)->where('evaluation_schedule_id',$evaluation_schedule_id)->sum('self_score');
 | 
						||
            $result[$key]['department_score_scoringrecord'] = Db::name('scoringrecord')->where('user_id', $value['id'])->where('term', $time)->where('evaluation_schedule_id',$evaluation_schedule_id)->sum('department_score');
 | 
						||
            $result[$key]['hospital_score_scoringrecord'] = Db::name('scoringrecord')->where('user_id', $value['id'])->where('term', $time)->where('evaluation_schedule_id',$evaluation_schedule_id)->sum('hospital_score');
 | 
						||
            $result[$key]['party_score_scoringrecord'] = Db::name('scoringrecord')->where('user_id', $value['id'])->where('term', $time)->where('evaluation_schedule_id',$evaluation_schedule_id)->sum('party_branch_score');
 | 
						||
            $result[$key]['overall_score_scoringrecord'] = Db::name('scoringrecord')->where('user_id', $value['id'])->where('term', $time)->where('evaluation_schedule_id',$evaluation_schedule_id)->sum('overall_party_score');
 | 
						||
            //获取改年总加分的分值
 | 
						||
            $result[$key]['zongjiafenfenzhi'] = Db::name('addition_and_subtraction_records')->where('user_id', $value['id'])->where('YEAR(createtime)', $time)->where('status',2)->where('assessment_type',1)->sum('score_value');
 | 
						||
            //获取改年总减分的分值
 | 
						||
            $result[$key]['zongjianfenfenzhi'] = Db::name('addition_and_subtraction_records')->where('user_id', $value['id'])->where('YEAR(createtime)', $time)->where('status',2)->where('assessment_type',2)->sum('score_value');
 | 
						||
            $result[$key]['time'] = $time;
 | 
						||
            $result[$key]['zongjiafen'] = 0;
 | 
						||
            $result[$key]['scoringrecord_status'] = '1';
 | 
						||
            $result[$key]['total_score'] = 0;
 | 
						||
            
 | 
						||
            if($result[$key]['user_scoringrecord'] == 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '2';
 | 
						||
            }
 | 
						||
            if($result[$key]['department_score_scoringrecord'] != 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '3';
 | 
						||
            }
 | 
						||
            if($result[$key]['party_score_scoringrecord'] != 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '5';
 | 
						||
            }
 | 
						||
            if($result[$key]['overall_score_scoringrecord'] != 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '6';
 | 
						||
            }
 | 
						||
             if($result[$key]['hospital_score_scoringrecord'] != 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '4';
 | 
						||
                $result[$key]['total_score'] = $result[$key]['user_scoringrecord'] * 0.2 + $result[$key]['department_score_scoringrecord'] * 0.4 + $result[$key]['party_score_scoringrecord'] * 0.4 + $result[$key]['zongjiafenfenzhi'] - $result[$key]['zongjianfenfenzhi'];
 | 
						||
            }
 | 
						||
 | 
						||
        }
 | 
						||
        $data = [
 | 
						||
                'result' => $result,
 | 
						||
                'count' => $count,
 | 
						||
        ];
 | 
						||
        return $this->success('查询成功', $data);
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    public function getMonthlyListFind(){
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $month = $this->request->post('month',2025);
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
        $where = [
 | 
						||
            'user_id' => $user_id,
 | 
						||
            'term' => $month,
 | 
						||
            'scoring_period' => 1,
 | 
						||
            'evaluation_schedule_id' => $evaluation_schedule_id,
 | 
						||
        ];
 | 
						||
        $ids = Db::name('evaluation_schedule')->where('id', $evaluation_schedule_id)->find();
 | 
						||
        if(!$ids){
 | 
						||
            return $this->error('未查询到相关计划');
 | 
						||
        }
 | 
						||
        // 从 schedule 中获取 basic_rating_id 字段
 | 
						||
        $basicRatingIds = $ids['basic_rating_id'];
 | 
						||
        
 | 
						||
        // 将 basic_rating_id 字符串转换为数组
 | 
						||
        $basicRatingIdArray = explode(',', $basicRatingIds);
 | 
						||
        
 | 
						||
        // 查询 basic_rating_table 表中 id 在 basic_rating_id 数组中的记录
 | 
						||
        $data = Db::name('basic_rating_table')
 | 
						||
            ->whereIn('id', $basicRatingIdArray)
 | 
						||
            ->select();
 | 
						||
        $scoringrecord = Db::name('scoringrecord')
 | 
						||
                ->where($where)
 | 
						||
                ->select();
 | 
						||
        $scoringrecordMap = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $scoringrecordMap[$record['basic_id']] = $record;
 | 
						||
        }
 | 
						||
        
 | 
						||
        foreach ($data as &$item) {
 | 
						||
            if (isset($scoringrecordMap[$item['id']])) {
 | 
						||
                $item['self_score'] = $scoringrecordMap[$item['id']]['self_score'];
 | 
						||
                $item['content_score'] = $scoringrecordMap[$item['id']]['self_score'];
 | 
						||
                $item['department_score'] = $scoringrecordMap[$item['id']]['department_score'];
 | 
						||
                $item['hospital_score'] = $scoringrecordMap[$item['id']]['hospital_score'];
 | 
						||
                $item['party_branch_score'] = $scoringrecordMap[$item['id']]['party_branch_score'];
 | 
						||
                $item['overall_party_score'] = $scoringrecordMap[$item['id']]['overall_party_score'];
 | 
						||
                if(!$item['self_score']){
 | 
						||
                    $item['self_score'] = 0;
 | 
						||
                }
 | 
						||
                if(!$item['department_score']){
 | 
						||
                    $item['department_score'] = 0;
 | 
						||
                }
 | 
						||
                if(!$item['hospital_score']){
 | 
						||
                    $item['hospital_score'] = 0;
 | 
						||
                }
 | 
						||
                if(!$item['party_branch_score']){
 | 
						||
                    $item['party_branch_score'] = 0;
 | 
						||
                }
 | 
						||
                if(!$item['overall_party_score']){
 | 
						||
                    $item['overall_party_score'] = 0;
 | 
						||
                }
 | 
						||
            } else {
 | 
						||
                $item['self_score'] = 0; // 或者其他适当的默认值
 | 
						||
                $item['department_score'] = 0;
 | 
						||
                $item['hospital_score'] = 0;
 | 
						||
                $item['party_branch_score'] = 0;
 | 
						||
                $item['overall_party_score'] = 0;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        // 构建层级结构
 | 
						||
        $tree = $this->buildTreetwo($data);
 | 
						||
        return $this->success('请求成功',$tree);
 | 
						||
    }
 | 
						||
 | 
						||
    public function getMonthlyListUpdate() {
 | 
						||
        // 获取 JSON 数据(假设通过 POST 请求传入)
 | 
						||
    $jsonData = htmlspecialchars_decode($this->request->post('json'));
 | 
						||
    $user_id = $this->request->post('user_id');
 | 
						||
    $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
    $term = $this->request->post('term',date('Y'));
 | 
						||
    $token = $this->request->header('Token');
 | 
						||
    $type = intval($this->request->post('type'));
 | 
						||
    $auth = Db::name('user')
 | 
						||
                ->field('a.*,w.level')
 | 
						||
                ->alias('a')
 | 
						||
                ->join('auth_group w','a.auth_group_id = w.id','LEFT')
 | 
						||
                ->where('a.token', $token)
 | 
						||
                ->find();
 | 
						||
    
 | 
						||
    if(!$auth){
 | 
						||
        return $this->error('您无权限');
 | 
						||
    }
 | 
						||
    $level = $auth['level'];
 | 
						||
 
 | 
						||
    // 定义权限映射关系(使用关联数组提高可维护性)
 | 
						||
    $permissionMap = [
 | 
						||
        1 => 1,   // level1 → type1
 | 
						||
        3 => 6,   // level3 → type4
 | 
						||
        4 => 3,   // level5 → type6
 | 
						||
        5 => 5    // level6 → type5
 | 
						||
    ];
 | 
						||
     
 | 
						||
    // 双重验证:1.是否存在映射关系 2.类型值是否匹配
 | 
						||
    if (isset($permissionMap[$level]) && $permissionMap[$level] === $type) {
 | 
						||
        // 权限验证通过,继续执行业务逻辑
 | 
						||
    } else {
 | 
						||
        return $this->error('您无权限操作');
 | 
						||
    }
 | 
						||
    
 | 
						||
    
 | 
						||
    
 | 
						||
    
 | 
						||
    
 | 
						||
    
 | 
						||
    
 | 
						||
    // 将 JSON 解码为 PHP 数组
 | 
						||
    $dataArray = json_decode($jsonData, true);
 | 
						||
    // return $this->success('json', $dataArray);
 | 
						||
    if (!$dataArray) {
 | 
						||
        return $this->error('JSON 数据解析失败');
 | 
						||
    }
 | 
						||
    if (!$user_id) {
 | 
						||
        return $this->error('缺少参数');
 | 
						||
    }
 | 
						||
 | 
						||
    // 查询用户是否存在
 | 
						||
        $user = Db::name('user')->where('id', $user_id)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('用户不存在');
 | 
						||
        }
 | 
						||
        
 | 
						||
        // 检查该用户在该月份和评分周期是否已有记录
 | 
						||
        $existingRecords = Db::name('scoringrecord')
 | 
						||
            ->where('term', $term)
 | 
						||
            ->where('user_id', $user['id'])
 | 
						||
            ->where('type', 1)
 | 
						||
            ->where('scoring_period', 1)
 | 
						||
            ->where('evaluation_schedule_id', $evaluation_schedule_id)
 | 
						||
            ->select();
 | 
						||
        
 | 
						||
        if (empty($existingRecords)) {
 | 
						||
            return $this->error('未找到该月份的记录,无法更新');
 | 
						||
        }
 | 
						||
        
 | 
						||
        // 准备更新的数据
 | 
						||
        $updateData = [];
 | 
						||
        $recordMap = []; // 用于快速匹配子项目记录
 | 
						||
        
 | 
						||
        // 构建记录索引(以 `basic_id` 为键)
 | 
						||
        foreach ($existingRecords as $record) {
 | 
						||
            $recordMap[$record['basic_id']] = $record['id'];
 | 
						||
        }
 | 
						||
 | 
						||
        // 遍历 JSON 数据,处理子项目
 | 
						||
        foreach ($dataArray as $parent) {
 | 
						||
            if (isset($parent['children']) && is_array($parent['children'])) {
 | 
						||
                foreach ($parent['children'] as $child) {
 | 
						||
                    $basicId = $child['id'] ?? null;
 | 
						||
                    $contentScore = $child['content_score'] ?? null;
 | 
						||
                    $department_score = $child['department_score'] ?? null;
 | 
						||
                    $hospital_score = $child['hospital_score'] ?? null;
 | 
						||
                    $party_branch_score = $child['party_branch_score'] ?? null;
 | 
						||
                    $overall_party_score = $child['overall_party_score'] ?? null;
 | 
						||
                    // 检查数据有效性
 | 
						||
                    if ($basicId && $contentScore !== null && isset($recordMap[$basicId])) {
 | 
						||
                        $updateData[] = [
 | 
						||
                            'id' => $recordMap[$basicId],
 | 
						||
                            'self_score' => $contentScore,
 | 
						||
                            'department_score' => $department_score,
 | 
						||
                            'hospital_score' => $hospital_score,
 | 
						||
                            'party_branch_score' => $party_branch_score,
 | 
						||
                            'overall_party_score' => $overall_party_score,
 | 
						||
                            // 'updatetime' => date('Y-m-d H:i:s'),
 | 
						||
                        ];
 | 
						||
                    }
 | 
						||
                }
 | 
						||
            }
 | 
						||
        }
 | 
						||
        // return $this->success('数据更新成功', $updateData);
 | 
						||
        // 执行批量更新
 | 
						||
        if (!empty($updateData)) {
 | 
						||
            // 严格模式下更新
 | 
						||
            foreach($updateData as $key => $val){
 | 
						||
                $result =  Db::table('lr_scoringrecord')->where('id',$val['id'])->update($val);
 | 
						||
                // if(!$result){
 | 
						||
 | 
						||
                //     return $this->error('数据更新失败',$val);
 | 
						||
                // }
 | 
						||
            }
 | 
						||
            return $this->success('数据更新成功', $result);
 | 
						||
 | 
						||
 | 
						||
            // $result = Db::table('lr_scoringrecord')->strict(true)->update($updateData);
 | 
						||
            // if ($result) {
 | 
						||
            //     return $this->success('数据更新成功', $result);
 | 
						||
            // } else {
 | 
						||
            //     return $this->error('数据更新失败');
 | 
						||
            // }
 | 
						||
        } else {
 | 
						||
            return $this->error('没有可更新的数据');
 | 
						||
        }
 | 
						||
    }
 | 
						||
    
 | 
						||
    public function getAnnualFindData()
 | 
						||
    {   
 | 
						||
        $token = $this->request->header('Token');
 | 
						||
        $id = $this->request->post('user_id');
 | 
						||
        $evaluation_id = $this->request->post('evaluation_id');
 | 
						||
        $time = $this->request->post('time', date('Y'));
 | 
						||
        
 | 
						||
        if(!$token || !$id){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
    
 | 
						||
        $if_user = Db::name('user')->where('token', $token)->value('id');
 | 
						||
        if(!$if_user){
 | 
						||
            return $this->error('该角色不存在');
 | 
						||
        }
 | 
						||
    
 | 
						||
        $data = Db::name('evaluation_schedule')->where('id', $evaluation_id)->find();
 | 
						||
        if (!$data) {
 | 
						||
            return $this->error('暂无数据');
 | 
						||
        }
 | 
						||
    
 | 
						||
        $currentYear = date('Y');
 | 
						||
        $startYear = ($time == date('Y')) ? date('Y') : $time;
 | 
						||
        $endYear = $currentYear;
 | 
						||
    
 | 
						||
        // 初始化年度数组
 | 
						||
        $periods = [];
 | 
						||
        while ($startYear <= $endYear) {
 | 
						||
            $periods[] = $startYear;
 | 
						||
            $startYear++;
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 获取年度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')
 | 
						||
            ->where('scoring_period', 1)
 | 
						||
            ->where('evaluation_schedule_id', $evaluation_id)
 | 
						||
            ->where('user_id', $id)
 | 
						||
            ->select();
 | 
						||
    
 | 
						||
        // 整理评分记录,以年度为键
 | 
						||
        $scoringYears = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordYear = date('Y', strtotime($record['term']));
 | 
						||
            
 | 
						||
            if (!isset($scoringYears[$recordYear])) {
 | 
						||
                $scoringYears[$recordYear] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'party_branch_score' => 0,
 | 
						||
                    'overall_party_score' => 0,
 | 
						||
                    'createtime' => null,
 | 
						||
                ];
 | 
						||
            }
 | 
						||
    
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringYears[$recordYear]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringYears[$recordYear]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
    
 | 
						||
            $scoringYears[$recordYear]['self_score'] += $record['self_score'];
 | 
						||
            $scoringYears[$recordYear]['department_score'] += $record['department_score'];
 | 
						||
            $scoringYears[$recordYear]['hospital_score'] += $record['hospital_score'];
 | 
						||
            $scoringYears[$recordYear]['party_branch_score'] += $record['party_branch_score'];
 | 
						||
            $scoringYears[$recordYear]['overall_party_score'] += $record['overall_party_score'];
 | 
						||
            $scoringYears[$recordYear]['createtime'] = $record['createtime'];
 | 
						||
        }
 | 
						||
    
 | 
						||
        $user = Db::name('user')->where('id', $id)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 对比并输出结果
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $period) {
 | 
						||
            $currentPeriod = date('Y');
 | 
						||
            $if_period = ($period != $currentPeriod) ? 1 : 2;
 | 
						||
            $createtime = null;
 | 
						||
            $total_score = 0;
 | 
						||
            $scoringrecord_status = 2;
 | 
						||
    
 | 
						||
            // 初始化当前年份的评分数据
 | 
						||
            if (!isset($scoringYears[$period])) {
 | 
						||
                $scoringYears[$period] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'party_branch_score' => 0,
 | 
						||
                    'overall_party_score' => 0,
 | 
						||
                    'createtime' => null,
 | 
						||
                ];
 | 
						||
            }
 | 
						||
    
 | 
						||
            if ($scoringYears[$period]['self_score'] > 0) {
 | 
						||
                $scoringrecord_status = 1;
 | 
						||
            }
 | 
						||
            if ($scoringYears[$period]['department_score'] > 0) {
 | 
						||
                $scoringrecord_status = 3;
 | 
						||
            }
 | 
						||
            if ($scoringYears[$period]['hospital_score'] > 0) {
 | 
						||
                $scoringrecord_status = 4;
 | 
						||
                // 获取该年总加分和减分的分值
 | 
						||
                $zongjiafenfenzhi = Db::name('addition_and_subtraction_records')
 | 
						||
                    ->where('user_id', $id)
 | 
						||
                    ->where('YEAR(createtime)', $period)
 | 
						||
                    ->where('status', 2)
 | 
						||
                    ->where('assessment_type', 1)
 | 
						||
                    ->sum('score_value');
 | 
						||
                
 | 
						||
                $zongjianfenfenzhi = Db::name('addition_and_subtraction_records')
 | 
						||
                    ->where('user_id', $id)
 | 
						||
                    ->where('YEAR(createtime)', $period)
 | 
						||
                    ->where('status', 2)
 | 
						||
                    ->where('assessment_type', 2)
 | 
						||
                    ->sum('score_value');
 | 
						||
                
 | 
						||
                $total_score = $scoringYears[$period]['self_score'] * 0.2 
 | 
						||
                             + $scoringYears[$period]['department_score'] * 0.4 
 | 
						||
                             + $scoringYears[$period]['party_branch_score'] * 0.4 
 | 
						||
                             + $zongjiafenfenzhi 
 | 
						||
                             - $zongjianfenfenzhi;
 | 
						||
            }
 | 
						||
    
 | 
						||
            $result[] = [
 | 
						||
                'id' => $id,
 | 
						||
                'year' => $period,
 | 
						||
                'user' => $user['nickname'],
 | 
						||
                'currentYear' => $currentPeriod,
 | 
						||
                'plus_score' => $scoringYears[$period]['plus_score'],
 | 
						||
                'minus_score' => $scoringYears[$period]['minus_score'],
 | 
						||
                'self_score' => $scoringYears[$period]['self_score'],
 | 
						||
                'department_score' => $scoringYears[$period]['department_score'],
 | 
						||
                'hospital_score' => $scoringYears[$period]['hospital_score'],
 | 
						||
                'party_branch_score' => $scoringYears[$period]['party_branch_score'],
 | 
						||
                'overall_party_score' => $scoringYears[$period]['overall_party_score'],
 | 
						||
                'if' => isset($scoringYears[$period]) ? 2 : 1,
 | 
						||
                'if_period' => $if_period,
 | 
						||
                'scoringrecord_status' => $scoringrecord_status,
 | 
						||
                'year_range' => "{$period}-01至{$period}-12",
 | 
						||
                'createtime' => $scoringYears[$period]['createtime'],
 | 
						||
                'total_score' => $total_score,
 | 
						||
            ];
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 返回结果
 | 
						||
        $sortedResult = $this->sortByMonthDescending($result);
 | 
						||
        return $this->success('请求成功', $sortedResult);
 | 
						||
    }
 | 
						||
    
 | 
						||
   public function data()
 | 
						||
{
 | 
						||
    $time = $this->request->post('time', date('Y'));
 | 
						||
    $group_id = $this->request->post('group_id');
 | 
						||
 
 | 
						||
    // 统计总人数(添加时间筛选和分组)
 | 
						||
    $totalUsers = Db::name('user')
 | 
						||
        // ->where('year', $time)
 | 
						||
        ->where('group_id', $group_id)
 | 
						||
        ->count();
 | 
						||
 
 | 
						||
    // 统计已考评人数(添加时间筛选和分组)
 | 
						||
    $evaluatedUsers = Db::name('scoringrecord')
 | 
						||
        // ->where('year', $time)
 | 
						||
        ->where('group_id', $group_id)
 | 
						||
        ->field('COUNT(DISTINCT user_id) as total')
 | 
						||
        ->find()['total'];
 | 
						||
 
 | 
						||
    // 构建带筛选条件的复杂SQL
 | 
						||
    $sql = "SELECT 
 | 
						||
                COUNT(*) AS total,
 | 
						||
                SUM(CASE WHEN total_score >= 90 THEN 1 ELSE 0 END) AS excellent,
 | 
						||
                SUM(CASE WHEN total_score >= 60 AND total_score < 90 THEN 1 ELSE 0 END) AS qualified,
 | 
						||
                SUM(CASE WHEN total_score < 60 THEN 1 ELSE 0 END) AS unqualified
 | 
						||
            FROM (
 | 
						||
                SELECT 
 | 
						||
                    u.id,
 | 
						||
                    COALESCE(
 | 
						||
                        (SUM(CASE WHEN sr.type = 1 THEN sr.self_score ELSE 0 END) * 0.2) +
 | 
						||
                        (SUM(CASE WHEN sr.type = 1 THEN sr.department_score ELSE 0 END) * 0.4) +
 | 
						||
                        (SUM(CASE WHEN sr.type = 1 THEN sr.party_branch_score ELSE 0 END) * 0.4) +
 | 
						||
                        SUM(CASE WHEN sr.type = 2 THEN sr.value ELSE 0 END),
 | 
						||
                    0) AS total_score
 | 
						||
                FROM lr_user u
 | 
						||
                LEFT JOIN lr_scoringrecord sr ON u.id = sr.user_id
 | 
						||
                WHERE sr.term = :time AND sr.group_id = :group_id  -- 添加筛选条件
 | 
						||
                GROUP BY u.id
 | 
						||
            ) AS score_table";
 | 
						||
 
 | 
						||
    // 使用参数绑定防止SQL注入
 | 
						||
    $result = Db::query($sql, [
 | 
						||
        'time' => $time,
 | 
						||
        'group_id' => $group_id
 | 
						||
    ]);
 | 
						||
 
 | 
						||
    // 计算比率(添加空值保护)
 | 
						||
    $excellentRate = $totalUsers > 0 ? round(($result[0]['excellent'] ?? 0) / $totalUsers * 100, 2) : 0;
 | 
						||
    $qualifiedRate = $totalUsers > 0 ? round(($result[0]['qualified'] ?? 0) / $totalUsers * 100, 2) : 0;
 | 
						||
    $unqualifiedRate = $totalUsers > 0 ? round(($result[0]['unqualified'] ?? 0) / $totalUsers * 100, 2) : 0;
 | 
						||
 
 | 
						||
    // 返回统计结果
 | 
						||
    $res =  [
 | 
						||
        'total'           => $totalUsers,
 | 
						||
        'evaluated'       => $evaluatedUsers,
 | 
						||
        'unevaluated'     => $totalUsers > 0 ? ($totalUsers - $evaluatedUsers) : 0,  // 添加保护
 | 
						||
        'excellent'       => $result[0]['excellent'] ?? 0,
 | 
						||
        'excellent_rate'  => $excellentRate . '%',
 | 
						||
        'qualified'       => $result[0]['qualified'] ?? 0,
 | 
						||
        'qualified_rate'  => $qualifiedRate . '%',
 | 
						||
        'unqualified'     => $result[0]['unqualified'] ?? 0,
 | 
						||
        'unqualified_rate' => $unqualifiedRate . '%'
 | 
						||
    ];
 | 
						||
 
 | 
						||
    return $this->success('请求成功', $res);
 | 
						||
}
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        
 | 
						||
        // $time = $this->request->post('time', date('Y'));
 | 
						||
        // $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
        
 | 
						||
        // // 统计总人数
 | 
						||
        // $totalUsers = Db::name('user')->count();
 | 
						||
        // return $this->success('统计完成', $totalUsers);
 | 
						||
        // // 获取所有需要统计的用户列表
 | 
						||
        // $users = Db::name('user')
 | 
						||
        //     ->where($where)
 | 
						||
        //     ->field('id,group_id,username')
 | 
						||
        //     ->select();
 | 
						||
    
 | 
						||
        // $statistics = [
 | 
						||
        //     'total' => $totalUsers,
 | 
						||
        //     'evaluated' => 0,
 | 
						||
        //     'unevaluated' => 0,
 | 
						||
        //     'excellent' => 0,
 | 
						||
        //     'good' => 0,
 | 
						||
        //     'unqualified' => 0,
 | 
						||
        //     'excellent_rate' => 0,
 | 
						||
        //     'good_rate' => 0,
 | 
						||
        //     'unqualified_rate' => 0
 | 
						||
        // ];
 | 
						||
    
 | 
						||
        // // 评分统计标准(需要根据实际业务调整阈值)
 | 
						||
        // $scoreThresholds = [
 | 
						||
        //     'excellent' => 90,   // 优秀阈值
 | 
						||
        //     'good' => 60,        // 合格阈值
 | 
						||
        //     'unqualified' => 0   // 不合格阈值
 | 
						||
        // ];
 | 
						||
    
 | 
						||
        // foreach ($users as &$user) {
 | 
						||
        //     // 获取用户评分记录
 | 
						||
        //     $scoringRecords = Db::name('scoringrecord')
 | 
						||
        //         ->where('user_id', $user['id'])
 | 
						||
        //         ->where($where)
 | 
						||
        //         ->select();
 | 
						||
    
 | 
						||
        //     // 获取用户加减分记录
 | 
						||
        //     $additionSubtraction = Db::name('addition_and_subtraction_records')
 | 
						||
        //         ->where('user_id', $user['id'])
 | 
						||
        //         ->where('YEAR(createtime)', $time)
 | 
						||
        //         ->where('status', 1) // 假设1为有效状态
 | 
						||
        //         ->sum('value');
 | 
						||
    
 | 
						||
        //     if (empty($scoringRecords)) {
 | 
						||
        //         $statistics['unevaluated']++;
 | 
						||
        //         continue;
 | 
						||
        //     }
 | 
						||
    
 | 
						||
        //     $statistics['evaluated']++;
 | 
						||
    
 | 
						||
        //     // 计算总分
 | 
						||
        //     $totalScore = 0;
 | 
						||
        //     foreach ($scoringRecords as $record) {
 | 
						||
        //         $totalScore += (
 | 
						||
        //             $record['self_score'] * 0.2 +
 | 
						||
        //             $record['department_score'] * 0.4 +
 | 
						||
        //             $record['party_branch_score'] * 0.4
 | 
						||
        //         );
 | 
						||
        //     }
 | 
						||
    
 | 
						||
        //     // 加上加减分项
 | 
						||
        //     $totalScore += $additionSubtraction ?: 0;
 | 
						||
    
 | 
						||
        //     // 统计分类
 | 
						||
        //     if ($totalScore >= $scoreThresholds['excellent']) {
 | 
						||
        //         $statistics['excellent']++;
 | 
						||
        //     } elseif ($totalScore >= $scoreThresholds['good']) {
 | 
						||
        //         $statistics['good']++;
 | 
						||
        //     } else {
 | 
						||
        //         $statistics['unqualified']++;
 | 
						||
        //     }
 | 
						||
        // }
 | 
						||
    
 | 
						||
        // // 计算比率
 | 
						||
        // $totalEvaluated = $statistics['evaluated'];
 | 
						||
        // if ($totalEvaluated > 0) {
 | 
						||
        //     $statistics['excellent_rate'] = round($statistics['excellent'] / $totalEvaluated * 100, 2);
 | 
						||
        //     $statistics['good_rate'] = round($statistics['good'] / $totalEvaluated * 100, 2);
 | 
						||
        //     $statistics['unqualified_rate'] = round($statistics['unqualified'] / $totalEvaluated * 100, 2);
 | 
						||
        // }
 | 
						||
    
 | 
						||
        // // 补充用户分组信息
 | 
						||
        // $groupIds = array_column($users, 'group_id');
 | 
						||
        // $groups = Db::name('user_group')->where('id', 'in', $groupIds)->column('id,name', 'id');
 | 
						||
        
 | 
						||
        // foreach ($users as &$user) {
 | 
						||
        //     $user['group_name'] = $groups[$user['group_id']] ?? '未分组';
 | 
						||
        // }
 | 
						||
    
 | 
						||
        // return $this->success('统计完成', [
 | 
						||
        //     'statistics' => $statistics,
 | 
						||
        //     'users' => $users
 | 
						||
        // ]);
 | 
						||
    
 | 
						||
        
 | 
						||
    
 | 
						||
}
 |