Forráskód Böngészése

任务提交 余额扣减,orderid自增

张均强 4 éve
szülő
commit
0a77ebcdf2

+ 13 - 11
common/fire-common/src/main/java/com/fire/common/redisscript/RedisAmountScript.java

@@ -9,8 +9,8 @@ import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
 import java.util.Arrays;
 
-import static com.fire.dto.enums.RedisKey.ORDER_CUSTOMER;
-import static com.fire.dto.enums.RedisKey.ORDER_SUPPLIER;
+import static com.fire.dto.enums.RedisKey.CUSTOMER_AMOUNT;
+import static com.fire.dto.enums.RedisKey.SUPPLIER_AMOUNT;
 
 
 @Slf4j
@@ -20,13 +20,14 @@ public class RedisAmountScript {
     private JedisCluster jedisCluster;
 
     private String runCode = "";
+
     /*
      *初始化
      */
     @PostConstruct
-    public void initScript(){
-        String slotKey1 = ORDER_CUSTOMER.key();
-        String slotKey2 = ORDER_SUPPLIER.key();
+    public void initScript() {
+        String slotKey1 = CUSTOMER_AMOUNT.key();
+        String slotKey2 = SUPPLIER_AMOUNT.key();
 
         //LUA脚本
         String SCRIPT = "local key = KEYS[1]" +
@@ -48,16 +49,17 @@ public class RedisAmountScript {
                 " end" +
                 " end" +
                 " return totle";
-        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey1)){
+        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey1)) {
             //redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
             runCode = jedisCluster.scriptLoad(SCRIPT, slotKey1);
             log.info("slotKey1:" + runCode);
             runCode = jedisCluster.scriptLoad(SCRIPT, slotKey2);
             log.info("slotKey2:" + runCode);
-        }else{
+        } else {
             log.info("脚本已加载");
         }
     }
+
     /*
      * 更改金额
      * 1.amountName reids key 必须加前缀{XX}
@@ -67,11 +69,11 @@ public class RedisAmountScript {
      * 5.creditNum 授信金额
      * 返回 成功返回操作后金额 失败返回null
      */
-    public String changeAmount(String amountName,String orderId, String operating, String changeNum,String creditNum) {
-        Object res = jedisCluster.evalsha(runCode, Arrays.asList(amountName,orderId),Arrays.asList(operating,changeNum,creditNum));
-        if (res != null){
+    public String changeAmount(String amountName, String orderId, String operating, String changeNum, String creditNum) {
+        Object res = jedisCluster.evalsha(runCode, Arrays.asList(amountName, orderId), Arrays.asList(operating, changeNum, creditNum));
+        if (res != null) {
             return res.toString();
-        }else{
+        } else {
             return null;
         }
     }

+ 3 - 3
common/fire-dto/src/main/java/com/fire/dto/CustomerInfo.java

@@ -43,13 +43,13 @@ public class CustomerInfo {
     private Integer status;
 
     @ApiModelProperty(value = "帐号余额")
-    private Double balance;
+    private Long balance;
 
     @ApiModelProperty(value = "授信额度")
-    private Double creditAmount;
+    private Long creditAmount;
 
     @ApiModelProperty(value = "当前费用")
-    private Double currentAmount;
+    private Long currentAmount;
 
     @ApiModelProperty(value = "是否第一次登录,1:不是0:是")
     private Integer isFirstLogin;

+ 1 - 1
common/fire-dto/src/main/java/com/fire/dto/FlowOrderInfo.java

@@ -14,7 +14,7 @@ public class FlowOrderInfo {
     private String flowAppId;
 
     @ApiModelProperty(value = "订单ID号")
-    private String orderId;
+    private Long orderId;
 
     @ApiModelProperty(value = "产品ID号")
     private String packageId;

+ 26 - 0
common/fire-dto/src/main/java/com/fire/dto/enums/AmountOper.java

@@ -0,0 +1,26 @@
+package com.fire.dto.enums;
+
+public enum AmountOper {
+
+    //系统
+    SUB("sub", "扣减"),
+    ADD("add", "加");
+
+    private final String oper;
+
+    private final String desc;
+
+    AmountOper(String oper, String desc) {
+        this.oper = oper;
+        this.desc = desc;
+    }
+
+    public String oper() {
+        return oper;
+    }
+
+    public String desc() {
+        return desc;
+    }
+
+}

+ 4 - 3
common/fire-dto/src/main/java/com/fire/dto/enums/RedisKey.java

@@ -4,9 +4,10 @@ public enum RedisKey {
 
     //系统
     ORDER_ID_INCR("order.id.incr", "自增订单id"),
-    ORDER_CUSTOMER("{customer.order}", "客户订单"),
-    ORDER_SUPPLIER("{supplier.order}","供应商订单"),
-    ORDER_DOWN("{down.order}","客户下单号 非订单号");
+    ORDER_INFO("order.info", "订单详情"),
+    CUSTOMER_AMOUNT("{customer.amount}", "客户金额"),
+    SUPPLIER_AMOUNT("{supplier.amount}", "供应商金额"),
+    ORDER_DOWN("{down.order}", "客户下单号 非订单号");
 
     private final String key;
 

+ 5 - 7
modules/admin/src/main/java/com/fire/admin/rest/TestRest.java

@@ -1,7 +1,6 @@
 package com.fire.admin.rest;
 
 
-
 import com.fire.common.redisscript.RedisAmountScript;
 import com.fire.common.redisscript.RedisOrderIdScript;
 import lombok.extern.slf4j.Slf4j;
@@ -11,14 +10,14 @@ import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 import redis.clients.jedis.JedisCluster;
+
 import javax.annotation.Resource;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 
-import static com.fire.dto.enums.RedisKey.ORDER_CUSTOMER;
-import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
+import static com.fire.dto.enums.RedisKey.*;
 
 
 @Slf4j
@@ -52,7 +51,7 @@ public class TestRest {
         return date1;
     }
 
-//    @GetMapping("/insert")
+    //    @GetMapping("/insert")
 //    public String insert() {
 //        OrderEsDto orderEsDto = new OrderEsDto();
 //        orderEsDto.setOrderId("1621332591561198300");
@@ -100,13 +99,12 @@ public class TestRest {
 //    }
     @GetMapping("/redis")
     public String redis() {
-        jedisCluster.hset(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","1000");
+        jedisCluster.hset(CUSTOMER_AMOUNT.key(), CUSTOMER_AMOUNT.key() + "123456", "1000");
         redisOrderIdScript.checkOrderId(ORDER_DOWN.key() + "asdfgh", "123456789");
-        String res = redisAmountScript.changeAmount(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","sub","500","1000");
+        String res = redisAmountScript.changeAmount(SUPPLIER_AMOUNT.key(), SUPPLIER_AMOUNT.key() + "123456", "sub", "500", "1000");
         return res;
     }
 
 
-
 }
 

+ 42 - 4
modules/make-order/src/main/java/com/fire/order/service/impl/MakeOrderServiceImpl.java

@@ -4,6 +4,10 @@ package com.fire.order.service.impl;
 import com.alibaba.nacos.api.utils.StringUtils;
 import com.alibaba.nacos.common.utils.CollectionUtils;
 import com.alibaba.nacos.common.utils.MD5Utils;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fire.common.redisscript.RedisAmountScript;
+import com.fire.common.redisscript.RedisOrderIdScript;
 import com.fire.dto.*;
 import com.fire.order.data.DataPool;
 import com.fire.order.request.OrderRequestContent;
@@ -16,6 +20,7 @@ import com.fire.order.service.MakeOrderService;
 import com.fire.param.HeaderDto;
 import com.fire.utils.date.DateUtils;
 import com.fire.utils.string.FireStringUtils;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import redis.clients.jedis.JedisCluster;
 
@@ -25,9 +30,10 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import static com.fire.dto.enums.AmountOper.SUB;
 import static com.fire.dto.enums.PriceCheck.CHECK;
 import static com.fire.dto.enums.Province.QG_ALL;
-import static com.fire.dto.enums.RedisKey.ORDER_ID_INCR;
+import static com.fire.dto.enums.RedisKey.*;
 import static com.fire.dto.enums.Status.*;
 import static com.fire.dto.enums.ValidStatus.INVALID;
 
@@ -37,10 +43,15 @@ import static com.fire.dto.enums.ValidStatus.INVALID;
  * @author ZJQ 2021年5月14日17:13:54
  */
 @Service
+@Slf4j
 public class MakeOrderServiceImpl implements MakeOrderService {
 
     @Resource
     private JedisCluster jedisCluster;
+    @Resource
+    private RedisOrderIdScript redisOrderIdScript;
+    @Resource
+    private RedisAmountScript redisAmountScript;
 
     /**
      * 下单方法
@@ -225,7 +236,7 @@ public class MakeOrderServiceImpl implements MakeOrderService {
         }
         //如果要求校验结算价格
         CustomerInfo customerInfo = flowAppInfo.getCustomerInfo();
-        if (customerInfo != null && CHECK.status().equals(customerInfo.getPriceCheck()) && !product.getPrice().equals(content.getPrice())) {
+        if (customerInfo == null || CHECK.status().equals(customerInfo.getPriceCheck()) && !product.getPrice().equals(content.getPrice())) {
             orderResp.setRCode(FAILURE_SERVER.status());
             orderResp.setRMsg("结算价格不一致");
             return responseDto;
@@ -269,13 +280,40 @@ public class MakeOrderServiceImpl implements MakeOrderService {
 
             }
         }
+        //自增id 取自redis,长度17位,后面加两位00 为主订单号,后面分发订单号后两位从 01开始
+        Long orderId = jedisCluster.incr(ORDER_ID_INCR.key()) * 100;
 
-        jedisCluster.incr(ORDER_ID_INCR.key());
+        //客户订单id校验 这里订单id拼接上客户id,保证唯一
+        String customerOrder = content.getExtOrder() + "." + flowAppInfo.getCustomerId();
+        String checkOrder = redisOrderIdScript.checkOrderId(customerOrder, String.valueOf(orderId));
+        //0表示订单号重复
+        if ("0".equals(checkOrder)) {
+            orderResp.setRCode(ORDER_ID_ERROR.status());
+            orderResp.setRMsg(ORDER_ID_ERROR.message());
+            return responseDto;
+        }
 
         //初始化订单详情
         FlowOrderInfo order = new FlowOrderInfo();
         order.setFlowAppId(appId);
-        order.setOrderId("待生成");
+        order.setOrderId(orderId);
+
+        //订单转换为json入redis
+        String orderStr;
+        ObjectMapper om = new ObjectMapper();
+        try {
+            orderStr = om.writeValueAsString(order);
+        } catch (JsonProcessingException e) {
+            //订单转换的其他异常 如果执行到这里说明系统有bug
+            log.error("订单转换成json失败", e);
+            orderResp.setRCode(FAILURE_SERVER.status());
+            orderResp.setRMsg(FAILURE_SERVER.message());
+            return responseDto;
+        }
+        //扣减
+        redisAmountScript.changeAmount(CUSTOMER_AMOUNT.key(),CUSTOMER_AMOUNT.key()+flowAppInfo.getCustomerId(),SUB.oper(),String.valueOf(product.getPrice()),String.valueOf(customerInfo.getCreditAmount()));
+
+        jedisCluster.hset(ORDER_INFO.key(), String.valueOf(orderId), orderStr);
 
         return responseDto;
     }

+ 3 - 3
modules/make-order/src/main/resources/mapper/CustomerInfoMapper.xml

@@ -12,9 +12,9 @@
         <result property="linkmanEmail" column="linkman_email" jdbcType="VARCHAR"/>
         <result property="address" column="address" jdbcType="VARCHAR"/>
         <result property="status" column="status" jdbcType="INTEGER"/>
-        <result property="balance" column="balance" jdbcType="NUMERIC"/>
-        <result property="creditAmount" column="credit_amount" jdbcType="NUMERIC"/>
-        <result property="currentAmount" column="current_amount" jdbcType="NUMERIC"/>
+        <result property="balance" column="balance" jdbcType="BIGINT"/>
+        <result property="creditAmount" column="credit_amount" jdbcType="BIGINT"/>
+        <result property="currentAmount" column="current_amount" jdbcType="BIGINT"/>
         <result property="isFirstLogin" column="is_first_login" jdbcType="INTEGER"/>
         <result property="isDeleted" column="is_deleted" jdbcType="INTEGER"/>
         <result property="creator" column="creator" jdbcType="VARCHAR"/>

+ 1 - 1
modules/make-order/src/main/resources/mapper/FlowOrderInfoMapper.xml

@@ -4,7 +4,7 @@
 
     <resultMap type="com.fire.dto.FlowOrderInfo" id="FlowOrderInfoMap">
         <result property="flowAppId" column="flow_app_id" jdbcType="VARCHAR"/>
-        <result property="orderId" column="order_id" jdbcType="VARCHAR"/>
+        <result property="orderId" column="order_id" jdbcType="BIGINT"/>
         <result property="packageId" column="package_id" jdbcType="VARCHAR"/>
         <result property="customerId" column="customer_id" jdbcType="INTEGER"/>
         <result property="orderType" column="order_type" jdbcType="INTEGER"/>