DrawServiceImpl.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package com.minpay.common.service.impl;
  2. import com.minpay.common.bean.DrawDto;
  3. import com.minpay.common.exception.BusinessCodeException;
  4. import com.minpay.common.service.IDrawService;
  5. import com.minpay.common.service.IPublicService;
  6. import com.minpay.common.util.CommonUtil;
  7. import com.minpay.db.table.mapper.VmOrderInfMapper;
  8. import com.minpay.db.table.mapper.VmProEquRelMapper;
  9. import com.minpay.db.table.model.VmOrderDetails;
  10. import com.minpay.db.table.model.VmOrderInf;
  11. import com.minpay.db.table.model.VmProEquRel;
  12. import com.minpay.shouhuo.deliveryaction.DeliveryAction;
  13. import com.startup.minpay.frame.exception.MINBusinessException;
  14. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  15. import com.startup.minpay.frame.service.base.Service;
  16. import com.startup.minpay.frame.target.MINComponent;
  17. import java.math.BigDecimal;
  18. import java.util.List;
  19. /**
  20. * 中奖计算
  21. * @author xubaohai
  22. */
  23. @MINComponent
  24. public class DrawServiceImpl implements IDrawService {
  25. /**
  26. * 幸运购的玩法
  27. * @param payAmt 支付金额(元)
  28. * @param orderId 订单号
  29. * @param payOrderId 第三方支付订单号
  30. * @throws BusinessCodeException 系统异常
  31. * @throws MINBusinessException 框架异常
  32. */
  33. public List<DrawDto> xingyunDraw(String payAmt, String orderId, String payOrderId,
  34. String shouhuojiNo, List<DrawDto> drawDtoList) throws BusinessCodeException, MINBusinessException {
  35. System.out.println("参与商品的数量:" + drawDtoList.size());
  36. boolean isDraw = false;
  37. payAmt = CommonUtil.divide(payAmt, String.valueOf(drawDtoList.size()));
  38. System.out.println("每个商品的抽奖价格:" + payAmt);
  39. String amtFen = CommonUtil.multiply(payAmt, "100");
  40. BigDecimal big2 = new BigDecimal(amtFen);
  41. amtFen = big2.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
  42. // 参与计算的随机数
  43. String useOrderId = orderId;
  44. // 第三方支付订单号不为空
  45. if(CommonUtil.isNotEmpty(payOrderId)) {
  46. // 支付宝的支付订单号,取后10位+34 进行计算。(加的这个数字,总后台可以更改,或者在系统里面每次随机从1-99选择一个数字加进去)
  47. String suijishu = Service.lookup(IPublicService.class).getSysParValue("ZHONGJIANG_SUIJISHU");
  48. useOrderId = CommonUtil.add(payOrderId, suijishu);
  49. BigDecimal bigUse = new BigDecimal(useOrderId);
  50. useOrderId = bigUse.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
  51. }
  52. // TODO 汇率,获取机器的汇率
  53. String huilv = "40";
  54. // 汇率百分比
  55. String huilvBaiFenBi = CommonUtil.divide(huilv, "100");
  56. String mu = CommonUtil.multiply(amtFen, huilvBaiFenBi);
  57. BigDecimal big = new BigDecimal(mu);
  58. String muVal = big.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
  59. System.out.println("需要减去的汇率:" + muVal);
  60. // 需要获取的范围
  61. int jisuanAmtFen = Integer.parseInt(amtFen) - Integer.parseInt(muVal);
  62. System.out.println("随机数范围:" + jisuanAmtFen);
  63. for(DrawDto drawDto : drawDtoList) {
  64. String chouJiangFen = CommonUtil.multiply(drawDto.getChouJiangAmt(), "100");
  65. BigDecimal bigChouJiangFen = new BigDecimal(chouJiangFen);
  66. chouJiangFen = bigChouJiangFen.setScale(0, BigDecimal.ROUND_HALF_DOWN).toString();
  67. // 余数
  68. int yushu = Integer.parseInt(useOrderId) % Integer.parseInt(chouJiangFen);
  69. System.out.println("货到号:" + drawDto.getHuodaoNo() + ", 余数:" + yushu);
  70. if(yushu > 1 && yushu < jisuanAmtFen) {
  71. drawDto.setDraw(true);
  72. }
  73. System.out.println("货到号:" + drawDto.getHuodaoNo() + ", 是否中奖:" + drawDto.isDraw());
  74. }
  75. return drawDtoList;
  76. }
  77. public List<DrawDto> orderDrawGame(VmOrderInf orderInf, VmProEquRel proEquRel, VmOrderDetails vmOrderDetails) throws BusinessCodeException, MINBusinessException {
  78. //获取累计金额
  79. String accumulatedAmount = proEquRel.getAccumulatedAmount();
  80. //获取中奖范围
  81. String drawAmount = proEquRel.getDrawAmount();
  82. //订单金额
  83. String orderAmt = orderInf.getOrderAmt();
  84. //计算当前累计金额
  85. String amt = CommonUtil.add(accumulatedAmount,orderAmt);
  86. String[] drawAmountStr = drawAmount.split("-");
  87. //开始金额
  88. String startAmt = drawAmountStr[0];
  89. //结束金额
  90. String endAmt = drawAmountStr[0];
  91. List<DrawDto> drawDtoList = null;
  92. DrawDto drawDto = new DrawDto();
  93. drawDto.setHuodaoNo(proEquRel.getId());
  94. drawDto.setOrderId(orderInf.getId());
  95. drawDto.setOrderDetailsId(vmOrderDetails.getDetailsId());
  96. //在范围内中奖
  97. if(Double.parseDouble(startAmt) <= Double.parseDouble(amt) && Double.parseDouble(amt) >= Double.parseDouble(endAmt)){
  98. //重置货道累计金额
  99. proEquRel.setAccumulatedAmount("0");
  100. drawDto.setDraw(true);
  101. }else{
  102. drawDto.setDraw(false);
  103. if(Double.parseDouble(amt) > Double.parseDouble(endAmt)){
  104. //追加累计金额
  105. proEquRel.setAccumulatedAmount("0");
  106. }else{
  107. //追加累计金额
  108. proEquRel.setAccumulatedAmount(amt);
  109. }
  110. }
  111. Service.lookup(IMINDataBaseService.class)
  112. .getMybatisMapper(VmProEquRelMapper.class)
  113. .updateByPrimaryKeySelective(proEquRel);
  114. //todo 更新 订单表, 订单详情表,提货表
  115. return drawDtoList;
  116. }
  117. }