商品分组

This commit is contained in:
2024-03-05 15:19:57 +08:00
parent c092c469b3
commit d1fd229bf4
4 changed files with 37 additions and 12 deletions

View File

@@ -80,7 +80,6 @@ public class ListUtil {
for (String s : listString) {
integerList.add(Integer.parseInt(s));
}
System.out.println(integerList);
return integerList;
}

View File

@@ -17,6 +17,7 @@ package cn.ysk.cashier.dto.product;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* @website https://eladmin.vip
@@ -54,7 +55,7 @@ public class TbProductGroupDto implements Serializable {
private Integer sort;
/** 商品列表 */
private String productIds;
private List<String> productIds;
private Long createdAt;

View File

@@ -21,8 +21,14 @@ import cn.ysk.cashier.dto.product.TbProductGroupDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.ReportingPolicy;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* @website https://eladmin.vip
* @author lyf
@@ -31,4 +37,29 @@ import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface TbProductGroupMapper extends BaseMapper<TbProductGroupDto, TbProductGroup> {
@Mapping(target = "productIds", source = "productIds")
TbProductGroupDto toDto(TbProductGroup entity);
// 自定义的字符串到整数列表的转换方法
default List<Integer> map(String value) {
if (value == null || value.isEmpty()) {
return Collections.emptyList();
}
// 分割字符串然后将每个数字字符串转换为Integer收集到List中
return Arrays.stream(value.split(","))
.map(Integer::valueOf)
.collect(Collectors.toList());
}
// 如果需要从DTO转回实体也可能需要实现反向的映射方法
default String map(List<Integer> values) {
if (values == null || values.isEmpty()) {
return "";
}
// 将整数列表转换为由逗号分隔的字符串
return values.stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
}
}

View File

@@ -34,6 +34,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.CollectionUtils;
import java.time.Instant;
import java.util.List;
@@ -77,16 +78,9 @@ public class TbProductGroupServiceImpl implements TbProductGroupService {
@Override
public List<TbProduct> findByIdProduct(Integer productGroup) {
TbProductGroup tbProductGroup = tbProductGroupRepository.findById(productGroup).orElseGet(TbProductGroup::new);
if (tbProductGroup.getProductIds() != null){
List<String> list = ListUtil.stringChangeStringList(tbProductGroup.getProductIds());
List<Integer> intList = new ArrayList<>();
for (String str : list) {
intList.add(Integer.parseInt(str));
}
System.out.println(intList);
return tbProductRepository.findByIds(ListUtil.stringChangeIntegerList(list));
TbProductGroupDto dto = tbProductGroupMapper.toDto(tbProductGroup);
if (!CollectionUtils.isEmpty(dto.getProductIds())){
return tbProductRepository.findByIds(ListUtil.stringChangeIntegerList(dto.getProductIds()));
}
return new ArrayList<>();
}