123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- package com.fdkankan.tk.service;
- import cn.hutool.core.date.DateUnit;
- import cn.hutool.core.date.DateUtil;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.tk.common.PageInfo;
- import com.fdkankan.tk.entity.*;
- import com.fdkankan.tk.response.*;
- import com.fdkankan.tk.util.DataCountUtil;
- import com.fdkankan.tk.util.Dateutils;
- import com.google.common.collect.Lists;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- import java.util.stream.Collectors;
- @Service
- public class DataCountService {
- @Autowired
- IRoomService roomService;
- @Autowired
- IRoomVisitLogService roomVisitLogService;
- @Autowired
- IRoomShareLogService roomShareLogService;
- @Autowired
- IRoomDanmakuService roomDanmakuService;
- @Autowired
- IRoomNumService roomNumService;
- @Autowired
- ISceneService sceneService;
- @Autowired
- IRoomUserService roomUserService;
- @Autowired
- IWxUserService wxUserService;
- public Object roomData() {
- Long roomCount = roomService.count();
- Long visitManCount = roomVisitLogService.manCount();
- Long visitCount = roomVisitLogService.count();
- Long shareCount = roomShareLogService.count();
- RoomData roomData = new RoomData();
- roomData.setRoomCount(roomCount);
- roomData.setVisitManCount(visitManCount);
- roomData.setVisitCount(visitCount);
- roomData.setShareCount(shareCount);
- return roomData;
- }
- public Object takeLookTop5() {
- TakeLookTop5 takeLookTop5 = new TakeLookTop5();
- List<DataCount> takeLookList = new ArrayList<>();
- LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
- wrapper.orderByDesc(Room::getRoomViewCount);
- Page<Room> page = roomService.page(new Page<>(1, 5), wrapper);
- List<Room> roomList = page.getRecords();
- for (Room room : roomList) {
- DataCount dataCount = new DataCount();
- dataCount.setDataKey(room.getRoomTitle());
- dataCount.setDataCount(Long.valueOf(room.getRoomViewCount()));
- takeLookList.add(dataCount);
- }
- List<DataCount> danmakuList = roomDanmakuService.getDataCountTop5();
- takeLookTop5.setTakeLookList(takeLookList);
- takeLookTop5.setDanmakuList(danmakuList);
- return takeLookTop5;
- }
- public Object onlineTimeCount() {
- List<Integer> keys = DataCountUtil.timeShardingRes;
- List<DataCount> dataCounts = new ArrayList<>();
- for (Integer key : keys) {
- Long count = roomVisitLogService.getCountByTime(key);
- DataCount dataCount = new DataCount();
- dataCount.setDataKey(key.toString());
- dataCount.setDataCount(count);
- dataCounts.add(dataCount);
- }
- return dataCounts;
- }
- public Object roomVisitData(String roomId, String startTime, String endTime) {
- RoomVisitData roomVisitData = new RoomVisitData();
- List<DataCount> dbVisitList = roomVisitLogService.getByGroupRoomId("t_room_visit_log",roomId,startTime,endTime);
- List<DataCount> dbVisitListFmt = getDataCountListByList(dbVisitList, startTime, endTime);
- List<DataCount> dbSharList = roomVisitLogService.getByGroupRoomId("t_room_share_log",roomId,startTime,endTime);
- List<DataCount> dbSharListFmt = getDataCountListByList(dbSharList, startTime, endTime);
- List<DataCount> dbDanmakuList = roomVisitLogService.getByGroupRoomId("t_room_danmaku",roomId,startTime,endTime);
- List<DataCount> dbDanmakuListFmt = getDataCountListByList(dbDanmakuList, startTime, endTime);
- List<DataCount> dbDanmakuListD = roomVisitLogService.getByDGroupRoomId("t_room_danmaku",roomId,startTime,endTime);
- List<DataCount> dbDanmakuListFmtD = getDataCountListByList(dbDanmakuListD, startTime, endTime);
- roomVisitData.setUserVisitList(dbVisitListFmt);
- roomVisitData.setUserShareList(dbSharListFmt);
- roomVisitData.setUserMsgManList(dbDanmakuListFmtD);
- roomVisitData.setUserMsgCountList(dbDanmakuListFmt);
- return roomVisitData;
- }
- public List<DataCount> getDataCountListByList( List<DataCount> visitList,String startTime,String endTime){
- HashMap<String ,DataCount> map = new HashMap<>();
- visitList.forEach(entity -> map.put(entity.getDataKey(),entity));
- List<String> dates = Dateutils.findDatesStr(Dateutils.getDate(startTime), Dateutils.getDate(endTime),Dateutils.DAY );
- List<DataCount> dataCounts = new ArrayList<>();
- for (String date : dates) {
- DataCount dataCount = map.get(date);
- if(dataCount == null){
- dataCount = new DataCount();
- dataCount.setDataKey(date);
- dataCount.setDataCount(0L);
- }
- dataCounts.add(dataCount);
- }
- return dataCounts;
- }
- public Object roomDataList(String roomId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
- startTime = Dateutils.formatStartTime(startTime);
- endTime = Dateutils.formatEndTime(endTime);
- LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
- if(StringUtils.isNotBlank(roomId)){
- wrapper.eq(Room::getRoomId,roomId);
- }
- if(StringUtils.isNotBlank(startTime)){
- wrapper.ge(Room::getCreateTime,startTime);
- }
- if(StringUtils.isNotBlank(endTime)){
- wrapper.le(Room::getCreateTime,endTime);
- }
- wrapper.orderByDesc(Room::getCreateTime);
- List<RoomListDataVo> voList = new ArrayList<>();
- Page<Room> page = roomService.page(new Page<>(pageNum, pageSize), wrapper);
- List<String> roomIds = page.getRecords().stream().map(Room::getRoomId).collect(Collectors.toList());
- List<RoomNum> roomNumList = roomNumService.getListByRoomIds(roomIds);
- Set<String> numList = roomNumList.stream().map(RoomNum::getNum).collect(Collectors.toSet());
- HashMap<String,List<RoomNum>> numRoomMap = new HashMap<>();
- for (RoomNum roomNum : roomNumList) {
- if(numRoomMap.get(roomNum.getRoomId()) == null){
- numRoomMap.put(roomNum.getRoomId(),Arrays.asList(roomNum));
- }else {
- numRoomMap.get(roomNum.getRoomId()).add(roomNum);
- }
- }
- List<SceneVo> list = new ArrayList<>();
- HashMap<String,SceneVo> sceneMap = new HashMap<>();
- if(numList.size() >0){
- list = sceneService.getListByNumList(new ArrayList<>(numList));
- list.forEach(entity -> sceneMap.put(entity.getNum(),entity));
- }
- List<DataCount> roomList = roomVisitLogService.getGroupByRoomId();
- HashMap<String,Long> roomManMap = new HashMap<>();
- roomList.forEach(entity -> roomManMap.put(entity.getDataKey(),entity.getDataCount()));
- List<DataCount> roomShareList = roomShareLogService.getGroupByRoomId();
- HashMap<String,Long> roomShareMap = new HashMap<>();
- roomShareList.forEach(entity -> roomShareMap.put(entity.getDataKey(),entity.getDataCount()));
- for (Room record : page.getRecords()) {
- List<RoomNum> roomNums = numRoomMap.get(record.getRoomId());
- List<String> sceneTileList = new ArrayList<>();
- roomNums.forEach(entity -> {
- sceneTileList.add(sceneMap.get(entity.getNum()).getTitle());
- });
- List<RoomUser> roomUserList = roomUserService.getByRoomId(record.getRoomId());
- Long minute = Dateutils.getLongTime(record.getUseStartTime(), record.getUseEndTime(),DateUnit.MINUTE);
- for (RoomUser roomUser : roomUserList) {
- minute += Dateutils.getLongTime(roomUser.getUseStartTime(), roomUser.getUseEndTime(),DateUnit.MINUTE);
- }
- RoomListDataVo roomListDataVo = new RoomListDataVo();
- roomListDataVo.setRoomTitle(record.getRoomTitle());
- roomListDataVo.setSceneNameList(sceneTileList);
- roomListDataVo.setCreateTime(record.getCreateTime());
- roomListDataVo.setRoomStatus(record.getRoomStatus());
- roomListDataVo.setLookTime(minute);
- roomListDataVo.setLookManCount(roomManMap.get(record.getRoomId()) == null ? 0L:roomManMap.get(record.getRoomId()) );
- roomListDataVo.setShareCount(roomShareMap.get(record.getRoomId()) == null ? 0L:roomShareMap.get(record.getRoomId()) );
- voList.add(roomListDataVo);
- }
- Page<RoomListDataVo> pageVo = new Page<>(pageNum,pageSize);
- pageVo.setRecords(voList);
- pageVo.setTotal(page.getTotal());
- return PageInfo.PageInfo(pageVo);
- }
- public Object roomMsgList(String roomId, String startTime, String endTime, Integer pageNum, Integer pageSize) {
- startTime = Dateutils.formatStartTime(startTime);
- endTime = Dateutils.formatEndTime(endTime);
- Page<UserMsgVo> page = roomDanmakuService.pageList(new Page<>(pageNum,pageSize) ,roomId,startTime,endTime);
- HashMap<String,List<RoomVisitLog>> roomVisitLogsMap = new HashMap<>();
- HashMap<String,List<RoomDanmaku>> danmakusMap = new HashMap<>();
- for (UserMsgVo record : page.getRecords()) {
- List<RoomVisitLog> roomVisitLogs = roomVisitLogsMap.get(record.getRoomId());
- if(roomVisitLogs == null){
- roomVisitLogs = roomVisitLogService.getByRoomId(record.getRoomId());
- roomVisitLogsMap.put(record.getRoomId(),roomVisitLogs);
- }
- Long time = 0L;
- Date firstOutTime = null;
- Date endOutTime = null;
- for (RoomVisitLog roomVisitLog : roomVisitLogs) {
- if(roomVisitLog.getOutRoomTime() == null || roomVisitLog.getInRoomTime() == null){
- continue;
- }
- if(firstOutTime == null){
- firstOutTime = roomVisitLog.getInRoomTime();
- }else {
- firstOutTime = firstOutTime.getTime() <= roomVisitLog.getInRoomTime().getTime() ? firstOutTime : roomVisitLog.getInRoomTime();
- }
- if(endOutTime == null){
- endOutTime = roomVisitLog.getOutRoomTime();
- }else {
- endOutTime = endOutTime.getTime() >= roomVisitLog.getOutRoomTime().getTime() ? endOutTime : roomVisitLog.getOutRoomTime();
- }
- time += (roomVisitLog.getOutRoomTime().getTime() - roomVisitLog.getInRoomTime().getTime());
- }
- List<RoomDanmaku> danmakus = danmakusMap.get(record.getRoomId());
- if(danmakus == null){
- danmakus = roomDanmakuService.getByRoomId(record.getRoomId());
- danmakusMap.put(record.getRoomId(),danmakus);
- }
- List<String> collect = danmakus.stream().map(RoomDanmaku::getText).collect(Collectors.toList());
- record.setOnlineTime(time/1000/60);
- record.setFirstInRoomTime(firstOutTime);
- record.setLastOutRoomTime(endOutTime);
- record.setTextCount(Long.valueOf(danmakus.size()));
- record.setTexts(collect);
- }
- return PageInfo.PageInfo(page);
- }
- }
|