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

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterial.vo.IotMaterialRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmaterial.vo.IotMaterialSimpleRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterial.IotMaterialDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmaterialgroup.IotMaterialGroupDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

@Mapper
public interface IotMaterialConvert {

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

    default List<IotMaterialRespVO> convertList(List<IotMaterialDO> list, Map<Long, IotMaterialGroupDO> materialGroupMap) {
        return CollectionUtils.convertList(list, material -> convert(material, materialGroupMap.get(material.getMaterialGroupId())));
    }

    default IotMaterialRespVO convert(IotMaterialDO material, IotMaterialGroupDO materialGroup) {
        IotMaterialRespVO materialVO = BeanUtils.toBean(material, IotMaterialRespVO.class);
        if (materialGroup != null) {
            materialVO.setMaterialGroupName(materialGroup.getName());
        }
        return materialVO;
    }

    default List<IotMaterialSimpleRespVO> convertSimpleList(List<IotMaterialDO> list, Map<Long, IotMaterialGroupDO> materialGroupMap) {
        return CollectionUtils.convertList(list, material -> {
            IotMaterialSimpleRespVO materialVO = BeanUtils.toBean(material, IotMaterialSimpleRespVO.class);
            MapUtils.findAndThen(materialGroupMap, material.getMaterialGroupId(), group -> materialVO.setMaterialGroupName(group.getName()));
            return materialVO;
        });
    }

}
