Kaynağa Gözat

refactor(utils): 用自定义异常替换 RuntimeException

- 在 MemberAuthUtils、MercAuthUtils 和 MercCouponFactoryBase 中
- 将 RuntimeException 替换为 BusinessException
-优化了错误处理,提高了代码的可维护性和可读性
hechunping 1 hafta önce
ebeveyn
işleme
e1d4485dfd

+ 12 - 11
src/main/java/com/xynet/marketing/service/factory/coupon/merc/impl/base/MercCouponFactoryBase.java

@@ -17,6 +17,7 @@ import com.xynet.marketing.service.factory.coupon.merc.MercCouponFactory;
 import com.xynet.marketing.utils.Emptys;
 import com.xynet.marketing.utils.collections.map.JHashMap;
 import com.xynet.marketing.utils.collections.map.JMap;
+import com.xynet.marketing.utils.error.BusinessException;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
@@ -81,17 +82,17 @@ public class MercCouponFactoryBase extends ServiceImpl<MercCouponMapper, MercCou
     public void mercSend(JMap<String, Object> param) {
         List<Object> thirdMemberIdList = param.getList("thirdMemberIdList");
         if (!Emptys.check(thirdMemberIdList)) {
-            throw new RuntimeException("会员列表不能为空!");
+            throw new BusinessException("会员列表不能为空!");
         }
         LambdaQueryWrapper<MercCoupon> lqw = new LambdaQueryWrapper<MercCoupon>()
                 .eq(MercCoupon::getMercId, param.getInt("mercId"))
                 .in(MercCoupon::getId, param.getList("couponIdList"));
         List<MercCoupon> couponList = list(lqw);
         if (!Emptys.check(couponList)) {
-            throw new RuntimeException("没有可以赠送的优惠券!");
+            throw new BusinessException("没有可以赠送的优惠券!");
         }
         if (thirdMemberIdList.size() > 20) {
-            throw new RuntimeException("一次最多赠送20个用户!");
+            throw new BusinessException("一次最多赠送20个用户!");
         }
         couponList.forEach(coupon -> {
             thirdMemberIdList.forEach(memberId -> {
@@ -117,7 +118,7 @@ public class MercCouponFactoryBase extends ServiceImpl<MercCouponMapper, MercCou
                         .ge(MercCoupon::getSendEndDate, today));
         List<MercCoupon> list = list(lqw);
         if (!Emptys.check(list)) {
-            throw new RuntimeException("没有可赠送的优惠券!");
+            throw new BusinessException("没有可赠送的优惠券!");
         }
         list.forEach(mercCoupon -> {
             //查询券是否已领取过了
@@ -149,7 +150,7 @@ public class MercCouponFactoryBase extends ServiceImpl<MercCouponMapper, MercCou
                         .ge(MercCoupon::getSendEndDate, orderCreateTime));
         List<MercCoupon> list = list(lqw);
         if (!Emptys.check(list)) {
-            throw new RuntimeException("没有可赠送的优惠券!");
+            throw new BusinessException("没有可赠送的优惠券!");
         }
         //TODO 判断设备条件
         list.forEach(mercCoupon -> {
@@ -259,30 +260,30 @@ public class MercCouponFactoryBase extends ServiceImpl<MercCouponMapper, MercCou
         //查询会员优惠券信息
         Map<String, Object> memberCoupon = memberCouponService.obj(new JHashMap<String, Object>().set("id", memberCouponId));
         if (!Emptys.check(memberCoupon)) {
-            throw new RuntimeException("会员优惠券不存在!");
+            throw new BusinessException("会员优惠券不存在!");
         }
         //判断券是否属于该用户
         JMap<String, Object> memberCouponJMap = new JHashMap<String, Object>(memberCoupon);
         if (!Objects.equals(thirdMemberId, memberCouponJMap.getString("thirdMemberId"))) {
-            throw new RuntimeException("会员优惠券不属于该用户!");
+            throw new BusinessException("会员优惠券不属于该用户!");
         }
         //判断券是否已使用
         if (!Objects.equals(memberCouponJMap.getString("status"), MemberCouponStatusEnum.UNUSED.getCode())) {
-            throw new RuntimeException("该券已失效!");
+            throw new BusinessException("该券已失效!");
         }
         //查询商户券
         MercCoupon mercCoupon = getById(memberCouponJMap.getInt("couponId"));
         if (mercCoupon.getValidStartDate().isAfter(orderCreateTime.toLocalDate())) {
-            throw new RuntimeException("该券于!" + mercCoupon.getValidStartDate().toString() + "开始使用!");
+            throw new BusinessException("该券于!" + mercCoupon.getValidStartDate().toString() + "开始使用!");
         }
         if (mercCoupon.getValidEndDate().isBefore(orderCreateTime.toLocalDate())) {
-            throw new RuntimeException("该券于!" + mercCoupon.getValidEndDate().toString() + "结束!");
+            throw new BusinessException("该券于!" + mercCoupon.getValidEndDate().toString() + "结束!");
         }
         //判断优惠券是可用于该设备
         if (!mercCoupon.getIsAllDevice()) {
             Boolean deviceCheck = deviceService.checkDeviceId(mercCoupon.getId(), deviceId, "coupon");
             if (!deviceCheck) {
-                throw new RuntimeException("该券不可用于该设备!");
+                throw new BusinessException("该券不可用于该设备!");
             }
         }
         Map<String, Map<String, Object>> couponGoodsMap = new HashMap<>();

+ 4 - 3
src/main/java/com/xynet/marketing/utils/MemberAuthUtils.java

@@ -1,6 +1,7 @@
 package com.xynet.marketing.utils;
 
 import com.cow.support.dto.UserSessionModel;
+import com.xynet.marketing.utils.error.BusinessException;
 import com.xynet.marketing.utils.redis.utils.RedisService;
 
 import java.util.Map;
@@ -20,15 +21,15 @@ public class MemberAuthUtils {
         String providerId = headers.get("provider_id");
         RedisService<UserSessionModel> redisService = SpringBeanUtils.getBean(RedisService.class);
         if (!Emptys.check(token)) {
-            throw new RuntimeException("token不能为空");
+            throw new BusinessException("token不能为空");
         }
         if (!Emptys.check(providerId)) {
-            throw new RuntimeException("服务商ID不能为空");
+            throw new BusinessException("服务商ID不能为空");
         }
         //判断redis里是否有对应的商户ID
         UserSessionModel userSession = redisService.get(redisPrefix + token);
         if (!Emptys.check(userSession)) {
-            throw new RuntimeException("token已过期");
+            throw new BusinessException("token已过期");
         }
         return userSession.getUserInfo().getAuthlevel();
     }

+ 6 - 5
src/main/java/com/xynet/marketing/utils/MercAuthUtils.java

@@ -2,6 +2,7 @@ package com.xynet.marketing.utils;
 
 import com.cow.support.dto.UserSessionModel;
 import com.xynet.marketing.service.MercService;
+import com.xynet.marketing.utils.error.BusinessException;
 import com.xynet.marketing.utils.redis.utils.RedisService;
 
 import java.util.Map;
@@ -21,15 +22,15 @@ public class MercAuthUtils {
         String providerId = headers.get("provider_id");
         RedisService<UserSessionModel> redisService = SpringBeanUtils.getBean(RedisService.class);
         if (!Emptys.check(token)) {
-            throw new RuntimeException("token不能为空");
+            throw new BusinessException("token不能为空");
         }
         if (!Emptys.check(providerId)) {
-            throw new RuntimeException("服务商ID不能为空");
+            throw new BusinessException("服务商ID不能为空");
         }
         //判断redis里是否有对应的商户ID
         UserSessionModel userSession = redisService.get(redisPrefix + token);
         if (!Emptys.check(userSession)) {
-            throw new RuntimeException("token已过期");
+            throw new BusinessException("token已过期");
         }
         String thirdMercId = userSession.getUserInfo().getShbh();
         MercService mercService = SpringBeanUtils.getBean(MercService.class);
@@ -42,10 +43,10 @@ public class MercAuthUtils {
 //        String providerId = headers.get("provider_id");
 //        RedisService<Integer> redisService = SpringBeanUtils.getBean(RedisService.class);
 //        if (!Emptys.check(token)) {
-//            throw new RuntimeException("token不能为空");
+//            throw new BusinessException("token不能为空");
 //        }
 //        if (!Emptys.check(providerId)) {
-//            throw new RuntimeException("服务商ID不能为空");
+//            throw new BusinessException("服务商ID不能为空");
 //        }
 //        //判断redis里是否有对应的商户ID
 //        Integer mercId = redisService.get(redisPrefix + providerId + ":" + token);