package com.maintain.business.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 com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.maintain.business.domain.ErpExamineProject;
import com.maintain.business.domain.ErpProjectType;
import com.maintain.business.domain.ErpWarehouse;
import com.maintain.business.domain.ErpWarehouseLocation;
import com.maintain.business.domain.vo.ErpExamineProjectImportVo;
import com.maintain.business.domain.vo.ErpWarehouseLocationImportVo;
import com.maintain.business.domain.vo.ErpWarehouseLocationVo;
import com.maintain.business.mapper.ErpExamineProjectMapper;
import com.maintain.business.mapper.ErpProjectTypeMapper;
import com.maintain.business.mapper.ErpWarehouseLocationMapper;
import com.maintain.business.mapper.ErpWarehouseMapper;
import com.maintain.common.core.domain.entity.SysDept;
import com.maintain.common.enums.ProjectType;
import com.maintain.common.excel.ExcelListener;
import com.maintain.common.excel.ExcelResult;
import com.maintain.common.exception.ServiceException;
import com.maintain.common.helper.LoginHelper;
import com.maintain.common.utils.ValidatorUtils;
import com.maintain.common.utils.spring.SpringUtils;
import com.maintain.system.mapper.SysDeptMapper;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 仓库货位自定义导入
 *
 * @author liushuai
 */
@Slf4j
public class ErpWarehouseLocationImportListener extends AnalysisEventListener<ErpWarehouseLocationImportVo> implements ExcelListener<ErpWarehouseLocationImportVo> {

    private final ErpWarehouseLocationMapper warehouseLocationMapper;
    private final ErpWarehouseMapper warehouseMapper;
    private final SysDeptMapper deptMapper;

    private final Boolean isUpdateSupport;

    private final String operName;

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

    public ErpWarehouseLocationImportListener(Boolean isUpdateSupport) {
        this.warehouseLocationMapper = SpringUtils.getBean(ErpWarehouseLocationMapper.class);
        this.warehouseMapper = SpringUtils.getBean(ErpWarehouseMapper.class);
        this.deptMapper = SpringUtils.getBean(SysDeptMapper.class);
        this.isUpdateSupport = isUpdateSupport;
        this.operName = LoginHelper.getUsername();
    }

    @Override
    public void invoke(ErpWarehouseLocationImportVo locationImportVo, AnalysisContext context) {
        LambdaQueryWrapper<ErpWarehouseLocation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ErpWarehouseLocation::getWarehouseCode, locationImportVo.getWarehouseCode())
            .eq(ErpWarehouseLocation::getWarehouseName, locationImportVo.getWarehouseName());
        ErpWarehouseLocation warehouseLocation = warehouseLocationMapper.selectOne(lqw);
        try {
            // 验证是否存在
            if (ObjectUtil.isNull(warehouseLocation)) {
                warehouseLocation = BeanUtil.toBean(locationImportVo, ErpWarehouseLocation.class);
                ValidatorUtils.validate(warehouseLocation);
//                LambdaQueryWrapper<SysDept> deptLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                deptLambdaQueryWrapper.eq(SysDept::getDeptName, warehouseLocation.getDeptName());
//                SysDept sysDept = deptMapper.selectOne(deptLambdaQueryWrapper);
                /* 部门不存在新增部门 */
//                if (sysDept == null) {
//                    SysDept dept = new SysDept();
//                    dept.setParentId(100L);
//                    dept.setAncestors("0,100");
//                    dept.setOrderNum(1);
//                    dept.setDeptName(warehouseLocation.getDeptName());
//                    dept.setStatus("0");
//                    dept.setDelFlag("0");
//                    deptMapper.insert(dept);
//                    sysDept = dept;
//                    warehouseLocation.setDeptId(dept.getDeptId());
//                    warehouseLocation.setDeptName(dept.getDeptName());
//                }else {
//                    warehouseLocation.setDeptId(sysDept.getDeptId());
//                    warehouseLocation.setDeptName(sysDept.getDeptName());
//                }
//                // 仓库是否存在
//                LambdaQueryWrapper<ErpWarehouse> warehouseLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                warehouseLambdaQueryWrapper.eq(ErpWarehouse::getName, warehouseLocation.getWarehouseName())
//                    .eq(ErpWarehouse::getCode, warehouseLocation.getWarehouseCode());
//                ErpWarehouse erpWarehouse = warehouseMapper.selectOne(warehouseLambdaQueryWrapper);
//                if (erpWarehouse == null) {
//                    ErpWarehouse warehouse = new ErpWarehouse();
//                    warehouse.setCode(warehouseLocation.getWarehouseCode());
//                    warehouse.setName(warehouseLocation.getWarehouseName());
//                    warehouse.setDeptId(sysDept.getDeptId());
//                    warehouse.setDeptName(sysDept.getDeptName());
//                    warehouse.setCreateDeptId(LoginHelper.getDeptId());
//                    warehouseMapper.insert(warehouse);
//                    warehouseLocation.setWarehouseId(warehouse.getId());
//                    warehouseLocation.setWarehouseName(warehouse.getName());
//                }else {
//                    warehouseLocation.setWarehouseId(erpWarehouse.getId());
//                    warehouseLocation.setWarehouseName(erpWarehouse.getName());
//                }
                warehouseLocationMapper.insert(warehouseLocation);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、仓库货位 ").append(warehouseLocation.getLocationType()).append(" 导入成功");
            } else {
                failureNum++;
                failureMsg.append("<br/>").append(failureNum).append("、仓库货位 ").append(warehouseLocation.getLocationType()).append(" 已存在");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、仓库货位 " + warehouseLocation.getLocationType() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }

    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

            @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<ErpWarehouseLocationImportVo> getList() {
                return null;
            }

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