Browse Source

幸运购

zhangzz 4 years ago
parent
commit
a6bf8dc1ad

+ 19 - 0
src/main/java/TestMain.java

@@ -1,5 +1,24 @@
+import com.minpay.common.bean.DrawDto;
+import com.minpay.common.service.impl.DrawServiceImpl;
+
+import java.util.ArrayList;
+import java.util.List;
+
 public class TestMain {
 
 	public static void main(String[] args) throws Exception {
+		DrawServiceImpl draw = new DrawServiceImpl();
+		List<DrawDto> drawDtoList = new ArrayList<>();
+		DrawDto dto2 = new DrawDto();
+		dto2.setHuodaoNo("100002");
+		dto2.setChouJiangAmt("1");
+		drawDtoList.add(dto2);
+
+		DrawDto dto = new DrawDto();
+		dto.setHuodaoNo("100001");
+		dto.setChouJiangAmt("0.6");
+		drawDtoList.add(dto);
+
+		draw.xingyunDraw("1.2", "12263", "", "1212", drawDtoList);
 	}
 }

+ 40 - 0
src/main/java/com/minpay/common/bean/DrawDto.java

@@ -0,0 +1,40 @@
+package com.minpay.common.bean;
+
+/*
+ * 中奖商品列表
+ */
+public class DrawDto {
+
+    // 货到号
+    private String huodaoNo;
+
+    // 抽奖价格
+    private String chouJiangAmt;
+
+    // 是否中奖
+    private boolean isDraw = false;
+
+    public String getHuodaoNo() {
+        return huodaoNo;
+    }
+
+    public void setHuodaoNo(String huodaoNo) {
+        this.huodaoNo = huodaoNo;
+    }
+
+    public String getChouJiangAmt() {
+        return chouJiangAmt;
+    }
+
+    public void setChouJiangAmt(String chouJiangAmt) {
+        this.chouJiangAmt = chouJiangAmt;
+    }
+
+    public boolean isDraw() {
+        return isDraw;
+    }
+
+    public void setDraw(boolean draw) {
+        isDraw = draw;
+    }
+}

+ 33 - 0
src/main/java/com/minpay/common/service/IDrawService.java

@@ -0,0 +1,33 @@
+package com.minpay.common.service;
+
+import com.minpay.common.bean.DrawDto;
+import com.minpay.common.exception.BusinessCodeException;
+import com.startup.minpay.frame.exception.MINBusinessException;
+import com.startup.minpay.frame.service.base.IMINLocalService;
+
+import java.util.List;
+
+/**
+ * 中奖计算
+ * @author xubaohai
+ */
+public interface IDrawService extends IMINLocalService {
+
+	/**
+	 * 幸运购的玩法
+	 * https://a41on8.axshare.com/#p=%E5%B9%B8%E8%BF%90%E8%B4%AD&hi=1
+	 *
+	 * @param payAmt 支付金额(元)
+	 * @param orderId 订单号
+	 * @param payOrderId 第三方支付订单号
+	 * @param shouhuojiNo 售货机编号
+	 * @param drawDto 商品信息
+	 * @throws BusinessCodeException 系统异常
+	 * @throws MINBusinessException 框架异常
+	 * @return true: 中奖
+	 * 		   false:未中奖
+	 */
+	public List<DrawDto> xingyunDraw(String payAmt, String orderId, String payOrderId, String shouhuojiNo,
+							   List<DrawDto> drawDto) throws BusinessCodeException, MINBusinessException;
+
+}

+ 76 - 0
src/main/java/com/minpay/common/service/impl/DrawServiceImpl.java

@@ -0,0 +1,76 @@
+package com.minpay.common.service.impl;
+
+import com.minpay.common.bean.DrawDto;
+import com.minpay.common.exception.BusinessCodeException;
+import com.minpay.common.service.IDrawService;
+import com.minpay.common.service.IPublicService;
+import com.minpay.common.util.CommonUtil;
+import com.startup.minpay.frame.exception.MINBusinessException;
+import com.startup.minpay.frame.service.base.Service;
+import com.startup.minpay.frame.target.MINComponent;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+/**
+ * 中奖计算
+ * @author xubaohai
+ */
+@MINComponent
+public class DrawServiceImpl implements IDrawService {
+
+    /**
+     * 幸运购的玩法
+     * @param payAmt 支付金额(元)
+     * @param orderId 订单号
+     * @param payOrderId 第三方支付订单号
+     * @throws BusinessCodeException 系统异常
+     * @throws MINBusinessException 框架异常
+     */
+    public List<DrawDto> xingyunDraw(String payAmt, String orderId, String payOrderId,
+                                     String shouhuojiNo, List<DrawDto> drawDtoList) throws BusinessCodeException, MINBusinessException {
+        System.out.println("参与商品的数量:" + drawDtoList.size());
+        boolean isDraw = false;
+        payAmt = CommonUtil.divide(payAmt, String.valueOf(drawDtoList.size()));
+        System.out.println("每个商品的抽奖价格:" + payAmt);
+        String amtFen = CommonUtil.multiply(payAmt, "100");
+        BigDecimal big2 = new BigDecimal(amtFen);
+        amtFen = big2.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
+        // 参与计算的随机数
+        String useOrderId = orderId;
+        // 第三方支付订单号不为空
+        if(CommonUtil.isNotEmpty(payOrderId)) {
+            // 支付宝的支付订单号,取后10位+34  进行计算。(加的这个数字,总后台可以更改,或者在系统里面每次随机从1-99选择一个数字加进去)
+            String suijishu = Service.lookup(IPublicService.class).getSysParValue("ZHONGJIANG_SUIJISHU");
+            useOrderId = CommonUtil.add(payOrderId, suijishu);
+            BigDecimal bigUse = new BigDecimal(useOrderId);
+            useOrderId = bigUse.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
+        }
+
+        // TODO 汇率,获取机器的汇率
+        String huilv = "40";
+        // 汇率百分比
+        String huilvBaiFenBi = CommonUtil.divide(huilv, "100");
+        String mu = CommonUtil.multiply(amtFen, huilvBaiFenBi);
+        BigDecimal big = new BigDecimal(mu);
+        String muVal = big.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
+        System.out.println("需要减去的汇率:" + muVal);
+        // 需要获取的范围
+        int jisuanAmtFen = Integer.parseInt(amtFen) - Integer.parseInt(muVal);
+        System.out.println("随机数范围:" + jisuanAmtFen);
+        for(DrawDto drawDto : drawDtoList) {
+            String chouJiangFen = CommonUtil.multiply(drawDto.getChouJiangAmt(), "100");
+            BigDecimal bigChouJiangFen = new BigDecimal(chouJiangFen);
+            chouJiangFen = bigChouJiangFen.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
+            // 余数
+            int yushu = Integer.parseInt(useOrderId) % Integer.parseInt(chouJiangFen);
+            System.out.println("货到号:" + drawDto.getHuodaoNo() + ", 余数:" + yushu);
+            if(yushu > 1 && yushu < jisuanAmtFen) {
+                drawDto.setDraw(true);
+            }
+            System.out.println("货到号:" + drawDto.getHuodaoNo() + ", 是否中奖:" + drawDto.isDraw());
+        }
+        return drawDtoList;
+    }
+
+}