ServiceImpl中
getBaseMapper()的使用
public IPage<ProductPageVO> getProductPage(Integer regionOrCityCode, Integer brandId, LocalDate usedDate, Page<ProductPageVO> page) {
return getBaseMapper().getProductPage(regionOrCityCode, brandId, usedDate, page);
}
optional的使用
//查询产品
Optional<ProductManage> optionalProductManage = productManageService.lambdaQuery()
.eq(BaseEntity::getId, productId).oneOpt();
if (optionalProductManage.isEmpty()) {
return null;
}
ProductManage productManage = optionalProductManage.get();
lamdaquery 查询+流操作获取vo
List<CarInfoVO.Insure.Term> terms = insureTermService.lambdaQuery().in(BaseEntity::getId, termIds).orderByDesc(InsureTerm::getSort)
.list().stream()
.map(term -> {
//对item进行遍历
CarInfoVO.Insure.Term termVo = new CarInfoVO.Insure.Term();
termVo.setTitle(term.getTitle());
termVo.setSketch(term.getSketch());
termVo.setDetail(term.getDetails());
return termVo;
})
.collect(Collectors.toList());//聚集元素
流操作之分组
Map<String, List<CarInfoVO.GeneralConfiguration>> subscribeMap =
universalConfigureService.lambdaQuery()
.in(UniversalConfigure::getParentId, subscribes)
.eq(UniversalConfigure::getStatus, 1)
.orderByDesc(UniversalConfigure::getSort)
.list()
.stream().collect(
Collectors.groupingBy(UniversalConfigure::getParentId,//对数据进行分组,指定分组的key
Collectors.mapping(item -> {//组建分组中的value
CarInfoVO.GeneralConfiguration generalConfiguration = new CarInfoVO.GeneralConfiguration();
generalConfiguration.setTitle(item.getTitle());
generalConfiguration.setSketch(item.getSketch());
generalConfiguration.setDetail(item.getDetail());
if (StringUtils.isNotBlank(item.getMaterialId())) {
Optional<MaterialConfigure> optional =
materialConfigureService.lambdaQuery()
.eq(BaseEntity::getId, item.getMaterialId())
.oneOpt();
if (!optional.isEmpty()) {
generalConfiguration.setIcon(ossUtil.getCacheSignedUrl(optional.get().getUrl()));
}
}
return generalConfiguration;
}, Collectors.toList())
)
);
流操作之排序
List<CarInfoVO.Car> cars = carManages.stream().map
(carManage -> {
CarInfoVO.Car car = new CarInfoVO.Car();
car.setCarId(carManage.getId());
car.setTitle(carManage.getTitle());
car.setStatus(carManage.getStatus());
car.setPrice(carManage.getPrice());
car.setCreateTime(carManage.getCreateTime());
//车辆关系表
Map<Integer, List<String>> carRelations = carManageRelationService.lambdaQuery()
.eq(CarManageRelation::getCarId, carManage.getId())
.isNotNull(CarManageRelation::getRelationId)
.orderByDesc(CarManageRelation::getSort)
.list().stream()
.collect(Collectors.groupingBy(CarManageRelation::getType, Collectors.mapping(CarManageRelation::getRelationId, Collectors.toList())));
//排期判断
Integer integer = 0;
List<String> tenancyIds = carRelations.get(1);
//查询租期信息
if (CollectionUtils.isNotEmpty(tenancyIds)) {
//取最小租期
Optional<TenancyConfigure> optional = tenancyConfigureService.lambdaQuery()
.in(BaseEntity::getId, tenancyIds)
.orderByAsc(TenancyConfigure::getDuration)
.last("limit 1").oneOpt();
if (!optional.isEmpty()) {
//结束时间
LocalDate endDate = usedDate.plusDays(optional.get().getDuration() * 30);
if (usedDate.isAfter(carManage.getStartTime().toLocalDate()) && endDate.isBefore(carManage.getEndTime().toLocalDate())) {
//查询有无排期
integer = scheduleServerUtil.searchScheduleRequest(carManage.getCarId(), usedDate, endDate, false);
}
}
}
car.setSchedule(integer != 0 ? 1 : 0);
car.setRegistrationTime(carManage.getRegistrationTime().getYear() + "年" + carManage.getRegistrationTime().getMonthValue() + "月");
car.setCarModelTerritory(carManage.getCarModelTerritory());
car.setCurrentMileage(carManage.getCurrentMileage());
car.setCarNumber(carManage.getCarNumber());
Optional<MaterialConfigure> optional = materialConfigureService.lambdaQuery().eq(BaseEntity::getId, carManage.getCarCoverId()).oneOpt();
if (!optional.isEmpty()) {
car.setDisplay(ossUtil.getCacheSignedUrl(optional.get().getUrl()));
}
//外观 内饰 空间
List<String> pictureRelationIds = new ArrayList<>();
pictureRelationIds.addAll(carRelations.get(4));
pictureRelationIds.addAll(carRelations.get(5));
pictureRelationIds.addAll(carRelations.get(6));
if (CollectionUtils.isNotEmpty(pictureRelationIds)) {
Map<String, String> collect1 = materialConfigureService.lambdaQuery()
.in(BaseEntity::getId, pictureRelationIds)
.list()
.stream().collect(Collectors.toMap(BaseEntity::getId, MaterialConfigure::getUrl));
List<String> collect2 = pictureRelationIds.stream()
.map(item -> ossUtil.getCacheSignedUrl(collect1.get(item)))
.collect(Collectors.toList());
car.setPictures(collect2);
}
//自定义配置
List<String> customCarConfigIds = carRelations.get(2);
if (CollectionUtils.isNotEmpty(customCarConfigIds)) {
List<CarInfoVO.Car.CustomCarConfig> collect = carConfigureService.lambdaQuery().in(BaseEntity::getId, customCarConfigIds)
.orderByDesc(CarConfigure::getSort)
.list().stream()
.map(item -> {
CarInfoVO.Car.CustomCarConfig customCarConfig = new CarInfoVO.Car.CustomCarConfig();
customCarConfig.setTitle(item.getTitle());
customCarConfig.setResume(item.getResume());
customCarConfig.setDetail(item.getDetails());
return customCarConfig;
}).collect(Collectors.toList());
car.setCustomCarConfigs(collect);
}
return car;
})
//排序
.sorted(Comparator.comparing(CarInfoVO.Car::getSchedule).reversed().thenComparing(CarInfoVO.Car::getPrice).thenComparing(CarInfoVO.Car::getCreateTime))
.collect(Collectors.toList());
流操作之取大小
List<CarManage> carManages = carManageService.lambdaQuery()
.eq(CarManage::getProductId, productId)
.eq(CarManage::getStatus, 1)
.list();
Optional<CarManage> min = carManages.stream().min(Comparator.comparing(CarManage::getMarketPrice));
if (!min.isEmpty()) {
carInfoVO.setGuidePriceMin(min.get().getMarketPrice());
}
流操作之过滤出指定字段
//所有产品
List<ProductManage> list = productManageService.lambdaQuery()
.in(ProductManage::getCityCode, allCities)
.eq(ProductManage::getStatus, 1)
.le(ProductManage::getStartTime, usedDate)
.ge(ProductManage::getEndTime, usedDate)
.list();
if (ObjectUtils.isEmpty(list)) {
return new ArrayList<>();
}
//查询产品下是否存在车辆 没有车辆不显示车系
List<String> productIds = list.stream().map(BaseEntity::getId).collect(Collectors.toList());