819 lines
		
	
	
		
			31 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			819 lines
		
	
	
		
			31 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						||
 | 
						||
namespace app\api\controller\backend;
 | 
						||
 | 
						||
use app\common\controller\Api;
 | 
						||
use app\api\model\Admin as AdminModel;
 | 
						||
use think\Db;
 | 
						||
use app\api\controller\backend\Mail;
 | 
						||
 | 
						||
/**
 | 
						||
 * 月度控制器
 | 
						||
 */
 | 
						||
class Monthly extends Api
 | 
						||
{
 | 
						||
    protected $noNeedLogin = ['*'];
 | 
						||
    protected $noNeedRight = ['*'];
 | 
						||
 | 
						||
    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', 3)->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 getMonthlyData()
 | 
						||
    {   
 | 
						||
        $token = $this->request->header('Token');
 | 
						||
        $evaluation_id = $this->request->post('id');
 | 
						||
        $time = $this->request->post('time', date('Y'));
 | 
						||
        if(!$token){
 | 
						||
            return $this->error('缺少参数');
 | 
						||
        }
 | 
						||
        if(!$time){
 | 
						||
            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')){
 | 
						||
            $currentMonth = date('Y-m');
 | 
						||
            $createtime =  $time . '-01';
 | 
						||
        }else{
 | 
						||
            $createtime = $time. '-01';
 | 
						||
            $currentMonth =  $time. '-12';
 | 
						||
        }
 | 
						||
        
 | 
						||
        
 | 
						||
        // 初始化月份数组
 | 
						||
        $periods = [];
 | 
						||
        $startMonth = date('Y-m', strtotime($createtime));
 | 
						||
        
 | 
						||
        while ($startMonth <= $currentMonth) {
 | 
						||
            $periods[] = $startMonth;
 | 
						||
            $startMonth = date('Y-m', strtotime("+1 month", strtotime($startMonth)));
 | 
						||
            
 | 
						||
        }
 | 
						||
       
 | 
						||
        // 获取月度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')->where('scoring_period', 3)->where('evaluation_schedule_id',$evaluation_id)->where('user_id', $id)->select();
 | 
						||
        // 整理评分记录,以月份为键
 | 
						||
        $scoringMonths = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordMonth = date('Y-m', strtotime($record['term']));
 | 
						||
            
 | 
						||
            if (!isset($scoringMonths[$recordMonth])) {
 | 
						||
                $scoringMonths[$recordMonth] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'createtime' => $record['createtime'],
 | 
						||
                ];
 | 
						||
            }
 | 
						||
           
 | 
						||
            // 假设加减分通过 `value` 字段的正负来判断
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringMonths[$recordMonth]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringMonths[$recordMonth]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
        
 | 
						||
            $scoringMonths[$recordMonth]['self_score'] += $record['self_score'];
 | 
						||
            $scoringMonths[$recordMonth]['department_score'] += $record['department_score'];
 | 
						||
            $scoringMonths[$recordMonth]['hospital_score'] += $record['hospital_score'];
 | 
						||
        }
 | 
						||
        
 | 
						||
 | 
						||
        $user =  Db::name('user')->where('id', $id)->find();
 | 
						||
        if(!$user){
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
        // 对比并输出结果
 | 
						||
        
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $month) {
 | 
						||
            
 | 
						||
            if (isset($scoringMonths[$month])) {
 | 
						||
                // return $this->error('未查到该用户',$scoringMonths);
 | 
						||
                if($month != date('Y-m')){
 | 
						||
                    $if_month = 1;
 | 
						||
                }else{
 | 
						||
                    $if_month = 2;
 | 
						||
                }
 | 
						||
                if($scoringMonths[$month]['self_score']){
 | 
						||
                    $if_month = 1;
 | 
						||
                }
 | 
						||
                $result[] = [
 | 
						||
                    'month' => $month,
 | 
						||
                    'user' => $user['nickname'],
 | 
						||
                    'currentMonth' => $currentMonth,
 | 
						||
                    'plus_score' => $scoringMonths[$month]['plus_score'],
 | 
						||
                    'minus_score' => $scoringMonths[$month]['minus_score'],
 | 
						||
                    'self_score' => $scoringMonths[$month]['self_score'],
 | 
						||
                    'department_score' => $scoringMonths[$month]['department_score'],
 | 
						||
                    'hospital_score' => $scoringMonths[$month]['hospital_score'],
 | 
						||
                    'if' => 2,
 | 
						||
                    'if_month' => $if_month,
 | 
						||
                    'createtime' => $scoringMonths[$month]['createtime'],
 | 
						||
                ];
 | 
						||
            } else {
 | 
						||
                if($month != date('Y-m')){
 | 
						||
                    $if_month = 1;
 | 
						||
                }else{
 | 
						||
                    $if_month = 2;
 | 
						||
                }
 | 
						||
                $result[] = [
 | 
						||
                    'month' => $month,
 | 
						||
                    'user' => $user['nickname'],
 | 
						||
                    'currentMonth' => $currentMonth,
 | 
						||
                    'plus_score' => '',
 | 
						||
                    'minus_score' => '',
 | 
						||
                    'self_score' => '',
 | 
						||
                    'department_score' => '',
 | 
						||
                    'hospital_score' => '',
 | 
						||
                    'if' => 1, 
 | 
						||
                    'if_month' => $if_month,
 | 
						||
                    'createtime' => null,
 | 
						||
                ];
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        // 返回结果
 | 
						||
        $sortedResult = $this->sortByMonthDescending($result);
 | 
						||
 | 
						||
        return $this->success('请求成功',$sortedResult);
 | 
						||
    }
 | 
						||
    public function sortByMonthDescending($array) {
 | 
						||
        // 使用 usort 函数对数组进行排序
 | 
						||
        usort($array, function ($a, $b) {
 | 
						||
            // 将 month 字段转换为时间戳以便比较
 | 
						||
            $timeA = strtotime($a['month']);
 | 
						||
            $timeB = strtotime($b['month']);
 | 
						||
            
 | 
						||
            // 按时间戳降序排列
 | 
						||
            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');
 | 
						||
        // 将 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', date('Y-m'))
 | 
						||
                                        ->where('user_id',$user['id'])
 | 
						||
                                        ->where('type', 1)
 | 
						||
                                        ->where('scoring_period', 3)
 | 
						||
                                        ->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'];
 | 
						||
            // return $this->success('数据插入成功111', $parent);
 | 
						||
            // 遍历子项目
 | 
						||
            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' => 3, // 评分类型
 | 
						||
                        'term' => date('Y-m'), // 届数
 | 
						||
                        'status' => 1, // 默认状态
 | 
						||
                        'type' => 1, // 类型:1为自提
 | 
						||
                        'value' => null, // 基础分值
 | 
						||
                        'createtime' => date('Y-m-d H:i:s'), // 当前时间戳
 | 
						||
 | 
						||
                    ];
 | 
						||
                }
 | 
						||
            }
 | 
						||
        }
 | 
						||
       
 | 
						||
        // 插入数据到数据库
 | 
						||
        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);
 | 
						||
    }
 | 
						||
 | 
						||
    public function getMonthlyFind(){
 | 
						||
        $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' => 3,
 | 
						||
            '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 buildTree(array $data, $parentId = 0)
 | 
						||
    {
 | 
						||
        $tree = [];
 | 
						||
        foreach ($data as $item) {
 | 
						||
            if ($item['pid'] == $parentId) {
 | 
						||
                // 添加 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;
 | 
						||
    }
 | 
						||
 | 
						||
    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'] == 237) {
 | 
						||
            // 查询所有组别
 | 
						||
            $data = Db::name('user_group')->select();
 | 
						||
        } else {
 | 
						||
            // 查找用户所在组别信息
 | 
						||
            $group = Db::name('user_group')->where('id', $user['group_id'])->find();
 | 
						||
            // return $this->success('查询成功', $user);
 | 
						||
            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('查询成功', $data);
 | 
						||
            }
 | 
						||
        }
 | 
						||
    
 | 
						||
        // 构建树状结构
 | 
						||
        $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', '=', '3')
 | 
						||
                ->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-m'));
 | 
						||
        $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' => 3,
 | 
						||
            '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-m'));
 | 
						||
 
 | 
						||
    // 将 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', 3)
 | 
						||
            ->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 getMonthlyFindData()
 | 
						||
    {   
 | 
						||
        $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(!$time){
 | 
						||
            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')){
 | 
						||
            $currentMonth = date('Y-m');
 | 
						||
            $createtime =  $time . '-01';
 | 
						||
        }else{
 | 
						||
            $createtime = $time. '-01';
 | 
						||
            $currentMonth =  $time. '-12';
 | 
						||
        }
 | 
						||
        
 | 
						||
        
 | 
						||
        // 初始化月份数组
 | 
						||
        $periods = [];
 | 
						||
        $startMonth = date('Y-m', strtotime($createtime));
 | 
						||
        
 | 
						||
        while ($startMonth <= $currentMonth) {
 | 
						||
            $periods[] = $startMonth;
 | 
						||
            $startMonth = date('Y-m', strtotime("+1 month", strtotime($startMonth)));
 | 
						||
            
 | 
						||
        }
 | 
						||
       
 | 
						||
        // 获取月度的评分记录
 | 
						||
        $scoringrecord = Db::name('scoringrecord')->where('scoring_period', 3)->where('evaluation_schedule_id',$evaluation_id)->where('user_id', $id)->select();
 | 
						||
        // 整理评分记录,以月份为键
 | 
						||
        $scoringMonths = [];
 | 
						||
        foreach ($scoringrecord as $record) {
 | 
						||
            $recordMonth = date('Y-m', strtotime($record['term']));
 | 
						||
            
 | 
						||
            if (!isset($scoringMonths[$recordMonth])) {
 | 
						||
                $scoringMonths[$recordMonth] = [
 | 
						||
                    'plus_score' => 0,
 | 
						||
                    'minus_score' => 0,
 | 
						||
                    'self_score' => 0,
 | 
						||
                    'department_score' => 0,
 | 
						||
                    'hospital_score' => 0,
 | 
						||
                    'createtime' => $record['createtime'],
 | 
						||
                ];
 | 
						||
            }
 | 
						||
           
 | 
						||
            // 假设加减分通过 `value` 字段的正负来判断
 | 
						||
            if ($record['value'] > 0) {
 | 
						||
                $scoringMonths[$recordMonth]['plus_score'] += $record['value'];
 | 
						||
            } else {
 | 
						||
                $scoringMonths[$recordMonth]['minus_score'] += $record['value'];
 | 
						||
            }
 | 
						||
        
 | 
						||
            $scoringMonths[$recordMonth]['self_score'] += $record['self_score'];
 | 
						||
            $scoringMonths[$recordMonth]['department_score'] += $record['department_score'];
 | 
						||
            $scoringMonths[$recordMonth]['hospital_score'] += $record['hospital_score'];
 | 
						||
        }
 | 
						||
        
 | 
						||
 | 
						||
        $user =  Db::name('user')->where('id', $id)->find();
 | 
						||
        if(!$user){
 | 
						||
            return $this->error('未查到该用户');
 | 
						||
        }
 | 
						||
        // 对比并输出结果
 | 
						||
        
 | 
						||
        $result = [];
 | 
						||
        foreach ($periods as $month) {
 | 
						||
            
 | 
						||
            if (isset($scoringMonths[$month])) {
 | 
						||
                // return $this->error('未查到该用户',$scoringMonths);
 | 
						||
                if($month != date('Y-m')){
 | 
						||
                    $if_month = 1;
 | 
						||
                }else{
 | 
						||
                    $if_month = 2;
 | 
						||
                }
 | 
						||
                if($scoringMonths[$month]['self_score']){
 | 
						||
                    $if_month = 1;
 | 
						||
                }
 | 
						||
                $scoringrecord_status = 2;
 | 
						||
                if($scoringMonths[$month]['self_score']){
 | 
						||
                    $scoringrecord_status = 1;
 | 
						||
                }
 | 
						||
                if($scoringMonths[$month]['department_score']){
 | 
						||
                    $scoringrecord_status = 3;
 | 
						||
                }
 | 
						||
                if($scoringMonths[$month]['hospital_score']){
 | 
						||
                    $scoringrecord_status = 4;
 | 
						||
                }
 | 
						||
                $result[] = [
 | 
						||
                    'id' => $id,
 | 
						||
                    'month' => $month,
 | 
						||
                    'user' => $user['nickname'],
 | 
						||
                    'currentMonth' => $currentMonth,
 | 
						||
                    'plus_score' => $scoringMonths[$month]['plus_score'],
 | 
						||
                    'minus_score' => $scoringMonths[$month]['minus_score'],
 | 
						||
                    'self_score' => $scoringMonths[$month]['self_score'],
 | 
						||
                    'department_score' => $scoringMonths[$month]['department_score'],
 | 
						||
                    'hospital_score' => $scoringMonths[$month]['hospital_score'],
 | 
						||
                    'if' => 2,
 | 
						||
                    'if_month' => $if_month,
 | 
						||
                    'scoringrecord_status' => $scoringrecord_status,
 | 
						||
                    'createtime' => $scoringMonths[$month]['createtime'],
 | 
						||
                ];
 | 
						||
            } else {
 | 
						||
                if($month != date('Y-m')){
 | 
						||
                    $if_month = 1;
 | 
						||
                }else{
 | 
						||
                    $if_month = 2;
 | 
						||
                }
 | 
						||
                $result[] = [
 | 
						||
                    'id' => $id,
 | 
						||
                    'month' => $month,
 | 
						||
                    'user' => $user['nickname'],
 | 
						||
                    'currentMonth' => $currentMonth,
 | 
						||
                    'plus_score' => '',
 | 
						||
                    'minus_score' => '',
 | 
						||
                    'self_score' => '',
 | 
						||
                    'department_score' => '',
 | 
						||
                    'hospital_score' => '',
 | 
						||
                    'if' => 1, 
 | 
						||
                    'if_month' => $if_month,
 | 
						||
                    'scoringrecord_status' => 2,
 | 
						||
                    'createtime' => null,
 | 
						||
                ];
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        // 返回结果
 | 
						||
        $sortedResult = $this->sortByMonthDescending($result);
 | 
						||
 | 
						||
        return $this->success('请求成功',$sortedResult);
 | 
						||
    }
 | 
						||
}
 |