From 8026b76afecc5269fac490482c3504f0810fd19e Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Mon, 17 Feb 2025 20:05:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BA=86=E5=86=99=E7=9C=9F?= =?UTF-8?q?=E9=A2=84=E7=BA=A6=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../good/CartExperienceController.java | 4 +- .../controller/good/CartRecordController.java | 4 +- .../controller/good/CategoryController.java | 38 +++------ .../controller/good/CouponController.java | 46 +++++------ .../controller/good/GoodController.java | 77 ++++++++----------- 5 files changed, 65 insertions(+), 104 deletions(-) diff --git a/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java index 1ed7aab..7ee676e 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartExperienceController.java @@ -196,9 +196,7 @@ public class CartExperienceController { public BaseResponse> listInvalidCartIds (HttpServletRequest request) { User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("userId", userId); - List cartExperienceList = cartExperienceService.list(queryWrapper); + List cartExperienceList = commonService.findByFieldEqTargetField("userId", userId, cartExperienceService); // 找出存在问题的购物车id列表 List invalidCartIds = cartExperienceService.getInvalidCartIds(cartExperienceList); return ResultUtils.success(invalidCartIds); diff --git a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java index 4a79ccc..dabd624 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CartRecordController.java @@ -174,9 +174,7 @@ public class CartRecordController { public BaseResponse> listInvalidCartIds (HttpServletRequest request) { User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("userId", userId); - List cartRecords = cartRecordService.list(queryWrapper); + List cartRecords = commonService.findByFieldEqTargetField("userId", userId, cartRecordService); // 找出存在问题的购物车id列表 List invalidCartIds = cartRecordService.getInvalidCartIds(cartRecords); return ResultUtils.success(invalidCartIds); diff --git a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java index 1b6ea58..f4ab089 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CategoryController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CategoryController.java @@ -99,15 +99,11 @@ public class CategoryController { ThrowUtils.throwIf(category == null, ErrorCode.OPERATION_ERROR, "当前类别不存在"); String typeName = category.getTypeName(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("type", typeName); - List goodList = goodService.list(queryWrapper); + List goodList = commonService.findByFieldEqTargetField("type", typeName, goodService); + + boolean isSuccess = goodService.removeBatchByIds(goodList); + ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR, "商品批量删除失败"); - // 如果该类别下有商品,就删除 - if (!goodList.isEmpty()) { - boolean isSuccess = goodService.removeBatchByIds(goodList); - ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR, "商品批量删除失败"); - } // 删除当前类别 boolean result = categoryService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "类别删除失败"); @@ -131,16 +127,11 @@ public class CategoryController { List idList = commonDelBatchRequest.getIdList(); // 批量删除当前类别下的所有商品 List categoryList = commonService.findByFieldInTargetField(idList, categoryService, id -> id, "id"); - List typeNameList = categoryList.stream().map(Category::getTypeName).toList(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("type", typeNameList); - List goodList = goodService.list(queryWrapper); + List goodList = commonService.findByFieldInTargetField(categoryList, goodService, Category::getTypeName, "type"); - // 如果该类别下有商品,就删除 - if (!goodList.isEmpty()) { - boolean result = goodService.removeBatchByIds(goodList); - ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "商品批量删除失败"); - } + // 批量删除商品 + boolean result = goodService.removeBatchByIds(goodList); + ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "商品批量删除失败"); // 批量删除当前类别 boolean remove = categoryService.removeBatchByIds(categoryList); @@ -215,15 +206,10 @@ public class CategoryController { @Operation(summary = "小程序端用户查询商品类别", description = "参数:无,权限:所有人,方法名:listCategory") public BaseResponse> listCategory(HttpServletRequest request) { userService.getLoginUser(request); - QueryWrapper goodQueryWrapper = new QueryWrapper<>(); - goodQueryWrapper.eq("isShelves", 1); - List goodList = goodService.list(goodQueryWrapper); - List typeNameList = goodList.stream().map(Good::getType).toList(); - - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("typeName", typeNameList); - List categoryList = categoryService.list(queryWrapper); - + // 获取所有已上架的商品 + List goodList = commonService.findByFieldEqTargetField("isShelves", 1, goodService); + // 获取所有存在上架商品的商品类别 + List categoryList = commonService.findByFieldInTargetField(goodList, categoryService, Good::getType, "typeName"); return ResultUtils.success(categoryList); } diff --git a/src/main/java/com/cultural/heritage/controller/good/CouponController.java b/src/main/java/com/cultural/heritage/controller/good/CouponController.java index 889f810..2a6a91a 100644 --- a/src/main/java/com/cultural/heritage/controller/good/CouponController.java +++ b/src/main/java/com/cultural/heritage/controller/good/CouponController.java @@ -24,6 +24,7 @@ import com.cultural.heritage.model.entity.User; import com.cultural.heritage.model.entity.UserCoupon; import com.cultural.heritage.model.vo.coupon.CouponVO; import com.cultural.heritage.model.vo.coupon.UserCouponVO; +import com.cultural.heritage.service.common.CommonService; import com.cultural.heritage.service.good.CouponService; import com.cultural.heritage.service.good.UserCouponService; import com.cultural.heritage.service.user.UserService; @@ -39,7 +40,9 @@ import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; +import java.util.HashMap; import java.util.List; +import java.util.Map; @RestController @@ -60,6 +63,10 @@ public class CouponController { private UserCouponService userCouponService; + @Resource + private CommonService commonService; + + /** @@ -132,7 +139,6 @@ public class CouponController { boolean result = couponService.saveOrUpdate(coupon); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "优惠券更新失败"); - // 向消息队列中发送优惠券创建的消息 couponService.sendCouponCreateMessage(coupon); @@ -193,7 +199,6 @@ public class CouponController { if (couponQueryRequest == null) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - // 对优惠券是否过期预处理 long current = couponQueryRequest.getCurrent(); long pageSize = couponQueryRequest.getPageSize(); @@ -270,17 +275,13 @@ public class CouponController { } User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("userId", userId); - queryWrapper.eq("isUsed", 0); - List userCouponList = userCouponService.list(); + Map fieldConditions = new HashMap<>(); + fieldConditions.put("userId", userId); + fieldConditions.put("isUsed", 0); + List userCouponList = commonService.findByFieldEqTargetFields(fieldConditions, userCouponService); userCouponList = userCouponList.stream().filter(userCoupon -> userCoupon.getCouponVO().getStatus().equals(status)).toList(); - List userCouponVOS = userCouponList.stream().map(userCoupon -> { - UserCouponVO userCouponVO = new UserCouponVO(); - BeanUtils.copyProperties(userCoupon, userCouponVO); - return userCouponVO; - }).toList(); + List userCouponVOS = commonService.convertList(userCouponList, UserCouponVO.class); return ResultUtils.success(userCouponVOS); } @@ -296,11 +297,7 @@ public class CouponController { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("status", "可用"); List coupons = couponService.list(queryWrapper); - List couponVOS = coupons.stream().map(coupon -> { - CouponVO couponVO = new CouponVO(); - BeanUtils.copyProperties(coupon, couponVO); - return couponVO; - }).toList(); + List couponVOS = commonService.convertList(coupons, CouponVO.class); return ResultUtils.success(couponVOS); } @@ -372,11 +369,11 @@ public class CouponController { } User loginUser = userService.getLoginUser(request); Long userId = loginUser.getId(); - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.eq("userId", userId); - queryWrapper.eq("isUsed", 0); - queryWrapper.eq("status", "可用"); - List userCouponList = userCouponService.list(queryWrapper); + Map fieldConditions = new HashMap<>(); + fieldConditions.put("userId", userId); + fieldConditions.put("isUsed", 0); + fieldConditions.put("status", "可用"); + List userCouponList = commonService.findByFieldEqTargetFields(fieldConditions, userCouponService); userCouponList = userCouponList.stream().filter(userCoupon -> userCoupon.getCouponVO().getStatus().equals("可用")).toList(); // 获取(不)可使用的优惠券 @@ -388,12 +385,7 @@ public class CouponController { return isAvailable == (result >= 0); }).toList(); - List userCouponVOS = userCoupons.stream().map(userCoupon -> { - UserCouponVO userCouponVO = new UserCouponVO(); - BeanUtils.copyProperties(userCoupon, userCouponVO); - return userCouponVO; - }).toList(); - + List userCouponVOS = commonService.convertList(userCoupons, UserCouponVO.class); return ResultUtils.success(userCouponVOS); } 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 8e1bf59..3e34444 100644 --- a/src/main/java/com/cultural/heritage/controller/good/GoodController.java +++ b/src/main/java/com/cultural/heritage/controller/good/GoodController.java @@ -32,6 +32,7 @@ import com.cultural.heritage.model.vo.good.GoodPageVO; import com.cultural.heritage.model.vo.good.ServiceGoodCardVO; import com.cultural.heritage.model.vo.good.ServiceGoodVO; 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.user.UserService; @@ -86,6 +87,10 @@ public class GoodController { private PendingServiceGoodService pendingServiceGoodService; + @Resource + private CommonService commonService; + + /** @@ -225,21 +230,18 @@ public class GoodController { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = deleteRequest.getId(); + // 获取预约日期列表 + List appointmentDateList = commonService.findByFieldEqTargetField("goodId", id, appointmentDateService); + + // 获取预约时间段列表 + List timePeriodList = commonService.findByFieldInTargetField(appointmentDateList, timePeriodService, AppointmentDate::getId, "appointmentDateId"); // 删除预约时间段表中与该商品关联的记录 - QueryWrapper dateQueryWrapper = new QueryWrapper<>(); - dateQueryWrapper.eq("goodId", id); - List appointmentDateList = appointmentDateService.list(dateQueryWrapper); - List ids = appointmentDateList.stream().map(AppointmentDate::getId).toList(); - QueryWrapper timePeriodQueryWrapper = new QueryWrapper<>(); - timePeriodQueryWrapper.in("appointmentDateId", ids); - boolean remove = timePeriodService.remove(timePeriodQueryWrapper); + boolean remove = timePeriodService.removeBatchByIds(timePeriodList); ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "服务类商品预约时间段删除失败"); - // 删除预约日期表中与该商品关联的记录 - boolean isSuccess = appointmentDateService.remove(dateQueryWrapper); + boolean isSuccess = appointmentDateService.removeBatchByIds(appointmentDateList); ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR, "服务类商品预约日期删除失败"); - // 删除商品表中的服务类商品 boolean result = goodService.removeById(id); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品删除失败"); @@ -273,25 +275,20 @@ public class GoodController { throw new BusinessException(ErrorCode.PARAMS_ERROR); } List ids = commonBatchRequest.getIdList(); + // 获取预约日期列表 + List appointmentDateList = commonService.findByFieldEqTargetField("goodId", ids, appointmentDateService); + // 获取预约时间段列表 + List timePeriodList = commonService.findByFieldInTargetField(appointmentDateList, timePeriodService, AppointmentDate::getId, "appointmentDateId"); //删除预约时间段表中与该商品关联的记录 - QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("goodId", ids); - List appointmentDateList = appointmentDateService.list(queryWrapper); - List appointmentDateIds = appointmentDateList.stream().map(AppointmentDate::getId).toList(); - QueryWrapper timePeriodQueryWrapper = new QueryWrapper<>(); - timePeriodQueryWrapper.in("appointmentDateId", appointmentDateIds); - boolean success = timePeriodService.remove(timePeriodQueryWrapper); + boolean success = timePeriodService.removeBatchByIds(timePeriodList); ThrowUtils.throwIf(!success, ErrorCode.OPERATION_ERROR, "服务类商品预约时间段删除失败"); - // 删除预约日期表中与该商品关联的记录 - boolean remove = appointmentDateService.remove(queryWrapper); + boolean remove = appointmentDateService.removeBatchByIds(appointmentDateList); ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "服务类商品预约日期删除失败"); - // 删除商品表中的服务类商品 boolean result = goodService.removeBatchByIds(ids); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "服务类商品删除失败"); - // 批量删除服务类商品待处理记录 QueryWrapper goodQueryWrapper = new QueryWrapper<>(); goodQueryWrapper.in("goodId", ids); @@ -316,7 +313,6 @@ public class GoodController { if (goodUpdateRequest == null || goodUpdateRequest.getId() <= 0) { throw new BusinessException(ErrorCode.PARAMS_ERROR); } - // 获取原来的商品价格 Long sourceId = goodUpdateRequest.getId(); Good resourceGood = goodService.getById(sourceId); @@ -357,11 +353,7 @@ public class GoodController { QueryWrapper goodQueryWrapper = goodService.getGoodQueryWrapper(goodQueryRequest, false); Page page = goodService.page(new Page<>(current, pageSize), goodQueryWrapper); List records = page.getRecords(); - List goodPageVOS = records.stream().map(good -> { - GoodPageVO goodPageVO = new GoodPageVO(); - BeanUtils.copyProperties(good, goodPageVO); - return goodPageVO; - }).toList(); + List goodPageVOS = commonService.convertList(records, GoodPageVO.class); Page goodPageVOPage = new Page<>(); goodPageVOPage.setRecords(goodPageVOS); goodPageVOPage.setTotal(page.getTotal()); @@ -384,6 +376,9 @@ public class GoodController { @Operation(summary = "Web端管理员批量删除常规类商品", description = "参数:商品批量删除请求体,权限:管理员(admin, boss),方法名:delBatchGoods") @AuthCheck(mustRole = UserConstant.ADMIN_ROLE) public BaseResponse delBatchGoods(@RequestBody CommonBatchRequest commonDelBatchRequest) { + if (commonDelBatchRequest == null || CollectionUtils.isEmpty(commonDelBatchRequest.getIdList())) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } List idList = commonDelBatchRequest.getIdList(); boolean result = goodService.removeBatchByIds(idList); ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR); @@ -407,7 +402,7 @@ public class GoodController { } Long id = getByIdRequest.getId(); Good good = goodService.getById(id); - ThrowUtils.throwIf(good == null, ErrorCode.NOT_FOUND_ERROR); + ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "商品不存在"); GoodPageVO goodPageVO = new GoodPageVO(); BeanUtils.copyProperties(good, goodPageVO); return ResultUtils.success(goodPageVO); @@ -573,7 +568,6 @@ public class GoodController { AppointmentDateVO appointmentDateVO = dateMap.get(dateId); // 过滤掉没有空闲的预约日期 if (appointmentDateVO.getIsAvailable() == 0) continue; - // 过滤掉已过期的预约时间段 String specificDate = appointmentDateVO.getSpecificDate(); String today = DateUtil.today(); @@ -619,11 +613,7 @@ public class GoodController { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("isGoodType", 0); List goodList = goodService.list(queryWrapper); - List serviceGoodCardVOS = goodList.stream().map(good -> { - ServiceGoodCardVO serviceGoodCardVO = new ServiceGoodCardVO(); - BeanUtils.copyProperties(good, serviceGoodCardVO); - return serviceGoodCardVO; - }).toList(); + List serviceGoodCardVOS = commonService.convertList(goodList, ServiceGoodCardVO.class); return ResultUtils.success(serviceGoodCardVOS); } @@ -674,20 +664,16 @@ public class GoodController { throw new BusinessException(ErrorCode.PARAMS_ERROR); } Long id = commonRequest.getId(); - //删除预约时间段表中与该商品关联的记录 - QueryWrapper dateQueryWrapper = new QueryWrapper<>(); - dateQueryWrapper.eq("goodId", id); - List appointmentDateList = appointmentDateService.list(dateQueryWrapper); - List ids = appointmentDateList.stream().map(AppointmentDate::getId).toList(); - QueryWrapper timePeriodQueryWrapper = new QueryWrapper<>(); - timePeriodQueryWrapper.in("appointmentDateId", ids); - boolean remove = timePeriodService.remove(timePeriodQueryWrapper); + // 获取预约日期列表 + List appointmentDateList = commonService.findByFieldEqTargetField("goodId", id, appointmentDateService); + // 获取预约时间段列表 + List timePeriodList = commonService.findByFieldInTargetField(appointmentDateList, timePeriodService, AppointmentDate::getId, "appointmentDateId"); + // 删除预约时间段表中与该商品关联的记录 + boolean remove = timePeriodService.removeBatchByIds(timePeriodList); ThrowUtils.throwIf(!remove, ErrorCode.OPERATION_ERROR, "服务类商品预约时间段删除失败"); - // 删除预约日期表中与该商品关联的记录 - boolean isSuccess = appointmentDateService.remove(dateQueryWrapper); + boolean isSuccess = appointmentDateService.removeBatchByIds(appointmentDateList); ThrowUtils.throwIf(!isSuccess, ErrorCode.OPERATION_ERROR, "服务类商品预约日期删除失败"); - // 删除服务类商品待处理记录 QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("goodId", id); @@ -765,6 +751,7 @@ public class GoodController { // 获取当前服务类商品的上(下)架状态 Long id = commonRequest.getId(); Good good = goodService.getById(id); + ThrowUtils.throwIf(good == null, ErrorCode.OPERATION_ERROR, "商品不存在"); Integer status = good.getIsShelves() == 0 ? 1 : 0; UpdateWrapper updateWrapper = new UpdateWrapper<>(); updateWrapper.eq("id", id);