package com.zcloud.entity;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.zcloud.util.Const;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class HiddenExcel {

    private final List<String> title = new ArrayList<>();
    // 导出的字段的总和
    private final List<String> title_formwork = new ArrayList<>();
    private List<String> nuTitle = new ArrayList<>();
    private final List<List<String>> head = new ArrayList<>();
    private final List<List<Object>> hand = new ArrayList<>();
    private Map<String, PageData> foot = new HashMap<>();
    private Integer hiddenImgNumber = 0;
    private Integer afterImgNumber = 0;
    private Integer schemeImgNumber = 0;
    private Integer checkNumber = 0;
    private Integer checkImgNumber = 0;
    private String havePicture = "0";

    private final Map<String, String> sourceMap = new HashMap<>();
    private final Map<String, String> stateMap = new HashMap<>();
    private final Map<String, String> rectifyType = new HashMap<>();
    private final Map<String, String> haveSchemeMap = new HashMap<>();
    private final Map<String, String> ysStatusMap = new HashMap<>();
    private Map<String, String> leveMap = new HashMap<>();
    private Map<String, String> depMap = new HashMap<>();
    private Map<String, String> userMap = new HashMap<>();
    private Map<String, List<PageData>> imgMap = new HashMap<>();
    private Map<String, List<PageData>> checkMap = new HashMap<>();


    public HiddenExcel(Integer hiddenImgNumber, Integer afterImgNumber, Integer schemeImgNumber, Integer checkNumber, Integer checkImgNumber) {
        this.hiddenImgNumber = hiddenImgNumber;
        this.afterImgNumber = afterImgNumber;
        this.schemeImgNumber = schemeImgNumber;
        this.checkNumber = checkNumber;
        this.checkImgNumber = checkImgNumber;
        // 所有字段
        this.title_formwork.add("隐患来源");
        this.title_formwork.add("风险点（单元）");
        this.title_formwork.add("辨识部位");
        this.title_formwork.add("存在风险");
        this.title_formwork.add("风险分级");
        this.title_formwork.add("检查内容");
        this.title_formwork.add("隐患级别");
        this.title_formwork.add("隐患状态");
        this.title_formwork.add("隐患描述");
        this.title_formwork.add("隐患部位");
        this.title_formwork.add("隐患发现部门");
        this.title_formwork.add("发现人");
        this.title_formwork.add("发现时间");
        this.title_formwork.add("整改类型");
        this.title_formwork.add("整改期限");
        this.title_formwork.add("隐患图片");
        this.title_formwork.add("整改描述");
        this.title_formwork.add("整改部门");
        this.title_formwork.add("整改人");
        this.title_formwork.add("整改时间");
        this.title_formwork.add("整改后图片");
        this.title_formwork.add("整改方案");
        this.title_formwork.add("排查日期");
        this.title_formwork.add("隐患清单");
        this.title_formwork.add("治理标准要求");
        this.title_formwork.add("治理方法");
        this.title_formwork.add("经费和物资的落实");
        this.title_formwork.add("负责治理人员");
        this.title_formwork.add("工时安排");
        this.title_formwork.add("时限要求");
//        this.title_formwork.add("应急预案附件");
        this.title_formwork.add("工作要求");
        this.title_formwork.add("其他事项");
        this.title_formwork.add("方案图片");
        this.title_formwork.add("验收描述");
        this.title_formwork.add("是否合格");
        this.title_formwork.add("验收部门");
        this.title_formwork.add("验收人");
        this.title_formwork.add("验收时间");
        this.title_formwork.add("验收图片");

        // 字典初始化
        this.sourceMap.put("1", "隐患快报");
        this.sourceMap.put("2", "隐患排查");
        this.sourceMap.put("3", "隐患排查");

        this.stateMap.put("-1", "已过期");
        this.stateMap.put("0", "未提交");
        this.stateMap.put("1", "未整改");
        this.stateMap.put("2", "已整改");
        this.stateMap.put("3", "已整改");
        this.stateMap.put("4", "已验收");

        this.rectifyType.put("1", "立即整改");
        this.rectifyType.put("2", "限期整改");

        this.haveSchemeMap.put("0", "无");
        this.haveSchemeMap.put("1", "有");

        this.ysStatusMap.put("0", "否");
        this.ysStatusMap.put("1", "是");
    }

    public void initHead() {
        if (this.nuTitle.contains("隐患来源"))
            this.title.add("隐患来源");
        if (this.nuTitle.contains("风险点（单元）"))
            this.title.add("风险点（单元）");
        if (this.nuTitle.contains("辨识部位"))
            this.title.add("辨识部位");
        if (this.nuTitle.contains("存在风险"))
            this.title.add("存在风险");
        if (this.nuTitle.contains("风险分级"))
            this.title.add("风险分级");
        if (this.nuTitle.contains("检查内容"))
            this.title.add("检查内容");
        if (this.nuTitle.contains("隐患级别"))
            this.title.add("隐患级别");
        if (this.nuTitle.contains("隐患状态"))
            this.title.add("隐患状态");
        if (this.nuTitle.contains("隐患描述"))
            this.title.add("隐患描述");
        if (this.nuTitle.contains("隐患部位"))
            this.title.add("隐患部位");
        if (this.nuTitle.contains("隐患发现部门"))
            this.title.add("隐患发现部门");
        if (this.nuTitle.contains("发现人"))
            this.title.add("发现人");
        if (this.nuTitle.contains("发现时间"))
            this.title.add("发现时间");
        if (this.nuTitle.contains("整改类型"))
            this.title.add("整改类型");
        if (this.nuTitle.contains("整改期限"))
            this.title.add("整改期限");
        if (this.nuTitle.contains("隐患图片"))
            for (int x = 0; x < this.hiddenImgNumber; x++) this.title.add("隐患图片");

        if (this.nuTitle.contains("整改描述"))
            this.title.add("整改描述");
        if (this.nuTitle.contains("整改部门"))
            this.title.add("整改部门");
        if (this.nuTitle.contains("整改人"))
            this.title.add("整改人");
        if (this.nuTitle.contains("整改时间"))
            this.title.add("整改时间");

        if (this.nuTitle.contains("整改后图片"))
            for (int x = 0; x < this.afterImgNumber; x++) this.title.add("整改后图片");

        if (this.nuTitle.contains("整改方案"))
            this.title.add("整改方案");
        if (this.nuTitle.contains("排查日期"))
            this.title.add("排查日期");
        if (this.nuTitle.contains("隐患清单"))
            this.title.add("隐患清单");
        if (this.nuTitle.contains("治理标准要求"))
            this.title.add("治理标准要求");
        if (this.nuTitle.contains("治理方法"))
            this.title.add("治理方法");
        if (this.nuTitle.contains("经费和物资的落实"))
            this.title.add("经费和物资的落实");
        if (this.nuTitle.contains("负责治理人员"))
            this.title.add("负责治理人员");
        if (this.nuTitle.contains("工时安排"))
            this.title.add("工时安排");
        if (this.nuTitle.contains("时限要求"))
            this.title.add("时限要求");
//        if (this.nuTitle.contains("应急预案附件"))
//            this.title.add("应急预案附件");
        if (this.nuTitle.contains("工作要求"))
            this.title.add("工作要求");
        if (this.nuTitle.contains("其他事项"))
            this.title.add("其他事项");

        if (this.nuTitle.contains("方案图片"))
            for (int x = 0; x < this.schemeImgNumber; x++) this.title.add("方案图片");

        for (int x = 0; x < this.checkNumber; x++) {
            if (this.nuTitle.contains("验收描述"))
                this.title.add("验收描述");
            if (this.nuTitle.contains("是否合格"))
                this.title.add("是否合格");
            if (this.nuTitle.contains("验收部门"))
                this.title.add("验收部门");
            if (this.nuTitle.contains("验收人"))
                this.title.add("验收人");
            if (this.nuTitle.contains("验收时间"))
                this.title.add("验收时间");
            if (this.nuTitle.contains("验收图片"))
                for (int y = 0; y < this.checkImgNumber; y++) this.title.add("验收图片");
        }

        for (String x : this.title) {
            List<String> head = new ArrayList<>();
            head.add(x);
            this.head.add(head);
        }
    }

    private List<Object> getBody(PageData body) throws Exception {
        List<Object> list = new ArrayList<>();
        if (this.nuTitle.contains("隐患来源"))
            list.add(this.sourceMap.get(body.getString("SOURCE")));
        if (this.nuTitle.contains("风险点（单元）"))
            list.add(body.getString("RISK_UNIT"));
        if (this.nuTitle.contains("辨识部位"))
            list.add(body.getString("IDENTIFICATION"));
        if (this.nuTitle.contains("存在风险"))
            list.add(body.getString("RISK_DESCR"));
        if (this.nuTitle.contains("风险分级"))
            list.add(body.getString("LEVEL"));
        if (this.nuTitle.contains("检查内容"))
            list.add(body.getString("CHECK_CONTENT"));
        if (this.nuTitle.contains("隐患级别"))
            list.add(leveMap.get(body.getString("HIDDENLEVEL")));
        if (this.nuTitle.contains("隐患状态"))
            list.add(this.stateMap.get(body.getString("STATE")));
        if (this.nuTitle.contains("隐患描述"))
            list.add(body.getString("HIDDENDESCR"));
        if (this.nuTitle.contains("隐患部位"))
            list.add(body.getString("HIDDENPART"));
        if (this.nuTitle.contains("隐患发现部门"))
            list.add(depMap.get(body.getString("HIDDENFINDDEPT")));
        if (this.nuTitle.contains("发现人"))
            list.add(this.userMap.get(body.getString("CREATOR")));
        if (this.nuTitle.contains("发现时间"))
            list.add(body.getString("CREATTIME"));
        if (this.nuTitle.contains("整改类型"))
            list.add(this.rectifyType.get(body.getString("RECTIFICATIONTYPE")));
        if (this.nuTitle.contains("整改期限"))
            list.add(body.getString("RECTIFICATIONDEADLINE"));
        List<String> imgs;
        if (this.nuTitle.contains("隐患图片")) {
            if (imgMap.get(body.getString("HIDDEN_ID")) != null && imgMap.get(body.getString("HIDDEN_ID")).size() > 0) {
                imgs = imgMap.get(body.getString("HIDDEN_ID")).stream().filter(n -> "3".equals(n.get("TYPE").toString())).map(n -> n.getString("FILEPATH")).collect(Collectors.toList());
                for (String x : imgs) {
                    if ("1".equals(this.havePicture)){
                        list.add(new URL(Const.HTTPFILEURL + x));
                    }else {
                        list.add(Const.HTTPFILEURL + x);
                    }
                }
                if (imgs.size() <= this.hiddenImgNumber) {
                    for (int i = 0; i < this.hiddenImgNumber - imgs.size(); i++) {
                        list.add("");
                    }
                }
            } else {
                for (int i = 0; i < this.hiddenImgNumber; i++) {
                    list.add("");
                }
            }
        }
        if (this.nuTitle.contains("整改描述"))
            list.add(body.getString("RECTIFYDESCR"));// 整改描述
        if (this.nuTitle.contains("整改部门"))
            list.add(depMap.get(body.getString("RECTIFICATIONDEPT")));// 整改部门
        if (this.nuTitle.contains("整改人"))
            list.add(userMap.get(body.getString("RECTIFICATIONOR")));// 整改人
        if (this.nuTitle.contains("整改时间"))
            list.add(body.getString("RECTIFICATIONTIME"));// 整改时间
        if (this.nuTitle.contains("整改后图片")) {
            if (imgMap.get(body.getString("HIDDEN_ID")) != null && imgMap.get(body.getString("HIDDEN_ID")).size() > 0) {
                imgs = imgMap.get(body.getString("HIDDEN_ID")).stream().filter(n -> "4".equals(n.get("TYPE").toString())).map(n -> n.getString("FILEPATH")).collect(Collectors.toList());
                for (String x : imgs) {
                    if ("1".equals(this.havePicture)){
                        list.add(new URL(Const.HTTPFILEURL + x));
                    }else {
                        list.add(Const.HTTPFILEURL + x);
                    }
                }
                if (imgs.size() <= this.afterImgNumber) {
                    for (int i = 0; i < this.afterImgNumber - imgs.size(); i++) {
                        list.add("");
                    }
                }
            } else {
                for (int i = 0; i < this.afterImgNumber; i++) {
                    list.add("");
                }
            }
        }
        if (this.nuTitle.contains("整改方案")) {
            if ("0-11".contains(body.getString("STATE"))){
                list.add("");
            }else {
                list.add(body.get("HAVESCHEME") != null ? haveSchemeMap.get(body.getString("HAVESCHEME")) : "");// 整改方案
            }
        }

        PageData hiddenInfo = this.foot.get(body.getString("HIDDEN_ID"));
        if (this.nuTitle.contains("排查日期"))
            list.add(hiddenInfo != null && hiddenInfo.get("SCREENINGDATE") != null ? hiddenInfo.getString("SCREENINGDATE") : "");// 排查日期
        if (this.nuTitle.contains("隐患清单"))
            list.add(hiddenInfo != null && hiddenInfo.get("LISTNAME") != null ? hiddenInfo.getString("LISTNAME") : "");// 隐患清单
        if (this.nuTitle.contains("治理标准要求"))
            list.add(hiddenInfo != null && hiddenInfo.get("GOVERNSTANDARDS") != null ? hiddenInfo.getString("GOVERNSTANDARDS") : "");// 治理标准要求
        if (this.nuTitle.contains("治理方法"))
            list.add(hiddenInfo != null && hiddenInfo.get("GOVERNMETHOD") != null ? hiddenInfo.getString("GOVERNMETHOD") : "");// 治理方法
        if (this.nuTitle.contains("经费和物资的落实"))
            list.add(hiddenInfo != null && hiddenInfo.get("EXPENDITURE") != null ? hiddenInfo.getString("EXPENDITURE") : "");// 经费和物资的落实
        if (this.nuTitle.contains("负责治理人员"))
            list.add(hiddenInfo != null && hiddenInfo.get("PRINCIPAL") != null ? hiddenInfo.getString("PRINCIPAL") : "");// 负责治理人员
        if (this.nuTitle.contains("工时安排"))
            list.add(hiddenInfo != null && hiddenInfo.get("PROGRAMMING") != null ? hiddenInfo.getString("PROGRAMMING") : "");// 工时安排
        if (this.nuTitle.contains("时限要求"))
            list.add(hiddenInfo != null && hiddenInfo.get("TIMELIMITFOR") != null ? hiddenInfo.getString("TIMELIMITFOR") : "");// 时限要求
//        if (this.nuTitle.contains("应急预案附件"))
//            list.add(body.get("FILEPATH") != null ? body.getString("FILEPATH") : "");
        if (this.nuTitle.contains("工作要求"))
            list.add(hiddenInfo != null && hiddenInfo.get("TIMELIMITFOR") != null ? hiddenInfo.getString("JOBREQUIREMENT") : "");// 工作要求
        if (this.nuTitle.contains("其他事项"))
            list.add(hiddenInfo != null && hiddenInfo.get("TIMELIMITFOR") != null ? hiddenInfo.getString("OTHERBUSINESS") : "");// 其他事项
        if (this.nuTitle.contains("方案图片")) {
            if (hiddenInfo != null) {
                if (imgMap.get(hiddenInfo.getString("HIDDENSCHEME_ID")) != null) {
                    imgs = imgMap.get(hiddenInfo.getString("HIDDENSCHEME_ID")).stream().filter(n -> "8".equals(n.get("TYPE").toString())).map(n -> n.getString("FILEPATH")).collect(Collectors.toList());
                } else {
                    imgs = new ArrayList<>();
                }
            } else {
                imgs = new ArrayList<>();
            }
            for (String x : imgs) {
                if ("1".equals(this.havePicture)){
                    list.add(new URL(Const.HTTPFILEURL + x));
                }else {
                    list.add(Const.HTTPFILEURL + x);
                }

            }
            if (imgs.size() <= this.schemeImgNumber) {
                for (int i = 0; i < this.schemeImgNumber - imgs.size(); i++) {
                    list.add("");
                }
            }
        }

        if (checkMap.containsKey(body.getString("HIDDEN_ID"))) {
            for (PageData check : checkMap.get(body.getString("HIDDEN_ID"))) {
                // 2023-08-31 齐哥哥要求，状态为3时不展示验收信息
                if ("3".equals(body.getString("STATE"))) continue;
                if (this.nuTitle.contains("验收描述"))
                    list.add(check.getString("CHECKDESCR"));// 验收描述
                if (this.nuTitle.contains("是否合格"))
                    list.add("0".equals(check.getString("STATUS"))?"不合格":"合格");// 是否合格
                if (this.nuTitle.contains("验收部门"))
                    list.add(check.getString("CHECKDEPTNAME"));// 验收部门
                if (this.nuTitle.contains("验收人"))
                    list.add(check.getString("CHECKORNAME"));// 验收人
                if (this.nuTitle.contains("验收时间"))
                    list.add(check.getString("CHECK_TIME"));// 验收时间
                if (this.nuTitle.contains("验收图片")) {
                    if (imgMap.get(check.getString("HIDDENCHECK_ID")) != null) {
                        imgs = imgMap.get(check.getString("HIDDENCHECK_ID")).stream().filter(n -> "5".equals(n.get("TYPE").toString())).map(n -> n.getString("FILEPATH")).collect(Collectors.toList());
                    } else {
                        imgs = new ArrayList<>();
                    }
                    for (String x : imgs) {
                        if ("1".equals(this.havePicture)){
                            list.add(new URL(Const.HTTPFILEURL + x));
                        }else {
                            list.add(Const.HTTPFILEURL + x);
                        }
                    }
                    if (imgs.size() <= this.checkImgNumber) {
                        for (int j = 0; j < this.checkImgNumber - imgs.size(); j++) {
                            list.add("");
                        }
                    }
                }
            }
        }
        return list;
    }

    public void addAllBody(List<PageData> bodies) throws Exception {
        int index = 0;
        for (PageData body : bodies) {
            this.hand.add(this.getBody(body));
        }
    }

    /**
     * description: 隐患级别映射关系
     *
     * @param map（id：name）
     */
    public void setLeveMap(Map<String, String> map) {
        this.leveMap = map;
    }

    /**
     * description: 部门信息
     *
     * @param map（id：name）
     */
    public void setDepMap(Map<String, String> map) {
        this.depMap = map;
    }

    /**
     * description: 部门信息
     *
     * @param map（id：name）
     */
    public void setUserMap(Map<String, String> map) {
        this.userMap = map;
    }

    /**
     * description: 不打印的字段
     *
     * @param nuTitle（String ，字段中文名称）
     */
    public void setNuTitle(List<String> nuTitle) {
        this.nuTitle = nuTitle;
    }

    /**
     * description: 隐患信息
     *
     * @param foot（id：entity）
     */
    public void setFoot(Map<String, PageData> foot) {
        this.foot = foot;
    }

    public List<List<String>> getHead() {
        return head;
    }

    public List<List<Object>> getHand() {
        return this.hand;
    }

    public Map<String, PageData> getFoot() {
        return foot;
    }

    public List<String> getTitle_formwork() {
        return title_formwork;
    }

    public List<String> getTitle() {
        return title;
    }

    public void setImgMap(Map<String, List<PageData>> imgMap) {
        this.imgMap = imgMap;
    }

    public void setCheckMap(Map<String, List<PageData>> checkMap) {
        this.checkMap = checkMap;
    }

    public static WriteCellStyle getHeadStyle(){
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景颜色
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("黑体");//设置字体名字
        headWriteFont.setFontHeightInPoints((short)15);//设置字体大小
        headWriteFont.setBold(true);//字体加粗
        headWriteCellStyle.setWriteFont(headWriteFont); //在样式用应用设置的字体;
        // 样式
        headWriteCellStyle.setBorderBottom(BorderStyle.THIN);//设置底边框;
        headWriteCellStyle.setBottomBorderColor((short) 0);//设置底边框颜色;
        headWriteCellStyle.setBorderLeft(BorderStyle.THIN);  //设置左边框;
        headWriteCellStyle.setLeftBorderColor((short) 0);//设置左边框颜色;
        headWriteCellStyle.setBorderRight(BorderStyle.THIN);//设置右边框;
        headWriteCellStyle.setRightBorderColor((short) 0);//设置右边框颜色;
        headWriteCellStyle.setBorderTop(BorderStyle.THIN);//设置顶边框;
        headWriteCellStyle.setTopBorderColor((short) 0); //设置顶边框颜色;
        headWriteCellStyle.setWrapped(true);  //设置自动换行;
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);//设置水平对齐的样式为居中对齐;
        headWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);  //设置垂直对齐的样式为居中对齐;

        return headWriteCellStyle;
    }

    public void setHavePicture(String flag){
        if (StringUtils.isNotEmpty(flag)){
            this.havePicture = flag;
        }
    }
}




