package com.zfire.mall.manager.logic.manage.material;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.listV2.MaterialReferPriceVO;
import com.zfire.mall.manager.commonmapper.listV2.CommonCMapper;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.plus.entity.KingDeeMaterial;
import com.zfire.mall.manager.plus.entity.MaterialReferPrice;
import com.zfire.mall.manager.plus.service.KingDeeMaterialService;
import com.zfire.mall.manager.plus.service.MaterialReferPriceService;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.zfire.bean.ZfireParamBean;
import com.zfire.mall.manager.zfire.util.FieldUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 物料参考成本价格
 * @author ：lijh
 * @description：TODO
 * @date ：2024/2/19 15:44
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MaterialReferPriceLogic {

    private final MaterialReferPriceService materialReferPriceService;
    private final KingDeeMaterialService kingDeeMaterialService;
    private final CommonCMapper commonCMapper;


    /**
     * 列表
     * @param zfireParamBean
     * @return
     */
    public IPage<MaterialReferPriceVO> list(ZfireParamBean zfireParamBean){
        FieldUtils.supplyParam(zfireParamBean);
        IPage<MaterialReferPriceVO> list = commonCMapper.queryMaterialReferPriceList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return list;
    }


    /**
     * 新增/编辑
     * @param materialReferPrice
     */
    public void add(MaterialReferPrice materialReferPrice){
        if(StringUtils.isAnyBlank(materialReferPrice.getMaterialNumber())){
            throw new RemoteServiceException("物料编码不能为空");
        }
        if(materialReferPrice.getCostPrice() == null){
            throw new RemoteServiceException("参考成本价不能为空");
        }
        KingDeeMaterial kingDeeMaterial = kingDeeMaterialService.lambdaQuery().eq(KingDeeMaterial::getNumber, materialReferPrice.getMaterialNumber())
                .last("limit 1").one();

        KingDeeMaterial upKing = new KingDeeMaterial();
        upKing.setId(kingDeeMaterial.getId());
        upKing.setCostPrice(materialReferPrice.getCostPrice());

        upKing.updateById();

        materialReferPrice.insertOrUpdate();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    public MaterialReferPrice detail(String id){
        return materialReferPriceService.getById(id);
    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        materialReferPriceService.removeById(id);
    }

    /**
     * 导入
     * @param datas
     */
    public void importData(List<Object> datas){
        int index = 1;

        List<MaterialReferPrice> list = new ArrayList<>();
        List<KingDeeMaterial> kingDeeMaterials = new ArrayList<>();

        for(Object o : datas){

            index ++ ;
            List<Object> row = (List<Object>) o;
            if (StringUtils.isEmpty((String)row.get(0)))
                continue;

            CommonUtils.initList2(row,10);
            String materialNumber = (String)row.get(0);
            BigDecimal costPrice = new BigDecimal((String)row.get(4));
            if(StringUtils.isAnyBlank(materialNumber) || costPrice == null){
                throw new RemoteServiceException("第"+index+"行，黄色区域为必填区域");
            }
            KingDeeMaterial kingDeeMaterial = kingDeeMaterialService.lambdaQuery().eq(KingDeeMaterial::getNumber, materialNumber).last("limit 1").one();
            if(kingDeeMaterial == null){
                throw new RemoteServiceException("第"+index+"行，物料编号不存在于系统，请检查");
            }

            MaterialReferPrice materialReferPrice = materialReferPriceService.lambdaQuery().eq(MaterialReferPrice::getMaterialNumber, materialNumber).last("limit 1").one();
            if(materialReferPrice == null){
                materialReferPrice = new MaterialReferPrice();
            }
            materialReferPrice
                    .setMaterialNumber(materialNumber)
                    .setCostPrice(costPrice);

            KingDeeMaterial upKing = new KingDeeMaterial();
            upKing.setId(kingDeeMaterial.getId());
            upKing.setCostPrice(costPrice);

            kingDeeMaterials.add(upKing);
            list.add(materialReferPrice);
        }
        //检查是否有重复的物料编号
        long count = list.stream().map(MaterialReferPrice::getMaterialNumber).distinct().count();
        if(count != list.size()){
            throw new RemoteServiceException("excel表格存在重复相同的物料编号");
        }
        if(CollectionUtils.isNotEmpty(list)) {
            materialReferPriceService.saveOrUpdateBatch(list);
        }
        if(CollectionUtils.isNotEmpty(kingDeeMaterials)) {
            kingDeeMaterialService.saveOrUpdateBatch(kingDeeMaterials);
        }

    }
}
