package org.dromara.ygpassport.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.ygpassport.domain.bo.StudentLeaderExperienceBo;
import org.dromara.ygpassport.domain.vo.StudentLeaderExperienceVo;
import org.dromara.ygpassport.service.IStudentLeaderExperienceService;

import java.util.List;

/**
 描述：单表导入监听
 使用：目前只支持单表导入，可参考，项目中相关接口有使用该监听器
 **/
@Slf4j
public class StudentLeaderExperienceImportListener extends AnalysisEventListener<StudentLeaderExperienceVo> implements ExcelListener<StudentLeaderExperienceVo> {

    private final IStudentLeaderExperienceService studentLeaderExperienceService;

    private final Boolean isUpdateSupport;

    private final Long operUserId;

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

    public StudentLeaderExperienceImportListener(Boolean isUpdateSupport) {
        this.studentLeaderExperienceService = SpringUtils.getBean(IStudentLeaderExperienceService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }


    @Override
    public void invoke(StudentLeaderExperienceVo studentLeaderExperienceVo, AnalysisContext analysisContext) {
        StudentLeaderExperienceVo studentLeaderExperienceList = this.studentLeaderExperienceService.queryById(studentLeaderExperienceVo.getId());

        try {
            // 验证是否存在这条数据
            if (ObjectUtil.isNull(studentLeaderExperienceList)) {
                StudentLeaderExperienceBo studentLeaderExperienceInfo = BeanUtil.toBean(studentLeaderExperienceVo, StudentLeaderExperienceBo.class);
                ValidatorUtils.validate(studentLeaderExperienceInfo);
                studentLeaderExperienceInfo.setCreateBy(operUserId);
                studentLeaderExperienceService.insertByBo(studentLeaderExperienceInfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、学生干部经历 " + studentLeaderExperienceVo.getLeaderExperience() + " 导入成功");
            } else if (isUpdateSupport) {
                Long stuId = studentLeaderExperienceList.getId();
                StudentLeaderExperienceBo studentLeaderExperienceInfo = BeanUtil.toBean(studentLeaderExperienceList, StudentLeaderExperienceBo.class);
                studentLeaderExperienceInfo.setId(stuId);
                ValidatorUtils.validate(studentLeaderExperienceInfo);
                studentLeaderExperienceService.updateByBo(studentLeaderExperienceInfo);
                successNum++;
                successMsg.append("<br/>" + successNum + "、学生干部经历 " + studentLeaderExperienceList.getLeaderExperience() + " 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、成绩信息 ").append(studentLeaderExperienceList.getId()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、学生干部经历 " + studentLeaderExperienceList.getId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public ExcelResult<StudentLeaderExperienceVo> 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<StudentLeaderExperienceVo> getList() {
                return null;
            }

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