123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- package com.api.games.service.impl;
- import com.api.common.JSONUtils;
- import com.api.common.UtilFun;
- import com.api.core.response.Result;
- import com.api.core.response.ResultGenerator;
- import com.api.core.service.AbstractService;
- import com.api.games.dao.GameAnswerMapper;
- import com.api.games.dao.GameLogMapper;
- import com.api.games.dao.GamePlayTimeMapper;
- import com.api.games.model.GameAnswer;
- import com.api.games.model.GameLog;
- import com.api.games.model.GameLogDownload;
- import com.api.games.model.GameLogDownloadV2;
- import com.api.games.service.GameLogService;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import java.math.BigDecimal;
- import java.sql.Timestamp;
- import java.util.*;
- /**
- * Created by wanghuiwen on 2020/02/23.
- */
- @Service
- @Transactional
- public class GameLogServiceImpl extends AbstractService<GameLog> implements GameLogService {
- @Resource
- private GameLogMapper gameLogMapper;
- @Resource
- private GameAnswerMapper gameAnswerMapper;
- @Resource
- GamePlayTimeMapper gamePlayTimeMapper;
- @Override
- public Result listGameTime(Long uid) {
- return ResultGenerator.genSuccessResult(gamePlayTimeMapper.listUidOrderByPlayTime(uid));
- }
- @Override
- public Result list(String search, String ordermap, Integer page, Integer size) {
- Map<String, Object> params = JSONUtils.json2map(search);
- Map<String, Object> order = JSONUtils.json2map(ordermap);
- for (String key : order.keySet()) {
- if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
- if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
- }
- PageHelper.startPage(page, size);
- List<Map<String, Object>> res = gameLogMapper.list(params, order);
- for (Map<String, Object> item : res){
- long id = (Long) item.get("id");
- List<GameAnswer> list = gameAnswerMapper.listByGameIdNew((int)id);
- item.put("wrongRate", getRate(list, 2));
- item.put("missRate", getRate(list, 3));
- item.put("avgReaction", getAvgReaction(list));
- }
- PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(res);
- return ResultGenerator.genSuccessResult(pageInfo);
- }
- private float getAvgReaction(List<GameAnswer> list){
- if (list != null) {
- float size = 0;
- float reaction = 0;
- for (GameAnswer item : list){
- if (item.getReaction().intValue() != -1) {
- reaction += item.getReaction().floatValue();
- size += 1;
- }
- }
- return reaction == 0 ? 0 : reaction / size;
- } else {
- return 0;
- }
- }
- private float getRate(List<GameAnswer> list, int answer){
- if (list != null) {
- float size = 0;
- for (GameAnswer item : list){
- if (item.getCorrect() == answer) {
- size += 1;
- }
- }
- return size == 0 ? 0 : size / list.size();
- } else {
- return 0;
- }
- }
- @Override
- public Result detail(Integer id) {
- // List<Map<String, Object>> res = gameAnswerMapper.listByGameId(id);
- // Map<String, Object> params = new HashMap<>();
- // params.put("gameId",id);
- // Map<String, Object> statistics = gameAnswerMapper.statistics(params);
- // if(statistics == null ){
- // statistics = new HashMap<>();
- // }
- // statistics.put("list",res);
- return ResultGenerator.genSuccessResult(gameAnswerMapper.listByGameIdNew(id));
- }
- /**
- * 每天 早13:00 午:17:00;晚:21:00 执行
- *
- * @return
- */
- @Override
- public Result notPerformed() {
- Calendar calendar = Calendar.getInstance();
- int hour = calendar.get(Calendar.HOUR_OF_DAY);
- int type = 0;
- if (hour == 13) {
- type = 1;
- }
- if (hour == 17) {
- type = 2;
- }
- if (hour == 21) {
- type = 3;
- }
- if (type != 0) {
- List<GameLog> res = gameLogMapper.notPerformed(type);
- if (res != null && res.size() > 0) save(res);
- }
- return ResultGenerator.genSuccessResult();
- }
- @Override
- public List<GameLogDownloadV2> newDownload(String search, String orderMap) {
- Map<String, Object> params = JSONUtils.json2map(search);
- Map<String, Object> order = JSONUtils.json2map(orderMap);
- for (String key : order.keySet()) {
- if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
- if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
- }
- List<Map<String, Object>> gameLogs = gameLogMapper.list(params, order);
- List<GameLogDownloadV2> result = new ArrayList<>();
- for (int i = 0; i < gameLogs.size(); i ++){
- GameLogDownloadV2 item = new GameLogDownloadV2();
- Map<String, Object> gameLog = gameLogs.get(i);
- item.nickName = gameLog.get("nickname").toString();
- Date date = new Date(((Timestamp) gameLog.get("startTime")).getTime());
- Date endDate = new Date(((Timestamp) gameLog.get("endTime")).getTime());
- item.date = UtilFun.DateToString(date, UtilFun.YMD);
- item.sign = gameLog.get("sign").toString();
- item.version = gameLog.get("version").toString();
- item.rightRate = (((BigDecimal)gameLog.get("realCorrectRate")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
- item.right = (((BigDecimal)gameLog.get("correctRate")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
- item.trust = (((BigDecimal)gameLog.get("confidence")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
- item.startTime = UtilFun.DateToString(date, UtilFun.HHMMSS);
- item.finishTime = UtilFun.DateToString(endDate, UtilFun.HHMMSS);
- item.finish = (((BigDecimal)gameLog.get("schedule")).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() * 100) + "%";
- item.deviceId = gameLog.get("deviceId").toString();
- item.number = gameLog.get("selected").toString();
- item.gameAnswers = gameAnswerMapper.listByGameIdNew(((Long)gameLog.get("id")).intValue());
- item.wrongRate = (getRate(item.gameAnswers, 2) * 100) + "%";
- item.missRate = (getRate(item.gameAnswers, 3) * 100) + "%";
- item.reaction = getAvgReaction(item.gameAnswers) + " ms";
- result.add(item);
- }
- // List<Map<String, Object>> logs = gameAnswerMapper.list(params, order);
- //
- // List<GameLogDownload> result = new ArrayList<>();
- //
- // for (int i = 0; i < logs.size(); i ++){
- // GameLogDownload item = new GameLogDownload();
- // item.sign = (String) logs.get(i).get("sign");
- // item.userName = (String) logs.get(i).get("nickname");
- // item.version = (String) logs.get(i).get("version");
- //
- // Date date = new Date(((Timestamp) logs.get(i).get("startTime")).getTime());
- // Date end = new Date(((Timestamp) logs.get(i).get("endTime")).getTime());
- // item.date = UtilFun.DateToString(date, UtilFun.YMD);
- // item.startDate = UtilFun.DateToString(date, UtilFun.HHMMSS);
- // item.endDate = UtilFun.DateToString(end, UtilFun.HHMMSS);
- //
- // item.schedule = String.valueOf((((BigDecimal)logs.get(i).get("schedule")).floatValue() * 100));
- // item.number = (String) logs.get(i).get("selected");
- // item.showNumber = String.valueOf((Integer) logs.get(i).get("showNum"));
- // item.answer = ((Integer) logs.get(i).get("answer")) == 1 ? "按下" : "没按下";
- //
- // int right = (Integer) logs.get(i).get("correct");
- // item.right = right == 1 ? "正确" : right == 2 ? "错误" : "错失";
- // item.sumRight = String.valueOf((((BigDecimal)logs.get(i).get("correctRate")).floatValue() * 100));
- // item.reaction = String.valueOf((((BigDecimal)logs.get(i).get("reaction")).intValue()));
- // item.avgReaction = String.valueOf(gameAnswerMapper.getAvgReaction((Long)logs.get(i).get("gid")));
- // result.add(item);
- // }
- return result;
- }
- @Override
- public List<Map<String, Object>> download(String search,String ordermap) {
- Map<String, Object> params = JSONUtils.json2map(search);
- Map<String, Object> order = JSONUtils.json2map(ordermap);
- for (String key : order.keySet()) {
- if (order.get(key) != null && order.get(key).equals("ascending")) order.put(key, "asc");
- if (order.get(key) != null && order.get(key).equals("descending")) order.put(key, "desc");
- }
- return gameAnswerMapper.list(params, order);
- }
- public Map<String,Object> download(String search) {
- Map<String, Object> params = JSONUtils.json2map(search);
- return gameAnswerMapper.statistics(params);
- }
- }
|