Browse Source

增加话费抽奖接口

xiehui 3 years ago
parent
commit
038b536db9

+ 7 - 1
spring-wechars/pom.xml

@@ -59,7 +59,13 @@
             <artifactId>spring-boot-starter-test</artifactId>
             <scope>test</scope>
         </dependency>
-        
+
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.6.5</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 11 - 9
spring-wechars/src/main/java/cn/org/spring/wechar/bean/Result.java

@@ -39,17 +39,19 @@ public class Result<T> {
         this.data = data;
     }
 
-    public Result ok(T t) {
-        this.setCode(1000);
-        this.setMessage("成功");
-        this.setData(t);
-        return this;
+    public static <T> Result ok(T t) {
+        Result result = new Result<T>();
+        result.setCode(1000);
+        result.setMessage("成功");
+        result.setData(t);
+        return result;
     }
 
-    public Result fail(String msg) {
-        this.setCode(1001);
-        this.setMessage(msg);
-        return this;
+    public static Result fail(String msg) {
+        Result result = new Result();
+        result.setCode(1001);
+        result.setMessage(msg);
+        return result;
     }
 
 

+ 49 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/controller/PrizeController.java

@@ -0,0 +1,49 @@
+package cn.org.spring.wechar.controller;
+
+import cn.org.spring.common.util.StringUtil;
+import cn.org.spring.wechar.bean.Result;
+import cn.org.spring.wechar.utils.PrizeRandomUtil;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author: 谢辉
+ * @date: 2021/5/18
+ * @email: xieh_mail@163.com
+ * @description: 抽奖
+ * @modifiedBy:
+ * @version: 1.0
+ */
+@RestController
+@RequestMapping("/v1/prize")
+public class PrizeController {
+
+    // String 可以为任意类型 也可以自定义类型
+    static Map<String, Integer> keyChanceMap = new HashMap<String, Integer>();
+
+    static {
+        //key值的是奖品名称或者id。value为中奖的概率
+        keyChanceMap.put("0", 10);// 谢谢惠顾
+        keyChanceMap.put("1", 200);// 中1元
+        keyChanceMap.put("3", 500);// 中3元
+        keyChanceMap.put("4", 3000);// 中4元
+    }
+
+    /**
+     * 话费抽奖
+     *
+     * @return
+     */
+    @GetMapping("/getTelCharge")
+    public Result getTelCharge() {
+        String result = PrizeRandomUtil.chanceSelect(keyChanceMap);
+        if (StringUtil.isEmpty(result)) {
+            result = "0";
+        }
+        return Result.ok(result);
+    }
+}

+ 47 - 0
spring-wechars/src/main/java/cn/org/spring/wechar/utils/PrizeRandomUtil.java

@@ -0,0 +1,47 @@
+package cn.org.spring.wechar.utils;
+
+import java.util.Map;
+import java.util.Random;
+
+/**
+ * @author: 谢辉
+ * @date: 2021/5/18
+ * @email: xieh_mail@163.com
+ * @description:抽奖随机工具
+ * @modifiedBy:
+ * @version: 1.0
+ */
+public class PrizeRandomUtil {
+   /* // String 可以为任意类型 也可以自定义类型
+    static Map<String, Integer> keyChanceMap = new HashMap<String, Integer>();
+
+    static {
+        //key值的是奖品名称或者id。value为中奖的概率
+        keyChanceMap.put("0", 10);
+        keyChanceMap.put("1", 200);
+        keyChanceMap.put("3", 500);
+        keyChanceMap.put("4", 3000);
+    }*/
+
+    public static String chanceSelect(Map<String, Integer> keyChanceMap) {
+        if (keyChanceMap == null || keyChanceMap.size() == 0)
+            return null;
+
+        Integer sum = 0;
+        for (Integer value : keyChanceMap.values()) {
+            sum += value;
+        }
+        // 从1开始
+        Integer rand = new Random().nextInt(sum) + 1;
+
+        for (Map.Entry<String, Integer> entry : keyChanceMap.entrySet()) {
+            rand -= entry.getValue();
+            // 选中
+            if (rand <= 0) {
+                String item = entry.getKey();
+                return item;
+            }
+        }
+        return null;
+    }
+}