package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.ValidatorUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.SysUserBo;
import org.dromara.system.domain.score.bo.ScoreDataBo;
import org.dromara.system.domain.score.vo.ScoreDataVo;
import org.dromara.system.domain.vo.SysUserImportVo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysConfigService;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.score.IScoreDataService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 学生成绩自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class ScoreDataListener extends AnalysisEventListener<ScoreDataVo> implements ExcelListener<ScoreDataVo> {

    private IScoreDataService scoreDataService;


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

    /**
     * 是否更新已存在的数据
     * @param isUpdateSupport
     */
    public ScoreDataListener(Boolean isUpdateSupport) {

    }

    @Override
    public void invoke(ScoreDataVo scoreDataVo, AnalysisContext context) {
        ScoreDataBo scoreData = BeanUtil.toBean(scoreDataVo, ScoreDataBo.class);
        scoreDataService.insertByBo(scoreData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

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

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