gunshi-project-ss/src/main/java/com/gunshi/project/hsz/service/OsmoticWarnRuleService.java

188 lines
7.1 KiB
Java
Raw Normal View History

2025-07-17 15:26:39 +08:00
package com.gunshi.project.hsz.service;
2024-07-08 17:47:02 +08:00
2024-07-09 14:54:21 +08:00
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
2025-04-25 17:01:53 +08:00
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;
2025-07-17 15:26:39 +08:00
import com.gunshi.project.hsz.entity.so.WarnRulePageSo;
import com.gunshi.project.hsz.mapper.OsmoticWarnRuleMapper;
import com.gunshi.project.hsz.model.*;
2024-07-08 17:47:02 +08:00
import lombok.extern.slf4j.Slf4j;
2025-04-25 17:01:53 +08:00
import org.springframework.beans.factory.annotation.Autowired;
2024-07-08 17:47:02 +08:00
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
2025-04-25 17:01:53 +08:00
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
2024-07-08 17:47:02 +08:00
/**
* :
* author: xusan
* date: 2024-07-08 17:30:37
*/
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
2025-04-25 17:01:53 +08:00
public class OsmoticWarnRuleService extends ServiceImpl<OsmoticWarnRuleMapper, OsmoticWarnRule> {
@Autowired
private OsmoticWarnRService warnRService;
2024-07-08 17:47:02 +08:00
2024-07-09 14:54:21 +08:00
public Page<OsmoticWarnRule> queryPage(WarnRulePageSo warnRulePageSo) {
LambdaQueryWrapper<OsmoticWarnRule> queryWrapper = Wrappers.lambdaQuery();
2025-04-25 17:01:53 +08:00
if (warnRulePageSo.getType() != null) {
queryWrapper.eq(OsmoticWarnRule::getType, warnRulePageSo.getType());
2024-07-09 14:54:21 +08:00
}
2025-04-25 17:01:53 +08:00
if (StringUtils.isNotEmpty(warnRulePageSo.getStationCode())) {
queryWrapper.like(OsmoticWarnRule::getStationCode, warnRulePageSo.getStationCode());
2024-07-09 14:54:21 +08:00
}
queryWrapper.orderByDesc(OsmoticWarnRule::getStatus).orderByDesc(OsmoticWarnRule::getCreateTime);
2025-04-25 17:01:53 +08:00
return this.page(warnRulePageSo.getPageSo().toPage(), queryWrapper);
}
/*
value
*/
public void checkWarn(OsmoticPressR press) {
List<OsmoticWarnRule> rules = lambdaQuery().eq(OsmoticWarnRule::getStationCode, press.getStationCode())
.eq(OsmoticWarnRule::getType, OsmoticWarnRule.Type.PRESS.getType())
.eq(OsmoticWarnRule::getStatus, OsmoticWarnRule.Status.ENABLE.getStatus())
.list();
}
private boolean checkWarn(OsmoticPressR press, OsmoticWarnRule rule) {
BigDecimal valueToCompare = press.getValue();
OsmoticWarnRule.Relation matchRel = OsmoticWarnRule.Relation.match(rule.getCondition());
OsmoticWarnRule.Condition cond1 = OsmoticWarnRule.Condition.match(rule.getConditionOne());
if (cond1 == null) return false;
BigDecimal cond1value = rule.getValueOne();
if (cond1value == null) return false;
boolean cond1Violating = isViolating(valueToCompare, cond1, cond1value);
boolean cond2Violating = false;
if (matchRel != null) {
OsmoticWarnRule.Condition cond2 = OsmoticWarnRule.Condition.match(rule.getConditionTwo());
if (cond2 != null) {
BigDecimal cond2value = rule.getValueTwo();
if (cond2value != null) {
cond2Violating = isViolating(valueToCompare, cond2, cond2value);
}
}
}
boolean shoudWarn = false;
if (matchRel == null) {
if (cond1Violating) {
shoudWarn = true;
}
} else {
if (matchRel == OsmoticWarnRule.Relation.AND) {
if (cond1Violating && cond2Violating) {
shoudWarn = true;
}
} else if (matchRel == OsmoticWarnRule.Relation.OR) {
if (cond1Violating || cond2Violating) {
shoudWarn = true;
}
}
}
if (shoudWarn) {
insertWarn(press.getStationCode(), rule.getId(), press.getValue(), OsmoticWarnRule.Type.PRESS.getType(), rule.getLevel());
return true;
}
return false;
}
private void insertWarn(String stationCode, Long ruleId, BigDecimal value, Integer type, Integer level) {
OsmoticWarnR warn = new OsmoticWarnR();
warn.setId(IdWorker.getId());
warn.setTm(new Date());
warn.setStationCode(stationCode);
warn.setRuleId(ruleId);
warn.setValue(value);
warn.setType(type);
warn.setLevel(level);
warnRService.save(warn);
}
private boolean isViolating(BigDecimal value, OsmoticWarnRule.Condition cond, BigDecimal condValue) {
return switch (cond) {
case GT -> value.compareTo(condValue) > 0;
case GTE -> value.compareTo(condValue) >= 0;
case LT -> value.compareTo(condValue) < 0;
case LTE -> value.compareTo(condValue) <= 0;
case EQ -> value.compareTo(condValue) == 0;
case NE -> value.compareTo(condValue) != 0;
};
}
/*
xyh
*/
public void checkWarn(OsmoticShiftR shift) {
}
/*
q
*/
public void checkWarn(OsmoticFlowR flow) {
List<OsmoticWarnRule> rules = lambdaQuery().eq(OsmoticWarnRule::getStationCode, flow.getStationCode())
.eq(OsmoticWarnRule::getType, OsmoticWarnRule.Type.PRESS.getType())
.eq(OsmoticWarnRule::getStatus, OsmoticWarnRule.Status.ENABLE.getStatus())
.list();
BigDecimal valueToCompare = flow.getQ();
for (OsmoticWarnRule rule : rules) {
OsmoticWarnRule.Relation matchRel = OsmoticWarnRule.Relation.match(rule.getCondition());
OsmoticWarnRule.Condition cond1 = OsmoticWarnRule.Condition.match(rule.getConditionOne());
if (cond1 == null) continue;
BigDecimal cond1value = rule.getValueOne();
if (cond1value == null) continue;
boolean cond1Violating = isViolating(valueToCompare, cond1, cond1value);
boolean cond2Violating = false;
if (matchRel != null) {
OsmoticWarnRule.Condition cond2 = OsmoticWarnRule.Condition.match(rule.getConditionTwo());
if (cond2 != null) {
BigDecimal cond2value = rule.getValueTwo();
if (cond2value != null) {
cond2Violating = isViolating(valueToCompare, cond2, cond2value);
}
}
}
boolean shoudWarn = false;
if (matchRel == null) {
if (cond1Violating) {
shoudWarn = true;
}
} else {
if (matchRel == OsmoticWarnRule.Relation.AND) {
if (cond1Violating && cond2Violating) {
shoudWarn = true;
}
} else if (matchRel == OsmoticWarnRule.Relation.OR) {
if (cond1Violating || cond2Violating) {
shoudWarn = true;
}
}
}
if (shoudWarn) {
insertWarn(flow.getStationCode(), rule.getId(), flow.getQ(), OsmoticWarnRule.Type.PRESS.getType(), rule.getLevel());
}
}
2024-07-09 14:54:21 +08:00
}
2024-07-08 17:47:02 +08:00
}