package com.gunshi.project.ss.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.IdWorker; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.gunshi.project.ss.entity.so.WarnPageSo; import com.gunshi.project.ss.entity.so.WarnSo; import com.gunshi.project.ss.entity.vo.OsmoticWarnVo; import com.gunshi.project.ss.mapper.OsmoticWarnRMapper; import com.gunshi.project.ss.mapper.OsmoticWarnRuleMapper; import com.gunshi.project.ss.common.model.JcskGnssR; import com.gunshi.project.ss.model.OsmoticWarnR; import com.gunshi.project.ss.model.OsmoticWarnRule; import com.gunshi.project.ss.util.DateTransforUtil; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; /** * 描述: 隐患预警记录表 * author: xusan * date: 2024-07-08 17:30:37 */ @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class OsmoticWarnRService extends ServiceImpl { @Resource private OsmoticWarnRuleMapper warnRuleMapper; @Resource private MessageCenterService messageCenterService; public Page queryPage(WarnPageSo warnPageSo) { return this.baseMapper.queryPage(warnPageSo.getPageSo().toPage(),warnPageSo); } public Map stat(WarnSo warnSo) { LambdaQueryWrapper wrapper = Wrappers.lambdaQuery(); if(warnSo.getType() != null){ wrapper.eq(OsmoticWarnR::getType,warnSo.getType()); } if(warnSo.getLevel() != null){ wrapper.eq(OsmoticWarnR::getLevel,warnSo.getLevel()); } if(StringUtils.isNotEmpty(warnSo.getStationCode())){ wrapper.like(OsmoticWarnR::getStationCode,warnSo.getStationCode()); } if(warnSo.getDateTimeRangeSo() != null && warnSo.getDateTimeRangeSo().getStart() != null){ wrapper.ge(OsmoticWarnR::getTm,warnSo.getDateTimeRangeSo().getStart()); } if(warnSo.getDateTimeRangeSo() != null && warnSo.getDateTimeRangeSo().getEnd() != null){ wrapper.le(OsmoticWarnR::getTm,warnSo.getDateTimeRangeSo().getEnd()); } List list = this.list(wrapper); return list.stream().collect(Collectors.groupingBy(OsmoticWarnR::getLevel, Collectors.counting())); } /** * 生成位移预警 */ public void saveShiftWarn(JcskGnssR jcskGnssR){ String stationCode = jcskGnssR.getCd(); LocalDateTime tm = jcskGnssR.getTm(); Date date = DateTransforUtil.transforLocalDateTimeToDate(tm); BigDecimal x = jcskGnssR.getDe(); savePressWarn(stationCode,x,date,"x"); BigDecimal y = jcskGnssR.getDn(); savePressWarn(stationCode,y,date,"y"); BigDecimal h = jcskGnssR.getDu(); savePressWarn(stationCode,h,date,"h"); } /** * 生成渗压/渗流预警 * @param */ public void savePressWarn(String stationCode,BigDecimal value,Date tm,String direction) { //查询预警规则,每个测点至多只有2条预警规则 LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(OsmoticWarnRule::getStationCode,stationCode) .eq(OsmoticWarnRule::getStatus,1); if(StringUtils.isNotEmpty(direction)){ queryWrapper.eq(OsmoticWarnRule::getDirection,direction); } List ruleList = warnRuleMapper.selectList(queryWrapper); if(CollectionUtils.isNotEmpty(ruleList)){ Boolean isRedWarn = false; //先判断是否符合红色预警 Optional red = ruleList.stream().filter(o -> o.getLevel() == 2).findFirst(); if(red.isPresent()){ OsmoticWarnRule redRule = red.get(); isRedWarn = saveWarnData(redRule,value,tm); if (isRedWarn){ return; } //没有生成红色预警,再判断是否符合黄色预警 Optional yellow = ruleList.stream().filter(o -> o.getLevel() == 1).findFirst(); if(yellow.isPresent()){ OsmoticWarnRule yellowRule = yellow.get(); saveWarnData(yellowRule,value,tm); } }else { OsmoticWarnRule yellowRule = ruleList.get(0); saveWarnData(yellowRule,value,tm); } } } private Boolean saveWarnData(OsmoticWarnRule rule,BigDecimal value,Date tm){ String conditionOne = rule.getConditionOne(); BigDecimal valueOne = rule.getValueOne(); Integer condition = rule.getCondition(); Boolean flagOne = getFlag(conditionOne,valueOne,value); /** * 没有第二条件时,条件1满足就生成预警 * 有第二条件时,当为或时,条件1满足就生成预警,条件1不满足就判断条件2是否满足;当为且时,需条件1和条件2同时满足才生成预警 */ if(condition == null){ if(flagOne){ //生成预警 return geneWarn(rule,value,tm); } }else{ String conditionTwo = rule.getConditionTwo(); BigDecimal valueTwo = rule.getValueTwo(); Boolean flagTwo = getFlag(conditionTwo,valueTwo,value); if(condition == 1){ if(flagOne && flagTwo){ //生成预警 return geneWarn(rule,value,tm); } }else{ if(flagOne || flagTwo){ //生成预警 return geneWarn(rule,value,tm); } } } return false; } private Boolean geneWarn(OsmoticWarnRule rule,BigDecimal value,Date tm) { OsmoticWarnR warnR = new OsmoticWarnR(); warnR.setId(IdWorker.getId()); warnR.setStationCode(rule.getStationCode()); warnR.setRuleId(rule.getId()); warnR.setValue(value); warnR.setTm(tm); warnR.setType(rule.getType()); warnR.setLevel(rule.getLevel()); this.baseMapper.insert(warnR); //生成消息 messageCenterService.osmoticWarnMes(warnR,rule.getRuleDesc()); return true; } private Boolean getFlag(String condition,BigDecimal ruleValue,BigDecimal value){ Boolean flag = false; switch (condition) { case ">" : flag = value.compareTo(ruleValue) > 0; break; case ">=" : flag = value.compareTo(ruleValue) >= 0; break; case "<" : flag = value.compareTo(ruleValue) < 0; break; case "<=" : flag = value.compareTo(ruleValue) <= 0; break; case "=" : flag = value.compareTo(ruleValue) == 0; break; case "!=" : flag = value.compareTo(ruleValue) != 0; break; } return flag; } public OsmoticWarnR queryMaxTmByType(Integer press) { return this.baseMapper.queryMaxTmByType(press); } }