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.StringUtils;
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.EElevatorInfoBo;
import org.dromara.elevator.domain.vo.EElevatorInfoImportVo;
import org.dromara.elevator.domain.vo.EElevatorInfoVo;
import org.dromara.elevator.service.IEElevatorInfoService;

import java.util.List;

/**
 * 导入电梯基础信息
 *
 * @author zy
 */
@Slf4j
public class ElevatorInfoImportListener extends AnalysisEventListener<EElevatorInfoImportVo> implements ExcelListener<EElevatorInfoImportVo> {

    private final IEElevatorInfoService elevatorInfoService;

    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 ElevatorInfoImportListener(Boolean isUpdateSupport) {
        this.elevatorInfoService = SpringUtils.getBean(IEElevatorInfoService.class);
        this.isUpdateSupport = isUpdateSupport;
    }

    @Override
    public void invoke(EElevatorInfoImportVo elevatorVo, AnalysisContext context) {

        EElevatorInfoVo elevatorInfo = this.elevatorInfoService.selectByElevatorNum(elevatorVo.getElevatorNum());
        try {
            // 验证是否存在这个用户
            if (ObjectUtil.isNull(elevatorInfo)) {
                EElevatorInfoBo elevator = BeanUtil.toBean(elevatorVo, EElevatorInfoBo.class);
                ValidatorUtils.validate(elevator);
                if (StringUtils.isBlank(elevator.getMaintenanceId())) {
                    elevator.setMaintenanceId("0");
                }
                if (StringUtils.isBlank(elevator.getTenementId())) {
                    elevator.setTenementId("0");
                }
                elevatorInfoService.insertByBo(elevator);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、电梯 ").append(elevator.getElevatorName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                Long elevatorId = elevatorInfo.getId();
                EElevatorInfoBo elevatorInfoBo = BeanUtil.toBean(elevatorVo, EElevatorInfoBo.class);
                elevatorInfoBo.setId(elevatorId);
                ValidatorUtils.validate(elevatorInfoBo);
                if (StringUtils.isBlank(elevatorInfoBo.getMaintenanceId())) {
                    elevatorInfoBo.setMaintenanceId("0");
                }
                if (StringUtils.isBlank(elevatorInfoBo.getTenementId())) {
                    elevatorInfoBo.setTenementId("0");
                }
                elevatorInfoService.updateByBo(elevatorInfoBo);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、电梯 ").append(elevatorVo.getElevatorName()).append(" 更新成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、电梯 ").append(elevatorVo.getElevatorName()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、电梯 " + elevatorVo.getElevatorName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

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