浏览代码

添加校验订单的方法

yq 3 年之前
父节点
当前提交
8b8b3e4a0f

+ 178 - 0
src/main/java/com/usky/dxtop/common/utils/Assert.java

@@ -0,0 +1,178 @@
+package com.usky.dxtop.common.utils;
+
+
+import com.usky.dxtop.common.exception.CustomException;
+import org.apache.commons.collections4.CollectionUtils;
+
+import java.util.Collection;
+import java.util.Objects;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.function.Supplier;
+
+/**
+ * check相关方法, 用于判断,并抛出异常.
+ * default相关方法, 类似三元表达式
+ * apply相关方法, 用于判断, 并执行.
+ *
+ * @author barry chen
+ * @date 2021/1/20 7:34 下午
+ */
+public final class Assert {
+    private Assert() {
+    }
+
+    /**
+     * 如果表达式false, 则抛出异常.
+     *
+     * @param expression
+     * @param message
+     */
+    public static void check(boolean expression, String message) {
+        if (!expression) {
+            throw new CustomException(message);
+        }
+    }
+
+    /**
+     * 如果表达式false, 则抛出异常.
+     *
+     * @param expression
+     * @param exceptionSupplier
+     */
+    public static <X extends Throwable> void check(boolean expression, Supplier<? extends X> exceptionSupplier) throws X {
+        if (!expression) {
+            throw exceptionSupplier.get();
+        }
+    }
+
+    /**
+     * 判断字符串是否blank, 如果blank, 抛出异常
+     *
+     * @param str
+     * @param message
+     */
+    public static void checkNotBlank(String str, String message) {
+        if (StringUtils.isBlank(str)) {
+            throw new CustomException(message);
+        }
+    }
+
+
+    /**
+     * 判断字符串是否blank, 如果blank, 抛出异常
+     *
+     * @param str
+     * @param exceptionSupplier
+     */
+    public static <X extends Throwable> void checkNotBlank(String str, Supplier<? extends X> exceptionSupplier) throws X {
+        if (StringUtils.isBlank(str)) {
+            throw exceptionSupplier.get();
+        }
+    }
+
+    /**
+     * 判断集合是否为空, 如果空, 抛出异常
+     *
+     * @param collection
+     * @param message
+     */
+    public static void checkNotEmpty(Collection<?> collection, String message) {
+        if (CollectionUtils.isEmpty(collection)) {
+            throw new CustomException(message);
+        }
+    }
+
+    /**
+     * 判断集合是否为空, 如果空, 抛出异常
+     *
+     * @param collection
+     * @param exceptionSupplier
+     */
+    public static <X extends Throwable> void checkNotEmpty(Collection<?> collection, Supplier<? extends X> exceptionSupplier) throws X {
+        if (CollectionUtils.isEmpty(collection)) {
+            throw exceptionSupplier.get();
+        }
+    }
+
+    /**
+     * 如果assertFunction执行结果为true, 则执行runnable
+     *
+     * @param t
+     * @param assertFunction
+     * @param runnable
+     * @param <T>
+     */
+    public static <T> void apply(T t, Function<T, Boolean> assertFunction, Runnable runnable) {
+        apply(assertFunction.apply(t), runnable::run);
+    }
+
+    public static void apply(boolean expression, Runnable runnable) {
+        if (expression) {
+            runnable.run();
+        }
+    }
+
+    /**
+     * 如果assertFunction执行结果为true, 则执行consumer
+     *
+     * @param t
+     * @param assertFunction
+     * @param consumer
+     * @param <T>
+     */
+    public static <T> void apply(T t, Function<T, Boolean> assertFunction, Consumer<T> consumer) {
+        if (assertFunction.apply(t)) {
+            consumer.accept(t);
+        }
+    }
+
+    /**
+     * 如果assertFunction执行结果为true, 则执行trueFunction, 否则执行falseFunction
+     *
+     * @param t
+     * @param assertFunction
+     * @param trueFunction
+     * @param falseFunction
+     * @param <T>
+     * @param <R>
+     * @return
+     */
+    public static <T, R> R apply(T t, Function<T, Boolean> assertFunction, Function<T, R> trueFunction, Function<T, R> falseFunction) {
+        if (assertFunction.apply(t)) {
+            return trueFunction.apply(t);
+        } else {
+            return falseFunction.apply(t);
+        }
+    }
+
+    public static <T> T defaultIfNull(T t, T defaultT) {
+        return defaultIfTrue(t, Objects::isNull, defaultT);
+    }
+
+    /**
+     * 如果assertFunction执行结果为True, 则返回defaultT
+     *
+     * @param t
+     * @param assertFunction
+     * @param defaultT
+     * @param <T>
+     * @return
+     */
+    public static <T> T defaultIfTrue(T t, Function<T, Boolean> assertFunction, T defaultT) {
+        if (assertFunction.apply(t)) {
+            return defaultT;
+        } else {
+            return t;
+        }
+    }
+
+    public static String defaultIfBlank(String str, String defaultStr) {
+        return defaultIfTrue(str, StringUtils::isBlank, defaultStr);
+    }
+
+    public static Collection<?> defaultIfEmpty(Collection<?> collection, Collection<?> defaultCollection) {
+        return defaultIfTrue(collection, CollectionUtils::isEmpty, defaultCollection);
+    }
+
+}

+ 31 - 4
src/main/java/com/usky/dxtop/service/impl/OrderServiceImpl.java

@@ -46,6 +46,7 @@ import javax.imageio.ImageIO;
 import javax.servlet.http.HttpServletResponse;
 import java.awt.image.BufferedImage;
 import java.io.IOException;
+import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
 
@@ -83,7 +84,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         order.setOrderNumber(orderNumber);
         order.setOrderFlag(OrderStatus.NO_PAYMENT.getCode());
         order.setPayType(OrderPayType.XJ.getPayCode());
-        this.save(order);
+        verifyOrder(order);
         callCardTopApi(order);
         return this.updateById(order);
     }
@@ -137,7 +138,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         TreeMap<String, String> params = TopApi.generateScanPayApiParam(order.getOrderNumber(),
                                         new Double(Arith.mul(order.getMoney().doubleValue(),100)).intValue(), null,
                 null,null,null,null,null,null);
-        baseMapper.insert(order);
+        verifyOrder(order);
         //记录调用日志
         callApiLogService.saveOrUpdate(orderNumber,SCAN_PAY,TopApi.SCAN_TO_PAY_URL,JSONObject.toJSONString(params),null);
         return String.format("%s?%s",TopApi.SCAN_TO_PAY_URL,TopApi.generateParam(params));
@@ -392,6 +393,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
             appId = TopApi.WX_APP_ID;
             openId = order.getOpenId();
         }
+        verifyOrder(order);
         TreeMap<String, String> params = TopApi.generateUnifyParam(orderNumber,new Double(Arith.mul(order.getMoney().doubleValue(),100)).intValue(),
                 null, null,
                 null,null,openId,order.getTopPayType(),null,appId,
@@ -405,7 +407,6 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
         }else {
             throw new CustomException(obj.get("errorMessage").toString());
         }
-        baseMapper.insert(order);
         //记录调用日志
         callApiLogService.saveOrUpdate(orderNumber,SCAN_PAY,TopApi.SCAN_TO_PAY_URL,JSONObject.toJSONString(params),null);
         return payInfo;
@@ -450,7 +451,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
     }
 
     /**
-     * 处理数据
+     * 完善没有的数据
      * @param list
      * @param times
      * @param defaultTime
@@ -516,4 +517,30 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
             treeMap.put(orderNumber,1);
         }
     }
+
+    /**
+     * 校验订单添加订单
+     * @param order
+     */
+    public void verifyOrder(Order order){
+        Assert.check(null == order.getPayType(),"请选择支付方式");
+        Assert.check(null == order.getMoney() || order.getMoney().compareTo(BigDecimal.ZERO)  == 0,"请输入订单金额");
+        Assert.check(null == order.getScene(),"充值场景不能为空");
+        //非游客充值
+        if (!OrderSceneCode.VISITOR.getCode().equals(order.getScene())){
+            Assert.check(null == order.getUserId() || 0 == order.getUserId(),"用户编号不能为空");
+            Assert.check(StringUtils.isNotBlank(order.getUserName()),"用户名称不能为空");
+            Assert.check(StringUtils.isNotBlank(order.getUserPhone()),"用户手机号不能为空");
+            //app充值
+            if (OrderSceneCode.ERP_APP.getCode().equals(order.getScene()) || OrderSceneCode.COMMON_APP.getCode().equals(order.getScene())) {
+                Assert.check(StringUtils.isNotBlank(order.getTopPayType()), "请选择支付方式");
+                if ("W06".equals(order.getTopPayType())) {
+                    Assert.check(StringUtils.isNotBlank(order.getOpenId()), "openId不能为空");
+                }
+            }
+        }else {
+            Assert.check(null == order.getChannelId() || 0 == order.getChannelId(),"渠道编号不能为空");
+        }
+        this.save(order);
+    }
 }