package com.ruoyi.powerdistribution.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.powerdistribution.domain.*;
import com.ruoyi.powerdistribution.domain.dto.PdmWorkPlanDTO;
import com.ruoyi.powerdistribution.domain.vo.ReportDayVO;
import com.ruoyi.powerdistribution.domain.vo.ReportDayWeek;
import com.ruoyi.powerdistribution.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/power/report")
//@Api(value = "powerReport", tags = "报表查询")
public class ReportController extends BaseController {

    @Autowired
    private IPdmReportAppointmentDayService pdmReportAppointmentDayService;
    @Autowired
    private IPdmReportIllegalDayService pdmReportIllegalDayService;
    @Autowired
    private IPdmReportPlanDayService pdmReportPlanDayService;
    @Autowired
    private IPdmReportPlanNextDayService pdmReportPlanNextDayService;
    @Autowired
    private IPdmReportAppointmentWeekService pdmReportAppointmentWeekService;
    @Autowired
    private IPdmReportIllegalWeekService pdmReportIllegalWeekService;
    @Autowired
    private IPdmReportPlanWeekService pdmReportPlanWeekService;
    @Autowired
    private IPdmReportPlanNextWeekService pdmReportPlanNextWeekService;
    @Autowired
    private IPdmWorkPlanService pdmWorkPlanService;

    /**
     * 日报查询
     */
    @ApiOperation(value = "日报查询", notes = "日报查询")
    @GetMapping("/day")
    public ReportDayVO day(String reportDate)
    {
        PdmReportIllegalDay pdmReportIllegalDay = new PdmReportIllegalDay();
        pdmReportIllegalDay.setReportDate(reportDate);
        List<PdmReportIllegalDay> illegalList = pdmReportIllegalDayService.selectPdmReportIllegalDayList(pdmReportIllegalDay);
        PdmReportAppointmentDay pdmReportAppointmentDay = new PdmReportAppointmentDay();
        pdmReportAppointmentDay.setReportDate(reportDate);
        List<PdmReportAppointmentDay> appointmentlist = pdmReportAppointmentDayService.selectPdmReportAppointmentDayList(pdmReportAppointmentDay);
        PdmReportPlanDay pdmReportPlanDay = new PdmReportPlanDay();
        pdmReportPlanDay.setReportDate(reportDate);
        List<PdmReportPlanDay> planlist = pdmReportPlanDayService.selectPdmReportPlanDayList(pdmReportPlanDay);
        PdmReportPlanNextDay nextDay = new PdmReportPlanNextDay();
        nextDay.setReportDate(DateUtils.getNextDateStr(reportDate,DateUtils.YYYYMMDD,1));
        List<PdmReportPlanNextDay> nextDayList = pdmReportPlanNextDayService.selectPdmReportPlanNextDayList(nextDay);

        ReportDayVO vo = new ReportDayVO();
        vo.setAppointmentList(appointmentlist);
        vo.setIllegalList((illegalList==null||(illegalList.size()==1&&illegalList.get(0).getCompany().equals("总计")))?new ArrayList<>():illegalList);
        vo.setPlanList(planlist);
        vo.setNextDayList(nextDayList);
        return vo;
    }

    /**
     * 日报导出
     * @param response
     * @param reportDate
     * @throws IOException
     */
    @PostMapping("/dayExport")
    @ApiOperation(value = "日报导出")
    public void dayExport(HttpServletResponse response,String reportDate) throws IOException {
        PdmReportIllegalDay pdmReportIllegalDay = new PdmReportIllegalDay();
        pdmReportIllegalDay.setReportDate(reportDate);
        List<PdmReportIllegalDay> illegalList = pdmReportIllegalDayService.selectPdmReportIllegalDayList(pdmReportIllegalDay);
        PdmReportAppointmentDay pdmReportAppointmentDay = new PdmReportAppointmentDay();
        pdmReportAppointmentDay.setReportDate(reportDate);
        List<PdmReportAppointmentDay> appointmentlist = pdmReportAppointmentDayService.selectPdmReportAppointmentDayList(pdmReportAppointmentDay);
        PdmReportPlanDay pdmReportPlanDay = new PdmReportPlanDay();
        pdmReportPlanDay.setReportDate(reportDate);
        List<PdmReportPlanDay> planlist = pdmReportPlanDayService.selectPdmReportPlanDayList(pdmReportPlanDay);
        PdmReportPlanNextDay nextDay = new PdmReportPlanNextDay();
        nextDay.setReportDate(DateUtils.getNextDateStr(reportDate,DateUtils.YYYYMMDD,1));
        List<PdmReportPlanNextDay> nextDayList = pdmReportPlanNextDayService.selectPdmReportPlanNextDayList(nextDay);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = java.util.UUID.randomUUID() + ".xlsx";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        String templateFileName = "excelTemplate/dayReport.xlsx";
        InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(templateFileName);
        OutputStream ops = response.getOutputStream();

        ExcelWriter excelWriter = EasyExcel.write(ops).withTemplate(fis).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("违章情况").build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(illegalList, fillConfig, writeSheet);
        WriteSheet writeSheet2 = EasyExcel.writerSheet("作业计划").build();
        excelWriter.fill(planlist, fillConfig, writeSheet2);
        WriteSheet writeSheet3 = EasyExcel.writerSheet("履职情况").build();
        excelWriter.fill(appointmentlist, fillConfig, writeSheet3);
        WriteSheet writeSheet4 = EasyExcel.writerSheet("明日计划").build();
        excelWriter.fill(nextDayList, fillConfig, writeSheet4);
        excelWriter.finish();

    }

    /**
     * 周报查询
     */
    @ApiOperation(value = "周报查询", notes = "周报查询")
    @GetMapping("/week")
    public ReportDayWeek week(String startDate,String endDate)
    {
        PdmReportIllegalWeek pdmReportIllegal = new PdmReportIllegalWeek();
        pdmReportIllegal.setReportDate(startDate);
        pdmReportIllegal.setReportEndDate(endDate);
        List<PdmReportIllegalWeek> illegalList = pdmReportIllegalWeekService.selectPdmReportIllegalWeekList(pdmReportIllegal);
        PdmReportAppointmentWeek pdmReportAppointment = new PdmReportAppointmentWeek();
        pdmReportAppointment.setReportDate(startDate);
        pdmReportAppointment.setReportEndDate(endDate);
        List<PdmReportAppointmentWeek> appointmentlist = pdmReportAppointmentWeekService.selectPdmReportAppointmentWeekList(pdmReportAppointment);
        PdmReportPlanWeek pdmReportPlan = new PdmReportPlanWeek();
        pdmReportPlan.setReportDate(startDate);
        pdmReportPlan.setReportEndDate(endDate);
        List<PdmReportPlanWeek> planlist = pdmReportPlanWeekService.selectPdmReportPlanWeekList(pdmReportPlan);
        PdmReportPlanNextWeek next = new PdmReportPlanNextWeek();
        next.setReportDate(DateUtils.getNextMonday(startDate,DateUtils.YYYYMMDD));
        List<PdmReportPlanNextWeek> nextList = pdmReportPlanNextWeekService.selectPdmReportPlanNextWeekList(next);
        PdmWorkPlanDTO workPlan = new PdmWorkPlanDTO();
        workPlan.setStartDate(DateUtils.getNextMonday(startDate,DateUtils.YYYYMMDD));
        workPlan.setEndDate(DateUtils.getNextSunday(endDate,DateUtils.YYYYMMDD));
        workPlan.setLevel3Flag("1");//三级及以上作业
        List<PdmWorkPlan> workList = pdmWorkPlanService.selectPdmWorkPlanList(workPlan);

        ReportDayWeek vo = new ReportDayWeek();
        vo.setAppointmentList(appointmentlist);
        vo.setIllegalList(illegalList);
        vo.setPlanList(planlist);
        vo.setNextList(nextList);
        vo.setWorkList(workList);
        return vo;
    }

    /**
     * 周报导出
     * @param response
     * @param startDate
     * @param endDate
     * @throws IOException
     */
    @PostMapping("/weekExport")
    @ApiOperation(value = "周报导出")
    public void weekExport(HttpServletResponse response,String startDate,String endDate) throws IOException {
        PdmReportIllegalWeek pdmReportIllegal = new PdmReportIllegalWeek();
        pdmReportIllegal.setReportDate(startDate);
        pdmReportIllegal.setReportEndDate(endDate);
        List<PdmReportIllegalWeek> illegalList = pdmReportIllegalWeekService.selectPdmReportIllegalWeekList(pdmReportIllegal);
        PdmReportAppointmentWeek pdmReportAppointment = new PdmReportAppointmentWeek();
        pdmReportAppointment.setReportDate(startDate);
        pdmReportAppointment.setReportEndDate(endDate);
        List<PdmReportAppointmentWeek> appointmentlist = pdmReportAppointmentWeekService.selectPdmReportAppointmentWeekList(pdmReportAppointment);
        PdmReportPlanWeek pdmReportPlan = new PdmReportPlanWeek();
        pdmReportPlan.setReportDate(startDate);
        pdmReportPlan.setReportEndDate(endDate);
        List<PdmReportPlanWeek> planlist = pdmReportPlanWeekService.selectPdmReportPlanWeekList(pdmReportPlan);
        PdmReportPlanNextWeek next = new PdmReportPlanNextWeek();
        next.setReportDate(DateUtils.getNextMonday(startDate,DateUtils.YYYYMMDD));
        List<PdmReportPlanNextWeek> nextList = pdmReportPlanNextWeekService.selectPdmReportPlanNextWeekList(next);
        PdmWorkPlanDTO workPlan = new PdmWorkPlanDTO();
        workPlan.setStartDate(DateUtils.getNextMonday(startDate,DateUtils.YYYYMMDD));
        workPlan.setEndDate(DateUtils.getNextSunday(endDate,DateUtils.YYYYMMDD));
        workPlan.setLevel3Flag("1");//三级及以上作业
        List<PdmWorkPlan> workList = pdmWorkPlanService.selectPdmWorkPlanList(workPlan);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = java.util.UUID.randomUUID() + ".xlsx";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        String templateFileName = "excelTemplate/weekReport.xlsx";
        InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream(templateFileName);
        OutputStream ops = response.getOutputStream();

        ExcelWriter excelWriter = EasyExcel.write(ops).withTemplate(fis).build();
        WriteSheet writeSheet = EasyExcel.writerSheet("违章情况").build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(illegalList, fillConfig, writeSheet);
        WriteSheet writeSheet2 = EasyExcel.writerSheet("本周计划执行情况").build();
        excelWriter.fill(planlist, fillConfig, writeSheet2);
        WriteSheet writeSheet3 = EasyExcel.writerSheet("履职情况").build();
        excelWriter.fill(appointmentlist, fillConfig, writeSheet3);
        WriteSheet writeSheet4 = EasyExcel.writerSheet("下周计划安排").build();
        excelWriter.fill(nextList, fillConfig, writeSheet4);
        WriteSheet writeSheet5 = EasyExcel.writerSheet("三级及以上作业风险计划").build();
        excelWriter.fill(workList, fillConfig, writeSheet5);
        excelWriter.finish();

    }
}
