perf: 优化规则查询

dev
huangfeng 1 year ago
parent 834133dff2
commit 3831bd1399

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

@ -10,6 +10,6 @@ public class Constants {
public static final Integer TRUE = 1; public static final Integer TRUE = 1;
public static final Integer FALSE = 0; public static final Integer FALSE = 0;
public static String FloatCompare = "FloatCompare"; public static String FloatCompare = "float";
public static String IntCompare = "IntCompare"; 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.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List;
@Repository @Repository
public interface WarnRuleRepository extends JpaRepository<WarnRule, Integer>, JpaSpecificationExecutor<WarnRule> { 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 { public interface WarnRuleService {
List<WarnRule> listAll() throws Exception; List<WarnRule> listAll(Integer sensorId) throws Exception;
WarnRule add(WarnRule item) throws Exception; WarnRule add(WarnRule item) throws Exception;

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

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

Loading…
Cancel
Save