package cn.iocoder.yudao.module.pms.convert.iotmaterialreq;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterialrequisitiondetail.vo.IotMaterialRequisitionDetailRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialrequisitiondetail.IotMaterialRequisitionDetailDO;
import cn.iocoder.yudao.module.system.api.saporg.dto.SapOrgRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

@Mapper
public interface IotMaterialReqConvert {

    IotMaterialReqConvert INSTANCE = Mappers.getMapper(IotMaterialReqConvert.class);

    default List<IotMaterialRequisitionDetailRespVO> convertList(List<IotMaterialRequisitionDetailDO> list,
                                                                 Map<Long, SapOrgRespDTO> factoryMap,
                                                                 Map<Long, SapOrgRespDTO> costCenterMap, Map<String, BigDecimal> stockQuantityPair) {
        return CollectionUtils.convertList(list, requisitionDetail -> convert1(requisitionDetail, factoryMap.get(requisitionDetail.getFactoryId()),
                costCenterMap.get(requisitionDetail.getCostCenterId()), stockQuantityPair));
    }

    default IotMaterialRequisitionDetailRespVO convert1(IotMaterialRequisitionDetailDO requisitionDetail,
                                                        SapOrgRespDTO factory, SapOrgRespDTO costCenter, Map<String, BigDecimal> stockQuantityPair) {
        IotMaterialRequisitionDetailRespVO requisitionDetailVO = BeanUtils.toBean(requisitionDetail, IotMaterialRequisitionDetailRespVO.class);
        if (factory != null) {
            requisitionDetailVO.setFactory(factory.getFactoryName());
        }
        if (costCenter != null) {
            requisitionDetailVO.setCostCenter(costCenter.getCostCenterName());
        }
        // 设置当前 成本中心-物料 库存
        String uniqueKey = StrUtil.join("-", requisitionDetail.getCostCenterId(), requisitionDetail.getMaterialCode());
        if (stockQuantityPair.containsKey(uniqueKey)) {
            requisitionDetailVO.setQuantity(stockQuantityPair.get(uniqueKey));
        }
        return requisitionDetailVO;
    }

}
