832 lines
		
	
	
		
			32 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			832 lines
		
	
	
		
			32 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						||
 | 
						||
namespace app\api\controller\backend;
 | 
						||
 | 
						||
use app\common\controller\Api;
 | 
						||
use app\api\model\Admin as AdminModel;
 | 
						||
use think\Db;
 | 
						||
 | 
						||
/**
 | 
						||
 * 季度控制器
 | 
						||
 */
 | 
						||
class Quarter 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', 2)->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 getQuarterlyData()
 | 
						||
    {   
 | 
						||
        $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')) {
 | 
						||
            $currentYear = date('Y');
 | 
						||
            $currentQuarter = ceil(date('m') / 3);
 | 
						||
            $createtime = "{$time}-" . (($currentQuarter - 1) * 3 + 1);
 | 
						||
        } else {
 | 
						||
            $currentYear = $time;
 | 
						||
            $currentQuarter = 4;
 | 
						||
            $createtime = "{$time}-01";
 | 
						||
        }
 | 
						||
 | 
						||
        // $createtime = $data['start_time'];
 | 
						||
        // $currentYear = date('Y');
 | 
						||
        // $currentQuarter = ceil(date('m') / 3);
 | 
						||
        // return $this->error('暂无数据',$currentQuarter);
 | 
						||
    
 | 
						||
        // 初始化季度数组
 | 
						||
        $periods = [];
 | 
						||
        $startYear = date('Y', strtotime($createtime));
 | 
						||
        $startQuarter = ceil(date('m', strtotime($createtime)) / 3);
 | 
						||
    
 | 
						||
        while ($startYear < $currentYear || ($startYear == $currentYear && $startQuarter <= $currentQuarter)) {
 | 
						||
            $periods[] = $startYear . '-' . $startQuarter;
 | 
						||
            $startQuarter++;
 | 
						||
            if ($startQuarter > 4) {
 | 
						||
                $startQuarter = 1;
 | 
						||
                $startYear++;
 | 
						||
            }
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 获取季度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')->where('scoring_period', 2)->where('evaluation_schedule_id',$evaluation_id)->where('user_id', $id)->select();
 | 
						||
    
 | 
						||
        // 整理评分记录,以季度为键
 | 
						||
        $scoringQuarters = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordMonth = date('Y-m', strtotime($record['term']));
 | 
						||
            $recordYear = date('Y', strtotime($record['term']));
 | 
						||
            $recordQuarter = ceil(date('m', strtotime($record['term'])) / 3);
 | 
						||
            $quarterKey = $recordYear . '-' . $recordQuarter;
 | 
						||
    
 | 
						||
            if (!isset($scoringQuarters[$quarterKey])) {
 | 
						||
                $scoringQuarters[$quarterKey] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'createtime' => null, // 初始化为null
 | 
						||
                ];
 | 
						||
            }
 | 
						||
    
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringQuarters[$quarterKey]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringQuarters[$quarterKey]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
    
 | 
						||
            $scoringQuarters[$quarterKey]['self_score'] += $record['self_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['department_score'] += $record['department_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['hospital_score'] += $record['hospital_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['createtime'] = $record['createtime']; // 记录时间
 | 
						||
        }
 | 
						||
    
 | 
						||
        $user = Db::name('user')->where('id', $id)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
        
 | 
						||
        // 对比并输出结果
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $period) {
 | 
						||
            list($year, $quarter) = explode('-', $period);
 | 
						||
            $quarterStartMonth = ($quarter - 1) * 3 + 1;
 | 
						||
            $quarterEndMonth = $quarter * 3;
 | 
						||
            $currentPeriod = $currentYear . '-' . $currentQuarter;
 | 
						||
            
 | 
						||
            if (isset($scoringQuarters[$period])) {
 | 
						||
                if ($period != ceil(date('m') / 3)) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                if ($scoringQuarters[$period]['self_score']) {
 | 
						||
                    $if_period = 1;
 | 
						||
                }
 | 
						||
                $createtime = $scoringQuarters[$period]['createtime'] ?: null; // 有值显示原值,无值显示0
 | 
						||
            } else {
 | 
						||
                if ($period != ceil(date('m') / 3)) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                $createtime = null; // 无记录时显示0
 | 
						||
            }
 | 
						||
    
 | 
						||
            $result[] = [
 | 
						||
                'quarter' => $period,
 | 
						||
                'user' => $user['nickname'],
 | 
						||
                'currentQuarter' => $currentPeriod,
 | 
						||
                'plus_score' => $scoringQuarters[$period]['plus_score'] ?? '',
 | 
						||
                'minus_score' => $scoringQuarters[$period]['minus_score'] ?? '',
 | 
						||
                'self_score' => $scoringQuarters[$period]['self_score'] ?? '',
 | 
						||
                'department_score' => $scoringQuarters[$period]['department_score'] ?? '',
 | 
						||
                'hospital_score' => $scoringQuarters[$period]['hospital_score'] ?? '',
 | 
						||
                'if' => isset($scoringQuarters[$period]) ? 2 : 1,
 | 
						||
                'if_period' => $if_period,
 | 
						||
                'quarter_range' => "{$year}-{$quarterStartMonth}至{$year}-{$quarterEndMonth}",
 | 
						||
                'createtime' => $createtime,
 | 
						||
            ];
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 返回结果
 | 
						||
        return $this->success('请求成功', $result);
 | 
						||
    }
 | 
						||
 
 | 
						||
    /**
 | 
						||
     *添加数据
 | 
						||
     */
 | 
						||
    /**
 | 
						||
     *添加数据
 | 
						||
     */
 | 
						||
    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', 2)
 | 
						||
                                        ->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' => 2, // 评分类型
 | 
						||
                        '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 getMonthly(){
 | 
						||
        $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();
 | 
						||
 
 | 
						||
        // 构建层级结构
 | 
						||
        $tree = $this->buildTree($data);
 | 
						||
 
 | 
						||
        return $this->success('请求成功',$tree);
 | 
						||
    }
 | 
						||
 | 
						||
    private function buildTree(array $data, $parentId = 0)
 | 
						||
    {
 | 
						||
        $tree = [];
 | 
						||
        foreach ($data as $item) {
 | 
						||
            if ($item['pid'] == $parentId) {
 | 
						||
                if ($item['pid'] == 0) {
 | 
						||
                    $item['avg_score'] = 0;
 | 
						||
                }
 | 
						||
             // 添加 content_score 并设置其值与 base_score 相同
 | 
						||
            if (isset($item['base_score'])) {
 | 
						||
                    $item['content_score'] = 0;
 | 
						||
                }
 | 
						||
                $children = $this->buildTree($data, $item['id']);
 | 
						||
                if ($children) {
 | 
						||
                    $item['children'] = $children;
 | 
						||
                }
 | 
						||
                $tree[] = $item;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        return $tree;
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    public function getQuarterFind(){
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $month = $this->request->post('quarter');
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
        $where = [
 | 
						||
            'user_id' => $user_id,
 | 
						||
            'term' => $month,
 | 
						||
            'scoring_period' => 2,
 | 
						||
            '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', '=', '2')
 | 
						||
                ->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').'-'.ceil(date('m') / 3));
 | 
						||
        // return $this->success('查询成功', $time);
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');  
 | 
						||
        $page = $this->request->post('page',1);
 | 
						||
        $size = $this->request->post('size',10);
 | 
						||
        $where = [];
 | 
						||
        if(!$groupId){
 | 
						||
            return $this->error('参数错误');
 | 
						||
        }
 | 
						||
        if($user_id){
 | 
						||
            $where['id'] = $user_id;
 | 
						||
        }
 | 
						||
        $where['group_id'] = $groupId;
 | 
						||
        //  return $this->success('查询成功', $where);
 | 
						||
        $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]['time'] = $time;
 | 
						||
            $result[$key]['zongjiafen'] = 0;
 | 
						||
            $result[$key]['scoringrecord_status'] = '1';
 | 
						||
            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]['hospital_score_scoringrecord'] != 0){
 | 
						||
                $result[$key]['scoringrecord_status'] = '4';
 | 
						||
            }
 | 
						||
        }
 | 
						||
        $data = [
 | 
						||
                'result' => $result,
 | 
						||
                'count' => $count,
 | 
						||
        ];
 | 
						||
        return $this->success('查询成功', $data);
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
    public function getMonthlyListFind(){
 | 
						||
        $user_id = $this->request->post('user_id');
 | 
						||
        $month = $this->request->post('month');
 | 
						||
        $evaluation_schedule_id = $this->request->post('evaluation_schedule_id');
 | 
						||
        $where = [
 | 
						||
            'user_id' => $user_id,
 | 
						||
            'term' => $month,
 | 
						||
            'scoring_period' => 2,
 | 
						||
            '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'];
 | 
						||
                if(!$item['department_score']){
 | 
						||
                    $item['department_score'] = 0;
 | 
						||
                }
 | 
						||
                if(!$item['hospital_score']){
 | 
						||
                    $item['hospital_score'] = 0;
 | 
						||
                }
 | 
						||
            } else {
 | 
						||
                $item['self_score'] = null; // 或者其他适当的默认值
 | 
						||
                $item['department_score'] = 0;
 | 
						||
                $item['hospital_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').'-'.ceil(date('n') / 3));
 | 
						||
 
 | 
						||
    // 将 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', 2)
 | 
						||
            ->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;
 | 
						||
                    // 检查数据有效性
 | 
						||
                    if ($basicId && $contentScore !== null && isset($recordMap[$basicId])) {
 | 
						||
                        $updateData[] = [
 | 
						||
                            'id' => $recordMap[$basicId],
 | 
						||
                            'self_score' => $contentScore,
 | 
						||
                            'department_score' => $department_score,
 | 
						||
                            'hospital_score' => $hospital_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 getQuarterlyFindData()
 | 
						||
    {   
 | 
						||
        $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) {
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        if(!$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('暂无数据');
 | 
						||
        }
 | 
						||
        
 | 
						||
     
 | 
						||
        if ($time == date('Y')) {
 | 
						||
            $currentYear = date('Y');
 | 
						||
            $currentQuarter = ceil(date('m') / 3);
 | 
						||
            $createtime = "{$time}-" . (($currentQuarter - 1) * 3 + 1);
 | 
						||
        } else {
 | 
						||
            $currentYear = $time;
 | 
						||
            $currentQuarter = 4;
 | 
						||
            $createtime = "{$time}-01";
 | 
						||
        }
 | 
						||
 | 
						||
        // $createtime = $data['start_time'];
 | 
						||
        // $currentYear = date('Y');
 | 
						||
        // $currentQuarter = ceil(date('m') / 3);
 | 
						||
        // return $this->error('暂无数据',$currentQuarter);
 | 
						||
    
 | 
						||
        // 初始化季度数组
 | 
						||
        $periods = [];
 | 
						||
        $startYear = date('Y', strtotime($createtime));
 | 
						||
        $startQuarter = ceil(date('m', strtotime($createtime)) / 3);
 | 
						||
    
 | 
						||
        while ($startYear < $currentYear || ($startYear == $currentYear && $startQuarter <= $currentQuarter)) {
 | 
						||
            $periods[] = $startYear . '-' . $startQuarter;
 | 
						||
            $startQuarter++;
 | 
						||
            if ($startQuarter > 4) {
 | 
						||
                $startQuarter = 1;
 | 
						||
                $startYear++;
 | 
						||
            }
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 获取季度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')->where('scoring_period', 2)->where('evaluation_schedule_id',$evaluation_id)->where('user_id', $id)->select();
 | 
						||
    
 | 
						||
        // 整理评分记录,以季度为键
 | 
						||
        $scoringQuarters = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordMonth = date('Y-m', strtotime($record['term']));
 | 
						||
            $recordYear = date('Y', strtotime($record['term']));
 | 
						||
            $recordQuarter = ceil(date('m', strtotime($record['term'])) / 3);
 | 
						||
            $quarterKey = $recordYear . '-' . $recordQuarter;
 | 
						||
    
 | 
						||
            if (!isset($scoringQuarters[$quarterKey])) {
 | 
						||
                $scoringQuarters[$quarterKey] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'createtime' => null, // 初始化为null
 | 
						||
                ];
 | 
						||
            }
 | 
						||
    
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringQuarters[$quarterKey]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringQuarters[$quarterKey]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
    
 | 
						||
            $scoringQuarters[$quarterKey]['self_score'] += $record['self_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['department_score'] += $record['department_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['hospital_score'] += $record['hospital_score'];
 | 
						||
            $scoringQuarters[$quarterKey]['createtime'] = $record['createtime']; // 记录时间
 | 
						||
        }
 | 
						||
    
 | 
						||
        $user = Db::name('user')->where('id', $id)->find();
 | 
						||
        if (!$user) {
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
        
 | 
						||
        // 对比并输出结果
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $period) {
 | 
						||
            if (!isset($scoringQuarters[$period])) {
 | 
						||
        $scoringQuarters[$period] = [
 | 
						||
            'plus_score' => 0,
 | 
						||
            'minus_score' => 0,
 | 
						||
            'self_score' => 0,
 | 
						||
            'department_score' => 0,
 | 
						||
            'hospital_score' => 0,
 | 
						||
            'createtime' => null,
 | 
						||
        ];
 | 
						||
    }
 | 
						||
            list($year, $quarter) = explode('-', $period);
 | 
						||
            $quarterStartMonth = ($quarter - 1) * 3 + 1;
 | 
						||
            $quarterEndMonth = $quarter * 3;
 | 
						||
            $currentPeriod = $currentYear . '-' . $currentQuarter;
 | 
						||
            
 | 
						||
            if (isset($scoringQuarters[$period])) {
 | 
						||
                if ($period != ceil(date('m') / 3)) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                if ($scoringQuarters[$period]['self_score']) {
 | 
						||
                    $if_period = 1;
 | 
						||
                }
 | 
						||
                $createtime = $scoringQuarters[$period]['createtime'] ?: null; // 有值显示原值,无值显示0
 | 
						||
            } else {
 | 
						||
                if ($period != ceil(date('m') / 3)) {
 | 
						||
                    $if_period = 1;
 | 
						||
                } else {
 | 
						||
                    $if_period = 2;
 | 
						||
                }
 | 
						||
                $createtime = null; // 无记录时显示0
 | 
						||
            }
 | 
						||
            $scoringrecord_status = 2;
 | 
						||
                if($scoringQuarters[$period]['self_score']){
 | 
						||
                    $scoringrecord_status = 1;
 | 
						||
                }
 | 
						||
                if($scoringQuarters[$period]['department_score']){
 | 
						||
                    $scoringrecord_status = 3;
 | 
						||
                }
 | 
						||
                if($scoringQuarters[$period]['hospital_score']){
 | 
						||
                    $scoringrecord_status = 4;
 | 
						||
                }
 | 
						||
            $result[] = [
 | 
						||
                'id' => $id,
 | 
						||
                'quarter' => $period,
 | 
						||
                'user' => $user['nickname'],
 | 
						||
                'currentQuarter' => $currentPeriod,
 | 
						||
                'plus_score' => $scoringQuarters[$period]['plus_score'] ?? 0,
 | 
						||
                'minus_score' => $scoringQuarters[$period]['minus_score'] ?? 0,
 | 
						||
                'self_score' => $scoringQuarters[$period]['self_score'] ?? 0,
 | 
						||
                'department_score' => $scoringQuarters[$period]['department_score'] ?? 0,
 | 
						||
                'hospital_score' => $scoringQuarters[$period]['hospital_score'] ?? 0,
 | 
						||
                'if' => isset($scoringQuarters[$period]) ? 2 : 1,
 | 
						||
                'if_period' => $if_period,
 | 
						||
                'scoringrecord_status' => $scoringrecord_status ?? 2,
 | 
						||
                'quarter_range' => "{$year}-{$quarterStartMonth}至{$year}-{$quarterEndMonth}",
 | 
						||
                'createtime' => $createtime,
 | 
						||
            ];
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 返回结果
 | 
						||
        return $this->success('请求成功', $result);
 | 
						||
    }
 | 
						||
}
 |