package com.dsk.cscec.listener;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.dsk.common.excel.ExcelListener;
import com.dsk.common.excel.ExcelResult;
import com.dsk.common.exception.ServiceException;
import com.dsk.cscec.domain.vo.CbProjectOtherImportVo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 其他项目自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class CbProjectOtherImportListener extends AnalysisEventListener<CbProjectOtherImportVo> implements ExcelListener<CbProjectOtherImportVo> {

    private final Long projectId;
    private final Long fileId;
    private List<CbProjectOtherImportVo> dataList = new ArrayList<>();
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public CbProjectOtherImportListener(Long projectId, Long fileId) {
        this.fileId = fileId;
        this.projectId = projectId;
    }

    @Override
    public void invoke(CbProjectOtherImportVo importVo, AnalysisContext context) {
        Integer rowIndex = context.readRowHolder().getRowIndex();
//        if (StrUtil.isEmpty(importVo.getItemContent())) {
//            failureNum++;
//            failureMsg.append("<br/>" + rowIndex + "行 解析失败：清单内容不能为空");
//        }
        String number = importVo.getNumber();
//        if (StrUtil.isEmpty(number)) {
//            failureNum++;
//            failureMsg.append("<br/>" + rowIndex + "行 解析失败：序号不能为空");
//        }

        importVo.setCbProjectFileId(fileId);
        Boolean numberMatch = false;
        String regex = "[\\(（]*[一|二|三|四|五|六|七|八|九|十]+[\\)）]*";
        Pattern pattern = Pattern.compile("^\\d+(\\.\\d+)*$");
        Pattern pattern3 = Pattern.compile("^([1-9][0-9]*|0)$");

        if (ReUtil.isMatch(regex, number) || ReUtil.isMatch(pattern, number)||ReUtil.isMatch(pattern3, number)) {
            numberMatch = true;
        }
//        else {
//            failureNum++;
//            failureMsg.append("<br/>" + rowIndex + "行 解析失败：序号格式错误");
//        }
        if(numberMatch){
            if (ReUtil.isMatch(regex, number)){
                String replace = number.replaceAll("[\\\\(（]|[\\\\)）]", "");
                int no = Convert.chineseToNumber(replace);
                importVo.setDealWithNumber(String.valueOf(no));
            }else{
                importVo.setDealWithNumber(importVo.getNumber());
            }
        }
        dataList.add(importVo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }

    @Override
    public ExcelResult<CbProjectOtherImportVo> getExcelResult() {
        return new ExcelResult<CbProjectOtherImportVo>() {
            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                }
                return successMsg.toString();
            }
            @Override
            public List<CbProjectOtherImportVo> getList() {
                return dataList;
            }
            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }

}
