perf: 优化规则查询

dev
huangfeng 1 year ago
parent 834133dff2
commit 3831bd1399

@ -60,8 +60,8 @@ public class WarnRuleController extends BasicController {
@GetMapping("listAll")
@ApiOperation("查询全部列表")
public Response<List<WarnRule>> listAll() throws Exception {
List<WarnRule> result = service.listAll();
public Response<List<WarnRule>> listAll(Integer sensorId) throws Exception {
List<WarnRule> result = service.listAll(sensorId);
return Response.success(result);
}

@ -10,6 +10,6 @@ public class Constants {
public static final Integer TRUE = 1;
public static final Integer FALSE = 0;
public static String FloatCompare = "FloatCompare";
public static String IntCompare = "IntCompare";
public static String FloatCompare = "float";
public static String IntCompare = "int";
}

@ -5,8 +5,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface WarnRuleRepository extends JpaRepository<WarnRule, Integer>, JpaSpecificationExecutor<WarnRule> {
List<WarnRule> findBySensorId(Integer sensorId);
}

@ -6,7 +6,7 @@ import java.util.List;
public interface WarnRuleService {
List<WarnRule> listAll() throws Exception;
List<WarnRule> listAll(Integer sensorId) throws Exception;
WarnRule add(WarnRule item) throws Exception;

@ -35,17 +35,21 @@ public class WarnRuleServiceImpl implements WarnRuleService {
DataService dataService;
@Override
public List<WarnRule> listAll() throws Exception {
List<WarnRule> list = repository.findAll();
public List<WarnRule> listAll(Integer sensorId) throws Exception {
List<WarnRule> list;
if (sensorId != null) {
list = repository.findBySensorId(sensorId);
} else {
list = repository.findAll();
}
for (WarnRule item : list) {
NSensor sensor = sensorService.detail(item.getSensorId());
item.setNSensor(sensor);
Optional<ModevTypePoint> optionalModevTypePoint = typePointRepository.findById(item.getModevtypePointId());
if (!optionalModevTypePoint.isPresent()) {
throw new BusinessException("未找到该监测装置属性点" + item.getModevtypePointId());
}
if (optionalModevTypePoint.isPresent()) {
item.setTypePoint(optionalModevTypePoint.get());
}
}
return list;
}
@ -56,7 +60,7 @@ public class WarnRuleServiceImpl implements WarnRuleService {
NSensor sensor = sensorService.detail(item.getSensorId());
Optional<ModevTypePoint> optionalModevTypePoint = typePointRepository.findById(item.getModevtypePointId());
if (!optionalModevTypePoint.isPresent()) {
throw new BusinessException("未找到该监测装置属性点" + item.getModevtypePointId());
throw new BusinessException("未找到该监测装置类型(" + sensor.getTypeName() + ")的属性点" + item.getModevtypePointId());
}
ModevTypePoint typePoint = optionalModevTypePoint.get();
Map<String, Object> map = dataService.getLastOneData(sensor, typePoint);

@ -39,7 +39,7 @@ public class CacheTask {
@Scheduled(initialDelay = 60000, fixedDelay = 60000)
private void refreshRule() {
try {
List<WarnRule> list = ruleService.listAll();
List<WarnRule> list = ruleService.listAll(null);
for (WarnRule item : list) {
WarnRule rule = rule_Cache.get(item.getId());
if (rule == null) {

Loading…
Cancel
Save