Parcourir la source

任务提交 REDIS封装 加入订单检测脚本

秦国才 il y a 4 ans
Parent
commit
fade7695db

+ 0 - 2
common/fire-common/src/main/java/com/fire/common/handler/GlobalExceptionHandler.java

@@ -22,8 +22,6 @@ import static com.fire.dto.enums.Status.FAILURE;
 @Slf4j
 public class GlobalExceptionHandler {
 
-
-
     /**
      * 处理自定义基础异常
      */

+ 78 - 0
common/fire-common/src/main/java/com/fire/common/redisscript/RedisAmountScript.java

@@ -0,0 +1,78 @@
+package com.fire.common.redisscript;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.logging.log4j.util.Strings;
+import org.springframework.stereotype.Component;
+import redis.clients.jedis.JedisCluster;
+
+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;
+
+
+@Slf4j
+@Component
+public class RedisAmountScript {
+    @Resource
+    private JedisCluster jedisCluster;
+
+    private String runCode = "";
+    /*
+     *初始化
+     */
+    @PostConstruct
+    public void initScript(){
+        String slotKey1 = ORDER_CUSTOMER.key();
+        String slotKey2 = ORDER_SUPPLIER.key();
+
+        //LUA脚本
+        String SCRIPT = "local key = KEYS[1]" +
+                " local id = KEYS[2]" +
+                " local operating = ARGV[1]" +
+                " local num = tonumber(ARGV[2])" +
+                " local credit = tonumber(ARGV[3])" +
+                " local totle = nil" +
+                " local res = redis.pcall('HGET', key, id)" +
+                " if res then" +
+                " if operating =='add' then" +
+                " totle = tonumber(res) + num" +
+                " redis.pcall('HSET', key, id, tostring(totle))" +
+                " elseif operating=='sub' then" +
+                " if tonumber(res) + credit - num >= 0 then" +
+                " totle = tonumber(res) - num" +
+                " redis.pcall('HSET', key, id, tostring(totle))" +
+                " end" +
+                " end" +
+                " end" +
+                " return totle";
+        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{
+            log.info("脚本已加载");
+        }
+    }
+    /*
+     * 更改金额
+     * 1.amountName reids key 必须加前缀{XX}
+     * 2.orderId 订单ID key 名称 必须加前缀{XX}且和上面一样 格式为{key}12346578
+     * 3.operating 操作 sub 减法 add 加法
+     * 4.changeNum 操作的金额
+     * 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){
+            return res.toString();
+        }else{
+            return null;
+        }
+    }
+}

+ 58 - 0
common/fire-common/src/main/java/com/fire/common/redisscript/RedisOrderIdScript.java

@@ -0,0 +1,58 @@
+package com.fire.common.redisscript;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.logging.log4j.util.Strings;
+import org.springframework.stereotype.Component;
+import redis.clients.jedis.JedisCluster;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.Resource;
+import java.util.Arrays;
+import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
+
+@Slf4j
+@Component
+public class RedisOrderIdScript {
+    @Resource
+    private JedisCluster jedisCluster;
+
+    private String runCode = "";
+
+    /*
+     *初始化
+     */
+    @PostConstruct
+    public void initScript(){
+        String slotKey = ORDER_DOWN.key();
+        //LUA脚本
+                String SCRIPT = "local key = KEYS[1]" +
+                " local customerOrder = KEYS[2]" +
+                " local orderId = ARGV[1]" +
+                " local res = redis.pcall('HEXISTS', key, customerOrder)" +
+                " if res == 1 then" +
+                " return 0" +
+                " else" +
+                " redis.pcall('HSET', key, customerOrder, orderId)" +
+                " return 1" +
+                " end";
+        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)){
+            //redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
+            runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
+        }else{
+            log.info("脚本已加载");
+        }
+    }
+
+    /*
+     * 添加Oderid到redis
+     * 1.orderName reids key 必须加前缀{XX}
+     * 2.customerOrder  用户下单号 必须加前缀{XX}且和上面一样 格式为{key}12346578
+     * 3 OrderId 订单ID号
+     * 返回 1 成功加入 0 下单号重复
+     */
+    public String checkOrderId(String customerOrder,String OrderId) {
+        String orderName = ORDER_DOWN.key();
+        Object res = jedisCluster.evalsha(runCode, Arrays.asList(orderName,customerOrder),Arrays.asList(OrderId));
+        return res.toString();
+    }
+}

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

@@ -4,7 +4,9 @@ public enum RedisKey {
 
     //系统
     ORDER_ID_INCR("order.id.incr", "自增订单id"),
-    ORDER_ID_CUSTOMER("order.id.customer", "客户订单id");
+    ORDER_CUSTOMER("{customer.order}", "客户订单"),
+    ORDER_SUPPLIER("{supplier.order}","供应商订单"),
+    ORDER_DOWN("{down.order}","客户下单号 非订单号");
 
     private final String key;
 

+ 1 - 1
modules/admin/src/main/java/com/fire/admin/AdminApplication.java

@@ -9,7 +9,7 @@ import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.ComponentScans;
 import org.springframework.transaction.annotation.EnableTransactionManagement;
 
-@SpringBootApplication(scanBasePackages = {"com.fire.*","com.fire.admin"})
+@SpringBootApplication(scanBasePackages = {"com.fire.*","com.fire.admin","com.fire.common"})
 @EnableDiscoveryClient
 @MapperScan({"com.fire.admin.mapper","com.fire.admin.repository"})
 @EnableTransactionManagement(proxyTargetClass = true)

+ 17 - 51
modules/admin/src/main/java/com/fire/admin/rest/TestRest.java

@@ -1,27 +1,25 @@
 package com.fire.admin.rest;
 
 
-import com.fire.es.OrderEsDto;
+
+import com.fire.common.redisscript.RedisAmountScript;
+import com.fire.common.redisscript.RedisOrderIdScript;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
 import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
-import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
-import org.springframework.data.elasticsearch.core.query.IndexQuery;
-import org.springframework.util.StringUtils;
 import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
 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.Arrays;
 import java.util.Date;
 
+import static com.fire.dto.enums.RedisKey.ORDER_CUSTOMER;
+import static com.fire.dto.enums.RedisKey.ORDER_DOWN;
+
 
 @Slf4j
 @RestController
@@ -31,9 +29,16 @@ public class TestRest {
     @Autowired
     private ElasticsearchRestTemplate restTemplate;
 
+    @Resource
+    private RedisAmountScript redisAmountScript;
+
+    @Resource
+    private RedisOrderIdScript redisOrderIdScript;
+
     @Resource
     private JedisCluster jedisCluster;
 
+
     private String runCode = "";
 
     public Date timeSwap(String time) {
@@ -93,51 +98,12 @@ public class TestRest {
 //        restTemplate.index(indexQuery, indexCoordinates);
 //        return "sucees";
 //    }
-
-
     @GetMapping("/redis")
     public String redis() {
-        //初始化 判断有无加载脚本 ·
-        storeScript("{key}Amount");
-        //传入四个参数分别为
-        //key Hset key
-        //id 客户ID
-        //sub 减去 add加上
-        //金额
-        Object res = jedisCluster.evalsha(runCode,Arrays.asList("{key}Amount","{key}asd"),Arrays.asList("sub","111"));
-        if (null != res) {
-            log.info(res.toString());
-        }
-        return "sucees";
-    }
-
-    /*
-        *脚本加载到redis
-     */
-    public void storeScript(String slotKey){
-        //LUA脚本
-        String SCRIPT = "local key = KEYS[1]" +
-                " local id = KEYS[2]" +
-                " local operating = ARGV[1]" +
-                " local num = tonumber(ARGV[2])" +
-                " local totle = nil" +
-                " local res = redis.pcall('HGET', key, id)" +
-                " if res then" +
-                " if operating =='add' then" +
-                " totle = tonumber(res) + num" +
-                " redis.pcall('HSET', key, id, tostring(totle))" +
-                " elseif operating=='sub' then" +
-                " totle = tonumber(res) - num" +
-                " redis.pcall('HSET', key, id, tostring(totle))" +
-                " end" +
-                " end" +
-                " return totle";
-        if (Strings.isBlank(runCode) || !jedisCluster.scriptExists(runCode, slotKey)){
-            //redis支持脚本缓存,返回哈希码,后续可以继续用来调用脚本
-            runCode = jedisCluster.scriptLoad(SCRIPT, slotKey);
-        }else{
-            log.info("脚本已加载");
-        }
+        jedisCluster.hset(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","1000");
+        redisOrderIdScript.checkOrderId(ORDER_DOWN.key() + "asdfgh", "123456789");
+        String res = redisAmountScript.changeAmount(ORDER_CUSTOMER.key(),ORDER_CUSTOMER.key()+"123456","sub","500","1000");
+        return res;
     }