package com.ruoyi.oa.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.ruoyi.common.excel.ExcelListener;
import com.ruoyi.common.excel.ExcelResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ValidatorUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.oa.domain.SysOaWarehouseMaster;
import com.ruoyi.oa.domain.bo.SysOaWarehouseBo;
import com.ruoyi.oa.domain.bo.SysOaWarehouseDetailBo;
import com.ruoyi.oa.domain.bo.SysOaWarehouseMasterBo;
import com.ruoyi.oa.domain.vo.SysOaWarehouseVo;
import com.ruoyi.oa.service.ISysOaArticleService;
import com.ruoyi.oa.service.ISysOaWarehouseMasterService;
import com.ruoyi.oa.service.ISysOaWarehouseService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Slf4j
public class SysOaWarehouseListener extends AnalysisEventListener<SysOaWarehouseVo> implements ExcelListener<SysOaWarehouseVo> {

    private final ISysOaWarehouseService sysOaWarehouseService;

    private final ISysOaWarehouseMasterService warehouseMasterService;

    private final Boolean isUpdateSupport;


    private int successNum = 0;
    private int failureNum = 0;

    private final List<SysOaWarehouseVo> sysOaWarehouseVos= new ArrayList<>();
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public SysOaWarehouseListener(Boolean isUpdateSupport) {
        this.warehouseMasterService = SpringUtils.getBean(ISysOaWarehouseMasterService.class);
        this.sysOaWarehouseService = SpringUtils.getBean(ISysOaWarehouseService.class);
        this.isUpdateSupport = isUpdateSupport;

    }

    @Override
    public void invoke(SysOaWarehouseVo sysOaWarehouseVo, AnalysisContext context) {
        SysOaWarehouseBo bo = new SysOaWarehouseBo();
        sysOaWarehouseVo.setId(null);
        bo.setName(sysOaWarehouseVo.getName());
        if (Objects.nonNull(sysOaWarehouseVo.getModel())){
            bo.setModel(sysOaWarehouseVo.getModel().trim());

        }
        if(Objects.nonNull(sysOaWarehouseVo.getBrand())){
            bo.setBrand(sysOaWarehouseVo.getBrand().trim());
        }

        List<SysOaWarehouseVo> complaintVo = this.sysOaWarehouseService.queryList(bo);
        try {
            // 验证是否存在这个物料
            if (complaintVo.isEmpty()) {

                if (Objects.isNull(sysOaWarehouseVo.getPrice())) {
                    sysOaWarehouseVo.setPrice(0.0);
                }
                SysOaWarehouseBo add = BeanUtil.toBean(sysOaWarehouseVo, SysOaWarehouseBo.class);
                Long warehouseId = sysOaWarehouseService.insertByBo2(add);
                sysOaWarehouseVo.setId(warehouseId);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("、物料名称： ").append(add.getName()).append(" 导入成功");
            } else if (isUpdateSupport) {
                // 这里是将现有的进行更新
                SysOaWarehouseVo warehouseVo = complaintVo.get(0);

                SysOaWarehouseBo update  = BeanUtil.toBean(sysOaWarehouseVo, SysOaWarehouseBo.class);
                update.setId(warehouseVo.getId());
                update.setInventory(warehouseVo.getInventory()+sysOaWarehouseVo.getInventory());
                // 价格算加权平均
                if (Objects.nonNull(sysOaWarehouseVo.getPrice()) && Objects.nonNull(sysOaWarehouseVo.getInventory())) {
                    update.setPrice((warehouseVo.getPrice()*warehouseVo.getInventory()+sysOaWarehouseVo.getPrice()*sysOaWarehouseVo.getInventory())/(warehouseVo.getInventory()+sysOaWarehouseVo.getInventory()));
                }
                ValidatorUtils.validate(complaintVo);
                sysOaWarehouseService.updateByBo(update);
                warehouseVo.setInventory(sysOaWarehouseVo.getInventory());
                warehouseVo.setPrice(sysOaWarehouseVo.getPrice());
                sysOaWarehouseVo = warehouseVo;

                successNum++;
                successMsg.append("<br/>").append(successNum).append("、物料名称： ").append(update.getName()).append(" 更新成功");
            }
            sysOaWarehouseVos.add(sysOaWarehouseVo);
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、物料名称： " + sysOaWarehouseVo.getName() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

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

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

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

}
