gunshi-project-ss/src/main/java/com/gunshi/project/xyt/service/OsmoticWarnRService.java

191 lines
7.2 KiB
Java
Raw Normal View History

2024-07-08 17:47:02 +08:00
package com.gunshi.project.xyt.service;
2024-07-09 14:54:21 +08:00
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
2024-07-09 14:54:21 +08:00
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2024-07-08 17:47:02 +08:00
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
2024-07-09 14:54:21 +08:00
import com.gunshi.project.xyt.entity.so.WarnPageSo;
import com.gunshi.project.xyt.entity.so.WarnSo;
import com.gunshi.project.xyt.entity.vo.OsmoticWarnVo;
2024-07-08 17:47:02 +08:00
import com.gunshi.project.xyt.mapper.OsmoticWarnRMapper;
import com.gunshi.project.xyt.mapper.OsmoticWarnRuleMapper;
import com.gunshi.project.xyt.model.OsmoticShiftR;
2024-07-08 17:47:02 +08:00
import com.gunshi.project.xyt.model.OsmoticWarnR;
import com.gunshi.project.xyt.model.OsmoticWarnRule;
2024-07-09 14:54:21 +08:00
import jakarta.annotation.Resource;
2024-07-08 17:47:02 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Date;
2024-07-09 14:54:21 +08:00
import java.util.List;
import java.util.Map;
import java.util.Optional;
2024-07-09 14:54:21 +08:00
import java.util.stream.Collectors;
2024-07-08 17:47:02 +08:00
/**
* :
* author: xusan
* date: 2024-07-08 17:30:37
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OsmoticWarnRService extends ServiceImpl<OsmoticWarnRMapper, OsmoticWarnR>
{
2024-07-09 14:54:21 +08:00
@Resource
private OsmoticWarnRuleMapper warnRuleMapper;
2024-07-08 17:47:02 +08:00
2024-07-09 14:54:21 +08:00
public Page<OsmoticWarnVo> queryPage(WarnPageSo warnPageSo) {
return this.baseMapper.queryPage(warnPageSo.getPageSo().toPage(),warnPageSo);
2024-07-09 14:54:21 +08:00
}
public Map<Integer, Long> stat(WarnSo warnSo) {
LambdaQueryWrapper<OsmoticWarnR> 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<OsmoticWarnR> list = this.list(wrapper);
return list.stream().collect(Collectors.groupingBy(OsmoticWarnR::getLevel, Collectors.counting()));
}
/**
*
*/
public void saveShiftWarn(OsmoticShiftR shiftR){
String stationCode = shiftR.getStationCode();
Date tm = shiftR.getTm();
BigDecimal x = shiftR.getX();
savePressWarn(stationCode,x,tm,"x");
BigDecimal y = shiftR.getY();
savePressWarn(stationCode,y,tm,"y");
BigDecimal h = shiftR.getH();
savePressWarn(stationCode,h,tm,"h");
}
/**
* /
* @param
*/
public void savePressWarn(String stationCode,BigDecimal value,Date tm,String direction) {
//查询预警规则,每个测点至多只有2条预警规则
LambdaQueryWrapper<OsmoticWarnRule> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OsmoticWarnRule::getStationCode,stationCode)
.eq(OsmoticWarnRule::getStatus,1);
if(StringUtils.isNotEmpty(direction)){
queryWrapper.eq(OsmoticWarnRule::getDirection,direction);
}
List<OsmoticWarnRule> ruleList = warnRuleMapper.selectList(queryWrapper);
if(CollectionUtils.isNotEmpty(ruleList)){
Boolean isRedWarn = false;
//先判断是否符合红色预警
Optional<OsmoticWarnRule> 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<OsmoticWarnRule> 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
* 11212
*/
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);
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;
}
2024-07-08 17:47:02 +08:00
}