274 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			274 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php  
							 | 
						||
| 
								 | 
							
								namespace app\api\controller\backend;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\common\controller\Api;
							 | 
						||
| 
								 | 
							
								use PhpOffice\PhpSpreadsheet\Spreadsheet;  
							 | 
						||
| 
								 | 
							
								use PhpOffice\PhpSpreadsheet\Writer\Xlsx;  
							 | 
						||
| 
								 | 
							
								use think\Controller;  
							 | 
						||
| 
								 | 
							
								use think\Db;  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class ExcelController extends Api  
							 | 
						||
| 
								 | 
							
								{  
							 | 
						||
| 
								 | 
							
								    protected $noNeedLogin = ['*'];
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function dailyexport()  
							 | 
						||
| 
								 | 
							
								    {  
							 | 
						||
| 
								 | 
							
								        $ids = $this->request->get('ids','');
							 | 
						||
| 
								 | 
							
								        // $ids = '9,10,12,13,';
							 | 
						||
| 
								 | 
							
								        if(empty($ids)) {
							 | 
						||
| 
								 | 
							
								            return $this->error('缺少用户ID参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								         
							 | 
						||
| 
								 | 
							
								        // 转换ID格式
							 | 
						||
| 
								 | 
							
								        $idArray = explode(',', rtrim($ids, ','));
							 | 
						||
| 
								 | 
							
								        $idArray = array_filter($idArray); // 过滤空值
							 | 
						||
| 
								 | 
							
								        if(empty($idArray)) {
							 | 
						||
| 
								 | 
							
								            return $this->error('无效的ID参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								         
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        // 构建查询条件
							 | 
						||
| 
								 | 
							
								        $where = [
							 | 
						||
| 
								 | 
							
								            'a.id' => ['in', $idArray]
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								        //  $this->success('返回成功', $where);
							 | 
						||
| 
								 | 
							
								        // 执行查询
							 | 
						||
| 
								 | 
							
								        $date = Db::name('addition_and_subtraction_records')
							 | 
						||
| 
								 | 
							
								            ->alias('a')
							 | 
						||
| 
								 | 
							
								            ->field('a.*,u.nickname,g.name as group_name,w.nickname as zm_nickname,p.project_name,p.scoring_criteria,z.nickname as tb_nickname,a.fj_url')
							 | 
						||
| 
								 | 
							
								            ->join('user u','a.user_id = u.id','LEFT')
							 | 
						||
| 
								 | 
							
								            ->join('user_group g','a.group_id = g.id','LEFT')
							 | 
						||
| 
								 | 
							
								            ->join('user w','a.zm_user_id = w.id','LEFT')
							 | 
						||
| 
								 | 
							
								            ->join('user z','a.tb_user_id = z.id','LEFT')
							 | 
						||
| 
								 | 
							
								            ->join('plus_minus_scoring p','a.assessment_project = p.id','LEFT')
							 | 
						||
| 
								 | 
							
								            ->where($where)
							 | 
						||
| 
								 | 
							
								            ->order('a.id', 'desc')
							 | 
						||
| 
								 | 
							
								            ->select();
							 | 
						||
| 
								 | 
							
								                    // $this->success('返回成功', $date);
							 | 
						||
| 
								 | 
							
								        // 创建一个新的 Excel 文件  
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        $spreadsheet = new Spreadsheet();  
							 | 
						||
| 
								 | 
							
								        $sheet = $spreadsheet->getActiveSheet();  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 设置表头  
							 | 
						||
| 
								 | 
							
								        $headers = ['姓名', '记录时间', '加减分类型', '记录人', '加减分项目', '内容', '分值', '附件地址']; // 根据你的 member 表字段进行调整  
							 | 
						||
| 
								 | 
							
								        $columnIndex = 1; // A = 1, B = 2, ...  
							 | 
						||
| 
								 | 
							
								        foreach ($headers as $header) {  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex, 1, $header);  
							 | 
						||
| 
								 | 
							
								            $columnIndex++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 填充数据  
							 | 
						||
| 
								 | 
							
								        $rowNumber = 2; // 从第二行开始填充数据  
							 | 
						||
| 
								 | 
							
								        foreach ($date as $member) {  
							 | 
						||
| 
								 | 
							
								            $type = '加分';
							 | 
						||
| 
								 | 
							
								            if($member['assessment_type'] == 2){
							 | 
						||
| 
								 | 
							
								                $type = '减分';
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            $columnIndex = 1;  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['nickname']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['fsdate']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $type);
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['tb_nickname']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['project_name']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['scoring_criteria']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['score_value']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['fj_url']);  
							 | 
						||
| 
								 | 
							
								            $rowNumber++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 保存到 PHP 输出流  
							 | 
						||
| 
								 | 
							
								        $writer = new Xlsx($spreadsheet);  
							 | 
						||
| 
								 | 
							
								        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
							 | 
						||
| 
								 | 
							
								        header('Content-Disposition: attachment; filename="' . '加减分数据' . date('YmdHis') . '.xlsx"');
							 | 
						||
| 
								 | 
							
								        // header('Content-Disposition: attachment;filename="活动报名列表' . date('YmdHis') . '.xlsx"');  
							 | 
						||
| 
								 | 
							
								        header('Cache-Control: max-age=0');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 输出到浏览器供用户下载  
							 | 
						||
| 
								 | 
							
								        $writer->save('php://output');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 清理并退出  
							 | 
						||
| 
								 | 
							
								        exit; 
							 | 
						||
| 
								 | 
							
								    }  
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function groupIndexrecordsex()  
							 | 
						||
| 
								 | 
							
								    {  
							 | 
						||
| 
								 | 
							
								        $ids = $this->request->param('ids','');
							 | 
						||
| 
								 | 
							
								        $searchMonth = $this->request->param('month', date('Y-m'));
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        // 转换ID格式
							 | 
						||
| 
								 | 
							
								        $idArray = explode(',', rtrim($ids, ','));
							 | 
						||
| 
								 | 
							
								        $group_id = array_filter($idArray); // 过滤空值
							 | 
						||
| 
								 | 
							
								        if(empty($group_id)) {
							 | 
						||
| 
								 | 
							
								            return $this->error('无效的ID参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        // 参数验证
							 | 
						||
| 
								 | 
							
								        // if (!$group_id) {
							 | 
						||
| 
								 | 
							
								        //     $this->error('无效的id');
							 | 
						||
| 
								 | 
							
								        // }
							 | 
						||
| 
								 | 
							
								        if (!preg_match('/^\d{4}-\d{2}$/', $searchMonth)) {
							 | 
						||
| 
								 | 
							
								            $this->error('月份参数格式错误');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								        // 计算时间范围
							 | 
						||
| 
								 | 
							
								        $firstDay = $searchMonth . '-01 00:00:00';
							 | 
						||
| 
								 | 
							
								        $lastDay = date('Y-m-d 23:59:59', strtotime("last day of $searchMonth"));
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								        // 分页查询用户
							 | 
						||
| 
								 | 
							
								        $users = Db::name('user')
							 | 
						||
| 
								 | 
							
								            ->alias('u')
							 | 
						||
| 
								 | 
							
								            ->field(['u.id', 'u.nickname AS username', 'g.name AS group_name'])
							 | 
						||
| 
								 | 
							
								            ->join('user_group g', 'u.group_id = g.id')
							 | 
						||
| 
								 | 
							
								            ->where('u.id','in', $group_id)
							 | 
						||
| 
								 | 
							
								            ->select();
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								        // 统计分数(使用子查询优化)
							 | 
						||
| 
								 | 
							
								        $scores = Db::name('addition_and_subtraction_records')
							 | 
						||
| 
								 | 
							
								            ->field([
							 | 
						||
| 
								 | 
							
								                'user_id',
							 | 
						||
| 
								 | 
							
								                'SUM(CASE WHEN assessment_type = 1 THEN score_value ELSE 0 END) AS total_addition',
							 | 
						||
| 
								 | 
							
								                'SUM(CASE WHEN assessment_type = 2 THEN score_value ELSE 0 END) AS total_subtraction'
							 | 
						||
| 
								 | 
							
								            ])
							 | 
						||
| 
								 | 
							
								            ->whereTime('fsdate', 'between', [$firstDay, $lastDay])
							 | 
						||
| 
								 | 
							
								            ->group('user_id')
							 | 
						||
| 
								 | 
							
								            ->select();
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								        // 构建返回数据
							 | 
						||
| 
								 | 
							
								        $returnData = [];
							 | 
						||
| 
								 | 
							
								        foreach ($users as $user) {
							 | 
						||
| 
								 | 
							
								            $scoreArray = array_column($scores, null, 'user_id');
							 | 
						||
| 
								 | 
							
								            $total_addition = $scoreArray[$user['id']]['total_addition'] ?? 0;
							 | 
						||
| 
								 | 
							
								            $total_subtraction = $scoreArray[$user['id']]['total_subtraction'] ?? 0;
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								            $returnData[] = [
							 | 
						||
| 
								 | 
							
								                'user_id' =>  $user['id'],
							 | 
						||
| 
								 | 
							
								                'username' => $user['username'],
							 | 
						||
| 
								 | 
							
								                'group_name' => $user['group_name'],
							 | 
						||
| 
								 | 
							
								                'month' => $searchMonth,
							 | 
						||
| 
								 | 
							
								                'total_addition' => floatval($total_addition),
							 | 
						||
| 
								 | 
							
								                'total_subtraction' => floatval($total_subtraction)
							 | 
						||
| 
								 | 
							
								            ];
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								     
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // return $this->success('评价成功', $returnData);
							 | 
						||
| 
								 | 
							
								                    // $this->success('返回成功', $date);
							 | 
						||
| 
								 | 
							
								        // 创建一个新的 Excel 文件  
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        $spreadsheet = new Spreadsheet();  
							 | 
						||
| 
								 | 
							
								        $sheet = $spreadsheet->getActiveSheet();  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 设置表头  
							 | 
						||
| 
								 | 
							
								        $headers = ['姓名', '科室', '月份', '加分值', '扣分值']; // 根据你的 member 表字段进行调整  
							 | 
						||
| 
								 | 
							
								        $columnIndex = 1; // A = 1, B = 2, ...  
							 | 
						||
| 
								 | 
							
								        foreach ($headers as $header) {  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex, 1, $header);  
							 | 
						||
| 
								 | 
							
								            $columnIndex++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 填充数据  
							 | 
						||
| 
								 | 
							
								        $rowNumber = 2; // 从第二行开始填充数据  
							 | 
						||
| 
								 | 
							
								        foreach ($returnData as $member) {  
							 | 
						||
| 
								 | 
							
								            $columnIndex = 1;  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['username']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['group_name']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['month']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['total_addition']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['total_subtraction']);  
							 | 
						||
| 
								 | 
							
								            $rowNumber++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 保存到 PHP 输出流  
							 | 
						||
| 
								 | 
							
								        $writer = new Xlsx($spreadsheet);  
							 | 
						||
| 
								 | 
							
								        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
							 | 
						||
| 
								 | 
							
								        header('Content-Disposition: attachment; filename="' . '年终考评数据' . date('YmdHis') . '.xlsx"');
							 | 
						||
| 
								 | 
							
								        // header('Content-Disposition: attachment;filename="活动报名列表' . date('YmdHis') . '.xlsx"');  
							 | 
						||
| 
								 | 
							
								        header('Cache-Control: max-age=0');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 输出到浏览器供用户下载  
							 | 
						||
| 
								 | 
							
								        $writer->save('php://output');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 清理并退出  
							 | 
						||
| 
								 | 
							
								        exit; 
							 | 
						||
| 
								 | 
							
								    }  
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								    public function RejectRedEnvelopesEs()  
							 | 
						||
| 
								 | 
							
								    {  
							 | 
						||
| 
								 | 
							
								        $ids = $this->request->param('ids','');
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        // 转换ID格式
							 | 
						||
| 
								 | 
							
								        $idArray = explode(',', rtrim($ids, ','));
							 | 
						||
| 
								 | 
							
								        $idArray = array_filter($idArray); // 过滤空值
							 | 
						||
| 
								 | 
							
								        if(empty($idArray)) {
							 | 
						||
| 
								 | 
							
								            return $this->error('无效的ID参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $where = [
							 | 
						||
| 
								 | 
							
								            'a.id' => ['in', $idArray]
							 | 
						||
| 
								 | 
							
								        ];
							 | 
						||
| 
								 | 
							
								        $date = Db::name('reject_red_envelopes')
							 | 
						||
| 
								 | 
							
								                    ->field('a.*,u.nickname,g.name as group_name,w.nickname as zm_nickname')
							 | 
						||
| 
								 | 
							
								                    ->alias('a')
							 | 
						||
| 
								 | 
							
								                    ->join('user u','a.user_id = u.id','LEFT')
							 | 
						||
| 
								 | 
							
								                    ->join('user_group g','a.group_id = g.id','LEFT')
							 | 
						||
| 
								 | 
							
								                    ->join('user w','a.zm_user_id = w.id','LEFT')
							 | 
						||
| 
								 | 
							
								                    ->where($where)
							 | 
						||
| 
								 | 
							
								                    ->order('a.id', 'desc')
							 | 
						||
| 
								 | 
							
								                    ->select();
							 | 
						||
| 
								 | 
							
								       
							 | 
						||
| 
								 | 
							
								        // return $this->success('查询成功',$date);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // return $this->success('评价成功', $returnData);
							 | 
						||
| 
								 | 
							
								                    // $this->success('返回成功', $date);
							 | 
						||
| 
								 | 
							
								        // 创建一个新的 Excel 文件  
							 | 
						||
| 
								 | 
							
								        
							 | 
						||
| 
								 | 
							
								        $spreadsheet = new Spreadsheet();  
							 | 
						||
| 
								 | 
							
								        $sheet = $spreadsheet->getActiveSheet();  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 设置表头  
							 | 
						||
| 
								 | 
							
								        $headers = ['发生时间', '科室', '病区', '当事人姓名', '患者姓名', '人员编号', '退还金额', '退还日期', '退还方式', '备注', '附件地址链接']; // 根据你的 member 表字段进行调整  
							 | 
						||
| 
								 | 
							
								        $columnIndex = 1; // A = 1, B = 2, ...  
							 | 
						||
| 
								 | 
							
								        foreach ($headers as $header) {  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex, 1, $header);  
							 | 
						||
| 
								 | 
							
								            $columnIndex++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 填充数据  
							 | 
						||
| 
								 | 
							
								        $rowNumber = 2; // 从第二行开始填充数据  
							 | 
						||
| 
								 | 
							
								        foreach ($date as $member) {  
							 | 
						||
| 
								 | 
							
								            $fangshi = '现金';
							 | 
						||
| 
								 | 
							
								            if($member['refunding_type'] == 2){
							 | 
						||
| 
								 | 
							
								               $fangshi = '转账'; 
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            $columnIndex = 1;  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['fsdate']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['group_name']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['bq_name']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['nickname']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['hz_name']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['code']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['refunding_amount']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['thdate']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $fangshi);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['notes']);  
							 | 
						||
| 
								 | 
							
								            $sheet->setCellValueByColumnAndRow($columnIndex++, $rowNumber, $member['fj_url']);  
							 | 
						||
| 
								 | 
							
								            $rowNumber++;  
							 | 
						||
| 
								 | 
							
								        }  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 保存到 PHP 输出流  
							 | 
						||
| 
								 | 
							
								        $writer = new Xlsx($spreadsheet);  
							 | 
						||
| 
								 | 
							
								        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
							 | 
						||
| 
								 | 
							
								        header('Content-Disposition: attachment; filename="' . '拒收红包数据' . date('YmdHis') . '.xlsx"');
							 | 
						||
| 
								 | 
							
								        // header('Content-Disposition: attachment;filename="活动报名列表' . date('YmdHis') . '.xlsx"');  
							 | 
						||
| 
								 | 
							
								        header('Cache-Control: max-age=0');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 输出到浏览器供用户下载  
							 | 
						||
| 
								 | 
							
								        $writer->save('php://output');  
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // 清理并退出  
							 | 
						||
| 
								 | 
							
								        exit; 
							 | 
						||
| 
								 | 
							
								    }  
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								}
							 |