DataCountService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package com.fdkankan.tk.service;
  2. import cn.hutool.core.date.DateUnit;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  5. import com.fdkankan.tk.common.PageInfo;
  6. import com.fdkankan.tk.entity.*;
  7. import com.fdkankan.tk.response.*;
  8. import com.fdkankan.tk.util.DataCountUtil;
  9. import com.fdkankan.tk.util.Dateutils;
  10. import com.google.common.collect.Lists;
  11. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  12. import org.apache.commons.lang3.StringUtils;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.stereotype.Service;
  15. import java.util.*;
  16. import java.util.stream.Collectors;
  17. @Service
  18. public class DataCountService {
  19. @Autowired
  20. IRoomService roomService;
  21. @Autowired
  22. IRoomVisitLogService roomVisitLogService;
  23. @Autowired
  24. IRoomShareLogService roomShareLogService;
  25. @Autowired
  26. IRoomDanmakuService roomDanmakuService;
  27. @Autowired
  28. IRoomNumService roomNumService;
  29. @Autowired
  30. ISceneService sceneService;
  31. @Autowired
  32. IRoomUserService roomUserService;
  33. @Autowired
  34. IWxUserService wxUserService;
  35. public Object roomData() {
  36. Long roomCount = roomService.count();
  37. Long visitManCount = roomVisitLogService.manCount();
  38. Long visitCount = roomVisitLogService.count();
  39. Long shareCount = roomShareLogService.count();
  40. RoomData roomData = new RoomData();
  41. roomData.setRoomCount(roomCount);
  42. roomData.setVisitManCount(visitManCount);
  43. roomData.setVisitCount(visitCount);
  44. roomData.setShareCount(shareCount);
  45. return roomData;
  46. }
  47. public Object takeLookTop5() {
  48. TakeLookTop5 takeLookTop5 = new TakeLookTop5();
  49. List<DataCount> takeLookList = new ArrayList<>();
  50. LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
  51. wrapper.orderByDesc(Room::getRoomViewCount);
  52. Page<Room> page = roomService.page(new Page<>(1, 5), wrapper);
  53. List<Room> roomList = page.getRecords();
  54. for (Room room : roomList) {
  55. DataCount dataCount = new DataCount();
  56. dataCount.setDataKey(room.getRoomTitle());
  57. dataCount.setDataCount(Long.valueOf(room.getRoomViewCount()));
  58. takeLookList.add(dataCount);
  59. }
  60. List<DataCount> danmakuList = roomDanmakuService.getDataCountTop5();
  61. takeLookTop5.setTakeLookList(takeLookList);
  62. takeLookTop5.setDanmakuList(danmakuList);
  63. return takeLookTop5;
  64. }
  65. public Object onlineTimeCount() {
  66. List<Integer> keys = DataCountUtil.timeShardingRes;
  67. List<DataCount> dataCounts = new ArrayList<>();
  68. for (Integer key : keys) {
  69. Long count = roomVisitLogService.getCountByTime(key);
  70. DataCount dataCount = new DataCount();
  71. dataCount.setDataKey(key.toString());
  72. dataCount.setDataCount(count);
  73. dataCounts.add(dataCount);
  74. }
  75. return dataCounts;
  76. }
  77. public Object roomVisitData(String roomId, String startTime, String endTime) {
  78. RoomVisitData roomVisitData = new RoomVisitData();
  79. List<DataCount> dbVisitList = roomVisitLogService.getByGroupRoomId("t_room_visit_log",roomId,startTime,endTime);
  80. List<DataCount> dbVisitListFmt = getDataCountListByList(dbVisitList, startTime, endTime);
  81. List<DataCount> dbSharList = roomVisitLogService.getByGroupRoomId("t_room_share_log",roomId,startTime,endTime);
  82. List<DataCount> dbSharListFmt = getDataCountListByList(dbSharList, startTime, endTime);
  83. List<DataCount> dbDanmakuList = roomVisitLogService.getByGroupRoomId("t_room_danmaku",roomId,startTime,endTime);
  84. List<DataCount> dbDanmakuListFmt = getDataCountListByList(dbDanmakuList, startTime, endTime);
  85. List<DataCount> dbDanmakuListD = roomVisitLogService.getByDGroupRoomId("t_room_danmaku",roomId,startTime,endTime);
  86. List<DataCount> dbDanmakuListFmtD = getDataCountListByList(dbDanmakuListD, startTime, endTime);
  87. roomVisitData.setUserVisitList(dbVisitListFmt);
  88. roomVisitData.setUserShareList(dbSharListFmt);
  89. roomVisitData.setUserMsgManList(dbDanmakuListFmtD);
  90. roomVisitData.setUserMsgCountList(dbDanmakuListFmt);
  91. return roomVisitData;
  92. }
  93. public List<DataCount> getDataCountListByList( List<DataCount> visitList,String startTime,String endTime){
  94. HashMap<String ,DataCount> map = new HashMap<>();
  95. visitList.forEach(entity -> map.put(entity.getDataKey(),entity));
  96. List<String> dates = Dateutils.findDatesStr(Dateutils.getDate(startTime), Dateutils.getDate(endTime),Dateutils.DAY );
  97. List<DataCount> dataCounts = new ArrayList<>();
  98. for (String date : dates) {
  99. DataCount dataCount = map.get(date);
  100. if(dataCount == null){
  101. dataCount = new DataCount();
  102. dataCount.setDataKey(date);
  103. dataCount.setDataCount(0L);
  104. }
  105. dataCounts.add(dataCount);
  106. }
  107. return dataCounts;
  108. }
  109. public Object roomDataList(String roomId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
  110. startTime = Dateutils.formatStartTime(startTime);
  111. endTime = Dateutils.formatEndTime(endTime);
  112. LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
  113. if(StringUtils.isNotBlank(roomId)){
  114. wrapper.eq(Room::getRoomId,roomId);
  115. }
  116. if(StringUtils.isNotBlank(startTime)){
  117. wrapper.ge(Room::getCreateTime,startTime);
  118. }
  119. if(StringUtils.isNotBlank(endTime)){
  120. wrapper.le(Room::getCreateTime,endTime);
  121. }
  122. wrapper.orderByDesc(Room::getCreateTime);
  123. List<RoomListDataVo> voList = new ArrayList<>();
  124. Page<Room> page = roomService.page(new Page<>(pageNum, pageSize), wrapper);
  125. List<String> roomIds = page.getRecords().stream().map(Room::getRoomId).collect(Collectors.toList());
  126. List<RoomNum> roomNumList = roomNumService.getListByRoomIds(roomIds);
  127. Set<String> numList = roomNumList.stream().map(RoomNum::getNum).collect(Collectors.toSet());
  128. HashMap<String,List<RoomNum>> numRoomMap = new HashMap<>();
  129. for (RoomNum roomNum : roomNumList) {
  130. if(numRoomMap.get(roomNum.getRoomId()) == null){
  131. numRoomMap.put(roomNum.getRoomId(),Arrays.asList(roomNum));
  132. }else {
  133. numRoomMap.get(roomNum.getRoomId()).add(roomNum);
  134. }
  135. }
  136. List<SceneVo> list = new ArrayList<>();
  137. HashMap<String,SceneVo> sceneMap = new HashMap<>();
  138. if(numList.size() >0){
  139. list = sceneService.getListByNumList(new ArrayList<>(numList));
  140. list.forEach(entity -> sceneMap.put(entity.getNum(),entity));
  141. }
  142. List<DataCount> roomList = roomVisitLogService.getGroupByRoomId();
  143. HashMap<String,Long> roomManMap = new HashMap<>();
  144. roomList.forEach(entity -> roomManMap.put(entity.getDataKey(),entity.getDataCount()));
  145. List<DataCount> roomShareList = roomShareLogService.getGroupByRoomId();
  146. HashMap<String,Long> roomShareMap = new HashMap<>();
  147. roomShareList.forEach(entity -> roomShareMap.put(entity.getDataKey(),entity.getDataCount()));
  148. for (Room record : page.getRecords()) {
  149. List<RoomNum> roomNums = numRoomMap.get(record.getRoomId());
  150. List<String> sceneTileList = new ArrayList<>();
  151. roomNums.forEach(entity -> {
  152. sceneTileList.add(sceneMap.get(entity.getNum()).getTitle());
  153. });
  154. List<RoomUser> roomUserList = roomUserService.getByRoomId(record.getRoomId());
  155. Long minute = Dateutils.getLongTime(record.getUseStartTime(), record.getUseEndTime(),DateUnit.MINUTE);
  156. for (RoomUser roomUser : roomUserList) {
  157. minute += Dateutils.getLongTime(roomUser.getUseStartTime(), roomUser.getUseEndTime(),DateUnit.MINUTE);
  158. }
  159. RoomListDataVo roomListDataVo = new RoomListDataVo();
  160. roomListDataVo.setRoomTitle(record.getRoomTitle());
  161. roomListDataVo.setSceneNameList(sceneTileList);
  162. roomListDataVo.setCreateTime(record.getCreateTime());
  163. roomListDataVo.setRoomStatus(record.getRoomStatus());
  164. roomListDataVo.setLookTime(minute);
  165. roomListDataVo.setLookManCount(roomManMap.get(record.getRoomId()) == null ? 0L:roomManMap.get(record.getRoomId()) );
  166. roomListDataVo.setShareCount(roomShareMap.get(record.getRoomId()) == null ? 0L:roomShareMap.get(record.getRoomId()) );
  167. voList.add(roomListDataVo);
  168. }
  169. Page<RoomListDataVo> pageVo = new Page<>(pageNum,pageSize);
  170. pageVo.setRecords(voList);
  171. pageVo.setTotal(page.getTotal());
  172. return PageInfo.PageInfo(pageVo);
  173. }
  174. public Object roomMsgList(String roomId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
  175. startTime = Dateutils.formatStartTime(startTime);
  176. endTime = Dateutils.formatEndTime(endTime);
  177. Page<UserMsgVo> page = roomDanmakuService.pageList(new Page<>(pageNum,pageSize) ,roomId,startTime,endTime);
  178. HashMap<String,List<RoomVisitLog>> roomVisitLogsMap = new HashMap<>();
  179. HashMap<String,List<RoomDanmaku>> danmakusMap = new HashMap<>();
  180. for (UserMsgVo record : page.getRecords()) {
  181. List<RoomVisitLog> roomVisitLogs = roomVisitLogsMap.get(record.getRoomId());
  182. if(roomVisitLogs == null){
  183. roomVisitLogs = roomVisitLogService.getByRoomId(record.getRoomId());
  184. roomVisitLogsMap.put(record.getRoomId(),roomVisitLogs);
  185. }
  186. Long time = 0L;
  187. Date firstOutTime = null;
  188. Date endOutTime = null;
  189. for (RoomVisitLog roomVisitLog : roomVisitLogs) {
  190. if(roomVisitLog.getOutRoomTime() == null || roomVisitLog.getInRoomTime() == null){
  191. continue;
  192. }
  193. if(firstOutTime == null){
  194. firstOutTime = roomVisitLog.getInRoomTime();
  195. }else {
  196. firstOutTime = firstOutTime.getTime() <= roomVisitLog.getInRoomTime().getTime() ? firstOutTime : roomVisitLog.getInRoomTime();
  197. }
  198. if(endOutTime == null){
  199. endOutTime = roomVisitLog.getOutRoomTime();
  200. }else {
  201. endOutTime = endOutTime.getTime() >= roomVisitLog.getOutRoomTime().getTime() ? endOutTime : roomVisitLog.getOutRoomTime();
  202. }
  203. time += (roomVisitLog.getOutRoomTime().getTime() - roomVisitLog.getInRoomTime().getTime());
  204. }
  205. List<RoomDanmaku> danmakus = danmakusMap.get(record.getRoomId());
  206. if(danmakus == null){
  207. danmakus = roomDanmakuService.getByRoomId(record.getRoomId());
  208. danmakusMap.put(record.getRoomId(),danmakus);
  209. }
  210. List<String> collect = danmakus.stream().map(RoomDanmaku::getText).collect(Collectors.toList());
  211. record.setOnlineTime(time/1000/60);
  212. record.setFirstInRoomTime(firstOutTime);
  213. record.setLastOutRoomTime(endOutTime);
  214. record.setTextCount(Long.valueOf(danmakus.size()));
  215. record.setTexts(collect);
  216. }
  217. return PageInfo.PageInfo(page);
  218. }
  219. }