package com.gunshi.project.hsz.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.hsz.entity.so.WarnRulePageSo; import com.gunshi.project.hsz.mapper.OsmoticWarnRuleMapper; import com.gunshi.project.hsz.model.*; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.Date; import java.util.List; /** * 描述: 预警规则配置表 * author: xusan * date: 2024-07-08 17:30:37 */ @Service @Slf4j @Transactional(rollbackFor = Exception.class) public class OsmoticWarnRuleService extends ServiceImpl { @Autowired private OsmoticWarnRService warnRService; public Page queryPage(WarnRulePageSo warnRulePageSo) { LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); if (warnRulePageSo.getType() != null) { queryWrapper.eq(OsmoticWarnRule::getType, warnRulePageSo.getType()); } if (StringUtils.isNotEmpty(warnRulePageSo.getStationCode())) { queryWrapper.like(OsmoticWarnRule::getStationCode, warnRulePageSo.getStationCode()); } queryWrapper.orderByDesc(OsmoticWarnRule::getStatus).orderByDesc(OsmoticWarnRule::getCreateTime); return this.page(warnRulePageSo.getPageSo().toPage(), queryWrapper); } /* 渗压只看管水位value */ public void checkWarn(OsmoticPressR press) { List 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; }; } /* 位移要看x,y,h */ public void checkWarn(OsmoticShiftR shift) { } /* 渗流只看流量q */ public void checkWarn(OsmoticFlowR flow) { List 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()); } } } }