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

import cn.hutool.core.util.ObjectUtil;
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.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorderbommaterial.IotMainWorkOrderBomMaterialDO;
import cn.iocoder.yudao.module.system.api.saporg.dto.SapOrgRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface IotWorkOrderBomMaterialConvert {

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

    default List<IotMainWorkOrderBomMaterialRespVO> convertList(List<IotMainWorkOrderBomMaterialRespVO> list,
                                                                Map<Long, SapOrgRespDTO> factoryMap,
                                                                Map<Long, SapOrgRespDTO> costCenterMap, Map<Long, SapOrgRespDTO> stockLocationMap) {
        return CollectionUtils.convertList(list, stock -> convert1(stock, factoryMap.get(stock.getFactoryId()),
                costCenterMap.get(stock.getCostCenterId()), stockLocationMap.get(stock.getStorageLocationId())));
    }

    default List<IotMainWorkOrderBomMaterialRespVO> convertList1(List<IotMainWorkOrderBomMaterialDO> list,
                                                                Map<Long, SapOrgRespDTO> factoryMap,
                                                                Map<Long, SapOrgRespDTO> costCenterMap, Map<Long, SapOrgRespDTO> stockLocationMap) {
        return CollectionUtils.convertList(list, stock -> convert2(stock, factoryMap.get(stock.getFactoryId()),
                costCenterMap.get(stock.getCostCenterId()), stockLocationMap.get(stock.getStorageLocationId())));
    }

    default IotMainWorkOrderBomMaterialRespVO convert1(IotMainWorkOrderBomMaterialRespVO stock,
                                                       SapOrgRespDTO factory, SapOrgRespDTO costCenter, SapOrgRespDTO projectDepartment) {
        IotMainWorkOrderBomMaterialRespVO stockVO = BeanUtils.toBean(stock, IotMainWorkOrderBomMaterialRespVO.class);
        if (ObjectUtil.isNotEmpty(factory)) {
            stockVO.setFactory(factory.getFactoryName());
        }
        if (ObjectUtil.isNotEmpty(costCenter)) {
            stockVO.setCostCenter(costCenter.getCostCenterName());
        }
        if (ObjectUtil.isNotEmpty(projectDepartment)) {
            stockVO.setProjectDepartment(projectDepartment.getStorageLocationName());
        }
        return stockVO;
    }

    default IotMainWorkOrderBomMaterialRespVO convert2(IotMainWorkOrderBomMaterialDO stock,
                                                       SapOrgRespDTO factory, SapOrgRespDTO costCenter, SapOrgRespDTO projectDepartment) {
        IotMainWorkOrderBomMaterialRespVO stockVO = BeanUtils.toBean(stock, IotMainWorkOrderBomMaterialRespVO.class);
        if (ObjectUtil.isNotEmpty(factory)) {
            stockVO.setFactory(factory.getFactoryName());
        }
        if (ObjectUtil.isNotEmpty(costCenter)) {
            stockVO.setCostCenter(costCenter.getCostCenterName());
        }
        if (ObjectUtil.isNotEmpty(projectDepartment)) {
            stockVO.setProjectDepartment(projectDepartment.getStorageLocationName());
        }
        return stockVO;
    }

}
