package com.css.txw.gxzx.service.lsjr.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.cache.vo.DmbDTO;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.txw.gxzx.pojo.dto.GxzxLsjrcpImportDto;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class LsjrcpImportListener extends AnalysisEventListener<GxzxLsjrcpImportDto> {

    private List<GxzxLsjrcpImportDto> successList = new ArrayList<>();
    private List<GxzxLsjrcpImportDto> failedList = new ArrayList<>();

    @Override
    public void invoke(GxzxLsjrcpImportDto importData, AnalysisContext analysisContext) {
        log.debug("读取到一条数据：" + importData);
        StringBuilder errorMsg = new StringBuilder();
        importData.setCpuuid(IdUtil.fastSimpleUUID());
        try {
            // 这里可以添加更多的验证逻辑
            String sshy = importData.getSshy();
            if (GyUtils.isNull(sshy)) {
                errorMsg.append("【所属行业】不能为空;");
            } else if (!GyUtils.isNull(sshy) && (!"银行".equals(sshy) && !"保险".equals(sshy))) {
                errorMsg.append("【所属行业】必须为银行或保险;");
            } else {
                importData.setSshyDm("银行".equals(sshy) ? "xd" : "bx");
            }
            String ywlxmc = importData.getYwlxmc();
            if (GyUtils.isNull(ywlxmc)) {
                errorMsg.append("【产品业务类型】不能为空;");
            } else if (!GyUtils.isNull(sshy) && ("银行".equals(sshy) || "保险".equals(sshy))) {
                String tableName = "银行".equals(sshy) ? "LSJR_XDYWLX" : "LSJR_BXYWLX";
                List<DmbDTO> ywlxList = CacheUtils.getDmbData(tableName);
                Map<String, String> ywlxMap = ywlxList.stream().collect(Collectors.toMap(DmbDTO::getCaption, DmbDTO::getCode));
                String ywlxDm = ywlxMap.get(ywlxmc);
                if (GyUtils.isNull(ywlxDm)) {
                    errorMsg.append("【产品业务类型】未定义;");
                } else {
                    importData.setYwlxDm(ywlxDm);
                }
            }
            String cpmc = importData.getCpmc();
            if (GyUtils.isNull(cpmc)) {
                errorMsg.append("【产品名称】不能为空;");
            } else if (!GyUtils.isNull(cpmc) && !isValidInput(cpmc, 60)) {
                errorMsg.append("【产品名称】限制 60 个字符;");
            }
            if("银行".equals(sshy)){
                String edfw = importData.getEdfw();
                if (GyUtils.isNull(edfw)) {
                    errorMsg.append("【额度范围】不能为空;");
                } else if (!GyUtils.isNull(edfw) && !isValidInput(edfw, 60)) {
                    errorMsg.append("【额度范围】限制 60 个字符;");
                }
                String llfw = importData.getLlfw();
                if (GyUtils.isNull(llfw)) {
                    errorMsg.append("【利率范围】不能为空;");
                } else if (!GyUtils.isNull(llfw) && !isValidInput(llfw, 60)) {
                    errorMsg.append("【利率范围】限制 60 个字符;");
                }
                String dkqxfw = importData.getDkqxfw();
                if (GyUtils.isNull(dkqxfw)) {
                    errorMsg.append("【贷款期限范围】不能为空;");
                } else if (!GyUtils.isNull(dkqxfw) && !isValidInput(dkqxfw, 60)) {
                    errorMsg.append("【贷款期限范围】限制 60 个字符;");
                }
            }
            if("保险".equals(sshy)){
                String bzqxfw = importData.getBzqxfw();
                if (GyUtils.isNull(bzqxfw)) {
                    errorMsg.append("【保障期限范围】不能为空;");
                } else if (!GyUtils.isNull(bzqxfw) && !isValidInput(bzqxfw, 60)) {
                    errorMsg.append("【保障期限范围】限制 60 个字符;");
                }
                String bfjsfs = importData.getBfjsfs();
                if (GyUtils.isNull(bfjsfs)) {
                    errorMsg.append("【保费计算方式】不能为空;");
                } else if (!GyUtils.isNull(bfjsfs) && !isValidInput(bfjsfs, 60)) {
                    errorMsg.append("【保费计算方式】限制 60 个字符;");
                }
            }

            String carbonTypeMc = importData.getCarbonTypeMc();
            if (GyUtils.isNull(carbonTypeMc)) {
                errorMsg.append("【碳类型】不能为空;");
            } else {
                List<DmbDTO> ywlxList = CacheUtils.getDmbData("CARBON_TYPE");
                Map<String, String> tlxMap = ywlxList.stream().collect(Collectors.toMap(DmbDTO::getCaption, DmbDTO::getCode));
                String tlxDm = tlxMap.get(carbonTypeMc);
                if (GyUtils.isNull(tlxDm)) {
                    errorMsg.append("【碳类型】未定义;");
                } else {
                    importData.setCarbonType(Integer.valueOf(tlxDm));
                }
            }
            String qymc = importData.getQymc();
            if (GyUtils.isNull(qymc)) {
                errorMsg.append("【机构名称】不能为空;");
            } else if (!GyUtils.isNull(sshy) && ("银行".equals(sshy) || "保险".equals(sshy))) {
                String tableName = "银行".equals(sshy) ? "LSJRYH" : "LSJRBXJG";
                List<DmbDTO> jgList = CacheUtils.getDmbData(tableName);
                Map<String, String> jgMap = jgList.stream().collect(Collectors.toMap(DmbDTO::getCaption, DmbDTO::getCode));
                String jgDm = jgMap.get(qymc);
                if (GyUtils.isNull(jgDm)) {
                    errorMsg.append("【机构名称】未定义;");
                } else {
                    importData.setQyuuid(jgDm);
                }
            }
            String fwcpms = importData.getFwcpms();
            if (GyUtils.isNull(fwcpms)) {
                errorMsg.append("【产品描述】不能为空;");
            } else if (!GyUtils.isNull(fwcpms) && !isValidInput(fwcpms, 1000)) {
                errorMsg.append("【产品描述】限制 1000 个字符;");
            }
        } catch (Exception e) {
            errorMsg.append(e.getMessage());
        }
        if (errorMsg.length() > 0) {
            importData.setRowNum(analysisContext.readRowHolder().getRowIndex());
            importData.setErrorMsg(errorMsg.toString());
            failedList.add(importData);
        } else {
            successList.add(importData);
        }
    }

    private boolean isValidInput(String input, int num) throws UnsupportedEncodingException {
        if (input == null) {
            return true;
        }
        byte[] bytes = input.getBytes("GBK");
        int charCount = bytes.length;
        return charCount <= num;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 进行代码转换
        log.debug("所有数据解析完成！");
    }

    public List<GxzxLsjrcpImportDto> getSuccessList() {
        return successList;
    }

    public List<GxzxLsjrcpImportDto> getFailedList() {
        return failedList;
    }
}
