From f3c39590ef109149491864c52ae973fc5e4d14c6 Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Sat, 29 Mar 2025 10:22:17 +0800 Subject: [PATCH] this is 3.29 update --- .../good/AppointmentDateController.java | 22 +++++----- .../controller/good/GoodController.java | 33 +++++++++----- .../order/PendingServiceGoodController.java | 5 +++ .../cultural/heritage/utils/DynamicSort.java | 43 +++++++++++++++++++ 4 files changed, 82 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/cultural/heritage/utils/DynamicSort.java diff --git a/src/main/java/com/cultural/heritage/controller/good/AppointmentDateController.java b/src/main/java/com/cultural/heritage/controller/good/AppointmentDateController.java index 93bddb6..cde9bbb 100644 --- a/src/main/java/com/cultural/heritage/controller/good/AppointmentDateController.java +++ b/src/main/java/com/cultural/heritage/controller/good/AppointmentDateController.java @@ -15,10 +15,7 @@ import com.cultural.heritage.model.dto.appointment.single.AppointmentDateSingleA import com.cultural.heritage.model.dto.timePeriod.TimePeriodAddRequest; import com.cultural.heritage.model.dto.timePeriod.single.TimePeriodSingleAddRequest; import com.cultural.heritage.model.dto.timePeriod.single.TimePeriodSingleUpdateRequest; -import com.cultural.heritage.model.entity.AppointmentDate; -import com.cultural.heritage.model.entity.Good; -import com.cultural.heritage.model.entity.PendingServiceGood; -import com.cultural.heritage.model.entity.TimePeriod; +import com.cultural.heritage.model.entity.*; import com.cultural.heritage.model.vo.appointment.AppointmentDateTimePeriodVO; import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.good.AppointmentDateService; @@ -136,15 +133,18 @@ public class AppointmentDateController { // 删除服务类商品待处理记录 QueryWrapper goodQueryWrapper = new QueryWrapper<>(); goodQueryWrapper.eq("timePeriodId", id); -// List pendingServiceGoodList = pendingServiceGoodService.list(goodQueryWrapper); -// PendingServiceGood pendingServiceGood = pendingServiceGoodList.get(0); -// Long pendingId = pendingServiceGood.getId(); -// QueryWrapper queryWrapper = new QueryWrapper<>(); -// queryWrapper.eq("pendingId", pendingId); - boolean result = pendingServiceGoodService.remove(goodQueryWrapper); - ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品待处理记录删除失败"); + List pendingServiceGoodList = pendingServiceGoodService.list(goodQueryWrapper); + PendingServiceGood pendingServiceGood = pendingServiceGoodList.get(0); + Long pendingId = pendingServiceGood.getId(); + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("pendingId", pendingId); + long count = pendingServiceOrderService.count(queryWrapper); + if (count == 0) { + boolean result = pendingServiceGoodService.remove(goodQueryWrapper); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品待处理记录删除失败"); + } return ResultUtils.success(true); } diff --git a/src/main/java/com/cultural/heritage/controller/good/GoodController.java b/src/main/java/com/cultural/heritage/controller/good/GoodController.java index b1d3585..b66f28a 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -38,6 +38,7 @@ import com.cultural.heritage.model.vo.timePeriod.TimePeriodVO; import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.good.*; import com.cultural.heritage.service.order.PendingServiceGoodService; +import com.cultural.heritage.service.order.PendingServiceOrderService; import com.cultural.heritage.service.user.UserService; import com.cultural.heritage.utils.DecoderUtils; import io.swagger.v3.oas.annotations.Operation; @@ -99,6 +100,10 @@ public class GoodController { private FestivalService festivalService; + @Resource + private PendingServiceOrderService pendingServiceOrderService; + + /** * 小程序端校验常规类商品立即购买 @@ -299,10 +304,8 @@ public class GoodController { // 删除预约时间段表中与该商品关联的记录 timePeriodService.removeBatchByIds(timePeriodList); -// ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "服务类商品预约时间段删除失败"); // 删除预约日期表中与该商品关联的记录 appointmentDateService.removeBatchByIds(appointmentDateList); -// ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR, "服务类商品预约日期删除失败"); // 删除商品表中的服务类商品 boolean result = goodService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品删除失败"); @@ -310,7 +313,15 @@ public class GoodController { // 删除服务类商品待处理记录 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("goodId", id); - boolean removeBatch = pendingServiceGoodService.remove(queryWrapper); + + List pendingServiceGoodList = pendingServiceGoodService.list(queryWrapper); + List pendingServiceOrderList = pendingServiceOrderService.list(); + Map map = new HashMap<>(); + for (PendingServiceOrder pendingServiceOrder : pendingServiceOrderList) { + map.put(pendingServiceOrder.getPendingId(), 1); + } + pendingServiceGoodList = pendingServiceGoodList.stream().filter(pendingServiceGood -> map.get(pendingServiceGood.getId()) == null).toList(); + boolean removeBatch = pendingServiceGoodService.removeBatchByIds(pendingServiceGoodList); ThrowUtils.throwIf(!removeBatch, ErrorCode.OPERATION_ERROR, "服务类商品待处理记录删除失败"); return ResultUtils.success(true); @@ -319,9 +330,6 @@ public class GoodController { } - - - /** * Web端用户批量删除服务类商品 * @param commonBatchRequest 服务类商品id列表 @@ -342,10 +350,8 @@ public class GoodController { List timePeriodList = commonService.findByFieldInTargetField(appointmentDateList, timePeriodService, AppointmentDate::getId, "appointmentDateId"); //删除预约时间段表中与该商品关联的记录 timePeriodService.removeBatchByIds(timePeriodList); -// ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "服务类商品预约时间段删除失败"); // 删除预约日期表中与该商品关联的记录 appointmentDateService.removeBatchByIds(appointmentDateList); -// ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "服务类商品预约日期删除失败"); // 删除商品表中的服务类商品 boolean result = goodService.removeBatchByIds(ids); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品删除失败"); @@ -353,8 +359,15 @@ public class GoodController { // 批量删除服务类商品待处理记录 QueryWrapper goodQueryWrapper = new QueryWrapper<>(); goodQueryWrapper.in("goodId", ids); - boolean removeBatch = pendingServiceGoodService.remove(goodQueryWrapper); - ThrowUtils.throwIf(!removeBatch, ErrorCode.OPERATION_ERROR, "服务类商品待处理记录批量删除失败"); + List pendingServiceGoodList = pendingServiceGoodService.list(goodQueryWrapper); + List pendingServiceOrderList = pendingServiceOrderService.list(); + Map map = new HashMap<>(); + for (PendingServiceOrder pendingServiceOrder : pendingServiceOrderList) { + map.put(pendingServiceOrder.getPendingId(), 1); + } + pendingServiceGoodList = pendingServiceGoodList.stream().filter(pendingServiceGood -> map.get(pendingServiceGood.getId()) == null).toList(); + boolean removeBatch = pendingServiceGoodService.removeBatchByIds(pendingServiceGoodList); + ThrowUtils.throwIf(!removeBatch, ErrorCode.OPERATION_ERROR, "服务类商品待处理记录删除失败"); return ResultUtils.success(true); diff --git a/src/main/java/com/cultural/heritage/controller/order/PendingServiceGoodController.java b/src/main/java/com/cultural/heritage/controller/order/PendingServiceGoodController.java index 43027ae..588e36a 100644 --- a/src/main/java/com/cultural/heritage/controller/order/PendingServiceGoodController.java +++ b/src/main/java/com/cultural/heritage/controller/order/PendingServiceGoodController.java @@ -20,11 +20,13 @@ import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.order.PendingServiceGoodService; import com.cultural.heritage.service.order.PendingServiceOrderService; import com.cultural.heritage.service.user.UserService; +import com.cultural.heritage.utils.DynamicSort; import com.cultural.heritage.utils.Pagination; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; @@ -106,11 +108,14 @@ public class PendingServiceGoodController { long current = bookingOrderQueryRequest.getCurrent(); long pageSize = bookingOrderQueryRequest.getPageSize(); + String sortOrder = bookingOrderQueryRequest.getSortOrder(); + String sortField = bookingOrderQueryRequest.getSortField(); long total = pendingServiceGoodVOList.size(); long pages = (total + pageSize - 1) / pageSize; Page voPage = new Page<>(); pendingServiceGoodVOList = Pagination.paginate(pendingServiceGoodVOList, current, pageSize); + if (!StringUtils.isAnyBlank(sortField, sortField)) DynamicSort.sortList(pendingServiceGoodVOList, sortField, sortOrder); voPage.setRecords(pendingServiceGoodVOList); voPage.setTotal(total); voPage.setSize(pageSize); diff --git a/src/main/java/com/cultural/heritage/utils/DynamicSort.java b/src/main/java/com/cultural/heritage/utils/DynamicSort.java new file mode 100644 index 0000000..77f775e --- /dev/null +++ b/src/main/java/com/cultural/heritage/utils/DynamicSort.java @@ -0,0 +1,43 @@ +package com.cultural.heritage.utils; + +import java.lang.reflect.Method; +import java.util.*; + +public class DynamicSort { + + public static List sortList(List sourceList, String sortField, String sortOrder) { + // 使用 Comparator 来定义排序逻辑 + Comparator comparator = (T o1, T o2) -> { + try { + // 通过反射获取字段的 getter 方法 + Method getter = o1.getClass().getMethod("get" + capitalize(sortField)); + Comparable value1 = (Comparable) getter.invoke(o1); + Comparable value2 = (Comparable) getter.invoke(o2); + + // 根据 sortOrder 决定是升序还是降序 + if ("ascend".equalsIgnoreCase(sortOrder)) { + return value1.compareTo(value2); + } else { + return value2.compareTo(value1); + } + } catch (Exception e) { + e.printStackTrace(); + } + return 0; + }; + + // 使用 Collections.sort() 排序 + sourceList.sort(comparator); + return sourceList; + } + + // 将字段名首字母大写,生成 getter 方法的名称 + private static String capitalize(String str) { + if (str == null || str.isEmpty()) { + return str; + } + return str.substring(0, 1).toUpperCase() + str.substring(1); + } + + +}