package org.dromara.elevator.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.elevator.domain.bo.EMaintenanceInfoBo;
import org.dromara.elevator.domain.vo.EMaintenanceInfoImportVo;
import org.dromara.elevator.domain.vo.EMaintenanceInfoVo;
import org.dromara.elevator.service.IEMaintenanceInfoService;

import java.util.List;

/**
 * 导入维保单位基础信息
 *
 * @author zy
 */
@Slf4j
public class EMaintenanceInfoImportListener extends AnalysisEventListener<EMaintenanceInfoImportVo> implements ExcelListener<EMaintenanceInfoImportVo> {

    private final IEMaintenanceInfoService maintenanceInfoService;

    private final Boolean isUpdateSupport;

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

    public EMaintenanceInfoImportListener(Boolean isUpdateSupport) {
        this.maintenanceInfoService = SpringUtils.getBean(IEMaintenanceInfoService.class);
        this.isUpdateSupport = isUpdateSupport;
    }

    @Override
    public void invoke(EMaintenanceInfoImportVo maintenanceVo, AnalysisContext context) {

        EMaintenanceInfoVo maintenanceInfo = this.maintenanceInfoService.selectByCode(maintenanceVo.getCode());
        try {
            // 验证是否存在这个用户
            if (ObjectUtil.isNull(maintenanceInfo)) {
                EMaintenanceInfoBo maintenance = BeanUtil.toBean(maintenanceVo, EMaintenanceInfoBo.class);
                ValidatorUtils.validate(maintenance);
                maintenanceInfoService.insertByBo(maintenance);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、维保单位 ").append(maintenance.getName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long maintenanceId = maintenanceInfo.getId();
                EMaintenanceInfoBo maintenanceBo = BeanUtil.toBean(maintenanceVo, EMaintenanceInfoBo.class);
                maintenanceBo.setId(maintenanceId);
                ValidatorUtils.validate(maintenanceBo);
                maintenanceInfoService.updateByBo(maintenanceBo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、维保单位 ").append(maintenanceVo.getName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、维保单位 ").append(maintenanceVo.getName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、维保单位 " + maintenanceVo.getName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

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