GameLogServiceImpl.java 9.4 KB

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