package com.gs.xiaomi.util; import com.gs.xiaomi.dto.SnListItemDto; import com.gs.xiaomi.entity.SnListItem; import org.springframework.beans.BeanUtils; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.stream.Collectors; /** * SnListItem DTO和Entity转换工具类 */ public class SnListItemConverter { /** * DTO转Entity * * @param dto DTO对象 * @return Entity对象 */ public static SnListItem toEntity(SnListItemDto dto) { if (dto == null) { return null; } SnListItem entity = new SnListItem(); BeanUtils.copyProperties(dto, entity); entity.setCreatedTime(new Date()); entity.setUpdatedTime(new Date()); return entity; } /** * DTO转Entity,带关联信息 * * @param dto DTO对象 * @param deliveryMainId 送货单主表ID * @param zzasn 送货单号 * @return Entity对象 */ public static SnListItem toEntity(SnListItemDto dto, Long deliveryMainId, String zzasn) { if (dto == null) { return null; } SnListItem entity = toEntity(dto); entity.setDeliveryMainId(deliveryMainId); entity.setZzasn(zzasn); return entity; } /** * Entity转DTO * * @param entity Entity对象 * @return DTO对象 */ public static SnListItemDto toDto(SnListItem entity) { if (entity == null) { return null; } SnListItemDto dto = new SnListItemDto(); BeanUtils.copyProperties(entity, dto); return dto; } /** * DTO列表转Entity列表 * * @param dtoList DTO列表 * @return Entity列表 */ public static List toEntityList(List dtoList) { if (dtoList == null || dtoList.isEmpty()) { return new ArrayList<>(); } return dtoList.stream() .map(SnListItemConverter::toEntity) .collect(Collectors.toList()); } /** * DTO列表转Entity列表,带关联信息 * * @param dtoList DTO列表 * @param deliveryMainId 送货单主表ID * @param zzasn 送货单号 * @return Entity列表 */ public static List toEntityList(List dtoList, Long deliveryMainId, String zzasn) { if (dtoList == null || dtoList.isEmpty()) { return new ArrayList<>(); } return dtoList.stream() .map(dto -> toEntity(dto, deliveryMainId, zzasn)) .collect(Collectors.toList()); } /** * Entity列表转DTO列表 * * @param entityList Entity列表 * @return DTO列表 */ public static List toDtoList(List entityList) { if (entityList == null || entityList.isEmpty()) { return new ArrayList<>(); } return entityList.stream() .map(SnListItemConverter::toDto) .collect(Collectors.toList()); } }