OrderDrawAction.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. package com.minpay.shouhuo.orderdrawaction;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import com.minpay.common.service.IPropertiesService;
  6. import com.minpay.common.service.IPublicService;
  7. import com.minpay.common.util.CommonUtil;
  8. import com.minpay.common.util.DateUtil;
  9. import com.minpay.db.table.mapper.VmEquipmentInfMapper;
  10. import com.minpay.db.table.mapper.VmOrderDetailsMapper;
  11. import com.minpay.db.table.mapper.VmOrderDrawDetailMapper;
  12. import com.minpay.db.table.mapper.VmOrderInfMapper;
  13. import com.minpay.db.table.mapper.VmProEquRelMapper;
  14. import com.minpay.db.table.model.VmEquipmentInf;
  15. import com.minpay.db.table.model.VmOrderDetails;
  16. import com.minpay.db.table.model.VmOrderDetailsExample;
  17. import com.minpay.db.table.model.VmOrderDrawDetail;
  18. import com.minpay.db.table.model.VmOrderInf;
  19. import com.minpay.db.table.model.VmProEquRel;
  20. import com.minpay.db.table.own.mapper.OrderDrawMapper;
  21. import com.minpay.shouhuo.deliveryaction.DeliveryAction;
  22. import com.startup.minpay.frame.business.IMINAction;
  23. import com.startup.minpay.frame.business.res.MINActionResult;
  24. import com.startup.minpay.frame.exception.MINBusinessException;
  25. import com.startup.minpay.frame.service.base.IMINDataBaseService;
  26. import com.startup.minpay.frame.service.base.Service;
  27. import com.startup.minpay.frame.target.MINAction;
  28. import com.startup.minpay.frame.target.MINComponent;
  29. /**
  30. * 订单成功回调接口
  31. * @author dudm@minpay.cc
  32. *
  33. */
  34. @MINComponent
  35. public class OrderDrawAction implements IMINAction{
  36. /**订单成功回调接口*/
  37. public static final String EDIT_ORDER_DRAW = "editOrderDraw";
  38. /**幸运购*/
  39. public static final String ORDER_DRAW_LUCKY = "orderDrawLucky";
  40. /**游戏购*/
  41. public static final String ORDER_DRAW_GAME = "orderDrawGame";
  42. /**二游戏购*/
  43. public static final String TWO_ORDER_DRAW_GAME = "twoOrderDrawGame";
  44. /**
  45. * 订单成功回调接口
  46. * @param orderInf 付款订单
  47. * @return
  48. * @throws MINBusinessException
  49. */
  50. @MINAction(value = EDIT_ORDER_DRAW)
  51. public MINActionResult editOrderDraw(VmOrderInf orderInf) throws MINBusinessException{
  52. MINActionResult res = new MINActionResult();
  53. //是抽奖商品
  54. if("01".equals(orderInf.getProType())){
  55. //通过订单id获取商品设备关联表id
  56. VmOrderDetailsExample detailsExample = new VmOrderDetailsExample();
  57. detailsExample.createCriteria().andOrderIdEqualTo(orderInf.getId())
  58. .andChannelEqualTo(orderInf.getChannel());
  59. List<VmOrderDetails> list = Service.lookup(IMINDataBaseService.class)
  60. .getMybatisMapper(VmOrderDetailsMapper.class)
  61. .selectByExample(detailsExample);
  62. String gameId = orderInf.getGameId();
  63. if(list.size() != 0){
  64. //获取设备商品信息
  65. VmProEquRel proEquRel = Service.lookup(IMINDataBaseService.class)
  66. .getMybatisMapper(VmProEquRelMapper.class)
  67. .selectByPrimaryKey(list.get(0).getProEquRelId());
  68. // 游戏类型(GAME_TYEP:00:不参与,10:游戏购 20:幸运购)
  69. /*if("00".equals(proEquRel.getGameType())) {
  70. return res;
  71. }else if("20".equals(proEquRel.getGameType())) {//幸运购
  72. orderDrawLucky(orderInf,proEquRel);
  73. }else if("10".equals(proEquRel.getGameType())){//游戏购
  74. orderDrawGame(orderInf,proEquRel);
  75. }else if("30".equals(proEquRel.getGameType())){//幸运游戏购
  76. twoOrderDrawGame(orderInf,proEquRel);
  77. }*/
  78. if("100001".equals(gameId)) {//幸运购
  79. orderDrawLucky(orderInf,proEquRel);
  80. }else if("100002".equals(gameId)){//游戏购
  81. orderDrawGame(orderInf,proEquRel);
  82. }
  83. // 获取最终游戏类型
  84. /*if(CommonUtil.isEmpty(proEquRel.getGameType())) {
  85. // 获取设备信息
  86. VmEquipmentInf eqInf = Service.lookup(IMINDataBaseService.class)
  87. .getMybatisMapper(VmEquipmentInfMapper.class)
  88. .selectByPrimaryKey(proEquRel.getEquipmentId());
  89. if("20".equals(eqInf.getGameType())) {//幸运购
  90. orderDrawLucky(orderInf,proEquRel);
  91. }else if("10".equals(eqInf.getGameType())){//游戏购
  92. orderDrawGame(orderInf,proEquRel);
  93. }else if("30".equals(eqInf.getGameType())){//幸运游戏购
  94. twoOrderDrawGame(orderInf,proEquRel);
  95. }
  96. }*/
  97. }
  98. }
  99. return res;
  100. }
  101. /**
  102. * 幸运购判断
  103. * @return
  104. * @throws MINBusinessException
  105. */
  106. @MINAction(value = ORDER_DRAW_LUCKY)
  107. public void orderDrawLucky(VmOrderInf orderInf,VmProEquRel proEquRel) throws MINBusinessException{
  108. String payOrderNo = orderInf.getPayOrderno(); //支付订单号
  109. String orderAmt = orderInf.getOrderAmt(); //付款金额
  110. String sallPrice = proEquRel.getSallPrice(); //售货价
  111. //订单号除去金额*100的余数
  112. int remainder = CommonUtil.objToint((payOrderNo)) % CommonUtil.objToint((CommonUtil.multiply(sallPrice,"100")));
  113. //支付金额*100
  114. int luckyNum = CommonUtil.objToint(CommonUtil.multiply(orderAmt,"100"));
  115. //中奖
  116. if(luckyNum >= remainder){
  117. orderInf.setIsDraw("01");
  118. //中奖后生成提货信息
  119. DeliveryAction.createDeliveryInfo(orderInf.getId(),"");
  120. }
  121. orderInf.setIsUsed("00");
  122. Service.lookup(IMINDataBaseService.class)
  123. .getMybatisMapper(VmOrderInfMapper.class)
  124. .updateByPrimaryKey(orderInf);
  125. }
  126. /**
  127. * 游戏购判断
  128. * @return
  129. * @throws MINBusinessException
  130. */
  131. @MINAction(value = ORDER_DRAW_GAME)
  132. public void orderDrawGame(VmOrderInf orderInf,VmProEquRel proEquRel) throws MINBusinessException{
  133. VmOrderDetailsExample detailsExample = new VmOrderDetailsExample();
  134. detailsExample.createCriteria().andOrderIdEqualTo(orderInf.getId())
  135. .andChannelEqualTo(orderInf.getChannel());
  136. List<VmOrderDetails> list = Service.lookup(IMINDataBaseService.class)
  137. .getMybatisMapper(VmOrderDetailsMapper.class)
  138. .selectByExample(detailsExample);
  139. if(list.size()!=1){
  140. throw new MINBusinessException("订单信息错误!");
  141. }
  142. String sallPrice = proEquRel.getSallPrice(); //售货价
  143. String costPrice = proEquRel.getCostPrice(); //成本价
  144. String promotionPrice = "0";
  145. if(proEquRel.getIsPromotion().equals("0")){//促销价
  146. promotionPrice = proEquRel.getPromotionPrice();
  147. }
  148. Map<String,String> map = new HashMap<String,String>();
  149. map.put("channel", orderInf.getChannel());
  150. map.put("perId", list.get(0).getProEquRelId());
  151. //查询所有未使用的购买此货道订单
  152. List<Map<String, String>> drawGame = Service.lookup(IMINDataBaseService.class)
  153. .getMybatisMapper(OrderDrawMapper.class)
  154. .queryDrawGame(map);
  155. String allPrice = "0"; //所有订单总金额
  156. for (Map<String, String> map2 : drawGame) {
  157. allPrice = CommonUtil.add(map2.get("ordetAmt"),allPrice);
  158. }
  159. //总金额与价格比较
  160. int i = CommonUtil.compare(allPrice, CommonUtil.add(CommonUtil.add(sallPrice,costPrice),promotionPrice));
  161. //中奖
  162. if(i>=0){
  163. for (Map<String, String> map2 : drawGame) {
  164. VmOrderInf vmOrderInf = new VmOrderInf();
  165. vmOrderInf.setId(map2.get("ordNo"));
  166. vmOrderInf.setIsUsed("00");
  167. //修改所有购买此货道订单为已使用
  168. Service.lookup(IMINDataBaseService.class)
  169. .getMybatisMapper(VmOrderInfMapper.class)
  170. .updateByPrimaryKeySelective(vmOrderInf);
  171. }
  172. //修改本订单中奖
  173. orderInf.setIsDraw("01");
  174. Service.lookup(IMINDataBaseService.class)
  175. .getMybatisMapper(VmOrderInfMapper.class)
  176. .updateByPrimaryKey(orderInf);
  177. //中奖后生成提货信息
  178. DeliveryAction.createDeliveryInfo(orderInf.getId(),"");
  179. }
  180. }
  181. /**
  182. * 幸运游戏购
  183. * @return
  184. * @throws MINBusinessException
  185. */
  186. @MINAction(value = TWO_ORDER_DRAW_GAME)
  187. public void twoOrderDrawGame(VmOrderInf orderInf,VmProEquRel proEquRel) throws MINBusinessException{
  188. //获取当前时间
  189. String dateTime = DateUtil.getCurrentDateTimeString();
  190. //获取当前日期
  191. String nowDate = DateUtil.getCurrentDateString();
  192. //开始时间
  193. String startTime = nowDate.concat("000000");
  194. //结束时间
  195. String endTime = nowDate.concat("999999");
  196. VmOrderDetailsExample detailsExample = new VmOrderDetailsExample();
  197. detailsExample.createCriteria().andOrderIdEqualTo(orderInf.getId())
  198. .andChannelEqualTo(orderInf.getChannel());
  199. List<VmOrderDetails> list = Service.lookup(IMINDataBaseService.class)
  200. .getMybatisMapper(VmOrderDetailsMapper.class)
  201. .selectByExample(detailsExample);
  202. if(list.size()!=1){
  203. throw new MINBusinessException("订单信息错误!");
  204. }
  205. //查询金额条件
  206. Map<String,String> map = new HashMap<String,String>();
  207. map.put("channel", orderInf.getChannel());
  208. map.put("relId", proEquRel.getId());
  209. map.put("startTime", startTime);
  210. map.put("endTime", endTime);
  211. //查询所有订单金额
  212. List<Map<String, String>> drawGame = Service.lookup(IMINDataBaseService.class)
  213. .getMybatisMapper(OrderDrawMapper.class)
  214. .queryall(map);
  215. //幸运值幅度范围
  216. String rang = Service.lookup(IPropertiesService.class)
  217. .getSystemProperties()
  218. .get(orderInf.getChannel() + "_RANG")
  219. .getKey();
  220. //商品售货价
  221. String sallPrice = proEquRel.getSallPrice();
  222. System.out.println("===============================商品售货价"+sallPrice);
  223. //商品购买数量
  224. String buyNumber = list.get(0).getProNums();
  225. System.out.println("===============================商品购买数量"+buyNumber);
  226. //幸运值
  227. String luckNo = proEquRel.getLuck();
  228. /*if(CommonUtil.isEmpty(proEquRel.getLuck())){
  229. VmEquipmentInf equipmentInf = Service.lookup(IMINDataBaseService.class)
  230. .getMybatisMapper(VmEquipmentInfMapper.class)
  231. .selectByPrimaryKey(proEquRel.getEquipmentId());
  232. luckNo = equipmentInf.getLuck();
  233. }*/
  234. //幸运值区间最大值
  235. String max = CommonUtil.add(luckNo, rang);
  236. System.out.println("===============================幸运值区间最大值"+max);
  237. //幸运值区间最小值
  238. String min = CommonUtil.subtract(luckNo,rang);
  239. System.out.println("===============================幸运值区间最小值"+min);
  240. //支付类型 1:1倍支付 3:3倍支付 5:5倍支付
  241. String payType = orderInf.getPayType();
  242. System.out.println("===============================支付倍数"+payType);
  243. //本订单中奖金额
  244. String drawMoneys = orderInf.getDrawPrice();
  245. //抽奖次数
  246. int number = Integer.parseInt(orderInf.getNumber());
  247. System.out.println("===============================抽奖次数"+number);
  248. //正常商品购买总价格
  249. String buyAmount = drawGame.get(0).get("buyAmount");
  250. //抽奖商品购买总金额
  251. String drawAmount = drawGame.get(0).get("drawAmount");
  252. System.out.println("===============================货道今日总收入"+buyAmount+"+"+drawAmount);
  253. //抽奖商品支出总金额
  254. String drawPrice = drawGame.get(0).get("drawPrice");
  255. // 商品已经抽奖价格+商品价格本身/2
  256. drawPrice = CommonUtil.add(drawPrice, CommonUtil.divide(proEquRel.getSallPrice(), "2"));
  257. System.out.println("===============================货道今日中奖金额"+drawPrice);
  258. String drawNumber = "0";
  259. for (int i = 0; i < number; i++) {
  260. //准备中奖详情数据
  261. VmOrderDrawDetail detail = new VmOrderDrawDetail();
  262. //获取中奖详情表主键
  263. String id = Service.lookup(IPublicService.class).getSequence("VM_ORDER_DRAW_ID");
  264. detail.setId(id); //中奖详情id
  265. detail.setNumber(String.valueOf(i+1)); //抽奖第几次
  266. detail.setOrderId(orderInf.getId()); //订单id
  267. detail.setChannel(orderInf.getChannel()); //渠道
  268. detail.setIsDraw("00"); //未中奖
  269. detail.setCreateUser(orderInf.getCreateUser()); //创建人
  270. detail.setCreateTime(dateTime); //创建时间
  271. detail.setModifyUser(orderInf.getCreateUser()); //修改人
  272. detail.setModifyTime(dateTime); //修改时间
  273. //抽奖商品购买总金额增加
  274. drawAmount= CommonUtil.add(drawAmount, payType);
  275. //计算抽奖过程
  276. //利润比
  277. String divide = CommonUtil.multiply(CommonUtil.divide(CommonUtil.subtract(CommonUtil.add(buyAmount, drawAmount), drawPrice),
  278. CommonUtil.add(buyAmount, drawAmount)),"100");
  279. System.out.println("===============================利润比乘100"+divide);
  280. //利润比和最大值相比
  281. int divMax = CommonUtil.compare(divide, max);
  282. //利润比和最小值相比
  283. int divMin = CommonUtil.compare(divide, min);
  284. //声明本次概率
  285. String probability = Service.lookup(IPropertiesService.class)
  286. .getSystemProperties()
  287. .get(orderInf.getChannel() + "_PROBABILITY")
  288. .getKey();
  289. if(divMax == 1){
  290. //比幸运值大需要提升概率将利润比降低下来
  291. probability = Service.lookup(IPropertiesService.class)
  292. .getSystemProperties()
  293. .get(orderInf.getChannel() + "_PROBABILITY_UP")
  294. .getKey();
  295. }
  296. if(divMin == -1){
  297. //比幸运值小需要降低概率将利润比提升回来
  298. probability = Service.lookup(IPropertiesService.class)
  299. .getSystemProperties()
  300. .get(orderInf.getChannel() + "_PROBABILITY_DOWN")
  301. .getKey();
  302. }
  303. System.out.println("===============================中奖率"+probability);
  304. //获取1到100内的随机数
  305. double num = Math.random() * 100;
  306. String randNum = String.valueOf(num);
  307. System.out.println("===============================随机数"+randNum);
  308. int compare = CommonUtil.compare(probability, randNum);
  309. //如果中奖
  310. if(compare != -1){
  311. drawNumber = CommonUtil.add(drawNumber, buyNumber);
  312. //中奖后生成提货信息
  313. DeliveryAction.createDeliveryInfo(orderInf.getId(),drawNumber);
  314. //如果中奖 支出总金额增加
  315. drawPrice= CommonUtil.add(drawPrice, CommonUtil.multiply(sallPrice, buyNumber));
  316. //如果中奖 本订单中奖金额增加
  317. drawMoneys= CommonUtil.add(drawMoneys, CommonUtil.multiply(sallPrice, buyNumber));
  318. //将本订单中奖金额修改
  319. orderInf.setDrawPrice(drawMoneys);
  320. //将订单定为中奖状态
  321. orderInf.setIsDraw("01");
  322. //将中奖详情定为中奖状态
  323. detail.setIsDraw("01");
  324. System.out.println("===============================中奖"+",货道中奖金额为"+drawPrice);
  325. System.out.println("===============================中奖金额为"+drawMoneys);
  326. }
  327. Service.lookup(IMINDataBaseService.class)
  328. .getMybatisMapper(VmOrderDrawDetailMapper.class)
  329. .insertSelective(detail);
  330. }
  331. orderInf.setModifyTime(dateTime); //订单修改时间
  332. //修改订单
  333. Service.lookup(IMINDataBaseService.class)
  334. .getMybatisMapper(VmOrderInfMapper.class)
  335. .updateByPrimaryKeySelective(orderInf);
  336. }
  337. }