package com.dsk.cscec.listener;

import cn.hutool.core.bean.BeanUtil;
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.common.helper.LoginHelper;
import com.dsk.common.utils.spring.SpringUtils;
import com.dsk.cscec.domain.CbProjectExpenseSummary;
import com.dsk.cscec.domain.vo.CbCostMeasuresImportVo;
import com.dsk.cscec.domain.vo.CbProjectExpenseSummaryImportVo;
import com.dsk.cscec.service.CbProjectExpenseSummaryService;
import com.dsk.cscec.service.impl.CbCostMeasureServiceImpl;
import lombok.extern.slf4j.Slf4j;

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

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class ProjectCostMeasureSummaryImportListener extends AnalysisEventListener<CbProjectExpenseSummaryImportVo> implements ExcelListener<CbProjectExpenseSummaryImportVo> {

    private final CbProjectExpenseSummaryService cbProjectExpenseSummaryService;
//
//    private final String password;

    private final Long projectId;
    private final Long fileId;
    private final Integer dataType;

    private List<CbProjectExpenseSummaryImportVo> dataList = new ArrayList<>();
    private List<String> errorList = new ArrayList<>();

//    private final String operName;

    private int successNum = 0;
    private int failureNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public ProjectCostMeasureSummaryImportListener(Long projectId, Long fileId,Integer dataType) {
        this.cbProjectExpenseSummaryService = SpringUtils.getBean(CbProjectExpenseSummaryService.class);
        this.fileId = fileId;
        this.projectId = projectId;
        this.dataType=dataType;
    }

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

        CbProjectExpenseSummary cbProjectExpenseSummary=new CbProjectExpenseSummary();
        BeanUtil.copyProperties(importVo,cbProjectExpenseSummary);
        cbProjectExpenseSummary.setProjectId(projectId);
        cbProjectExpenseSummary.setProjectFileId(fileId);
        cbProjectExpenseSummary.setDataType(dataType);
//        cbProjectExpenseSummary.setCreateBy(LoginHelper.getUsername());
        boolean save = cbProjectExpenseSummaryService.save(cbProjectExpenseSummary);
        if(!save){
            String errmsg = context.readRowHolder().getRowIndex() + " error";
            log.debug("ProjectCostMeasureSummaryImportListener.invoke() "+errmsg);
            errorList.add(errmsg);
        }
        successNum+=1;
        dataList.add(importVo);

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

            @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<CbProjectExpenseSummaryImportVo> getList() {
                return dataList;
            }

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

}
