package com.dsk.cscec.listener;

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.CbSummaryCostAccountImportVo;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * 成本汇总-成本科目汇总自定义导入
 *
 * @author cyf
 */
@Slf4j
public class CbSummaryCostAccountImportListener extends AnalysisEventListener<CbSummaryCostAccountImportVo> implements ExcelListener<CbSummaryCostAccountImportVo> {

    private final Long projectId;
    private final Long fileId;

    private ArrayList<CbSummaryCostAccountImportVo> resultList = new ArrayList<>();
    private int sort = 0;
    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public CbSummaryCostAccountImportListener(Long projectId, Long fileId) {

        this.projectId = projectId;
        this.fileId = fileId;
    }

    @Override
    public void invoke(CbSummaryCostAccountImportVo importVo, AnalysisContext context) {


        importVo.setProjectId(projectId);
        importVo.setCbProjectFileId(fileId);
        importVo.setSort(++sort);
        //层级处理
        if (StringUtil.isNotBlank(importVo.getNumber())) {
            //序号包含n个 - 为n+1级
            int count = StringUtil.count(importVo.getNumber(), "-");
            importVo.setLevel(count + 1);
        } else {
            failureNum++;
            failureMsg.append("<br/>").append("第" + sort + "条数据序号为空");
            return;
        }
        //成本科目不能为空
        if (StringUtil.isBlank(importVo.getCbName())) {
            failureNum++;
            failureMsg.append("<br/>").append("第" + sort + "条数据成本科目为空");
            return;
        }

        //父级数据处理
        if (sort == 1) {
            importVo.setLevel(0);
        }
        resultList.add(importVo);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<CbSummaryCostAccountImportVo> getExcelResult() {
        return new ExcelResult<CbSummaryCostAccountImportVo>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "成本科目汇总数据已全部解析成功！共 " + getList().size() + " 条");
                }
                return successMsg.toString();
            }

            @Override
            public List<CbSummaryCostAccountImportVo> getList() {
                return resultList;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
