package com.ruoyi.project.patrol.patrol.controller;

import com.ruoyi.common.utils.file.ExcelUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.patrol.patrol.domain.TPatrolMain;
import com.ruoyi.project.patrol.patrol.service.ITPatrolMainService;
import com.ruoyi.project.system.service.ISysDictDataService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

/**
 * 消气防设施和器材检查计划总Controller
 *
 * @author ruoyi
 * @date 2023-08-22
 */
@RestController
@RequestMapping("/patrol/main")
public class TPatrolMainController extends BaseController {
    @Autowired
    private ITPatrolMainService tPatrolMainService;
    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询消气防设施和器材检查计划总列表
     */
    @PreAuthorize("@ss.hasPermi('patrol:main:list')")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.OTHER)
    @GetMapping("/list")
    public TableDataInfo list(TPatrolMain tPatrolMain) {
        startPage();
        List<TPatrolMain> list = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
        return getDataTable(list);
    }

    @GetMapping("/listAll")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.OTHER)
    public AjaxResult listAll(TPatrolMain tPatrolMain) {
        return AjaxResult.success(tPatrolMainService.selectTPatrolMainList(tPatrolMain));
    }

    /**
     * 导出消气防设施和器材检查计划总列表
     */
    
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(TPatrolMain tPatrolMain) {
        try {
            List<TPatrolMain> list = tPatrolMainService.selectTPatrolMainList(tPatrolMain);
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("检查策略");
            // 设置列宽
            sheet.setColumnWidth(0, 1500);
            sheet.setColumnWidth(1, 6000);
            sheet.setColumnWidth(2, 3500);
            sheet.setColumnWidth(3, 3500);
            sheet.setColumnWidth(4, 1000);
            sheet.setColumnWidth(5, 1000);
            sheet.setColumnWidth(6, 1000);
            sheet.setColumnWidth(7, 1000);
            sheet.setColumnWidth(8, 1000);
            sheet.setColumnWidth(9, 1000);
            sheet.setColumnWidth(10, 1000);
            sheet.setColumnWidth(11, 1000);
            sheet.setColumnWidth(12, 1000);
            sheet.setColumnWidth(13, 1000);
            sheet.setColumnWidth(14, 1000);
            sheet.setColumnWidth(15, 1000);
            sheet.setColumnWidth(16, 1000);
            sheet.setColumnWidth(17, 1000);
            sheet.setColumnWidth(18, 1000);
            sheet.setColumnWidth(19, 1000);
            sheet.setColumnWidth(20, 1000);
            sheet.setColumnWidth(21, 1000);
            sheet.setColumnWidth(22, 1000);
            sheet.setColumnWidth(23, 1000);
            sheet.setColumnWidth(24, 1000);
            sheet.setColumnWidth(25, 1000);
            sheet.setColumnWidth(26, 1000);
            sheet.setColumnWidth(27, 1000);
            sheet.setColumnWidth(28, 5000);
            XSSFRow row0 = sheet.createRow(0);
            row0.createCell(0).setCellValue("蒸汽裂解装置消气防设施和器材检查计划总表");
            ExcelUtils.createVoidCell(1, 28, row0);
            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 28));//合并
            XSSFRow row1 = sheet.createRow(1);
            row1.createCell(0).setCellValue("序号");
            row1.createCell(1).setCellValue("被检查设施名称");
            row1.createCell(2).setCellValue("设施数量");
            row1.createCell(3).setCellValue("检查周期");
            row1.createCell(4).setCellValue("年度：" + tPatrolMain.getYear());
            row1.createCell(28).setCellValue("检查责任人");
            ExcelUtils.createVoidCell(5, 27, row1);
            sheet.addMergedRegion(new CellRangeAddress(1,2, 0, 0));//合并
            sheet.addMergedRegion(new CellRangeAddress(1,2, 1, 1));//合并
            sheet.addMergedRegion(new CellRangeAddress(1,2, 2, 2));//合并
            sheet.addMergedRegion(new CellRangeAddress(1,2, 3, 3));//合并
            sheet.addMergedRegion(new CellRangeAddress(1,1, 4, 27));//合并
            sheet.addMergedRegion(new CellRangeAddress(1,2, 28, 28));//合并

            XSSFRow row2 = sheet.createRow(2);
            ExcelUtils.createVoidCell(0, 28, row2);
            row2.createCell(4).setCellValue("一月");
            row2.createCell(6).setCellValue("二月");
            row2.createCell(8).setCellValue("三月");
            row2.createCell(10).setCellValue("四月");
            row2.createCell(12).setCellValue("五月");
            row2.createCell(14).setCellValue("六月");
            row2.createCell(16).setCellValue("七月");
            row2.createCell(18).setCellValue("八月");
            row2.createCell(20).setCellValue("九月");
            row2.createCell(22).setCellValue("十月");
            row2.createCell(24).setCellValue("十一月");
            row2.createCell(26).setCellValue("十二月");
            sheet.addMergedRegion(new CellRangeAddress(2,2, 4, 5));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 6, 7));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 8, 9));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 10, 11));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 12, 13));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 14, 15));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 16, 17));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 18, 19));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 20, 21));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 22, 23));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 24, 25));//合并
            sheet.addMergedRegion(new CellRangeAddress(2,2, 26, 27));//合并
            for (int i = 0; i < list.size(); i++) {
                int rowNum = 3 + i;
                TPatrolMain main = list.get(i);
                XSSFRow row = sheet.createRow(rowNum);
                ExcelUtils.createVoidCell(0, 28, row);
                row.createCell(0).setCellValue((i+1)+"");
                row.createCell(1).setCellValue(main.getDevName());
                row.createCell(2).setCellValue(main.getDevNum());
                row.createCell(3).setCellValue(sysDictDataService.selectDictLabel("inspection_cycle", main.getCheckCycle()));
                row.createCell(4).setCellValue(getResult(main.getJan().charAt(0)));
                row.createCell(6).setCellValue(getResult(main.getFeb().charAt(0)));
                row.createCell(8).setCellValue(getResult(main.getMar().charAt(0)));
                row.createCell(10).setCellValue(getResult(main.getApr().charAt(0)));
                row.createCell(12).setCellValue(getResult(main.getMay().charAt(0)));
                row.createCell(14).setCellValue(getResult(main.getJun().charAt(0)));
                row.createCell(16).setCellValue(getResult(main.getJul().charAt(0)));
                row.createCell(18).setCellValue(getResult(main.getAug().charAt(0)));
                row.createCell(20).setCellValue(getResult(main.getSep().charAt(0)));
                row.createCell(22).setCellValue(getResult(main.getOct().charAt(0)));
                row.createCell(24).setCellValue(getResult(main.getNov().charAt(0)));
                row.createCell(26).setCellValue(getResult(main.getDec().charAt(0)));
                row.createCell(28).setCellValue(main.getPersonLiable());
                if (main.getPatrolType().equals("1")) {
                    row.createCell(5).setCellValue(getResult(main.getJan().charAt(2)));
                    row.createCell(7).setCellValue(getResult(main.getFeb().charAt(2)));
                    row.createCell(9).setCellValue(getResult(main.getMar().charAt(2)));
                    row.createCell(11).setCellValue(getResult(main.getApr().charAt(2)));
                    row.createCell(13).setCellValue(getResult(main.getMay().charAt(2)));
                    row.createCell(15).setCellValue(getResult(main.getJun().charAt(2)));
                    row.createCell(17).setCellValue(getResult(main.getJul().charAt(2)));
                    row.createCell(19).setCellValue(getResult(main.getAug().charAt(2)));
                    row.createCell(21).setCellValue(getResult(main.getSep().charAt(2)));
                    row.createCell(23).setCellValue(getResult(main.getOct().charAt(2)));
                    row.createCell(25).setCellValue(getResult(main.getNov().charAt(2)));
                    row.createCell(27).setCellValue(getResult(main.getDec().charAt(2)));
                } else {
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 4, 5));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 6, 7));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 8, 9));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 10, 11));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 12, 13));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 14, 15));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 16, 17));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 18, 19));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 20, 21));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 22, 23));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 24, 25));//合并
                    sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 26, 27));//合并
                }
            }

            // 设置单元格样式
            // 文字居中，自动换行，四周边框
            CellStyle style = workbook.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            style.setWrapText(true);
            style.setBorderTop(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            for (Row row : sheet) {
                if (row.getRowNum() == 0 || row.getRowNum() == 1|| row.getRowNum() == 2)
                    row.setHeightInPoints(30);
                else
                    row.setHeightInPoints(50);
                for (Cell cell : row) {
                    if (cell.getStringCellValue().equals("√")) {
                        CellStyle style2 = workbook.createCellStyle();
                        style2.setAlignment(HorizontalAlignment.CENTER);
                        style2.setVerticalAlignment(VerticalAlignment.CENTER);
                        style2.setWrapText(true);
                        style2.setBorderTop(BorderStyle.THIN);
                        style2.setBorderBottom(BorderStyle.THIN);
                        style2.setBorderLeft(BorderStyle.THIN);
                        style2.setBorderRight(BorderStyle.THIN);
                        XSSFFont font = workbook.createFont();
                        font.setColor(IndexedColors.LIGHT_BLUE.getIndex());
                        font.setUnderline(XSSFFont.U_SINGLE);
                        style2.setFont(font);
                        cell.setCellStyle(style2);
                    } else
                        cell.setCellStyle(style);
                }
            }
            int rowNum = list.size() + 3;
            XSSFRow row3 = sheet.createRow(rowNum);
            ExcelUtils.createVoidCell(0, 28, row3);
            row3.createCell(0).setCellValue("备注：");
            sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 28));//合并
            rowNum++;
            XSSFRow row4 = sheet.createRow(rowNum);
            ExcelUtils.createVoidCell(0, 28, row4);
            row4.createCell(0).setCellValue("\"√\" 表示已完成");
            sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 28));//合并
            rowNum++;
            XSSFRow row5 = sheet.createRow(rowNum);
            ExcelUtils.createVoidCell(0, 28, row5);
            row5.createCell(0).setCellValue("\"X\" 表示未完成");
            sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum, 0, 28));//合并
            //返回
            String fileName = ExcelUtil.encodingFilename("main");
            OutputStream os = Files.newOutputStream(Paths.get(ExcelUtil.getAbsoluteFile(fileName)));
            workbook.write(os);workbook.close();
            return AjaxResult.success(fileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private String getResult(char val) {
        if (val == '1') return "√";
        if (val == '0') return "X";
        if (val == '-') return "-";
        return "";
    }


    /**
     * 获取消气防设施和器材检查计划总详细信息
     */
    @PreAuthorize("@ss.hasPermi('patrol:main:query')")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.OTHER)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tPatrolMainService.selectTPatrolMainById(id));
    }

    /**
     * 新增消气防设施和器材检查计划总
     */
    @PreAuthorize("@ss.hasPermi('patrol:main:add')")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TPatrolMain tPatrolMain) {
        return toAjax(tPatrolMainService.insertTPatrolMain(tPatrolMain));
    }

    /**
     * 修改消气防设施和器材检查计划总
     */
    @PreAuthorize("@ss.hasPermi('patrol:main:edit')")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TPatrolMain tPatrolMain) {
        return toAjax(tPatrolMainService.updateTPatrolMain(tPatrolMain));
    }

    /**
     * 删除消气防设施和器材检查计划总
     */
    @PreAuthorize("@ss.hasPermi('patrol:main:remove')")
    @Log(title = "消气防设施和器材检查计划总", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tPatrolMainService.deleteTPatrolMainByIds(ids));
    }
}
