| | |
| | | import com.gkhy.hazmat.common.utils.SecurityUtils; |
| | | import com.gkhy.hazmat.common.utils.StringUtils; |
| | | import com.gkhy.hazmat.system.domain.*; |
| | | import com.gkhy.hazmat.system.domain.vo.HzCompanyMessage; |
| | | import com.gkhy.hazmat.system.domain.vo.HzEntryRecordVO; |
| | | import com.gkhy.hazmat.system.domain.vo.HzHazmatUseVO; |
| | | import com.gkhy.hazmat.system.domain.vo.HzHomeDataVO; |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public List<HzEntryRecordVO> dayUseStatistic(Long companyId) { |
| | | SysUser currentUser = SecurityUtils.getLoginUser().getUser(); |
| | | checkUserAllowed(currentUser); |
| | | Date currentDate = new Date(); |
| | | |
| | | // 设置当天的8点作为开始时间,20点作为结束时间 |
| | | DateTime beginOfDay = DateUtil.beginOfDay(currentDate); // 当天零点 |
| | | |
| | | // 从零点偏移8小时得到8点,偏移20小时得到20点 |
| | | DateTime startTime = DateUtil.offsetHour(beginOfDay, 8); // 08:00:00 |
| | | DateTime endTime = DateUtil.offsetHour(beginOfDay, 21); // 20:00:00 |
| | | |
| | | String startDate = DateUtil.format(startTime, DatePattern.NORM_DATETIME_FORMAT); |
| | | String endDate = DateUtil.format(endTime, DatePattern.NORM_DATETIME_FORMAT); |
| | | |
| | | // 设置分表ID逻辑 |
| | | if (currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode())) { |
| | | IdTableNameHandler.setCurrentId(companyId); |
| | | } else { |
| | | IdTableNameHandler.setCurrentId(currentUser.getCompanyId()); |
| | | } |
| | | |
| | | // 生成每小时的统计列表,初始count为0 |
| | | List<HzEntryRecordVO> hourEntryList = new ArrayList<>(); |
| | | DateTime currentHour = startTime; |
| | | Integer i = hazmatMapper.countTotal(currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode()) ? companyId:currentUser.getCompanyId()); |
| | | while (currentHour.isBefore(endTime)) { // 从8点开始到20点前,共12个小时 |
| | | String hour = DateUtil.format(currentHour, "HH"); // 两位小时格式,如08,09,...,19 |
| | | HzEntryRecordVO entryRecordVO = new HzEntryRecordVO(); |
| | | entryRecordVO.setHour(hour); |
| | | entryRecordVO.setCount(0); |
| | | entryRecordVO.setTotalCount(i); |
| | | hourEntryList.add(entryRecordVO); |
| | | currentHour = DateUtil.offsetHour(currentHour, 1); // 增加一小时 |
| | | } |
| | | // 查询数据库,按小时统计使用量 |
| | | List<HzEntryRecordVO> entryRecordVOList = hazmatMapper.useCountHourlyStatic(startDate, endDate, currentUser.getUserType().equals(UserTypeEnum.CHECK_USER.getCode()) ? companyId:currentUser.getCompanyId() |
| | | ); |
| | | hourEntryList.get(0).setTotalCount(i); |
| | | IdTableNameHandler.removeCurrentId(); |
| | | // 将查询结果合并到初始化的每小时列表中 |
| | | if (!entryRecordVOList.isEmpty()) { |
| | | Map<String, HzEntryRecordVO> resMap = entryRecordVOList.stream() |
| | | .collect(Collectors.toMap(HzEntryRecordVO::getHour, item -> item)); |
| | | for (HzEntryRecordVO hourEntry : hourEntryList) { |
| | | HzEntryRecordVO matched = resMap.get(hourEntry.getHour()); |
| | | if (matched != null) { |
| | | hourEntry.setCount(matched.getCount()); |
| | | } |
| | | } |
| | | } |
| | | return hourEntryList; |
| | | } |
| | | |
| | | @Override |
| | | public List<HzCompanyMessage> companyMessage(Long type) { |
| | | return hazmatMapper.companyMessage(type); |
| | | } |
| | | |
| | | |
| | | private void setExcelResponseProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { |
| | | //设置内容类型 |
| | | response.setContentType("application/vnd.vnd.ms-excel"); |