package com.gkhy.fourierSpecialGasMonitor.service.impl; import com.gkhy.fourierSpecialGasMonitor.commons.domain.Result; import com.gkhy.fourierSpecialGasMonitor.commons.domain.SearchResult; import com.gkhy.fourierSpecialGasMonitor.commons.enums.ResultCode; import com.gkhy.fourierSpecialGasMonitor.commons.exception.BusinessException; import com.gkhy.fourierSpecialGasMonitor.commons.model.PageQuery; import com.gkhy.fourierSpecialGasMonitor.domain.account.entity.User; import com.gkhy.fourierSpecialGasMonitor.domain.account.enums.UserStatusEnum; import com.gkhy.fourierSpecialGasMonitor.domain.account.repository.jpa.UserRepository; import com.gkhy.fourierSpecialGasMonitor.entity.GasWarnLog; import com.gkhy.fourierSpecialGasMonitor.entity.MonitorDailyReport; import com.gkhy.fourierSpecialGasMonitor.entity.query.FindDailyReportPageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.query.FindGasWarnLogPageQuery; import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindDailyReportPageRespDTO; import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasWarnLogPageRespDTO; import com.gkhy.fourierSpecialGasMonitor.entity.resp.FindGasWarnLogSmsUserPageRespDTO; import com.gkhy.fourierSpecialGasMonitor.enums.WarnHandleStatusEnum; import com.gkhy.fourierSpecialGasMonitor.repository.GasWarnLogRepository; import com.gkhy.fourierSpecialGasMonitor.service.GasWarnLogService; import com.gkhy.fourierSpecialGasMonitor.utils.ThreadLocalUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.stream.Collectors; /** * @author Mr.huang * @decription * @date 2023/8/9 16:47 */ @Service public class GasWarnLogServiceImpl implements GasWarnLogService { @Autowired private GasWarnLogRepository gasWarnLogRepository; @Autowired private UserRepository userRepository; private User getCurrentUser(){ Long userId = ThreadLocalUtil.get().getId(); User user = userRepository.findUserByIdAndStatus(userId, UserStatusEnum.STATUS_ACTIVE.getStatus()); if (user == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL.getCode(),"未成功获取用户信息"); return user; } @Override public Result findGasWarnLogPage(PageQuery pageQuery) { if (pageQuery == null || pageQuery.getPageIndex() == null || pageQuery.getPageSize() == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL,"分页参数不能为空"); } Pageable pageable = PageRequest.of(pageQuery.getPageIndex()-1, pageQuery.getPageSize(), Sort.Direction.DESC, "warnTime"); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { Set predicateList = new HashSet<>(); FindGasWarnLogPageQuery searchParams = pageQuery.getSearchParams(); if (searchParams != null && searchParams.getStartTime() != null && searchParams.getEndTime() != null){ predicateList.add(criteriaBuilder.between(root.get("gmtCreate").as(LocalDateTime.class),searchParams.getStartTime(),searchParams.getEndTime())); } if (searchParams != null && searchParams.getStatus() != null){ predicateList.add(criteriaBuilder.equal(root.get("status").as(Byte.class), searchParams.getStatus())); } if (searchParams != null && searchParams.getGasCategoryId() != null){ predicateList.add(criteriaBuilder.equal(root.get("gasCategoryId").as(Integer.class), searchParams.getGasCategoryId())); } if (searchParams != null && searchParams.getGasThresholdId() != null){ predicateList.add(criteriaBuilder.equal(root.get("gasThresholdId").as(Long.class), searchParams.getGasThresholdId())); } return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); } }; SearchResult> searchResult = new SearchResult<>(); searchResult.setPageIndex(pageQuery.getPageIndex()); searchResult.setPageSize(pageQuery.getPageSize()); searchResult.setSuccess(); Page pageResult = gasWarnLogRepository.findAll(specification,pageable); searchResult.setTotal(pageResult.getTotalElements()); searchResult.setPages(pageResult.getTotalPages()); if (!CollectionUtils.isEmpty(pageResult.getContent())){ List respDTOS = pageResult.getContent().stream().map(gasWarnLog -> { FindGasWarnLogPageRespDTO dto = new FindGasWarnLogPageRespDTO(); BeanUtils.copyProperties(gasWarnLog, dto); if (!CollectionUtils.isEmpty(gasWarnLog.getGasWarnLogSmsUsers())) { List gasWarnLogSmsUsers = new ArrayList<>(); BeanUtils.copyProperties(gasWarnLog.getGasWarnLogSmsUsers(), gasWarnLogSmsUsers); dto.setGasWarnLogSmsUsers(gasWarnLogSmsUsers); } return dto; }).collect(Collectors.toList()); searchResult.setData(respDTOS); } return searchResult; } @Override public Result handleGasWarnLog(Long id) { User currentUser = getCurrentUser(); if (id == null){ throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL,"参数不能为空"); } GasWarnLog gasWarnLog = gasWarnLogRepository.findByIdAndStatus(id, WarnHandleStatusEnum.HANDLE_NO.getStatus()); if (gasWarnLog == null) throw new BusinessException(this.getClass(), ResultCode.PARAM_ERROR_NULL,"预警信息不存在"); gasWarnLog.setHandlerId(currentUser.getId()); gasWarnLog.setHandlerName(currentUser.getName()); gasWarnLog.setHandlerRealName(currentUser.getRealName()); gasWarnLog.setHandlerTime(LocalDateTime.now()); gasWarnLog.setStatus(WarnHandleStatusEnum.HANDLE_YES.getStatus()); GasWarnLog save = gasWarnLogRepository.save(gasWarnLog); if (save == null) throw new BusinessException(this.getClass(), ResultCode.SYSTEM_ERROR_DATABASE_FAIL,"预警信息处理失败"); return Result.success(); } @Override public GasWarnLog save(GasWarnLog gasWarnLog) { return gasWarnLogRepository.save(gasWarnLog); } @Override public List listYesterday() { // 获取当前时间 LocalDateTime now = LocalDateTime.now(); // 获取昨天的日期 LocalDateTime yesterday = now.minusDays(1); // 获取昨天的0点时间(即凌晨) LocalDateTime yesterdayStart = LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MIN); // 获取昨天的24点时间(即今天的凌晨) LocalDateTime yesterdayEnd = LocalDateTime.of(yesterday.toLocalDate(), LocalTime.MAX); Specification specification = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { Set predicateList = new HashSet<>(); predicateList.add(criteriaBuilder.between(root.get("warnTime").as(LocalDateTime.class),yesterdayStart,yesterdayEnd)); return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()])); } }; List warnLogs = gasWarnLogRepository.findAll(specification); return warnLogs; } }