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 |  383 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 319 insertions(+), 64 deletions(-)

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 6774b7f..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
@@ -2,6 +2,8 @@
 
 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.*;
@@ -13,6 +15,7 @@
 import cn.hutool.core.util.ObjectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONUtil;
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -42,6 +45,8 @@
     private final MallShoppingCartMapper mallShoppingCartMapper;
     private final MallCarriageRuleMapper mallCarriageRuleMapper;
     private final MallCarriageRuleInfoMapper mallCarriageRuleInfoMapper;
+    private final MallGoodsCouponMapper mallGoodsCouponMapper;
+    private final CouponGoodsMapper couponGoodsMapper;
 
     @Override
     public IPage<AdminMallGoodsVo> getCategoryListInPage(MallGoods mallGoods, QueryRequest request) {
@@ -54,20 +59,21 @@
     @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("商品名称不能重复");
-        }
-        String goodsNo = addMallGoodsDto.getGoodsNo();
-        if (StrUtil.isEmpty(goodsNo)) {
-            return new FebsResponse().fail().message("商品编号不能为空");
-        }
-        Integer mallGoodsByGoodsNo = mallGoodsMapper.selectMallGoodsCountByGoodsNo(goodsNo);
-        if (mallGoodsByGoodsNo > 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);
+        Integer carriageType = addMallGoodsDto.getCarriageType();
+        if (addMallGoodsDto.getGoodsType() == 1) {
+            if (1 == carriageType) {
+                if (ObjectUtil.isEmpty(addMallGoodsDto.getCarriageAmount())
+                        || BigDecimal.ZERO.compareTo(addMallGoodsDto.getCarriageAmount()) > 0) {
+                    return new FebsResponse().fail().message("固定邮费不能小于零");
+                }
+            } else {
+                Long carriageRuleId = addMallGoodsDto.getCarriageRuleId();
+                ValidateEntityUtils.ensureColumnValid(carriageRuleId, MallCarriageRule::getId, mallCarriageRuleMapper::selectOne, "邮费模板不能为空");
+            }
         }
 
         // 付费商品
@@ -107,6 +113,15 @@
                     if (StrUtil.isEmpty(presentPrice)) {
                         return new FebsResponse().fail().message("商品规格现价不能为空");
                     }
+//                    if (StrUtil.isEmpty(addSku.getLevelOnePrice())
+//                    || StrUtil.isEmpty(addSku.getLevelTwoPrice())
+//                            || StrUtil.isEmpty(addSku.getLevelThreePrice())) {
+//                        return new FebsResponse().fail().message("商品规格级别价格不能为空");
+//                    }
+                    BigDecimal goodsWeight = addSku.getGoodsWeight();
+                    if(BigDecimal.ZERO.compareTo(goodsWeight) > 0){
+                        return new FebsResponse().fail().message("重量不能小于零");
+                    }
                 }
             }
         }
@@ -114,9 +129,9 @@
         MallGoods mallGoods = MallGoodsConversion.INSTANCE.dtoToEntity(addMallGoodsDto);
         mallGoods.setIsSale(MallGoods.ISSALE_STATUS_DISABLED);
 
-        if (mallGoods.getHasCarriage() == 2) {
-            mallGoods.setCarriage(BigDecimal.ZERO);
-        }
+//        if (mallGoods.getHasCarriage() == 2) {
+//            mallGoods.setCarriage(BigDecimal.ZERO);
+//        }
         mallGoodsMapper.insert(mallGoods);
 
         String thumbs = addMallGoodsDto.getThumbs();
@@ -132,6 +147,25 @@
                     mallGoodsImagesMapper.insert(mallGoodsImages);
                     i++;
                 }
+            }
+        }
+
+        Long goodsId = mallGoods.getId();
+        QueryWrapper<CouponGoods> objectQueryWrapper = new QueryWrapper<>();
+        objectQueryWrapper.eq("goods_id",goodsId);
+        List<CouponGoods> mallGoodsCoupons = couponGoodsMapper.selectList(objectQueryWrapper);
+        if(CollUtil.isNotEmpty(mallGoodsCoupons)){
+            for(CouponGoods couponGoods : mallGoodsCoupons){
+                couponGoodsMapper.deleteById(couponGoods);
+            }
+        }
+        if(CollUtil.isNotEmpty(addMallGoodsDto.getCouponIds())){
+            List<Long> couponIds = addMallGoodsDto.getCouponIds();
+            for(Long couponId : couponIds){
+                CouponGoods couponGoods = new CouponGoods();
+                couponGoods.setGoodsId(goodsId);
+                couponGoods.setCouponId(couponId);
+                couponGoodsMapper.insert(couponGoods);
             }
         }
 
@@ -151,11 +185,15 @@
             sku.setSkuImage(mallGoods.getThumb());
             sku.setSkuName(mallGoods.getGoodsName());
             sku.setCostPrice(mallGoods.getCostPrice());
-            sku.setPresentPrice(new BigDecimal(mallGoods.getPresentPrice()));
             sku.setOriginalPrice(new BigDecimal(mallGoods.getOriginalPrice()));
+            sku.setPresentPrice(new BigDecimal(mallGoods.getPresentPrice()));
+            sku.setLevelOnePrice(new BigDecimal(mallGoods.getPresentPrice()));
+            sku.setLevelTwoPrice(new BigDecimal(mallGoods.getPresentPrice()));
+            sku.setLevelThreePrice(new BigDecimal(mallGoods.getPresentPrice()));
             sku.setSkuVolume(mallGoods.getVolume());
             sku.setStock(mallGoods.getStock());
             sku.setStyleId(style.getId());
+            sku.setGoodsWeight(mallGoods.getGoodsWeight());
             mallGoodsSkuMapper.insert(sku);
             return new FebsResponse().success().message("添加成功");
         }
@@ -189,9 +227,15 @@
                 mallGoodsSku.setSkuVolume(addMallGoodsSkuDto.getSkuVolume() == null ? 0 : addMallGoodsSkuDto.getSkuVolume());
                 mallGoodsSku.setOriginalPrice(new BigDecimal(addMallGoodsSkuDto.getOriginalPrice()));
                 mallGoodsSku.setPresentPrice(new BigDecimal(addMallGoodsSkuDto.getPresentPrice()));
-                mallGoodsSku.setStyleId(mallGoodsStyleSku.getId());
                 mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
+
+                mallGoodsSku.setLevelOnePrice(new BigDecimal(addMallGoodsSkuDto.getPresentPrice()));
+                mallGoodsSku.setLevelTwoPrice(new BigDecimal(addMallGoodsSkuDto.getPresentPrice()));
+                mallGoodsSku.setLevelThreePrice(new BigDecimal(addMallGoodsSkuDto.getPresentPrice()));
+                mallGoodsSku.setStyleId(mallGoodsStyleSku.getId());
                 mallGoodsSku.setGoodsId(mallGoods.getId());
+                mallGoodsSku.setGoodsWeight(addMallGoodsSkuDto.getGoodsWeight());
+                mallGoodsSku.setSample(addMallGoodsSkuDto.getSample());
                 mallGoodsSkuMapper.insert(mallGoodsSku);
             }
         }
@@ -201,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();
@@ -212,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();
@@ -224,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);
@@ -269,8 +301,10 @@
 
         List<MallGoodsSku> skus = mallGoodsSkuMapper.selectSkuByGoodsId(mallGoods.getId());
         List<String> thumbs = mallGoodsImagesMapper.selectByGoodId(mallGoods.getId());
+        List<Long> couponIds = couponGoodsMapper.selectByGoodId(mallGoods.getId());
         mallGoods.setImages(thumbs);
         mallGoods.setSkus(skus);
+        mallGoods.setCouponIds(couponIds);
         return mallGoods;
     }
 
@@ -292,6 +326,19 @@
         Integer mallGoodsByGoodsNo = mallGoodsMapper.selectMallGoodsCountByGoodsNoAndGoodId(goodsNo, mallGoodsUpdateDto.getId());
         if (mallGoodsByGoodsNo > 0) {
             return new FebsResponse().fail().message("商品编号不能重复");
+        }
+        Integer carriageType = mallGoodsUpdateDto.getCarriageType();
+        if(1 == carriageType){
+            if(ObjectUtil.isEmpty(mallGoodsUpdateDto.getCarriageAmount())
+            || BigDecimal.ZERO.compareTo(mallGoodsUpdateDto.getCarriageAmount()) > 0){
+                return new FebsResponse().fail().message("固定邮费不能小于零");
+            }
+        }else{
+            Long carriageRuleId = mallGoodsUpdateDto.getCarriageRuleId();
+            MallCarriageRule mallCarriageRule = mallCarriageRuleMapper.selectById(carriageRuleId);
+            if(ObjectUtil.isEmpty(mallCarriageRule)){
+                return new FebsResponse().fail().message("邮费模板不能为空");
+            }
         }
 
         if (mallGoodsUpdateDto.getGoodsType() != 2) {
@@ -330,6 +377,10 @@
                     if (presentPrice.compareTo(BigDecimal.ZERO) < 0) {
                         return new FebsResponse().fail().message("商品规格现价不能小于0");
                     }
+                    BigDecimal goodsWeight = addSku.getGoodsWeight();
+                    if(BigDecimal.ZERO.compareTo(goodsWeight) > 0){
+                        return new FebsResponse().fail().message("重量不能小于零");
+                    }
 
                 }
             }
@@ -345,11 +396,17 @@
         }
 
         BeanUtil.copyProperties(mallGoodsUpdateDto, mallGoods);
-        if (mallGoods.getHasCarriage() == 2) {
-            mallGoods.setCarriage(BigDecimal.ZERO);
-        }
+//        if (mallGoods.getHasCarriage() == 2) {
+//            mallGoods.setCarriage(BigDecimal.ZERO);
+//        }
 
         mallGoodsMapper.updateById(mallGoods);
+
+        if( 1 == mallGoods.getCarriageType()){
+            mallGoodsMapper.updateCarriageRuleId(mallGoods.getId());
+        }else{
+            mallGoodsMapper.updateCarriageAmount(mallGoods.getId());
+        }
 
         mallGoodsImagesMapper.deleteByGoodsId(mallGoodsUpdateDto.getId());
         String thumbs = mallGoodsUpdateDto.getThumbs();
@@ -365,6 +422,25 @@
                     mallGoodsImagesMapper.insert(mallGoodsImages);
                     i++;
                 }
+            }
+        }
+
+        Long goodsId = mallGoodsUpdateDto.getId();
+        QueryWrapper<CouponGoods> objectQueryWrapper = new QueryWrapper<>();
+        objectQueryWrapper.eq("goods_id",goodsId);
+        List<CouponGoods> mallGoodsCoupons = couponGoodsMapper.selectList(objectQueryWrapper);
+        if(CollUtil.isNotEmpty(mallGoodsCoupons)){
+            for(CouponGoods couponGoods : mallGoodsCoupons){
+                couponGoodsMapper.deleteById(couponGoods);
+            }
+        }
+        if(CollUtil.isNotEmpty(mallGoodsUpdateDto.getCouponIds())){
+            List<Long> couponIds = mallGoodsUpdateDto.getCouponIds();
+            for(Long couponId : couponIds){
+                CouponGoods couponGoods = new CouponGoods();
+                couponGoods.setGoodsId(goodsId);
+                couponGoods.setCouponId(couponId);
+                couponGoodsMapper.insert(couponGoods);
             }
         }
 
@@ -386,14 +462,16 @@
             sku.setCostPrice(mallGoods.getCostPrice());
             sku.setPresentPrice(new BigDecimal(mallGoods.getPresentPrice()));
             sku.setOriginalPrice(new BigDecimal(mallGoods.getOriginalPrice()));
+            sku.setLevelOnePrice(new BigDecimal(mallGoods.getPresentPrice()));
+            sku.setLevelTwoPrice(new BigDecimal(mallGoods.getPresentPrice()));
+            sku.setLevelThreePrice(new BigDecimal(mallGoods.getPresentPrice()));
             sku.setSkuVolume(mallGoods.getVolume());
             sku.setStock(mallGoods.getStock());
             sku.setStyleId(style.getId());
+            sku.setGoodsWeight(mallGoods.getGoodsWeight());
             mallGoodsSkuMapper.insert(sku);
             return new FebsResponse().success().message("添加成功");
         }
-
-
 
         // 删除已存在sku
         List<Long> delSkuIds = mallGoodsUpdateDto.getDelSkuId();
@@ -423,6 +501,10 @@
                     if (ObjectUtil.isEmpty(mallGoodsStyleSku)) {
                         styles.add(addStyleDto.getStyleName());
                     }
+                }else{
+                    MallGoodsStyle mallGoodsStyle = mallGoodsStyleMapper.selectById(addStyleDto.getStyleId());
+                    mallGoodsStyle.setName(addStyleDto.getStyleName());
+                    mallGoodsStyleMapper.updateById(mallGoodsStyle);
                 }
             }
         }
@@ -447,9 +529,14 @@
                         mallGoodsSku.setSkuVolume(addMallGoodsSkuDto.getSkuVolume() == null ? 0 : addMallGoodsSkuDto.getSkuVolume());
                         mallGoodsSku.setOriginalPrice(addMallGoodsSkuDto.getOriginalPrice());
                         mallGoodsSku.setPresentPrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
+                        mallGoodsSku.setLevelOnePrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setLevelTwoPrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setLevelThreePrice(addMallGoodsSkuDto.getPresentPrice());
                         mallGoodsSku.setStyleId(addMallGoodsSkuDto.getStyleId());
                         mallGoodsSku.setGoodsId(mallGoods.getId());
-                        mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
+                        mallGoodsSku.setGoodsWeight(addMallGoodsSkuDto.getGoodsWeight());
+                        mallGoodsSku.setSample(addMallGoodsSkuDto.getSample());
                         mallGoodsSkuMapper.updateById(mallGoodsSku);
                     } else {
                         //新增商品规格
@@ -461,9 +548,14 @@
                         mallGoodsSku.setSkuVolume(addMallGoodsSkuDto.getSkuVolume() == null ? 0 : addMallGoodsSkuDto.getSkuVolume());
                         mallGoodsSku.setOriginalPrice(addMallGoodsSkuDto.getOriginalPrice());
                         mallGoodsSku.setPresentPrice(addMallGoodsSkuDto.getPresentPrice());
-                        mallGoodsSku.setStyleId(mallGoodsStyleSku.getId());
                         mallGoodsSku.setCostPrice(addMallGoodsSkuDto.getCostPrice());
+                        mallGoodsSku.setLevelOnePrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setLevelTwoPrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setLevelThreePrice(addMallGoodsSkuDto.getPresentPrice());
+                        mallGoodsSku.setStyleId(mallGoodsStyleSku.getId());
                         mallGoodsSku.setGoodsId(mallGoods.getId());
+                        mallGoodsSku.setGoodsWeight(addMallGoodsSkuDto.getGoodsWeight());
+                        mallGoodsSku.setSample(addMallGoodsSkuDto.getSample());
                         mallGoodsSkuMapper.insert(mallGoodsSku);
                     }
                 }
@@ -479,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();
@@ -490,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();
@@ -539,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();
@@ -564,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);
@@ -623,6 +701,7 @@
 
         List<MallCarriageRuleInfo> mallCarriageRuleInfos = mallCarriageRuleInfoMapper.selectMallCarriageRuleInfoByRuleIdAndAreaAddress(id,null);
         if(CollUtil.isNotEmpty(mallCarriageRuleInfos)){
+            StringBuffer areaAddressStr = new StringBuffer();
             List<AdminMallCarriageRuleInfoVo> adminCarriageRuleInfoVos = new ArrayList<>();
             for(MallCarriageRuleInfo mallCarriageRuleInfo : mallCarriageRuleInfos){
                 if(StrUtil.isNotEmpty(mallCarriageRuleInfo.getAreaAddress())){
@@ -634,6 +713,8 @@
                     adminMallCarriageRuleInfoVo.setMoreCnt(mallCarriageRuleInfo.getMoreCnt());
                     adminMallCarriageRuleInfoVo.setMorePrice(mallCarriageRuleInfo.getMorePrice());
                     adminCarriageRuleInfoVos.add(adminMallCarriageRuleInfoVo);
+
+                    areaAddressStr.append(mallCarriageRuleInfo.getAreaAddress());
                 }else{
                     //默认规则
                     adminMallCarriageRuleVo.setBasicCntDefault(mallCarriageRuleInfo.getBasicCnt());
@@ -644,6 +725,7 @@
                 }
             }
             adminMallCarriageRuleVo.setAdminCarriageRuleInfoVos(adminCarriageRuleInfoVos);
+            adminMallCarriageRuleVo.setAreaAddressStr(areaAddressStr.toString());
         }
         return adminMallCarriageRuleVo;
     }
@@ -652,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)){
@@ -666,6 +745,9 @@
             }
             return new FebsResponse().fail().message(goodsNameBuffer+"正在使用当前模板,请先下架商品");
         }
+
+        mallCarriageRule.setName(adminCarriageRuleUpdateDto.getRuleName());
+        mallCarriageRuleMapper.updateById(mallCarriageRule);
         //删除现有的所有运费模板子表数据
         List<MallCarriageRuleInfo> mallCarriageRuleInfos = mallCarriageRuleInfoMapper.selectMallCarriageRuleInfoByRuleIdAndAreaAddress(id, null);
         if(CollUtil.isNotEmpty(mallCarriageRuleInfos)){
@@ -711,4 +793,177 @@
 
         return new FebsResponse().success().message("操作成功");
     }
+
+    @Override
+    public List<MallCarriageRule> findAllCarriageRuleTree() {
+        List<MallCarriageRule> mallCarriageRules = mallCarriageRuleMapper.selectList(new QueryWrapper<>());
+        return mallCarriageRules;
+    }
+
+    @Override
+    public IPage<MallGoodsCoupon> getMallGoodsCouponListInPage(MallGoodsCoupon mallGoodsCoupon, QueryRequest request) {
+        Page<MallGoodsCoupon> page = new Page<>(request.getPageNum(), request.getPageSize());
+        IPage<MallGoodsCoupon> mallGoodsCouponIPage = mallGoodsCouponMapper.selectMallGoodsCouponListInPage(page, mallGoodsCoupon);
+        return mallGoodsCouponIPage;
+    }
+
+    @Override
+    public FebsResponse couponRuleAdd(CouponRuleAddDto couponRuleAddDto) {
+        String name = couponRuleAddDto.getName();
+        ValidateEntityUtils.ensureUnique(name,MallGoodsCoupon::getName,mallGoodsCouponMapper::selectCount,"优惠卷名称重复了");
+
+        MallGoodsCoupon mallGoodsCoupon = new MallGoodsCoupon();
+        mallGoodsCoupon.setName(name);
+        mallGoodsCoupon.setExpireDay(couponRuleAddDto.getExpireDay());
+        mallGoodsCoupon.setCostAmount(couponRuleAddDto.getCostAmount());
+        mallGoodsCoupon.setType(couponRuleAddDto.getType());
+        mallGoodsCoupon.setRealAmount(couponRuleAddDto.getRealAmount());
+        mallGoodsCouponMapper.insert(mallGoodsCoupon);
+
+        Long couponId = mallGoodsCoupon.getId();
+        // 先判断是否存在关联商品,再进行查询和删除
+        if (CollUtil.isNotEmpty(couponGoodsMapper.selectList(
+                new LambdaQueryWrapper<CouponGoods>()
+                        .select(CouponGoods::getId)
+                        .eq(CouponGoods::getCouponId, couponId)))) {
+            couponGoodsMapper.delete(new LambdaQueryWrapper<CouponGoods>().eq(CouponGoods::getCouponId, couponId));
+        }
+
+        // 处理 goodsIds 为 null 或空的情况
+        List<Long> goodsIds = couponRuleAddDto.getGoodsIds();
+        if (CollUtil.isNotEmpty(goodsIds)) {
+            for (Long goodsId : goodsIds) {
+                CouponGoods couponGoods = new CouponGoods();
+                couponGoods.setGoodsId(goodsId);
+                couponGoods.setCouponId(couponId);
+                couponGoodsMapper.insert(couponGoods);
+            }
+        }
+        return new FebsResponse().success().message("操作成功");
+    }
+
+    @Override
+    public FebsResponse couponRuleDel(Long id) {
+        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 = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
+        mallGoodsCoupon.setState(2);
+        mallGoodsCouponMapper.updateById(mallGoodsCoupon);
+        return new FebsResponse().success();
+    }
+
+    @Override
+    public FebsResponse downCoupon(Long id) {
+        MallGoodsCoupon mallGoodsCoupon = ValidateEntityUtils.ensureColumnReturnEntity(id, MallGoodsCoupon::getId, mallGoodsCouponMapper::selectOne, "优惠卷不存在");
+        mallGoodsCoupon.setState(1);
+        mallGoodsCouponMapper.updateById(mallGoodsCoupon);
+        return new FebsResponse().success().message("操作成功");
+    }
+
+    @Override
+    public AdminMallGoodsCouponVo selectGoodsCouponById(long id) {
+        List<Long> goodsIds = couponGoodsMapper.selectByCouponId(id);
+        AdminMallGoodsCouponVo adminMallGoodsCouponVo = mallGoodsCouponMapper.selectGoodsCouponById(id);
+        adminMallGoodsCouponVo.setGoodsIds(goodsIds);
+        return adminMallGoodsCouponVo;
+    }
+
+    @Override
+    public FebsResponse couponUpdate(AdminMallGoodsCouponVo adminMallGoodsCouponVo) {
+        Long id = adminMallGoodsCouponVo.getId();
+        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());
+        mallGoodsCoupon.setType(adminMallGoodsCouponVo.getType());
+        mallGoodsCoupon.setRealAmount(adminMallGoodsCouponVo.getRealAmount());
+        mallGoodsCouponMapper.updateById(mallGoodsCoupon);
+
+        Long couponId = mallGoodsCoupon.getId();
+        // 先判断是否存在关联商品,再进行查询和删除
+        if (CollUtil.isNotEmpty(couponGoodsMapper.selectList(
+                new LambdaQueryWrapper<CouponGoods>()
+                        .select(CouponGoods::getId)
+                        .eq(CouponGoods::getCouponId, couponId)))) {
+            couponGoodsMapper.delete(new LambdaQueryWrapper<CouponGoods>().eq(CouponGoods::getCouponId, couponId));
+        }
+
+        // 处理 goodsIds 为 null 或空的情况
+        List<Long> goodsIds = adminMallGoodsCouponVo.getGoodsIds();
+        if (CollUtil.isNotEmpty(goodsIds)) {
+            for (Long goodsId : goodsIds) {
+                CouponGoods couponGoods = new CouponGoods();
+                couponGoods.setGoodsId(goodsId);
+                couponGoods.setCouponId(couponId);
+                couponGoodsMapper.insert(couponGoods);
+            }
+        }
+        return new FebsResponse().success().message("操作成功");
+    }
+
+    @Override
+    public List<AdminMallGoodsCouponTreeVo> findAdminMallGoodsCouponVoTree() {
+        QueryWrapper<MallGoodsCoupon> objectQueryWrapper = new QueryWrapper<>();
+        objectQueryWrapper.eq("state",2);
+        List<MallGoodsCoupon> mallGoodsCoupons = mallGoodsCouponMapper.selectList(objectQueryWrapper);
+
+        List<AdminMallGoodsCouponTreeVo> objects = new ArrayList<>();
+        if(CollUtil.isNotEmpty(mallGoodsCoupons)){
+            for(MallGoodsCoupon mallGoodsCoupon : mallGoodsCoupons){
+                AdminMallGoodsCouponTreeVo adminMallGoodsCouponTreeVo = new AdminMallGoodsCouponTreeVo();
+                adminMallGoodsCouponTreeVo.setCouponId(mallGoodsCoupon.getId());
+                adminMallGoodsCouponTreeVo.setCouponName(mallGoodsCoupon.getName());
+                objects.add(adminMallGoodsCouponTreeVo);
+            }
+        }
+        return objects;
+    }
+
+    @Override
+    public List<AdminMallGoodsCouponTreeListVo> findAdminMallGoodsCouponVoTreeList() {
+        QueryWrapper<MallGoodsCoupon> objectQueryWrapper = new QueryWrapper<>();
+        objectQueryWrapper.eq("state",2);
+        List<MallGoodsCoupon> mallGoodsCoupons = mallGoodsCouponMapper.selectList(objectQueryWrapper);
+
+        List<AdminMallGoodsCouponTreeListVo> objects = new ArrayList<>();
+        if(CollUtil.isNotEmpty(mallGoodsCoupons)){
+            for(MallGoodsCoupon mallGoodsCoupon : mallGoodsCoupons){
+                AdminMallGoodsCouponTreeListVo adminMallGoodsCouponTreeListVo = new AdminMallGoodsCouponTreeListVo();
+                adminMallGoodsCouponTreeListVo.setId(mallGoodsCoupon.getId());
+                adminMallGoodsCouponTreeListVo.setName(mallGoodsCoupon.getName());
+                objects.add(adminMallGoodsCouponTreeListVo);
+            }
+        }
+        return objects;
+    }
+
+    @Override
+    public List<AdminMallGoodsCouponTreeListVo> findAdminMallGoodsVoTreeList() {
+        List<MallGoods> mallGoodsList = mallGoodsMapper.selectList(
+                new LambdaQueryWrapper<MallGoods>()
+                        .select(MallGoods::getId, MallGoods::getGoodsName)
+                        .eq(MallGoods::getIsSale, MallGoods.ISSALE_STATUS_ENABLE)
+                        .eq(MallGoods::getGoodsType, MallGoods.ISSALE_STATUS_ENABLE)
+                        .eq(MallGoods::getIsNormal, MallGoods.ISSALE_STATUS_ENABLE)
+        );
+
+        List<AdminMallGoodsCouponTreeListVo> objects = new ArrayList<>();
+        if(CollUtil.isNotEmpty(mallGoodsList)){
+            for(MallGoods mallGoods : mallGoodsList){
+                AdminMallGoodsCouponTreeListVo adminMallGoodsCouponTreeListVo = new AdminMallGoodsCouponTreeListVo();
+                adminMallGoodsCouponTreeListVo.setId(mallGoods.getId());
+                adminMallGoodsCouponTreeListVo.setName(mallGoods.getGoodsName());
+                objects.add(adminMallGoodsCouponTreeListVo);
+            }
+        }
+        return objects;
+    }
 }

--
Gitblit v1.9.1