package com.securitycontrol.background.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.securitycontrol.background.service.DutyPlanService;
import com.securitycontrol.background.service.HumanService;
import com.securitycontrol.background.service.ITodayTaskService;
import com.securitycontrol.background.service.TeamService;
import com.securitycontrol.common.core.utils.ExcelStyleUtil;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.aes.StringHelper;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.background.dto.ParamDto;
import com.securitycontrol.entity.background.dto.TodayTaskDto;
import com.securitycontrol.entity.background.entity.*;
import com.securitycontrol.entity.background.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;


/**
 * @author：cwchen
 * @date：2024-03-11-14:55
 * @version：1.0
 * @description：导出文件-web层
 */
@RestController
@RequestMapping("/back/export/")
@Slf4j
public class ExportFileController {

    @Resource(name = "TeamService")
    private TeamService service;

    @Resource(name = "HumanService")
    private HumanService humanService;

    @Resource(name = "DutyPlanService")
    private DutyPlanService dutyPlanService;

    @Resource(name = "ITodayTaskService")
    private ITodayTaskService iTodayTaskService;

    @GetMapping("exportDutyPlanData")
    @Log(title = "值班计划管理", menu = "风险值班管理->值班计划管理", grade = OperationType.EXPORT_BUSINESS, details = "导出值班计划", type = "业务日志")
    public void exportDutyPlanData(HttpServletRequest request, HttpServletResponse response, DutyPlanVo dto) {
        try {
            List<DutyPlanExportVo> teamExportVoList = new ArrayList<>();
            List<DutyPlanVo> teamLists = dutyPlanService.getDutyPlan(dto);
            String time="";
            for (int i = 0; i < teamLists.size(); i++) {
                if("1".equals(teamLists.get(i).getType())){
                    teamLists.get(i).setType("上午");
                }else{
                    teamLists.get(i).setType("下午");
                }
                time= teamLists.get(i).getCreateDay();
                DutyPlanExportVo exportVo = new DutyPlanExportVo();
                BeanUtils.copyProperties(teamLists.get(i), exportVo);
                teamExportVoList.add(exportVo);
            }
            if(StringHelper.isEmpty(time)){
                time="("+time+")";
            }
            ExportParams exportParams = new ExportParams("值班计划列表"+time, "值班计划列表", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, DutyPlanExportVo.class, teamExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("班组列表" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出值班计划", e);
        }
    }

    @GetMapping("exportTeamData")
    @Log(title = "人车管理", menu = "人车管理->班组管理", grade = OperationType.EXPORT_BUSINESS, details = "导出班组", type = "业务日志")
    public void exportTeamData(HttpServletRequest request, HttpServletResponse response, ParamDto dto) {
        try {
            List<TeamExportVo> teamExportVoList = new ArrayList<>();
            List<TeamManageVo> teamLists = service.getTeamLists(dto);
            for (int i = 0; i < teamLists.size(); i++) {
                teamLists.get(i).setTeamId((i + 1) + "");
                TeamExportVo exportVo = new TeamExportVo();
                BeanUtils.copyProperties(teamLists.get(i), exportVo);
                teamExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("班组列表", "班组列表", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, TeamExportVo.class, teamExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("班组列表" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出班组", e);
        }
    }

    @GetMapping("exportTeamUserData")
    @Log(title = "人车管理", menu = "人车管理->人员管理", grade = OperationType.EXPORT_BUSINESS, details = "导出班组人员", type = "业务日志")
    public void exportTeamUserData(HttpServletRequest request, HttpServletResponse response, ParamDto dto) {
        try {
            List<TeamUserExportVo> teamUserExportVoList = new ArrayList<>();
            List<HumanManageVo> teamUserLists = humanService.getHumanLists(dto);
            for (int i = 0; i < teamUserLists.size(); i++) {
                teamUserLists.get(i).setUserId((i + 1) + "");
                TeamUserExportVo teamUserExportVo = new TeamUserExportVo();
                BeanUtils.copyProperties(teamUserLists.get(i), teamUserExportVo);
                teamUserExportVoList.add(teamUserExportVo);
            }
            ExportParams exportParams = new ExportParams("班组人员", "班组人员", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, TeamUserExportVo.class, teamUserExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("班组人员" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出班组人员", e);
        }
    }

    @GetMapping("exportUserAccessData")
    @Log(title = "人车管理", menu = "人车管理->人员管理", grade = OperationType.EXPORT_BUSINESS, details = "导出人员进出场记录", type = "业务日志")
    public void exportUserAccessData(HttpServletRequest request, HttpServletResponse response, ParamDto dto) {
        try {
            if(StringUtils.isNotEmpty(dto.getTime())){
                String[] timeArr = dto.getTime().split(" - ");
                dto.setStartTime(timeArr[0]);
                dto.setEndTime(timeArr[1]);
            }else {
                dto.setStartTime(DateTimeHelper.getNowDate());
                dto.setEndTime(DateTimeHelper.getNowDate());
            }
            List<UserAccessExportVo> userAccessExportVoList = new ArrayList<>();
            List<UserAccessVo> userAccessLists = humanService.getPersonnelAccessLists(dto);
            for (int i = 0; i < userAccessLists.size(); i++) {
                userAccessLists.get(i).setUserId((i + 1) + "");
                UserAccessExportVo userAccessExportVo = new UserAccessExportVo();
                BeanUtils.copyProperties(userAccessLists.get(i), userAccessExportVo);
                userAccessExportVoList.add(userAccessExportVo);
            }
            ExportParams exportParams = new ExportParams("人员进出场记录", "人员进出场记录", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, UserAccessExportVo.class, userAccessExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("人员进出场记录" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出人员进出场记录", e);
        }
    }

    @GetMapping("exportTodayTaskData")
    @Log(title = "风险值班管理", menu = "风险值班管理->当日任务", grade = OperationType.EXPORT_BUSINESS, details = "导出当日任务", type = "业务日志")
    public void exportTodayTaskData(HttpServletRequest request, HttpServletResponse response, TodayTaskDto dto) {
        try {
            List<TodayTaskExportVo> todayTaskExportVoList = new ArrayList<>();
            List<TodayTaskVo> todayTaskVoList = iTodayTaskService.getToDayTaskLists(dto);
            for (int i = 0; i < todayTaskVoList.size(); i++) {
                todayTaskVoList.get(i).setClassId((i + 1) + "");
                TodayTaskExportVo todayTaskExportVo = new TodayTaskExportVo();
                BeanUtils.copyProperties(todayTaskVoList.get(i), todayTaskExportVo);
                todayTaskExportVoList.add(todayTaskExportVo);
            }
            ExportParams exportParams = new ExportParams("当日任务", "当日任务", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, TodayTaskExportVo.class, todayTaskExportVoList);
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("当日任务" + ".xlsx", "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            log.error("导出当日任务", e);
        }
    }
}
