PrizeController.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.apache.commons.io.filefilter.FalseFileFilter;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. import com.hssx.entity.Prize;
  17. import com.hssx.entity.PrizeExample;
  18. import com.hssx.entity.PrizeOpportunity;
  19. import com.hssx.entity.PrizeRecord;
  20. import com.hssx.entity.PrizeRecordExample;
  21. import com.hssx.entity.User;
  22. import com.hssx.entity.UserPrizeCount;
  23. import com.hssx.entity.UserPrizeCountExample;
  24. import com.hssx.mapper.PrizeMapper;
  25. import com.hssx.mapper.PrizeOpportunityMapper;
  26. import com.hssx.mapper.PrizeRecordMapper;
  27. import com.hssx.mapper.UserMapper;
  28. import com.hssx.mapper.UserPrizeCountMapper;
  29. import com.hssx.utils.HttpRespMsg;
  30. import com.sun.javafx.collections.MappingChange.Map;
  31. @Controller
  32. @RequestMapping("/prize")
  33. public class PrizeController {
  34. @Autowired
  35. UserMapper usermapper;
  36. @Autowired
  37. PrizeMapper prizeMapper;
  38. @Autowired
  39. PrizeRecordMapper prizeRecordMapper;
  40. @Autowired
  41. UserPrizeCountMapper UserPrizeCountMapper;
  42. @Autowired
  43. PrizeOpportunityMapper prizeOpportunityMapper;
  44. /**
  45. * 小游戏抽奖 参数: userId:当前抽奖人id
  46. *
  47. * @return
  48. */
  49. @RequestMapping(value = "/luckDraw")
  50. public void luckDraw(@RequestParam(required = false) Integer userId, HttpServletResponse response)
  51. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  52. HttpRespMsg msg = new HttpRespMsg();
  53. userId = new Random().nextInt(6)+1;//测试用
  54. Integer count = -1;
  55. PrizeRecord prizeRecord = new PrizeRecord();
  56. User user = usermapper.selectByPrimaryKey(userId);
  57. prizeRecord.setUid(user.getId());
  58. prizeRecord.setType(user.getType());
  59. prizeRecord.setVoucherId(user.getVoucherId());
  60. prizeRecord.setNickName(user.getNickName());
  61. UserPrizeCountExample uExp = new UserPrizeCountExample();
  62. Integer luckDrawCount = UserPrizeCountMapper.selectCountByTodayByUid(userId);
  63. HashMap<String, Object> map = new HashMap<String, Object>();
  64. // 模拟无限次数抽奖
  65. // 测试必定中奖账号
  66. if ("o1L3L5vwFkGSUMpec3uwRgsI6A-M".equals(user.getVoucherId())) {
  67. Prize prize = prizeMapper.selectByPrimaryKey(1);
  68. // UserPrizeCount userPrizeCount = new UserPrizeCount();
  69. // userPrizeCount.setUserId(userId);
  70. // UserPrizeCountMapper.insert(userPrizeCount);
  71. map.put("prizeLevel", 0);
  72. map.put("prizeRecord", prizeRecord);
  73. map.put("prize", prize);
  74. msg.data = map;
  75. response.setContentType("application/json");
  76. response.setCharacterEncoding("UTF-8");
  77. response.getWriter().println(msg.toJSONStr());
  78. return;
  79. }
  80. // if (luckDrawCount <= 1) {
  81. // 直接抽奖(每日默认2次抽奖机会)
  82. count = getLuckDraw(prizeRecord, count);
  83. // 添加抽奖记录
  84. UserPrizeCount userPrizeCount = new UserPrizeCount();
  85. userPrizeCount.setUserId(userId);
  86. UserPrizeCountMapper.insert(userPrizeCount);
  87. map.put("prizeLevel", count);
  88. map.put("prizeRecord", prizeRecord);
  89. msg.data = map;
  90. // } else if (luckDrawCount == 2) {
  91. // int opportunityCount =
  92. // prizeOpportunityMapper.selectCountByToday(userId);
  93. // if (opportunityCount > 0) {
  94. // // 直接抽奖
  95. // count = getLuckDraw(prizeRecord, count);
  96. // // 添加抽奖记录
  97. // UserPrizeCount userPrizeCount = new UserPrizeCount();
  98. // userPrizeCount.setUserId(userId);
  99. // UserPrizeCountMapper.insert(userPrizeCount);
  100. // map.put("prizeLevel", count);
  101. // map.put("prizeRecord", prizeRecord);
  102. // msg.data = map;
  103. // }else{
  104. // msg.setError("抽奖次数已达上限,去分享即可获得抽奖机会");
  105. // }
  106. //
  107. // } else {
  108. // msg.setError("抽奖次数已达上限");
  109. // }
  110. response.setContentType("application/json");
  111. response.setCharacterEncoding("UTF-8");
  112. response.getWriter().println(msg.toJSONStr());
  113. }
  114. public synchronized Integer getLuckDraw(PrizeRecord prizeRecord, Integer count) {
  115. Random ran = new Random();
  116. int num = ran.nextInt(9999);
  117. Prize prize = prizeMapper.selectByPrimaryKey(num);
  118. if (prize == null) {
  119. count = -1;
  120. } else {
  121. if (prize.getPrizeLevel() == 5 || prize.getIsSelected() == 1) {
  122. count = -1;
  123. } else {
  124. if (prize.getPrizeLevel() == 0 && prize.getIsSelected() == 0) {
  125. count = 0;
  126. } else if (prize.getPrizeLevel() == 1 && prize.getIsSelected() == 0) {
  127. count = 1;
  128. } else if (prize.getPrizeLevel() == 2 && prize.getIsSelected() == 0) {
  129. count = 2;
  130. } else if (prize.getPrizeLevel() == 3 && prize.getIsSelected() == 0) {
  131. count = 3;
  132. } else if (prize.getPrizeLevel() == 4 && prize.getIsSelected() == 0) {
  133. count = 4;
  134. }
  135. // prize.setIsSelected(1);
  136. // prizeMapper.updateByPrimaryKeySelective(prize);
  137. prizeRecord.setPrizeId(prize.getPrizeLevel());
  138. prizeRecord.setPrize(prize.getName());
  139. prizeRecordMapper.insert(prizeRecord);
  140. }
  141. }
  142. return count;
  143. }
  144. /**
  145. * 小游戏抽奖列表
  146. *
  147. * @return
  148. */
  149. @RequestMapping(value = "/luckDrawList")
  150. public void luckDrawList(HttpServletResponse response)
  151. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  152. HttpRespMsg msg = new HttpRespMsg();
  153. List<PrizeRecord> list = prizeRecordMapper.selectByExample(new PrizeRecordExample());
  154. msg.data = list;
  155. response.setContentType("application/json");
  156. response.setCharacterEncoding("UTF-8");
  157. response.getWriter().println(msg.toJSONStr());
  158. }
  159. /**
  160. * 小游戏抽奖列表后台查看
  161. *
  162. * @return
  163. */
  164. @RequestMapping(value = "/luckDrawListBack")
  165. public void luckDrawListBack(HttpServletResponse response)
  166. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  167. HttpRespMsg msg = new HttpRespMsg();
  168. PrizeRecordExample example = new PrizeRecordExample();
  169. example.createCriteria().andUidIsNotNull();
  170. List<PrizeRecord> list = prizeRecordMapper.selectByExample(example);
  171. msg.data = list;
  172. response.setContentType("application/json");
  173. response.setCharacterEncoding("UTF-8");
  174. response.getWriter().println(msg.toJSONStr());
  175. }
  176. /**
  177. * 小游戏抽奖填写用户信息 参数:phone:电话 username:姓名 id:中奖纪录id
  178. *
  179. * @return
  180. */
  181. @RequestMapping(value = "/luckDrawAddInfo")
  182. public void luckDrawAddInfo(PrizeRecord prizeRecord, HttpServletResponse response)
  183. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  184. HttpRespMsg msg = new HttpRespMsg();
  185. prizeRecordMapper.updateByPrimaryKeySelective(prizeRecord);
  186. msg.data = prizeRecord;
  187. response.setContentType("application/json");
  188. response.setCharacterEncoding("UTF-8");
  189. response.getWriter().println(msg.toJSONStr());
  190. }
  191. /**
  192. * 转发链接添加抽奖次数 参数: uid:用户id
  193. *
  194. * @return
  195. */
  196. @RequestMapping(value = "/luckDrawAddShare")
  197. public void luckDrawAddCount(PrizeRecord prizeRecord, HttpServletResponse response)
  198. throws Exception, KeyManagementException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
  199. HttpRespMsg msg = new HttpRespMsg();
  200. int count = prizeOpportunityMapper.selectCountByToday(prizeRecord.getUid());
  201. if (count == 0) {
  202. PrizeOpportunity prizeOpportunity = new PrizeOpportunity();
  203. prizeOpportunity.setIsShare(1);
  204. prizeOpportunity.setUid(prizeRecord.getUid());
  205. prizeOpportunityMapper.insert(prizeOpportunity);
  206. }
  207. response.setContentType("application/json");
  208. response.setCharacterEncoding("UTF-8");
  209. response.getWriter().println(msg.toJSONStr());
  210. }
  211. /**
  212. * 查看个人获奖信息
  213. *
  214. * 传递的参数: userId:用户id message:寄语
  215. *
  216. * @return
  217. * @throws IOException
  218. */
  219. @RequestMapping("/getluckDrawByUserId")
  220. public void getluckDrawByUserId(Integer UserId, HttpServletResponse response) throws IOException {
  221. HttpRespMsg msg = new HttpRespMsg();
  222. PrizeRecordExample exp = new PrizeRecordExample();
  223. exp.createCriteria().andUidEqualTo(UserId);
  224. List<PrizeRecord> list = prizeRecordMapper.selectByExample(exp);
  225. msg.data = list;
  226. response.setContentType("application/json");
  227. response.setCharacterEncoding("UTF-8");
  228. response.getWriter().println(msg.toJSONStr());
  229. }
  230. }