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.VolunteeringBo;
import org.dromara.ygpassport.domain.vo.VolunteeringVo;
import org.dromara.ygpassport.service.IVolunteeringService;

import java.util.List;

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

    private final IVolunteeringService volunteeringService;

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

    @Override
    public void invoke(VolunteeringVo volunteeringVo, AnalysisContext analysisContext) {
        VolunteeringVo volunteeringList = this.volunteeringService.queryById(volunteeringVo.getId());;
        try {
            if (ObjectUtil.isNull(volunteeringList)) {
                VolunteeringBo volunteeringInfo = BeanUtil.toBean(volunteeringVo, VolunteeringBo.class);
                ValidatorUtils.validate(volunteeringInfo);
                volunteeringInfo.setCreateBy(operUserId);
                volunteeringService.insertByBo(volunteeringInfo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、志愿服务信息 ").append(volunteeringList.getId()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long stuId = volunteeringList.getId();
                VolunteeringBo volunteeringInfo = BeanUtil.toBean(volunteeringVo, VolunteeringBo.class);
                volunteeringInfo.setId(stuId);
                ValidatorUtils.validate(volunteeringInfo);
                volunteeringService.updateByBo(volunteeringInfo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、志愿服务信息 ").append(volunteeringList.getId()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、志愿服务信息 ").append(volunteeringList.getId()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、志愿服务信息 " + volunteeringList.getId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

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

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