From 20e0c1036ebc6d558e57644b770b9c9de8bcf383 Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Thu, 23 Jan 2025 14:16:39 +0800
Subject: [PATCH] refactor(mall): 重构商品相关操作的验证逻辑

---
 src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java |  110 +++++++-----------------------------
 src/main/java/cc/mrbird/febs/common/utils/ValidateEntityUtils.java        |   13 +++-
 src/main/java/cc/mrbird/febs/mall/dto/AddMallGoodsDto.java                |    3 
 3 files changed, 32 insertions(+), 94 deletions(-)

diff --git a/src/main/java/cc/mrbird/febs/common/utils/ValidateEntityUtils.java b/src/main/java/cc/mrbird/febs/common/utils/ValidateEntityUtils.java
index 4833a8e..83a8b90 100644
--- a/src/main/java/cc/mrbird/febs/common/utils/ValidateEntityUtils.java
+++ b/src/main/java/cc/mrbird/febs/common/utils/ValidateEntityUtils.java
@@ -17,6 +17,7 @@
 
     /**
      * 确保指定列的有效性,并返回相应的实体对象
+     *      selectOne方法
      *      此方法用于验证数据库中某列的值是否存在,并返回包含该值的实体对象
      *      如果指定的值不存在或为null,将抛出异常
      *
@@ -54,7 +55,8 @@
             if (columnExtractors != null && columnExtractors.length > 0) {
                 wrapper.select(columnExtractors);
             }
-            wrapper.eq(columnExtractor, valueToCheck);
+            wrapper.eq(columnExtractor, valueToCheck)
+                    .last("limit 1");
 
             // 执行查询并获取结果实体
             entity = queryWrapperExtractor.apply(wrapper);
@@ -85,8 +87,9 @@
 
     /**
      * 确保指定列的有效性,并返回相应的实体对象列表
-     * 此方法用于验证数据库中某列的值是否存在,并返回包含该值的实体对象列表
-     * 如果指定的值不存在或为null,将抛出异常
+     *      selectList方法
+     *      此方法用于验证数据库中某列的值是否存在,并返回包含该值的实体对象列表
+     *      如果指定的值不存在或为null,将抛出异常
      *
      * @param valueToCheck 需要验证的值
      * @param columnExtractor 列值提取器,用于指定需要验证的列
@@ -135,7 +138,8 @@
     }
 
     /**
-     * 确保指定列的值在数据库中是存在的,
+     * 确保指定列的值在数据库中是存在的
+     *      selectOne方法
      *      该方法通过查询数据库来验证给定的列值是否存在如果不存在,则抛出异常
      *
      * @param valueToCheck 需要验证的列值
@@ -183,6 +187,7 @@
 
     /**
      * 确保指定值在数据库中是唯一的
+     *      selectCount方法
      *      该方法通过查询数据库来验证给定的列值是否已经存在,如果存在,则抛出异常,以确保数据的唯一性
      *
      * @param valueToCheck 需要检查的值
diff --git a/src/main/java/cc/mrbird/febs/mall/dto/AddMallGoodsDto.java b/src/main/java/cc/mrbird/febs/mall/dto/AddMallGoodsDto.java
index fdf6ed7..06d4837 100644
--- a/src/main/java/cc/mrbird/febs/mall/dto/AddMallGoodsDto.java
+++ b/src/main/java/cc/mrbird/febs/mall/dto/AddMallGoodsDto.java
@@ -3,15 +3,16 @@
 import io.swagger.annotations.ApiModel;
 import lombok.Data;
 
+import javax.validation.constraints.NotBlank;
 import java.math.BigDecimal;
 import java.util.List;
 
 @Data
 @ApiModel(value = "AddMallGoodsDto", description = "参数接收类")
 public class AddMallGoodsDto {
-
     private String goodsNo;
 
+    @NotBlank(message = "商品名称不能为空")
     private String goodsName;
 
     private List<AddMallGoodsSkuDto> addMallGoodsSkuDtos;
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
index 81b862c..d188de7 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/AdminMallGoodsService.java
@@ -3,6 +3,7 @@
 import cc.mrbird.febs.common.entity.FebsResponse;
 import cc.mrbird.febs.common.entity.QueryRequest;
 import cc.mrbird.febs.common.utils.MallUtils;
+import cc.mrbird.febs.common.utils.ValidateEntityUtils;
 import cc.mrbird.febs.mall.conversion.MallGoodsConversion;
 import cc.mrbird.febs.mall.dto.*;
 import cc.mrbird.febs.mall.entity.*;
@@ -58,22 +59,10 @@
     @Transactional
     public FebsResponse addMallGoods(AddMallGoodsDto addMallGoodsDto) {
         String goodsName = addMallGoodsDto.getGoodsName();
-        if (StrUtil.isEmpty(goodsName)) {
-            return new FebsResponse().fail().message("商品名称不能为空");
-        }
-        Integer mallGoodsByGoodsName = mallGoodsMapper.selectMallGoodsCountByGoodsName(goodsName);
-        if (mallGoodsByGoodsName > 0) {
-            return new FebsResponse().fail().message("商品名称不能重复");
-        }
+        ValidateEntityUtils.ensureUnique(goodsName, MallGoods::getGoodsName, mallGoodsMapper::selectCount, "商品名称不能重复");
         String goodsNo = MallUtils.getOrderNum("PT");
+        ValidateEntityUtils.ensureUnique(goodsNo, MallGoods::getGoodsNo, mallGoodsMapper::selectCount, "商品编号不能重复");
         addMallGoodsDto.setGoodsNo(goodsNo);
-        if (StrUtil.isEmpty(goodsNo)) {
-            return new FebsResponse().fail().message("商品编号不能为空");
-        }
-        Integer mallGoodsByGoodsNo = mallGoodsMapper.selectMallGoodsCountByGoodsNo(goodsNo);
-        if (mallGoodsByGoodsNo > 0) {
-            return new FebsResponse().fail().message("商品编号不能重复");
-        }
         Integer carriageType = addMallGoodsDto.getCarriageType();
         if (addMallGoodsDto.getGoodsType() == 1) {
             if (1 == carriageType) {
@@ -83,13 +72,9 @@
                 }
             } else {
                 Long carriageRuleId = addMallGoodsDto.getCarriageRuleId();
-                MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(carriageRuleId);
-                if (ObjectUtil.isEmpty(mallCarriageRule)) {
-                    return new FebsResponse().fail().message("邮费模板不能为空");
-                }
+                ValidateEntityUtils.ensureColumnValid(carriageRuleId, MallCarriageRule::getId, mallCarriageRuleMapper::selectOne, "邮费模板不能为空");
             }
         }
-
 
         // 付费商品
         if (addMallGoodsDto.getGoodsType() != 2) {
@@ -260,10 +245,7 @@
 
     @Override
     public FebsResponse upMallGoods(Long id) {
-        MallGoods mallGoods = mallGoodsMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoods)) {
-            return new FebsResponse().fail().message("商品不存在,请刷新当前页面");
-        }
+        MallGoods mallGoods = ValidateEntityUtils.ensureColumnReturnEntity(id,MallGoods::getId,mallGoodsMapper::selectOne,"商品不存在,请刷新当前页面");
         mallGoods.setIsSale(MallGoods.ISSALE_STATUS_ENABLE);
         mallGoodsMapper.updateById(mallGoods);
         return new FebsResponse().success();
@@ -271,10 +253,7 @@
 
     @Override
     public FebsResponse downMallGoods(Long id) {
-        MallGoods mallGoods = mallGoodsMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoods)) {
-            return new FebsResponse().fail().message("商品不存在,请刷新当前页面");
-        }
+        MallGoods mallGoods = ValidateEntityUtils.ensureColumnReturnEntity(id,MallGoods::getId,mallGoodsMapper::selectOne,"商品不存在,请刷新当前页面");
         mallGoods.setIsSale(MallGoods.ISSALE_STATUS_DISABLED);
         mallGoodsMapper.updateById(mallGoods);
         return new FebsResponse().success();
@@ -283,14 +262,8 @@
     @Override
     @Transactional
     public FebsResponse delMallGoods(Long id) {
-        MallGoods mallGoods = mallGoodsMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoods)) {
-            return new FebsResponse().fail().message("商品不存在,请刷新当前页面");
-        }
-        Integer isSale = mallGoods.getIsSale();
-        if (MallGoods.ISSALE_STATUS_DISABLED != isSale) {
-            return new FebsResponse().fail().message("请先下架该商品");
-        }
+        MallGoods mallGoods = ValidateEntityUtils.ensureColumnReturnEntity(id,MallGoods::getId,mallGoodsMapper::selectOne,"商品不存在,请刷新当前页面");
+        ValidateEntityUtils.ensureEqual(mallGoods.getIsSale(), MallGoods.ISSALE_STATUS_DISABLED, "请先下架该商品");
 
         mallGoodsStyleMapper.deleteByGoodsId(id);
         mallGoodsSkuMapper.deleteByGoodsId(id);
@@ -598,10 +571,7 @@
 
     @Override
     public FebsResponse goodsHot(Long id) {
-        MallGoods mallGoods = mallGoodsMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoods)) {
-            return new FebsResponse().fail().message("商品不存在,请刷新当前页面");
-        }
+        MallGoods mallGoods = ValidateEntityUtils.ensureColumnReturnEntity(id,MallGoods::getId,mallGoodsMapper::selectOne,"商品不存在,请刷新当前页面");
         mallGoods.setIsHot(MallGoods.ISHOT_STATE_YES);
         mallGoodsMapper.updateById(mallGoods);
         return new FebsResponse().success();
@@ -609,10 +579,7 @@
 
     @Override
     public FebsResponse goodsNotHot(Long id) {
-        MallGoods mallGoods = mallGoodsMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoods)) {
-            return new FebsResponse().fail().message("商品不存在,请刷新当前页面");
-        }
+        MallGoods mallGoods = ValidateEntityUtils.ensureColumnReturnEntity(id,MallGoods::getId,mallGoodsMapper::selectOne,"商品不存在,请刷新当前页面");
         mallGoods.setIsHot(MallGoods.ISHOT_STATE_NO);
         mallGoodsMapper.updateById(mallGoods);
         return new FebsResponse().success();
@@ -658,10 +625,7 @@
     @Override
     @Transactional
     public FebsResponse carriageRuleDelById(Long id) {
-        MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(id);
-        if(ObjectUtil.isEmpty(mallCarriageRule)){
-            return new FebsResponse().fail().message("操作失败");
-        }
+        ValidateEntityUtils.ensureColumnValid(id, MallCarriageRule::getId, mallCarriageRuleMapper::selectOne, "模板不存在");
         List<MallGoods> mallGoods = this.baseMapper.selectMallGoodsListQueryByRuleId(id);
         if(CollUtil.isNotEmpty(mallGoods)){
             StringBuffer goodsNameBuffer = new StringBuffer();
@@ -683,12 +647,7 @@
     @Transactional
     public FebsResponse carriageRuleAdd(AdminCarriageRuleAddDto adminCarriageRuleAddDto) {
         String ruleName = adminCarriageRuleAddDto.getRuleName();
-        QueryWrapper<MallCarriageRule> objectQueryWrapper = new QueryWrapper<>();
-        objectQueryWrapper.eq("name",ruleName);
-        List<MallCarriageRule> mallCarriageRules = mallCarriageRuleMapper.selectList(objectQueryWrapper);
-        if(CollUtil.isNotEmpty(mallCarriageRules)){
-            return new FebsResponse().fail().message("标题不能重复");
-        }
+        ValidateEntityUtils.ensureUnique(ruleName,MallCarriageRule::getName,mallCarriageRuleMapper::selectCount,"标题不能重复");
 
         MallCarriageRule mallCarriageRule = new MallCarriageRule();
         mallCarriageRule.setName(ruleName);
@@ -775,10 +734,7 @@
     @Transactional
     public FebsResponse carriageRuleUpdate(AdminCarriageRuleUpdateDto adminCarriageRuleUpdateDto) {
         Long id = adminCarriageRuleUpdateDto.getId();
-        MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(id);
-        if(ObjectUtil.isEmpty(mallCarriageRule)){
-            return new FebsResponse().fail().message("运费模板不存在");
-        }
+        MallCarriageRule mallCarriageRule = ValidateEntityUtils.ensureColumnReturnEntity(id,MallCarriageRule::getId,mallCarriageRuleMapper::selectOne,"运费模板不存在");
 
         List<MallGoods> mallGoods = this.baseMapper.selectMallGoodsListQueryByRuleIdAndIsSale(id,MallGoods.ISHOT_STATE_YES);
         if(CollUtil.isNotEmpty(mallGoods)){
@@ -854,14 +810,7 @@
     @Override
     public FebsResponse couponRuleAdd(CouponRuleAddDto couponRuleAddDto) {
         String name = couponRuleAddDto.getName();
-        List<MallGoodsCoupon> mallGoodsCoupons = mallGoodsCouponMapper.selectList(
-                new LambdaQueryWrapper<MallGoodsCoupon>()
-                        .select(MallGoodsCoupon::getId)
-                        .eq(MallGoodsCoupon::getName,name)
-        );
-        if(CollUtil.isNotEmpty(mallGoodsCoupons)){
-            return new FebsResponse().fail().message("优惠卷名称重复了");
-        }
+        ValidateEntityUtils.ensureUnique(name,MallGoodsCoupon::getName,mallGoodsCouponMapper::selectCount,"优惠卷名称重复了");
 
         MallGoodsCoupon mallGoodsCoupon = new MallGoodsCoupon();
         mallGoodsCoupon.setName(name);
@@ -895,24 +844,15 @@
 
     @Override
     public FebsResponse couponRuleDel(Long id) {
-        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoodsCoupon)) {
-            return new FebsResponse().fail().message("优惠卷不存在,请刷新当前页面");
-        }
-        Integer state = mallGoodsCoupon.getState();
-        if(2 == state){
-            return new FebsResponse().fail().message("请先下架优惠卷");
-        }
+        MallGoodsCoupon mallGoodsCoupon = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
+        ValidateEntityUtils.ensureNotEqual(mallGoodsCoupon.getState(),2,"优惠卷已上架,请先下架");
         mallGoodsCouponMapper.deleteById(id);
         return new FebsResponse().success();
     }
 
     @Override
     public FebsResponse upCoupon(Long id) {
-        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoodsCoupon)) {
-            return new FebsResponse().fail().message("优惠卷不存在,请刷新当前页面");
-        }
+        MallGoodsCoupon mallGoodsCoupon = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
         mallGoodsCoupon.setState(2);
         mallGoodsCouponMapper.updateById(mallGoodsCoupon);
         return new FebsResponse().success();
@@ -920,10 +860,7 @@
 
     @Override
     public FebsResponse downCoupon(Long id) {
-        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoodsCoupon)) {
-            return new FebsResponse().fail().message("优惠卷不存在,请刷新当前页面");
-        }
+        MallGoodsCoupon mallGoodsCoupon = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
         mallGoodsCoupon.setState(1);
         mallGoodsCouponMapper.updateById(mallGoodsCoupon);
         return new FebsResponse().success().message("操作成功");
@@ -940,14 +877,9 @@
     @Override
     public FebsResponse couponUpdate(AdminMallGoodsCouponVo adminMallGoodsCouponVo) {
         Long id = adminMallGoodsCouponVo.getId();
-        MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(id);
-        if (ObjectUtil.isEmpty(mallGoodsCoupon)) {
-            return new FebsResponse().fail().message("优惠卷不存在,请刷新当前页面");
-        }
-        Integer state = mallGoodsCoupon.getState();
-        if(2 == state){
-            return new FebsResponse().fail().message("请先下架优惠卷");
-        }
+        MallGoodsCoupon mallGoodsCoupon = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
+        ValidateEntityUtils.ensureNotEqual(mallGoodsCoupon.getState(),2,"优惠卷已上架,请先下架");
+
         mallGoodsCoupon.setName(adminMallGoodsCouponVo.getName());
         mallGoodsCoupon.setExpireDay(adminMallGoodsCouponVo.getExpireDay());
         mallGoodsCoupon.setCostAmount(adminMallGoodsCouponVo.getCostAmount());

--
Gitblit v1.9.1