GameLogServiceImpl.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.api.games.service.impl;
  2. import com.api.common.JSONUtils;
  3. import com.api.common.UtilFun;
  4. import com.api.core.response.Result;
  5. import com.api.core.response.ResultGenerator;
  6. import com.api.core.service.AbstractService;
  7. import com.api.games.dao.GameAnswerMapper;
  8. import com.api.games.dao.GameLogMapper;
  9. import com.api.games.dao.GamePlayTimeMapper;
  10. import com.api.games.model.GameAnswer;
  11. import com.api.games.model.GameLog;
  12. import com.api.games.model.GameLogDownload;
  13. import com.api.games.model.GameLogDownloadV2;
  14. import com.api.games.service.GameLogService;
  15. import com.github.pagehelper.PageHelper;
  16. import com.github.pagehelper.PageInfo;
  17. import org.springframework.stereotype.Service;
  18. import org.springframework.transaction.annotation.Transactional;
  19. import javax.annotation.Resource;
  20. import java.math.BigDecimal;
  21. import java.sql.Timestamp;
  22. import java.util.*;
  23. /**
  24. * Created by wanghuiwen on 2020/02/23.
  25. */
  26. @Service
  27. @Transactional
  28. public class GameLogServiceImpl extends AbstractService<GameLog> implements GameLogService {
  29. @Resource
  30. private GameLogMapper gameLogMapper;
  31. @Resource
  32. private GameAnswerMapper gameAnswerMapper;
  33. @Resource
  34. GamePlayTimeMapper gamePlayTimeMapper;
  35. @Override
  36. public Result listGameTime(Long uid) {
  37. return ResultGenerator.genSuccessResult(gamePlayTimeMapper.listUidOrderByPlayTime(uid));
  38. }
  39. @Override
  40. public Result list(String search, String ordermap, Integer page, Integer size) {
  41. Map<String, Object> params = JSONUtils.json2map(search);
  42. Map<String, Object> order = JSONUtils.json2map(ordermap);
  43. for (String key : order.keySet()) {
  44. if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
  45. if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
  46. }
  47. PageHelper.startPage(page, size);
  48. List<Map<String, Object>> res = gameLogMapper.list(params, order);
  49. for (Map<String, Object> item : res){
  50. long id = (Long) item.get("id");
  51. List<GameAnswer> list = gameAnswerMapper.listByGameIdNew((int)id);
  52. item.put("wrongRate", getRate(list, 2));
  53. item.put("missRate", getRate(list, 3));
  54. item.put("avgReaction", getAvgReaction(list));
  55. }
  56. PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
  57. return ResultGenerator.genSuccessResult(pageInfo);
  58. }
  59. private float getAvgReaction(List<GameAnswer> list){
  60. if (list != null) {
  61. float size = 0;
  62. float reaction = 0;
  63. for (GameAnswer item : list){
  64. if (item.getReaction().intValue() != -1) {
  65. reaction += item.getReaction().floatValue();
  66. size += 1;
  67. }
  68. }
  69. return reaction == 0 ? 0 : reaction / size;
  70. } else {
  71. return 0;
  72. }
  73. }
  74. private float getRate(List<GameAnswer> list, int answer){
  75. if (list != null) {
  76. float size = 0;
  77. for (GameAnswer item : list){
  78. if (item.getCorrect() == answer) {
  79. size += 1;
  80. }
  81. }
  82. return size == 0 ? 0 : size / list.size();
  83. } else {
  84. return 0;
  85. }
  86. }
  87. @Override
  88. public Result detail(Integer id) {
  89. // List<Map<String, Object>> res = gameAnswerMapper.listByGameId(id);
  90. // Map<String, Object> params = new HashMap<>();
  91. // params.put("gameId",id);
  92. // Map<String, Object> statistics = gameAnswerMapper.statistics(params);
  93. // if(statistics == null ){
  94. // statistics = new HashMap<>();
  95. // }
  96. // statistics.put("list",res);
  97. return ResultGenerator.genSuccessResult(gameAnswerMapper.listByGameIdNew(id));
  98. }
  99. /**
  100. * 每天 早13:00 午:17:00;晚:21:00 执行
  101. *
  102. * @return
  103. */
  104. @Override
  105. public Result notPerformed() {
  106. Calendar calendar = Calendar.getInstance();
  107. int hour = calendar.get(Calendar.HOUR_OF_DAY);
  108. int type = 0;
  109. if (hour == 13) {
  110. type = 1;
  111. }
  112. if (hour == 17) {
  113. type = 2;
  114. }
  115. if (hour == 21) {
  116. type = 3;
  117. }
  118. if (type != 0) {
  119. List<GameLog> res = gameLogMapper.notPerformed(type);
  120. if (res != null && res.size() > 0) save(res);
  121. }
  122. return ResultGenerator.genSuccessResult();
  123. }
  124. @Override
  125. public List<GameLogDownloadV2> newDownload(String search, String orderMap) {
  126. Map<String, Object> params = JSONUtils.json2map(search);
  127. Map<String, Object> order = JSONUtils.json2map(orderMap);
  128. for (String key : order.keySet()) {
  129. if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
  130. if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
  131. }
  132. List<Map<String, Object>> gameLogs = gameLogMapper.list(params, order);
  133. List<GameLogDownloadV2> result = new ArrayList<>();
  134. for (int i = 0; i < gameLogs.size(); i ++){
  135. GameLogDownloadV2 item = new GameLogDownloadV2();
  136. Map<String, Object> gameLog = gameLogs.get(i);
  137. item.nickName = gameLog.get("nickname").toString();
  138. Date date = new Date(((Timestamp) gameLog.get("startTime")).getTime());
  139. Date endDate = new Date(((Timestamp) gameLog.get("endTime")).getTime());
  140. item.date = UtilFun.DateToString(date, UtilFun.YMD);
  141. item.sign = gameLog.get("sign").toString();
  142. item.version = gameLog.get("version").toString();
  143. item.rightRate = (((BigDecimal)gameLog.get("realCorrectRate")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
  144. item.right = (((BigDecimal)gameLog.get("correctRate")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
  145. item.trust = (((BigDecimal)gameLog.get("confidence")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
  146. item.startTime = UtilFun.DateToString(date, UtilFun.HHMMSS);
  147. item.finishTime = UtilFun.DateToString(endDate, UtilFun.HHMMSS);
  148. item.finish = (((BigDecimal)gameLog.get("schedule")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
  149. item.deviceId = gameLog.get("deviceId").toString();
  150. item.number = gameLog.get("selected").toString();
  151. item.gameAnswers = gameAnswerMapper.listByGameIdNew(((Long)gameLog.get("id")).intValue());
  152. item.wrongRate = (getRate(item.gameAnswers, 2) * 100) + "%";
  153. item.missRate = (getRate(item.gameAnswers, 3) * 100) + "%";
  154. item.reaction = getAvgReaction(item.gameAnswers) + " ms";
  155. result.add(item);
  156. }
  157. // List<Map<String, Object>> logs = gameAnswerMapper.list(params, order);
  158. //
  159. // List<GameLogDownload> result = new ArrayList<>();
  160. //
  161. // for (int i = 0; i < logs.size(); i ++){
  162. // GameLogDownload item = new GameLogDownload();
  163. // item.sign = (String) logs.get(i).get("sign");
  164. // item.userName = (String) logs.get(i).get("nickname");
  165. // item.version = (String) logs.get(i).get("version");
  166. //
  167. // Date date = new Date(((Timestamp) logs.get(i).get("startTime")).getTime());
  168. // Date end = new Date(((Timestamp) logs.get(i).get("endTime")).getTime());
  169. // item.date = UtilFun.DateToString(date, UtilFun.YMD);
  170. // item.startDate = UtilFun.DateToString(date, UtilFun.HHMMSS);
  171. // item.endDate = UtilFun.DateToString(end, UtilFun.HHMMSS);
  172. //
  173. // item.schedule = String.valueOf((((BigDecimal)logs.get(i).get("schedule")).floatValue() * 100));
  174. // item.number = (String) logs.get(i).get("selected");
  175. // item.showNumber = String.valueOf((Integer) logs.get(i).get("showNum"));
  176. // item.answer = ((Integer) logs.get(i).get("answer")) == 1 ? "按下" : "没按下";
  177. //
  178. // int right = (Integer) logs.get(i).get("correct");
  179. // item.right = right == 1 ? "正确" : right == 2 ? "错误" : "错失";
  180. // item.sumRight = String.valueOf((((BigDecimal)logs.get(i).get("correctRate")).floatValue() * 100));
  181. // item.reaction = String.valueOf((((BigDecimal)logs.get(i).get("reaction")).intValue()));
  182. // item.avgReaction = String.valueOf(gameAnswerMapper.getAvgReaction((Long)logs.get(i).get("gid")));
  183. // result.add(item);
  184. // }
  185. return result;
  186. }
  187. @Override
  188. public List<Map<String, Object>> download(String search,String ordermap) {
  189. Map<String, Object> params = JSONUtils.json2map(search);
  190. Map<String, Object> order = JSONUtils.json2map(ordermap);
  191. for (String key : order.keySet()) {
  192. if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
  193. if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
  194. }
  195. return gameAnswerMapper.list(params, order);
  196. }
  197. public Map<String,Object> download(String search) {
  198. Map<String, Object> params = JSONUtils.json2map(search);
  199. return gameAnswerMapper.statistics(params);
  200. }
  201. }