package org.eco.als.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.eco.als.domain.bo.FlightSplitTaskBo;
import org.eco.als.domain.vo.FlightSplitTaskImportVo;
import org.eco.als.domain.vo.FlightSplitTaskVo;
import org.eco.als.service.IFlightSplitTaskService;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.utils.SpringUtils;
import org.eco.common.core.utils.ValidatorUtils;
import org.eco.common.excel.core.ExcelListener;
import org.eco.common.excel.core.ExcelResult;
import org.eco.common.excel.entity.ExcelResultRes;

import java.util.List;

/**
 * 飞行分解任务自定义导入
 *
 * @author wgk
 */
@Slf4j
public class FlightSplitTaskImportListener extends AnalysisEventListener<FlightSplitTaskImportVo> implements ExcelListener<FlightSplitTaskImportVo> {
    private final IFlightSplitTaskService flightSplitTaskService;

    private final Boolean isUpdateSupport;
    private final LoginUser loginUser;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();
    private int successNum = 0;
    private int failureNum = 0;

    public FlightSplitTaskImportListener(Boolean isUpdateSupport, LoginUser loginUser) {
        this.flightSplitTaskService = SpringUtils.getBean(IFlightSplitTaskService.class);
        this.isUpdateSupport = isUpdateSupport;
        this.loginUser = loginUser;
    }

    @Override
    public void invoke(FlightSplitTaskImportVo flightSplitTaskVo, AnalysisContext context) {
        try {

            FlightSplitTaskBo flightSplitTaskBo = BeanUtil.toBean(flightSplitTaskVo, FlightSplitTaskBo.class);

            //TODO:根据某个字段，查询数据库表中是否存在记录，不存在就新增，存在就更新
            FlightSplitTaskVo flightSplitTaskVo1 = null;

            //flightSplitTaskVo1 = flightSplitTaskService.selectBySomefield(flightSplitTaskVo.getSomefield());
            if (ObjectUtil.isNull(flightSplitTaskVo1)) {
                //不存在就新增
                setBo(flightSplitTaskBo);
                ValidatorUtils.validate(flightSplitTaskBo);
                boolean inserted = flightSplitTaskService.insert(flightSplitTaskBo);

                if (inserted) {
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、飞行分解任务 记录导入成功");
                    return;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、飞行分解任务 记录导入失败");
                    return;
                }
            } else if (isUpdateSupport) {
                //存在就更新
                flightSplitTaskBo.setId(flightSplitTaskVo1.getId());//主键
                flightSplitTaskBo.setVersion(flightSplitTaskVo1.getVersion());
                boolean updated = flightSplitTaskService.update(flightSplitTaskBo);
                if (updated) {
                    successNum++;
                    successMsg.append("<br/>").append(successNum).append("、飞行分解任务 记录更新成功");
                    return;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("、飞行分解任务 记录更新失败");
                    return;
                }
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、飞行分解任务 记录导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    private void setBo(FlightSplitTaskBo flightSplitTaskBo) {
        flightSplitTaskBo.setVersion(0);
        flightSplitTaskBo.setCreateBy(loginUser.getUserId());
        flightSplitTaskBo.setUpdateBy(loginUser.getUserId());
        flightSplitTaskBo.setTenantId(loginUser.getTenantId());
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("解析完成");
    }

    @Override
    public ExcelResult<FlightSplitTaskImportVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public ExcelResultRes getAnalysis() {
                if (failureNum > 0 && successNum == 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据没有成功导入，错误如下：");
                    return ExcelResultRes.builder().logInfo(failureMsg.toString()).status("0").build();
                } else if (failureNum > 0 && successNum > 0) {
                    failureMsg.insert(0, "很抱歉，部分导入失败！共 " + failureNum + " 条数据没有成功导入，错误如下：");
                    return ExcelResultRes.builder().logInfo(failureMsg.toString()).status("2").build();
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
                    return ExcelResultRes.builder().logInfo(successMsg.toString()).status("1").build();
                }
            }

            @Override
            public List<FlightSplitTaskImportVo> getList() {
                return null;
            }

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