更新了写真预约模块

This commit is contained in:
chen-xin-zhi 2025-02-17 11:54:15 +08:00
parent 4d9ecd757f
commit 64e2f360e3
13 changed files with 199 additions and 122 deletions

View File

@ -111,7 +111,6 @@ public class BookingDateController {
} }
Long id = commonRequest.getId(); Long id = commonRequest.getId();
BookingDate bookingDate = bookingDateService.getById(id); BookingDate bookingDate = bookingDateService.getById(id);
ThrowUtils.throwIf(bookingDate == null, ErrorCode.OPERATION_ERROR, "预约日期不存在"); ThrowUtils.throwIf(bookingDate == null, ErrorCode.OPERATION_ERROR, "预约日期不存在");
Integer status = bookingDate.getIsAvailable() == 0 ? 1 : 0; Integer status = bookingDate.getIsAvailable() == 0 ? 1 : 0;
UpdateWrapper<BookingDate> updateWrapper = new UpdateWrapper<>(); UpdateWrapper<BookingDate> updateWrapper = new UpdateWrapper<>();

View File

@ -1,7 +1,6 @@
package com.cultural.heritage.controller.book; package com.cultural.heritage.controller.book;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cultural.heritage.annotation.AuthCheck; import com.cultural.heritage.annotation.AuthCheck;
import com.cultural.heritage.common.BaseResponse; import com.cultural.heritage.common.BaseResponse;
@ -36,8 +35,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/photoCategory") @RequestMapping("/photoCategory")
@ -142,20 +142,12 @@ public class PhotoCategoryController {
Long id = commonRequest.getId(); Long id = commonRequest.getId();
PhotoCategory photoCategory = photoCategoryService.getById(id); PhotoCategory photoCategory = photoCategoryService.getById(id);
ThrowUtils.throwIf(photoCategory == null, ErrorCode.OPERATION_ERROR, "写真类别不存在"); ThrowUtils.throwIf(photoCategory == null, ErrorCode.OPERATION_ERROR, "写真类别不存在");
QueryWrapper<PhotoProducts> queryWrapper = new QueryWrapper<>(); // 获取写真产品列表
queryWrapper.eq("categoryName", photoCategory.getName()); List<PhotoProducts> photoProductsList = commonService.findByFieldValue("categoryName", photoCategory.getName(), photoProductsService);
List<PhotoProducts> photoProductsList = photoProductsService.list(queryWrapper);
// 获取预约日期表 // 获取预约日期表
List<Long> photoProductIds = photoProductsList.stream().map(PhotoProducts::getId).toList(); List<BookingDate> bookingDateList = commonService.findBySourceFieldValues(photoProductsList, bookingDateService, "id", "photoProductId");
QueryWrapper<BookingDate> bookingDateQueryWrapper = new QueryWrapper<>();
bookingDateQueryWrapper.in("photoProductId", photoProductIds);
List<BookingDate> bookingDateList = bookingDateService.list(bookingDateQueryWrapper);
// 获取预约时间表 // 获取预约时间表
List<Long> bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); List<BookingTime> bookingTimeList = commonService.findBySourceFieldValues(bookingDateList, bookingTimeService, "id", "bookingDateId");
QueryWrapper<BookingTime> bookingTimeQueryWrapper = new QueryWrapper<>();
bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds);
List<BookingTime> bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper);
// 批量删除预约时间 // 批量删除预约时间
boolean removeTime = bookingTimeService.removeBatchByIds(bookingTimeList); boolean removeTime = bookingTimeService.removeBatchByIds(bookingTimeList);
ThrowUtils.throwIf(!removeTime, ErrorCode.OPERATION_ERROR, "预约时间删除失败"); ThrowUtils.throwIf(!removeTime, ErrorCode.OPERATION_ERROR, "预约时间删除失败");
@ -186,16 +178,14 @@ public class PhotoCategoryController {
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE) @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<List<PhotoCategoryVO>> listCategory() { public BaseResponse<List<PhotoCategoryVO>> listCategory() {
List<PhotoCategory> photoCategoryList = photoCategoryService.list(); List<PhotoCategory> photoCategoryList = photoCategoryService.list();
List<PhotoCategoryVO> photoCategoryVOS = photoCategoryList.stream().map(photoCategory -> { // 获取写真产品VO列表
PhotoCategoryVO photoCategoryVO = new PhotoCategoryVO(); List<PhotoCategoryVO> photoCategoryVOS = commonService.convertList(photoCategoryList, PhotoCategoryVO.class);
BeanUtils.copyProperties(photoCategory, photoCategoryVO);
return photoCategoryVO;
}).toList();
return ResultUtils.success(photoCategoryVOS); return ResultUtils.success(photoCategoryVOS);
} }
/** /**
* 小程序端用户查询写真类别 * 小程序端用户查询写真类别
* @return 写真类别列表 * @return 写真类别列表
@ -204,24 +194,14 @@ public class PhotoCategoryController {
@Operation(summary = "小程序端用户查询写真类别", description = "参数权限所有人方法名listCategoryByUsers") @Operation(summary = "小程序端用户查询写真类别", description = "参数权限所有人方法名listCategoryByUsers")
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE) @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
public BaseResponse<List<PhotoCategoryVO>> listCategoryByUsers() { public BaseResponse<List<PhotoCategoryVO>> listCategoryByUsers() {
QueryWrapper<PhotoProducts> queryWrapper = new QueryWrapper<>(); // 获取已上架的写真产品列表
queryWrapper.eq("isShelves", 1); List<PhotoProducts> photoProductsList = commonService.findByFieldValue("isShelves", 1, photoProductsService);
List<PhotoProducts> photoProductsList = photoProductsService.list(queryWrapper); // 获取类别写真产品类别列表
List<String> typeNameList = photoProductsList.stream().map(PhotoProducts::getCategoryName).toList(); List<PhotoCategory> photoCategoryList = commonService.findBySourceFieldValues(photoProductsList, photoCategoryService, "categoryName", "name");
// 获取写真产品类别VO列表
List<PhotoCategoryVO> photoCategoryVOList = commonService.convertList(photoCategoryList, PhotoCategoryVO.class);
QueryWrapper<PhotoCategory> categoryWrapper = new QueryWrapper<>(); return ResultUtils.success(photoCategoryVOList);
List<PhotoCategory> categoryList = new ArrayList<>();
if (!typeNameList.isEmpty()) {
categoryWrapper.in("name", typeNameList);
categoryList = photoCategoryService.list(categoryWrapper);
}
List<PhotoCategoryVO> photoCategoryVOS = categoryList.stream().map(photoCategory -> {
PhotoCategoryVO photoCategoryVO = new PhotoCategoryVO();
BeanUtils.copyProperties(photoCategory, photoCategoryVO);
return photoCategoryVO;
}).toList();
return ResultUtils.success(photoCategoryVOS);
} }
@ -241,15 +221,12 @@ public class PhotoCategoryController {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
String categoryName = commonStringRequest.getType(); String categoryName = commonStringRequest.getType();
QueryWrapper<PhotoProducts> queryWrapper = new QueryWrapper<>(); Map<String, Object> fieldConditions = new HashMap<>();
queryWrapper.eq("categoryName", categoryName).eq("isShelves", 1); fieldConditions.put("categoryName", categoryName);
List<PhotoProducts> photoProductsList = photoProductsService.list(queryWrapper); fieldConditions.put("isShelves", 1);
List<PhotoProducts> photoProductsList = commonService.findByMultipleFieldValues(fieldConditions, photoProductsService);
List<PhotoProductsVO> photoProductsVOS = photoProductsList.stream().map(photoProducts -> { // 获取写真产品VO列表
PhotoProductsVO photoProductsVO = new PhotoProductsVO(); List<PhotoProductsVO> photoProductsVOS = commonService.convertList(photoProductsList, PhotoProductsVO.class);
BeanUtils.copyProperties(photoProducts, photoProductsVO);
return photoProductsVO;
}).toList();
return ResultUtils.success(photoProductsVOS); return ResultUtils.success(photoProductsVOS);
} }

View File

@ -43,9 +43,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/photoProducts") @RequestMapping("/photoProducts")
@ -93,9 +91,6 @@ public class PhotoProductsController {
boolean result = photoProductsService.save(photoProducts); boolean result = photoProductsService.save(photoProducts);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "写真产品添加失败"); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "写真产品添加失败");
// 封装map集合预约日期id, 预约时间列表
Map<Long, List<BookingTimeAddRequest>> map = new HashMap<>();
// 添加预约日期 // 添加预约日期
List<BookingDateAddRequest> bookingDateAddRequestList = photoProductsAddRequest.getBookingDateAddRequestList(); List<BookingDateAddRequest> bookingDateAddRequestList = photoProductsAddRequest.getBookingDateAddRequestList();
List<BookingDate> bookingDateList = bookingDateAddRequestList.stream().map(bookingDateAddRequest -> { List<BookingDate> bookingDateList = bookingDateAddRequestList.stream().map(bookingDateAddRequest -> {
@ -171,17 +166,15 @@ public class PhotoProductsController {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
Long id = commonRequest.getId(); Long id = commonRequest.getId();
// 获取预约日期列表
List<BookingDate> bookingDateList = commonService.findByFieldValue("photoProductId", id, bookingDateService);
// 获取预约时间列表
List<BookingTime> bookingTimeList = commonService.findBySourceFieldValues(bookingDateList, bookingTimeService, "id", "bookingDateId");
// 删除写真产品下的预约时间 // 删除写真产品下的预约时间
QueryWrapper<BookingDate> queryWrapper = new QueryWrapper<>(); boolean remove = bookingTimeService.removeBatchByIds(bookingTimeList);
queryWrapper.eq("photoProductId", id);
List<BookingDate> bookingDateList = bookingDateService.list(queryWrapper);
List<Long> bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList();
QueryWrapper<BookingTime> bookingTimeQueryWrapper = new QueryWrapper<>();
bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds);
boolean remove = bookingTimeService.remove(bookingTimeQueryWrapper);
ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "预约时间批量删除失败"); ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "预约时间批量删除失败");
// 删除写真产品下的预约时间 // 删除写真产品下的预约日期
boolean success = bookingDateService.remove(queryWrapper); boolean success = bookingDateService.removeBatchByIds(bookingDateList);
ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "预约日期批量删除失败"); ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "预约日期批量删除失败");
// 删除写真产品 // 删除写真产品
boolean result = photoProductsService.removeById(id); boolean result = photoProductsService.removeById(id);
@ -205,19 +198,16 @@ public class PhotoProductsController {
throw new BusinessException(ErrorCode.PARAMS_ERROR); throw new BusinessException(ErrorCode.PARAMS_ERROR);
} }
List<Long> idList = commonBatchRequest.getIdList(); List<Long> idList = commonBatchRequest.getIdList();
// 获取写真产品的预约日期
List<BookingDate> bookingDateList = commonService.findByMyIds(idList, bookingDateService);
// 获取写真产品的预约时间
List<BookingTime> bookingTimeList = commonService.findBySourceFieldValues(bookingDateList, bookingTimeService, "id", "bookingDateId");
// 删除写真产品下的预约时间 // 删除写真产品下的预约时间
QueryWrapper<BookingDate> queryWrapper = new QueryWrapper<>(); boolean remove = bookingTimeService.removeBatchByIds(bookingTimeList);
queryWrapper.in("photoProductId", idList);
List<BookingDate> bookingDateList = bookingDateService.list(queryWrapper);
List<Long> bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList();
QueryWrapper<BookingTime> bookingTimeQueryWrapper = new QueryWrapper<>();
bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds);
boolean remove = bookingTimeService.remove(bookingTimeQueryWrapper);
ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "预约时间批量删除失败"); ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "预约时间批量删除失败");
// 删除写真产品下的预约时间 // 删除写真产品下的预约日期
boolean success = bookingDateService.remove(queryWrapper); boolean success = bookingDateService.removeBatchByIds(bookingDateList);
ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "预约日期批量删除失败"); ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "预约日期批量删除失败");
// 批量删除写真产品 // 批量删除写真产品
boolean result = photoProductsService.removeBatchByIds(idList); boolean result = photoProductsService.removeBatchByIds(idList);
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "写真产品批量删除失败"); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "写真产品批量删除失败");
@ -246,26 +236,16 @@ public class PhotoProductsController {
// 获取写真产品列表 // 获取写真产品列表
List<PhotoProducts> records = page.getRecords(); List<PhotoProducts> records = page.getRecords();
// 获取预约日期表 // 获取预约日期表
List<Long> photoProductIds = records.stream().map(PhotoProducts::getId).toList(); List<BookingDate> bookingDateList = commonService.findBySourceFieldValues(records, bookingDateService, "id", "photoProductId");
QueryWrapper<BookingDate> bookingDateQueryWrapper = new QueryWrapper<>();
bookingDateQueryWrapper.in("photoProductId", photoProductIds);
List<BookingDate> bookingDateList = bookingDateService.list(bookingDateQueryWrapper);
// 获取预约时间表 // 获取预约时间表
List<Long> bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); List<BookingTime> bookingTimeList = commonService.findBySourceFieldValues(bookingDateList, bookingTimeService, "id", "bookingDateId");
QueryWrapper<BookingTime> bookingTimeQueryWrapper = new QueryWrapper<>();
bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds);
List<BookingTime> bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper);
// 封装成BookingTimeVO列表, 并将预约时间从小到大排序 // 封装成BookingTimeVO列表, 并将预约时间从小到大排序
List<BookingTimeVO> bookingTimeVOList = bookingTimeList.stream().map(bookingTime -> { List<BookingTimeVO> bookingTimeVOList = commonService.convertList(bookingTimeList, BookingTimeVO.class);
BookingTimeVO bookingTimeVO = new BookingTimeVO(); bookingTimeVOList.sort((b1, b2) -> {
BeanUtils.copyProperties(bookingTime, bookingTimeVO);
return bookingTimeVO;
}).sorted((b1, b2) -> {
DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm"); DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm");
DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm"); DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm");
return time1.compareTo(time2); // 按照时间升序排序 return time1.compareTo(time2); // 按照时间升序排序
}).toList(); });
// 封装成BookingDateVO列表 // 封装成BookingDateVO列表
List<BookingDateVO> bookingDateVOS = new ArrayList<>(); List<BookingDateVO> bookingDateVOS = new ArrayList<>();
@ -324,29 +304,18 @@ public class PhotoProductsController {
} }
Long id = commonRequest.getId(); Long id = commonRequest.getId();
PhotoProducts photoProducts = photoProductsService.getById(id); PhotoProducts photoProducts = photoProductsService.getById(id);
System.out.println("\n\n\n\n\n");
System.out.println(photoProducts);
ThrowUtils.throwIf(photoProducts == null || photoProducts.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "该商品已被删除或者已下架"); ThrowUtils.throwIf(photoProducts == null || photoProducts.getIsShelves() == 0, ErrorCode.OPERATION_ERROR, "该商品已被删除或者已下架");
// 获取预约日期表 // 获取预约日期表
QueryWrapper<BookingDate> queryWrapper = new QueryWrapper<>(); List<BookingDate> bookingDateList = commonService.findByFieldValue("photoProductId", id, bookingDateService);
queryWrapper.eq("photoProductId", id);
List<BookingDate> bookingDateList = bookingDateService.list(queryWrapper);
// 获取预约时间表 // 获取预约时间表
List<Long> bookingDateIds = bookingDateList.stream().map(BookingDate::getId).toList(); List<BookingTime> bookingTimeList = commonService.findBySourceFieldValues(bookingDateList, bookingTimeService, "id", "bookingDateId");
QueryWrapper<BookingTime> bookingTimeQueryWrapper = new QueryWrapper<>();
bookingTimeQueryWrapper.in("bookingDateId", bookingDateIds);
List<BookingTime> bookingTimeList = bookingTimeService.list(bookingTimeQueryWrapper);
// 封装成BookingTimeVO列表, 并将预约时间从小到大排序 // 封装成BookingTimeVO列表, 并将预约时间从小到大排序
List<BookingTimeVO> bookingTimeVOList = bookingTimeList.stream().map(bookingTime -> { List<BookingTimeVO> bookingTimeVOList = commonService.convertList(bookingTimeList, BookingTimeVO.class);
BookingTimeVO bookingTimeVO = new BookingTimeVO(); bookingTimeVOList.sort((b1, b2) -> {
BeanUtils.copyProperties(bookingTime, bookingTimeVO);
return bookingTimeVO;
}).sorted((b1, b2) -> {
DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm"); DateTime time1 = DateUtil.parse(b1.getTimePoint(), "HH:mm");
DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm"); DateTime time2 = DateUtil.parse(b2.getTimePoint(), "HH:mm");
return time1.compareTo(time2); // 按照时间升序排序 return time1.compareTo(time2); // 按照时间升序排序
}).toList(); });
// 封装成BookingDateVO列表 // 封装成BookingDateVO列表
List<BookingDateVO> bookingDateVOS = new ArrayList<>(); List<BookingDateVO> bookingDateVOS = new ArrayList<>();

View File

@ -288,7 +288,7 @@ public class AppointmentDateController {
ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "当前商品不存在"); ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "当前商品不存在");
// 获取预约日期列表 // 获取预约日期列表
List<AppointmentDate> appointmentDateList = commonService.getItemsByIds(timePeriodList, appointmentDateService, TimePeriod::getAppointmentDateId); List<AppointmentDate> appointmentDateList = commonService.findByOtherIds(timePeriodList, appointmentDateService, TimePeriod::getAppointmentDateId);
// 封装map集合预约日期id, 是否可预约 // 封装map集合预约日期id, 是否可预约
Map<Long, Integer> map = new HashMap<>(); Map<Long, Integer> map = new HashMap<>();

View File

@ -95,7 +95,7 @@ public class CartExperienceController {
userService.getLoginUser(request); userService.getLoginUser(request);
// 获取服务类购物车商品列表 // 获取服务类购物车商品列表
List<CartExperience> cartExperienceList = commonService.getItemsByIds(cartExperienceUpdateRequestList, cartExperienceService, CartExperienceUpdateRequest::getId); List<CartExperience> cartExperienceList = commonService.findByOtherIds(cartExperienceUpdateRequestList, cartExperienceService, CartExperienceUpdateRequest::getId);
// 封装map集合购物车id, 商品数量 // 封装map集合购物车id, 商品数量
Map<Long, Integer> QuantityMap = new HashMap<>(); Map<Long, Integer> QuantityMap = new HashMap<>();
@ -114,7 +114,7 @@ public class CartExperienceController {
} }
// 获取商品列表 // 获取商品列表
List<Good> goodList = commonService.getItemsByIds(cartExperienceList, goodService, CartExperience::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartExperienceList, goodService, CartExperience::getGoodId);
// 封装map集合商品id, 商品价格 // 封装map集合商品id, 商品价格
Map<Long, BigDecimal> priceMap = new HashMap<>(); Map<Long, BigDecimal> priceMap = new HashMap<>();
@ -222,10 +222,10 @@ public class CartExperienceController {
userService.getLoginUser(request); userService.getLoginUser(request);
List<Long> cartExperienceIds = commonBatchRequest.getIdList(); List<Long> cartExperienceIds = commonBatchRequest.getIdList();
// 获取购物车信息列表 // 获取购物车信息列表
List<CartExperience> cartExperiences = commonService.getListByIds(cartExperienceIds, cartExperienceService); List<CartExperience> cartExperiences = commonService.findByMyIds(cartExperienceIds, cartExperienceService);
// 获取购物车中的商品列表 // 获取购物车中的商品列表
List<Good> goodList = commonService.getItemsByIds(cartExperiences, goodService, CartExperience::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartExperiences, goodService, CartExperience::getGoodId);
// 封装goodMap(根据商品id查询商品) // 封装goodMap(根据商品id查询商品)
Map<Long, Good> goodMap = new HashMap<>(); Map<Long, Good> goodMap = new HashMap<>();

View File

@ -88,7 +88,7 @@ public class CartRecordController {
userService.getLoginUser(request); userService.getLoginUser(request);
// 获取购物车列表 // 获取购物车列表
List<CartRecord> cartRecordList = commonService.getItemsByIds(cartRecordUpdateRequestList, cartRecordService, CartRecordUpdateRequest::getId); List<CartRecord> cartRecordList = commonService.findByOtherIds(cartRecordUpdateRequestList, cartRecordService, CartRecordUpdateRequest::getId);
// 封装map集合购物车id, 商品数量 // 封装map集合购物车id, 商品数量
Map<Long, Integer> QuantityMap = new HashMap<>(); Map<Long, Integer> QuantityMap = new HashMap<>();
@ -97,7 +97,7 @@ public class CartRecordController {
} }
// 获取商品列表 // 获取商品列表
List<Good> goodList = commonService.getItemsByIds(cartRecordList, goodService, CartRecord::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartRecordList, goodService, CartRecord::getGoodId);
// 封装map集合商品id, 商品价格 // 封装map集合商品id, 商品价格
Map<Long, BigDecimal> priceMap = new HashMap<>(); Map<Long, BigDecimal> priceMap = new HashMap<>();
@ -200,10 +200,10 @@ public class CartRecordController {
userService.getLoginUser(request); userService.getLoginUser(request);
List<Long> cartRecordIds = commonBatchRequest.getIdList(); List<Long> cartRecordIds = commonBatchRequest.getIdList();
// 根据id列表获取购物车信息列表 // 根据id列表获取购物车信息列表
List<CartRecord> cartRecords = commonService.getListByIds(cartRecordIds, cartRecordService); List<CartRecord> cartRecords = commonService.findByMyIds(cartRecordIds, cartRecordService);
// 提取出购物车中的商品列表 // 提取出购物车中的商品列表
List<Good> goodList = commonService.getItemsByIds(cartRecords, goodService, CartRecord::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartRecords, goodService, CartRecord::getGoodId);
// 封装goodMap(根据商品id查询商品) // 封装goodMap(根据商品id查询商品)
Map<Long, Good> goodMap = new HashMap<>(); Map<Long, Good> goodMap = new HashMap<>();

View File

@ -130,7 +130,7 @@ public class CategoryController {
} }
List<Long> idList = commonDelBatchRequest.getIdList(); List<Long> idList = commonDelBatchRequest.getIdList();
// 批量删除当前类别下的所有商品 // 批量删除当前类别下的所有商品
List<Category> categoryList = commonService.getListByIds(idList, categoryService); List<Category> categoryList = commonService.findByMyIds(idList, categoryService);
List<String> typeNameList = categoryList.stream().map(Category::getTypeName).toList(); List<String> typeNameList = categoryList.stream().map(Category::getTypeName).toList();
QueryWrapper<Good> queryWrapper = new QueryWrapper<>(); QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
queryWrapper.in("type", typeNameList); queryWrapper.in("type", typeNameList);

View File

@ -11,4 +11,5 @@ public interface PhotoCategoryService extends IService<PhotoCategory> {
*/ */
void validPhotoCategory(PhotoCategory photoCategory, boolean update); void validPhotoCategory(PhotoCategory photoCategory, boolean update);
} }

View File

@ -18,7 +18,7 @@ public class PhotoCategoryServiceImpl extends ServiceImpl<PhotoCategoryMapper, P
@Resource @Resource
private PhotoProductsService clothesInfoService; private PhotoProductsService photoProductsService;
/** /**
@ -52,4 +52,5 @@ public class PhotoCategoryServiceImpl extends ServiceImpl<PhotoCategoryMapper, P
} }

View File

@ -3,6 +3,7 @@ package com.cultural.heritage.service.common;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
public interface CommonService { public interface CommonService {
@ -11,14 +12,14 @@ public interface CommonService {
/** /**
* 取出一个集合的某个id列表, 去另外一个集合中筛选出属于这个id列表的集合 * 取出一个集合的某个id列表, 去另外一个集合中筛选出属于这个id列表的集合
*/ */
<T, R> List<R> getItemsByIds(List<T> records, IService<R> genericService, Function<T, Long> getId); <T, R> List<R> findByOtherIds(List<T> sourceList, IService<R> genericService, Function<T, Long> getId);
/** /**
* 根据 ID 列表获取对应的实体列表 * 根据 ID 列表获取对应的实体列表
*/ */
<T> List<T> getListByIds(List<Long> ids, IService<T> genericService); <T> List<T> findByMyIds(List<Long> ids, IService<T> genericService);
@ -31,5 +32,47 @@ public interface CommonService {
* @param <T> 目标查询实体类型 * @param <T> 目标查询实体类型
* @return 查询结果集合 * @return 查询结果集合
*/ */
<T> List<T> findListByField(List<?> sourceList, IService<T> service, String sourceField, String targetField); <T> List<T> findBySourceFieldValues(List<?> sourceList, IService<T> service, String sourceField, String targetField);
/**
* 根据指定字段名和值使用给定的服务对象查询对应的实体类列表
* @param <T> 实体类类型
* @param fieldName 查询字段的名称
* @param fieldValue 查询字段的值
* @param service 用于执行查询的服务层对象
* @return 返回符合条件的实体类列表`List<T>`如果没有符合条件的记录返回空列表
*/
<T> List<T> findByFieldValue(String fieldName, Object fieldValue, IService<T> service);
/**
* 根据多个字段和对应的值进行查询
* 该方法可以动态构建查询条件并执行查询
*
* @param fieldConditions 查询条件的字段和值使用Map存储
* @param service 执行查询操作的服务对象通常是MyBatis-Plus的 IService
* @return 返回查询结果的列表
*/
<T> List<T> findByMultipleFieldValues(Map<String, Object> fieldConditions, IService<T> service);
/**
* 将一个类型的 List 转换为另一个类型的 List
* @param sourceList 源列表类型为 T
* @param targetClass 目标类型的 Class 对象
* @param <T> 源类型
* @param <R> 目标类型
* @return 转换后的目标类型列表
*/
<T, R> List<R> convertList(List<T> sourceList, Class<R> targetClass);
} }

View File

@ -3,10 +3,12 @@ package com.cultural.heritage.service.common.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.common.CommonService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -18,12 +20,15 @@ public class CommonServiceImpl implements CommonService {
* 取出一个集合的某个id列表, 去另外一个集合中筛选出属于这个id列表的集合 * 取出一个集合的某个id列表, 去另外一个集合中筛选出属于这个id列表的集合
*/ */
@Override @Override
public <T, R> List<R> getItemsByIds(List<T> records, IService<R> genericService, Function<T, Long> getId) { public <T, R> List<R> findByOtherIds(List<T> sourceList, IService<R> genericService, Function<T, Long> getId) {
// 提取ID // 提取ID
List<Long> ids = records.stream() List<Long> ids = sourceList.stream()
.map(getId) // 提取每个元素的ID .map(getId) // 提取每个元素的ID
.collect(Collectors.toList()); .collect(Collectors.toList());
if (ids.isEmpty()) {
return List.of(); // 返回空集合
}
// 构造查询条件 // 构造查询条件
QueryWrapper<R> queryWrapper = new QueryWrapper<>(); QueryWrapper<R> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", ids); queryWrapper.in("id", ids);
@ -37,7 +42,10 @@ public class CommonServiceImpl implements CommonService {
* 根据 ID 列表获取对应的实体列表 * 根据 ID 列表获取对应的实体列表
*/ */
@Override @Override
public <T> List<T> getListByIds(List<Long> ids, IService<T> genericService) { public <T> List<T> findByMyIds(List<Long> ids, IService<T> genericService) {
if (ids.isEmpty()) {
return List.of(); // 返回空集合
}
// 构造查询条件 // 构造查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>(); QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.in("id", ids); // 使用 IN 条件 queryWrapper.in("id", ids); // 使用 IN 条件
@ -59,7 +67,7 @@ public class CommonServiceImpl implements CommonService {
* @return 查询结果集合 * @return 查询结果集合
*/ */
@Override @Override
public <T> List<T> findListByField(List<?> sourceList, IService<T> service, String sourceField, String targetField) { public <T> List<T> findBySourceFieldValues(List<?> sourceList, IService<T> service, String sourceField, String targetField) {
// 使用反射获取源集合中对应字段的值 // 使用反射获取源集合中对应字段的值
List<Object> fieldValues = sourceList.stream() List<Object> fieldValues = sourceList.stream()
.map(item -> getFieldValue(item, sourceField)) // 获取字段值 .map(item -> getFieldValue(item, sourceField)) // 获取字段值
@ -76,6 +84,8 @@ public class CommonServiceImpl implements CommonService {
return service.list(queryWrapper); // 执行查询并返回结果 return service.list(queryWrapper); // 执行查询并返回结果
} }
/** /**
* 使用反射获取对象的字段值 * 使用反射获取对象的字段值
* @param object 对象 * @param object 对象
@ -93,4 +103,81 @@ public class CommonServiceImpl implements CommonService {
} }
/**
* 根据指定字段名和值使用给定的服务对象查询对应的实体类列表
* @param <T> 实体类类型
* @param fieldName 查询字段的名称
* @param fieldValue 查询字段的值
* @param service 用于执行查询的服务层对象
*
* @return 返回符合条件的实体类列表`List<T>`如果没有符合条件的记录返回空列表
*/
@Override
public <T> List<T> findByFieldValue(String fieldName, Object fieldValue, IService<T> service) {
// 创建 QueryWrapper动态构建查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(fieldName, fieldValue); // 设置等值查询条件
// 执行查询
return service.list(queryWrapper);
}
/**
* 根据多个字段和对应的值进行查询
* 该方法根据输入的查询条件动态构建查询并执行数据库查询
* @param fieldConditions 查询条件的字段和值使用Map存储
* @param service 执行查询操作的服务对象通常是MyBatis-Plus的 IService
* @return 返回查询结果的列表
*/
@Override
public <T> List<T> findByMultipleFieldValues(Map<String, Object> fieldConditions, IService<T> service) {
// 创建 QueryWrapper动态构建查询条件
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
// 遍历传入的条件Map逐个设置查询条件
for (Map.Entry<String, Object> entry : fieldConditions.entrySet()) {
// 设置等值查询条件
queryWrapper.eq(entry.getKey(), entry.getValue());
}
// 执行查询并返回结果
return service.list(queryWrapper);
}
/**
* 将源列表 List<T> 转换为目标类型的列表 List<R>
* @param sourceList 源列表包含 T 类型的对象
* @param targetClass 目标类型的 Class 对象
* @param <T> 源类型
* @param <R> 目标类型
* @return 转换后的目标类型的 List
*/
@Override
public <T, R> List<R> convertList(List<T> sourceList, Class<R> targetClass) {
// 使用 Stream 流式操作来遍历源列表并将每个元素转换为目标类型
return sourceList.stream()
.map(source -> {
try {
// 通过反射创建目标类型的实例
R target = targetClass.getDeclaredConstructor().newInstance();
// 使用 BeanUtils.copyProperties 复制属性
BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
// 捕获异常并抛出运行时异常
throw new RuntimeException("Error copying properties", e);
}
})
.collect(Collectors.toList()); // 将转换后的对象收集到目标类型的列表中
}
} }

View File

@ -233,7 +233,7 @@ public class CartExperienceServiceImpl extends ServiceImpl<CartExperienceMapper,
@Override @Override
public List<Long> getInvalidCartIds(List<CartExperience> cartExperienceList) { public List<Long> getInvalidCartIds(List<CartExperience> cartExperienceList) {
// 获取购物车的商品id列表 // 获取购物车的商品id列表
List<Good> goodList = commonService.getItemsByIds(cartExperienceList, goodService, CartExperience::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartExperienceList, goodService, CartExperience::getGoodId);
// 封装map集合商品id, 商品详情信息 // 封装map集合商品id, 商品详情信息
Map<Long, Good> map = new HashMap<>(); Map<Long, Good> map = new HashMap<>();
for (Good good : goodList) { for (Good good : goodList) {

View File

@ -179,7 +179,7 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
map.put(cartOrderItemAddRequest.getCartRecordId(), cartOrderItemAddRequest.getQuantity()); map.put(cartOrderItemAddRequest.getCartRecordId(), cartOrderItemAddRequest.getQuantity());
} }
// 修改购物车商品项的购买数量 // 修改购物车商品项的购买数量
List<CartRecord> cartRecordList = commonService.getItemsByIds(cartOrderItemAddRequestList, this, CartOrderItemAddRequest::getCartRecordId); List<CartRecord> cartRecordList = commonService.findByOtherIds(cartOrderItemAddRequestList, this, CartOrderItemAddRequest::getCartRecordId);
for (CartRecord cartRecord : cartRecordList) { for (CartRecord cartRecord : cartRecordList) {
Integer quantity = map.get(cartRecord.getId()); Integer quantity = map.get(cartRecord.getId());
cartRecord.setQuantity(quantity); cartRecord.setQuantity(quantity);
@ -195,7 +195,7 @@ public class CartRecordServiceImpl extends ServiceImpl<CartRecordMapper, CartRec
@Override @Override
public List<Long> getInvalidCartIds(List<CartRecord> cartRecords) { public List<Long> getInvalidCartIds(List<CartRecord> cartRecords) {
// 获取购物车的商品列表 // 获取购物车的商品列表
List<Good> goodList = commonService.getItemsByIds(cartRecords, goodService, CartRecord::getGoodId); List<Good> goodList = commonService.findByOtherIds(cartRecords, goodService, CartRecord::getGoodId);
// 封装map集合商品id, 商品详情信息 // 封装map集合商品id, 商品详情信息
Map<Long, Good> map = new HashMap<>(); Map<Long, Good> map = new HashMap<>();
for (Good good : goodList) { for (Good good : goodList) {