PrizeController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.hssx.controller;
  2. import java.io.IOException;
  3. import java.security.KeyManagementException;
  4. import java.security.NoSuchAlgorithmException;
  5. import java.security.NoSuchProviderException;
  6. import java.util.Date;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Random;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestParam;
  15. import com.hssx.entity.Prize;
  16. import com.hssx.entity.PrizeExample;
  17. import com.hssx.entity.PrizeRecord;
  18. import com.hssx.entity.PrizeRecordExample;
  19. import com.hssx.entity.User;
  20. import com.hssx.entity.UserPrizeCountExample;
  21. import com.hssx.mapper.PrizeMapper;
  22. import com.hssx.mapper.PrizeRecordMapper;
  23. import com.hssx.mapper.UserMapper;
  24. import com.hssx.mapper.UserPrizeCountMapper;
  25. import com.hssx.utils.HttpRespMsg;
  26. import com.sun.javafx.collections.MappingChange.Map;
  27. @Controller
  28. @RequestMapping("/prize")
  29. public class PrizeController {
  30. @Autowired
  31. UserMapper usermapper;
  32. @Autowired
  33. PrizeMapper prizeMapper;
  34. @Autowired
  35. PrizeRecordMapper prizeRecordMapper;
  36. @Autowired
  37. UserPrizeCountMapper UserPrizeCountMapper;
  38. /**
  39. * 小游戏抽奖 参数: userId:当前抽奖人id
  40. *
  41. * @return
  42. */
  43. @RequestMapping(value = "/luckDraw")
  44. public void luckDraw(Integer userId, HttpServletResponse response)
  45. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  46. HttpRespMsg msg = new HttpRespMsg();
  47. Integer count = -1;
  48. PrizeRecord prizeRecord = new PrizeRecord();
  49. User user = usermapper.selectByPrimaryKey(userId);
  50. prizeRecord.setUid(user.getId());
  51. prizeRecord.setType(user.getType());
  52. prizeRecord.setVoucherId(user.getVoucherId());
  53. UserPrizeCountExample uExp = new UserPrizeCountExample();
  54. Integer luckDrawCount = UserPrizeCountMapper.selectCountByToday();
  55. if (luckDrawCount < 2) {
  56. synchronized (this) {
  57. Random ran = new Random();
  58. int num = ran.nextInt(9999);
  59. Prize prize = prizeMapper.selectByPrimaryKey(num);
  60. if (prize == null) {
  61. count = -1;
  62. } else {
  63. if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
  64. count = 0;
  65. // prize.setIsSelected(1);
  66. // prizeMapper.updateByPrimaryKeySelective(prize);
  67. prizeRecord.setPrizeId(prize.getPrizeLevel());
  68. prizeRecord.setPrize(prize.getName());
  69. prizeRecordMapper.insert(prizeRecord);
  70. } else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
  71. count = 1;
  72. // prize.setIsSelected(1);
  73. // prizeMapper.updateByPrimaryKeySelective(prize);
  74. prizeRecord.setPrizeId(prize.getPrizeLevel());
  75. prizeRecord.setPrize(prize.getName());
  76. prizeRecordMapper.insert(prizeRecord);
  77. } else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
  78. count = 2;
  79. // prize.setIsSelected(1);
  80. // prizeMapper.updateByPrimaryKeySelective(prize);
  81. prizeRecord.setPrizeId(prize.getPrizeLevel());
  82. prizeRecord.setPrize(prize.getName());
  83. prizeRecordMapper.insert(prizeRecord);
  84. } else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
  85. count = 3;
  86. // prize.setIsSelected(1);
  87. // prizeMapper.updateByPrimaryKeySelective(prize);
  88. prizeRecord.setPrizeId(prize.getPrizeLevel());
  89. prizeRecord.setPrize(prize.getName());
  90. prizeRecordMapper.insert(prizeRecord);
  91. } else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
  92. count = 4;
  93. // prize.setIsSelected(1);
  94. // prizeMapper.updateByPrimaryKeySelective(prize);
  95. prizeRecord.setPrizeId(prize.getPrizeLevel());
  96. prizeRecord.setPrize(prize.getName());
  97. prizeRecordMapper.insert(prizeRecord);
  98. } else {
  99. count = -1;
  100. }
  101. }
  102. }
  103. HashMap<String, Object> map = new HashMap<String, Object>();
  104. map.put("prizeLevel", count);
  105. map.put("prizeRecord", prizeRecord);
  106. msg.data = map;
  107. }else{
  108. }
  109. response.setContentType("application/json");
  110. response.setCharacterEncoding("UTF-8");
  111. response.getWriter().println(msg.toJSONStr());
  112. }
  113. /**
  114. * 小游戏抽奖列表
  115. *
  116. * @return
  117. */
  118. @RequestMapping(value = "/luckDrawList")
  119. public void luckDrawList(HttpServletResponse response)
  120. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  121. HttpRespMsg msg = new HttpRespMsg();
  122. List<PrizeRecord> list = prizeRecordMapper.selectByExample(new PrizeRecordExample());
  123. msg.data = list;
  124. response.setContentType("application/json");
  125. response.setCharacterEncoding("UTF-8");
  126. response.getWriter().println(msg.toJSONStr());
  127. }
  128. /**
  129. * 小游戏抽奖填写用户信息 参数:phone:电话 username:姓名 id:中奖纪录id
  130. *
  131. * @return
  132. */
  133. @RequestMapping(value = "/luckDrawAddInfo")
  134. public void luckDrawAddInfo(PrizeRecord prizeRecord, HttpServletResponse response)
  135. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  136. HttpRespMsg msg = new HttpRespMsg();
  137. prizeRecordMapper.updateByPrimaryKeySelective(prizeRecord);
  138. msg.data = prizeRecord;
  139. response.setContentType("application/json");
  140. response.setCharacterEncoding("UTF-8");
  141. response.getWriter().println(msg.toJSONStr());
  142. }
  143. }