package com.bonus.bracelet.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.bonus.bracelet.mapper.BraceletMapper;
import com.bonus.bracelet.mapper.DeviceLyRecordMapper;
import com.bonus.bracelet.mapper.DeviceMapper;
import com.bonus.bracelet.service.*;
import com.bonus.common.core.constant.BusinessConstants;
import com.bonus.common.core.utils.ExcelStyleUtil;
import com.bonus.common.entity.bracelet.BraceletParamsDto;
import com.bonus.common.entity.bracelet.exportVo.*;
import com.bonus.common.entity.bracelet.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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.bonus.common.core.text.Convert.str;


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


    @Resource(name = "IPersonMgeService")
    private IPersonMgeService iPersonMgeService;

    @Resource(name = "ITeamMgeService")
    private ITeamMgeService iTeamMgeService;

    @Resource(name = "IDeviceService")
    private IDeviceService iDeviceService;

    @Resource(name = "IBraceletService")
    private IBraceletService iBraceletService;

    @Resource(name = "IDeviceLyRecordService")
    private  IDeviceLyRecordService  iDeviceLyRecordService;

    @Resource(name = "DeviceMapper")
    private DeviceMapper deviceMapper;

    @Resource(name = "BraceletMapper")
    private BraceletMapper braceletMapper;

    @Resource(name = "DeviceLyRecordMapper")
    private DeviceLyRecordMapper deviceLyRecordMapper;

    /**
     * 导出人员
     *
     * @param request
     * @param response
     * @param dto
     * @return void
     * @author cwchen
     * @date 2024/7/16 21:13
     */
    @GetMapping("exportPerson")
    public void exportPerson(HttpServletRequest request, HttpServletResponse response, BraceletParamsDto dto) {
        try {
            List<PersonExportVo> personExportVoList = new ArrayList<>();
            List<PersonVo> personLists = iPersonMgeService.getPersonLists(dto);
            for (int i = 0; i < personLists.size(); i++) {
                PersonExportVo exportVo = new PersonExportVo();
                PersonVo vo = personLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setSex(vo.getSex() == BusinessConstants.TYPE ? "男" : vo.getSex() == BusinessConstants.TYPE2 ? "其他" : "女");
                personExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("人员", "人员", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, PersonExportVo.class, personExportVoList);
            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.toString(), e);
        }
    }

    /**
     * 导出班组
     * @param request
     * @param response
     * @param dto
     * @return void
     * @author cwchen
     * @date 2024/7/17 18:00
     */
    @GetMapping("exportTeam")
    public void exportTeam(HttpServletRequest request, HttpServletResponse response, BraceletParamsDto dto) {
        try {
            List<TeamExportVo> teamExportVoList = new ArrayList<>();
            List<TeamVo> teamLists = iTeamMgeService.getTeamLists(dto);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (int i = 0; i < teamLists.size(); i++) {
                TeamExportVo exportVo = new TeamExportVo();
                TeamVo vo = teamLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setTeamLeaderAndPhone(vo.getTeamLeader() + "/" + vo.getLeaderPhone());
                exportVo.setNum(vo.getWorkNum() + "/" + vo.getNotWorkNum());
                exportVo.setTeamStatus(Objects.equals(vo.getTeamStatus(),BusinessConstants.TYPE) ? "已解散":"正常");
                exportVo.setCreateTime(format.format(vo.getCreateTime()));
                if(vo.getJsTime() != null){
                    exportVo.setJsTime(format.format(vo.getJsTime()));
                }
                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.toString(), e);
        }
    }

    @GetMapping("exportDevice")
    public void exportDevice(HttpServletRequest request, HttpServletResponse response, DeviceVo dto) {
        try {
            List<DeviceExportVo> deviceExportVoList = new ArrayList<>();
            List<DeviceVo> deviceLists = iDeviceService.getDeviceLists(dto);
            for (int i = 0; i < deviceLists.size(); i++) {
                DeviceExportVo exportVo = new DeviceExportVo();
                if(deviceLists.get(i).getLyType()==0){
                    String name = deviceMapper.getDeviceLyName(deviceLists.get(i).getLyId());
                    deviceLists.get(i).setLyName(name);
                }else if(deviceLists.get(i).getLyType()==1){
                    String name = deviceMapper.getDeviceLyNameTemp(deviceLists.get(i).getLyId());
                    deviceLists.get(i).setLyName(name);
                }
                DeviceVo vo = deviceLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setDeviceName(vo.getDeviceName());
                exportVo.setDeviceType(vo.getDeviceType());
                exportVo.setDeviceCode(vo.getDeviceCode());
                exportVo.setLyName(vo.getLyName());
                exportVo.setDeviceStatus(vo.getDeviceStatus() == BusinessConstants.TYPE3 ? "在用" : vo.getDeviceStatus() == BusinessConstants.TYPE ? "未用" : "其他");
                deviceExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("设备", "设备", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, DeviceExportVo.class, deviceExportVoList);
            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.toString(), e);
        }
    }

    @GetMapping("exportSh")
    public void exportSh(HttpServletRequest request, HttpServletResponse response, BraceletVo dto) {
        try {
            List<ShExportVo> shExportVoList = new ArrayList<>();
            List<BraceletVo> shLists = iBraceletService.getBraceletLists(dto);
            for (int i = 0; i < shLists.size(); i++) {
                ShExportVo exportVo = new ShExportVo();
                BraceletVo vo = shLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setShStatus(vo.getShStatus() == BusinessConstants.TYPE3 ? "在用" : vo.getShStatus() == BusinessConstants.TYPE ? "未用" : "其他");
                shExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("手环", "手环", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, ShExportVo.class, shExportVoList);
            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.toString(), e);
        }
    }

    @GetMapping("exportDeviceLyRecord")
    public void exportDeviceLyRecord(HttpServletRequest request, HttpServletResponse response, EquipmentReqVo dto) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            List<DeviceLyRecordExportVo> deviceLyRecordExportVoList = new ArrayList<>();
            List<EquipmentReqVo> deviceLists = iDeviceLyRecordService.getEquipmentReqLists(dto);
            for (int i = 0; i < deviceLists.size(); i++) {
                DeviceLyRecordExportVo exportVo = new DeviceLyRecordExportVo();
                EquipmentReqVo vo = deviceLists.get(i);
                BeanUtils.copyProperties(vo, exportVo);
                exportVo.setId((i + 1) + "");
                exportVo.setTeamLeader(vo.getTeamLeader()+ "\\" +vo.getLeaderPhone());
                exportVo.setLyTime(sdf.format(vo.getLyTime()));
                exportVo.setGhTime(vo.getGhTime() == null ? "在用" : sdf.format(vo.getGhTime()));
                deviceLyRecordExportVoList.add(exportVo);
            }
            ExportParams exportParams = new ExportParams("设备领用记录", "设备领用记录", ExcelType.XSSF);
            exportParams.setStyle(ExcelStyleUtil.class);
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, DeviceLyRecordExportVo.class, deviceLyRecordExportVoList);
            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.toString(), e);
        }
    }
}
