package com.bonus.boot.manager.manager.utils;

import com.bonus.boot.manager.career.entity.CareerBean;
import com.bonus.boot.manager.career.service.CareerService;
import com.bonus.boot.manager.manager.table.PageTableRequest;
import com.bonus.boot.manager.physical.entity.EmployeeHealthCheckBean;
import com.bonus.boot.manager.physical.service.EmployeeHealthCheckService;
import io.swagger.annotations.Api;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 包名称：com.bonus.boot.manager.manager.utils
 * 类名称：ExportController
 * 类描述：导出
 * 创建人：@author tqzhang
 * 创建时间：2023年06月28日 09:36
 */

@Api(tags = "导出")
@RestController
@RequestMapping("/export")
public class ExportController {

    @Resource
    private EmployeeHealthCheckService employeeHealthCheckService;
    @Resource
    private CareerService careerService;
    /**
     * 体检结果管理导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/exportPhysicalCheckResult", method = RequestMethod.GET)
    public void exportPayroll(HttpServletRequest request, HttpServletResponse response, PageTableRequest page){
        EmployeeHealthCheckBean b = new EmployeeHealthCheckBean();
        b.setPhysicalTime(request.getParameter("physicalTime"));
        b.setPhysicalStatus(request.getParameter("physicalStatus"));
        b.setKeyWord(request.getParameter("keyword"));
        if ("1".equals(request.getParameter("type"))){
            b.setHospitalId(UserUtil.getLoginUser().getHospitalId());
        }else {
            b.setHospitalId("");
        }
        List<EmployeeHealthCheckBean> list = employeeHealthCheckService.getList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","身份证号","年龄","性别","联系方式",
                "部门","是否高职","人员类别","岗位","体检状态","体检时间","体检费用"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2,2,2};
        String fileName = "体检结果信息表";
        if (list != null) {
            int i = 0;
            for (EmployeeHealthCheckBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>();
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("身份证号",bean.getIdNumber());
                map.put("年龄",bean.getAge());
                map.put("性别",bean.getSex());
                map.put("联系方式",bean.getPhone());
                map.put("部门",bean.getDepartment());
                map.put("是否高职",bean.getHigherJob());
                map.put("人员类别",bean.getPersonType());
                map.put("岗位",bean.getSpecialPostName());
                map.put("体检状态",bean.getPhysicalStatus());
                map.put("体检时间",bean.getPhysicalTime());
                map.put("体检费用",bean.getPhysicalFree());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 职业健康结果管理导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/careerExport", method = RequestMethod.GET)
    public void careerExport(HttpServletRequest request, HttpServletResponse response, PageTableRequest page){
        CareerBean b = new CareerBean();
        b.setHazards(request.getParameter("hazards"));
        b.setStatus(request.getParameter("status"));
        b.setKeyWord(request.getParameter("keyWord"));
        b.setPhysicalTime(request.getParameter("physicalTime"));
        List<CareerBean> list = careerService.getList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","部门","性别","危害因素","身份证号码","手机号码",
                "工种","体检状态","总结报告状态","预约时间"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "职业健康体检结果信息表";
        if (list != null) {
            int i = 0;
            for (CareerBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>();
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("部门",bean.getUnit());
                map.put("性别",bean.getSex());
                map.put("危害因素",bean.getHazards());
                map.put("身份证号码",bean.getIdNumber());
                map.put("手机号码",bean.getPhone());
                map.put("工种",bean.getPostName());
                map.put("体检状态",bean.getPhysicalStatus());
                map.put("总结报告状态",bean.getStatus());
                map.put("预约时间",bean.getPhysicalTime());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 职业健康统计导出
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/careerStatisticsExport", method = RequestMethod.GET)
    public void careerStatisticsExport(HttpServletRequest request, HttpServletResponse response, PageTableRequest page){
        CareerBean b = new CareerBean();
        b.setHazards(request.getParameter("hazards"));
        b.setStatus(request.getParameter("status"));
        b.setKeyWord(request.getParameter("keyWord"));
        b.setPhysicalTime(request.getParameter("physicalTime"));
        List<CareerBean> list = careerService.getList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","身份证号码","年龄","性别","联系方式","部门","岗位","体检状态"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2, 2};
        String fileName = "职业健康体检结果统计信息表";
        if (list != null) {
            int i = 0;
            for (CareerBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>();
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("身份证号码",bean.getIdNumber());
                map.put("年龄",bean.getAge());
                map.put("性别",bean.getSex());
                map.put("联系方式",bean.getPhone());
                map.put("部门",bean.getUnit());
                map.put("岗位",bean.getPostName());
                map.put("体检状态",bean.getStatus());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 职业健康体检复查
     * @param request
     * @param response
     * @param page
     */
    @RequestMapping(value = "/careerExportReview", method = RequestMethod.GET)
    public void careerExportReview(HttpServletRequest request, HttpServletResponse response, PageTableRequest page){
        CareerBean b = new CareerBean();
        b.setStatus(request.getParameter("status"));
        b.setKeyWord(request.getParameter("keyWord"));
        b.setPhysicalTime(request.getParameter("physicalTime"));
        List<CareerBean> list = careerService.getList(b);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        String[] excelHeader = {"序号","姓名","性别","年龄","身份证号码","部门","结论","状态","体检次数"};
        int[] ds_format = {2, 2, 2, 2, 2, 2, 2, 2,2};
        String fileName = "职业健康体检复查统计表";
        if (list != null) {
            int i = 0;
            for (CareerBean bean : list){
                i++;
                Map<String, Object> map = new HashMap<>();
                map.put("序号",i);
                map.put("姓名",bean.getPersonName());
                map.put("性别",bean.getSex());
                map.put("年龄",bean.getAge());
                map.put("身份证号码",bean.getIdNumber());
                map.put("部门",bean.getUnit());
                map.put("结论",bean.getConclusion());
                map.put("状态",bean.getStatus());
                map.put("体检次数",bean.getCounts());
                mapList.add(map);
            }
        }
        try {
            ExcelUtils.export(fileName, fileName, excelHeader, excelHeader, ds_format, null, mapList, request,
                    response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
