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.StudentListBo;
import org.dromara.ygpassport.domain.vo.StudentListVo;
import org.dromara.ygpassport.service.IStudentListService;

import java.util.List;
/**
 描述：单表导入监听
 使用：目前只支持单表导入，可参考，项目中相关接口无使用该监听器
 **/
@Slf4j
public class StudentListImportListener extends AnalysisEventListener<StudentListVo> implements ExcelListener<StudentListVo> {

    private final IStudentListService studentListService;

    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 StudentListImportListener(Boolean isUpdateSupport) {
        this.studentListService = SpringUtils.getBean(IStudentListService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operUserId = LoginHelper.getUserId();
    }


    @Override
    public void invoke(StudentListVo studentListVo, AnalysisContext analysisContext) {
        StudentListVo studentList = this.studentListService.queryByStuNo(studentListVo.getStuNo());
        try {
            // 验证是否存在这个用户
            if (ObjectUtil.isNull(studentList)) {
                StudentListBo studentInfo = BeanUtil.toBean(studentListVo, StudentListBo.class);
                ValidatorUtils.validate(studentInfo);
                studentInfo.setCreateBy(operUserId);
                studentListService.insertByBo(studentInfo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、学生信息 ").append(studentInfo.getStuName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long stuId = studentList.getId();
                StudentListBo studentInfo = BeanUtil.toBean(studentListVo, StudentListBo.class);
                studentInfo.setId(stuId);
                ValidatorUtils.validate(studentInfo);
                studentInfo.setUpdateBy(operUserId);
                studentListService.updateByBo(studentInfo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、学生信息 ").append(studentInfo.getStuName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、学生信息 ").append(studentList.getStuName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、学生信息 " + studentList.getStuName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

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

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