|
|
@@ -0,0 +1,168 @@
|
|
|
+package com.xunmei.iot.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmRule;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleExpress;
|
|
|
+import com.xunmei.common.core.domain.mediator.domain.IotAlarmSystemField;
|
|
|
+import com.xunmei.common.core.util.BeanHelper;
|
|
|
+import com.xunmei.common.core.utils.IDHelper;
|
|
|
+import com.xunmei.common.core.utils.KeyValue;
|
|
|
+import com.xunmei.common.security.utils.SecurityUtils;
|
|
|
+import com.xunmei.iot.dto.alarm.IotAlarmRuleDto;
|
|
|
+import com.xunmei.iot.dto.alarm.IotAlarmRuleExpressDto;
|
|
|
+import com.xunmei.iot.mapper.IotAlarmRuleMapper;
|
|
|
+import com.xunmei.iot.mapper.IotAlarmSystemFieldMapper;
|
|
|
+import com.xunmei.iot.service.IotAlarmRuleExpressService;
|
|
|
+import com.xunmei.iot.service.IotAlarmRuleService;
|
|
|
+import com.xunmei.iot.vo.alarm.IotAlarmRuleEditVo;
|
|
|
+import com.xunmei.iot.vo.alarm.IotAlarmRuleExpressEditVo;
|
|
|
+import com.xunmei.iot.vo.alarm.IotAlarmSystemFieldVo;
|
|
|
+import com.xunmei.iot.vo.alarm.IotInitAlarmRuleVo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class IotAlarmRuleServiceImpl extends ServiceImpl<IotAlarmRuleMapper, IotAlarmRule> implements IotAlarmRuleService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotAlarmSystemFieldMapper iotAlarmSystemFieldMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IotAlarmRuleExpressService iotAlarmRuleExpressService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotInitAlarmRuleVo getAddInitAlarmRuleData()throws Exception {
|
|
|
+
|
|
|
+ IotInitAlarmRuleVo ruleVo = new IotInitAlarmRuleVo();
|
|
|
+ LambdaQueryWrapper<IotAlarmSystemField> lqw = new LambdaQueryWrapper<IotAlarmSystemField>().eq(IotAlarmSystemField::getEnable, 1).orderByDesc(IotAlarmSystemField::getId);
|
|
|
+ List<IotAlarmSystemField> systemFieldList = iotAlarmSystemFieldMapper.selectList(lqw);
|
|
|
+ //
|
|
|
+ List<IotAlarmSystemFieldVo> appAlarmSystemFieldVoList = new ArrayList<>();
|
|
|
+ for(IotAlarmSystemField systemField:systemFieldList){
|
|
|
+ IotAlarmSystemFieldVo appAlarmSystemFieldVo = new IotAlarmSystemFieldVo();
|
|
|
+ BeanHelper.copyProperties(appAlarmSystemFieldVo, systemField);
|
|
|
+ List<KeyValue> specsList = new ArrayList<>();
|
|
|
+ List<KeyValue> operatorsList = new ArrayList<>();
|
|
|
+ if(systemField.getType().equals("ENUM")){
|
|
|
+ String specs = systemField.getSpecs();
|
|
|
+ JSONObject obj = JSON.parseObject(specs);
|
|
|
+ Iterator iterator = obj.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Map.Entry entry = (Map.Entry) iterator.next();
|
|
|
+ KeyValue keyValue = new KeyValue();
|
|
|
+ keyValue.setKey(entry.getKey());
|
|
|
+ keyValue.setValue(entry.getValue());
|
|
|
+ specsList.add(keyValue);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String operators = systemField.getOperators();
|
|
|
+ JSONObject obj = JSON.parseObject(operators);
|
|
|
+ Iterator iterator = obj.entrySet().iterator();
|
|
|
+ while (iterator.hasNext()){
|
|
|
+ Map.Entry entry = (Map.Entry) iterator.next();
|
|
|
+ KeyValue keyValue = new KeyValue();
|
|
|
+ keyValue.setKey(entry.getKey());
|
|
|
+ keyValue.setValue(entry.getValue());
|
|
|
+ operatorsList.add(keyValue);
|
|
|
+ }
|
|
|
+ appAlarmSystemFieldVo.setSpecsList(specsList);
|
|
|
+ appAlarmSystemFieldVo.setOperatorsList(operatorsList);
|
|
|
+ appAlarmSystemFieldVoList.add(appAlarmSystemFieldVo);
|
|
|
+ }
|
|
|
+ Map<Integer,List<IotAlarmSystemFieldVo>> appAlarmSystemFieldVoMapList = appAlarmSystemFieldVoList.stream().collect(Collectors.groupingBy(r->r.getSourceType()));
|
|
|
+ List<Map<String,Object>> mapList = new ArrayList<>();
|
|
|
+ for(Integer key:appAlarmSystemFieldVoMapList.keySet()){
|
|
|
+ Map<String,Object> map = new HashMap<>();
|
|
|
+ List<IotAlarmSystemFieldVo> systemFieldVos = appAlarmSystemFieldVoMapList.get(key);
|
|
|
+ map.put("sourceTypeDes",systemFieldVos.get(0).getSourceTypeDes());
|
|
|
+ map.put("sourceType",systemFieldVos.get(0).getSourceType());
|
|
|
+ List<Map<String,Object>> systemFieldsList = new ArrayList<>();
|
|
|
+ for(IotAlarmSystemFieldVo vo:systemFieldVos){
|
|
|
+ Map<String,Object> mapa = new HashMap<>();
|
|
|
+ mapa.put("sysFieldCode",vo.getSysFieldCode());
|
|
|
+ mapa.put("name",vo.getName());
|
|
|
+ mapa.put("specsList",vo.getSpecsList());
|
|
|
+ mapa.put("operatorsList",vo.getOperatorsList());
|
|
|
+ mapa.put("type",vo.getType());
|
|
|
+ mapa.put("typeDes",vo.getTypeDes());
|
|
|
+ mapa.put("unit",vo.getUnit());
|
|
|
+ systemFieldsList.add(mapa);
|
|
|
+ }
|
|
|
+ map.put("systemFields",systemFieldsList);
|
|
|
+ mapList.add(map);
|
|
|
+ }
|
|
|
+ ruleVo.setSourceTypeList(mapList);
|
|
|
+ return ruleVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IotInitAlarmRuleVo getAlarmRuleData(Long ruleId) throws Exception {
|
|
|
+ IotInitAlarmRuleVo ruleVo = getAddInitAlarmRuleData();
|
|
|
+ IotAlarmRule appAlarmRule = this.baseMapper.selectById(ruleId);
|
|
|
+
|
|
|
+ IotAlarmRuleEditVo ruleEditVo = new IotAlarmRuleEditVo();
|
|
|
+ BeanHelper.copyProperties(ruleEditVo, appAlarmRule);
|
|
|
+ ruleVo.setIotAlarmRule(appAlarmRule);
|
|
|
+
|
|
|
+ List<IotAlarmRuleExpress> appAlarmRuleExpressList =iotAlarmRuleExpressService.list(new LambdaQueryWrapper<IotAlarmRuleExpress>().eq(IotAlarmRuleExpress::getRuleId,ruleId));
|
|
|
+ List<IotAlarmRuleExpressEditVo> appAlarmRuleExpressEditVos = new ArrayList<>();
|
|
|
+ for(IotAlarmRuleExpress express:appAlarmRuleExpressList){
|
|
|
+ IotAlarmRuleExpressEditVo expressNew = new IotAlarmRuleExpressEditVo();
|
|
|
+ BeanHelper.copyProperties(expressNew, express);
|
|
|
+ appAlarmRuleExpressEditVos.add(expressNew);
|
|
|
+ }
|
|
|
+ Map<Integer,List<IotAlarmRuleExpressEditVo>> appAlarmSystemFieldVoMapList = appAlarmRuleExpressEditVos.stream().collect(Collectors.groupingBy(r->r.getSourceType()));
|
|
|
+ List<Map<String,Object>> mapList = new ArrayList<>();
|
|
|
+ for(Integer key:appAlarmSystemFieldVoMapList.keySet()){
|
|
|
+ Map<String,Object> map = new HashMap();
|
|
|
+ map.put("sourceType",key);
|
|
|
+ map.put("AppAlarmRuleExpressList",appAlarmSystemFieldVoMapList.get(key));
|
|
|
+ mapList.add(map);
|
|
|
+ }
|
|
|
+ ruleVo.setIotAlarmRuleExpressEditVosBySourceType(mapList);
|
|
|
+ return ruleVo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveOrUpdateAlarmRule(IotAlarmRuleDto ruleDto) throws Exception {
|
|
|
+ IotAlarmRule rule = new IotAlarmRule();
|
|
|
+ BeanHelper.copyProperties(rule, ruleDto);
|
|
|
+ if(rule.getId()==null){//新增
|
|
|
+ rule.setId(IDHelper.id());
|
|
|
+ rule.setCreateTime(LocalDateTime.now());
|
|
|
+ rule.setCreateBy(SecurityUtils.getUsername());
|
|
|
+ rule.setUpdateBy(SecurityUtils.getUsername());
|
|
|
+ rule.setUpdateTime(LocalDateTime.now());
|
|
|
+ }else{//修改
|
|
|
+ rule.setUpdateBy(SecurityUtils.getUsername());
|
|
|
+ rule.setUpdateTime(LocalDateTime.now());
|
|
|
+ }
|
|
|
+ List<IotAlarmRuleExpressDto> appAlarmRuleExpressDtos = ruleDto.getIotAlarmRuleExpressDtoList();
|
|
|
+ List<IotAlarmRuleExpress> appAlarmRuleExpressesList = new ArrayList<>();
|
|
|
+ if(appAlarmRuleExpressDtos!=null&&appAlarmRuleExpressDtos.size()>0){
|
|
|
+ for(IotAlarmRuleExpressDto vo:appAlarmRuleExpressDtos){
|
|
|
+ IotAlarmRuleExpress express = new IotAlarmRuleExpress();
|
|
|
+ BeanHelper.copyProperties(express, vo);
|
|
|
+ express.setRuleId(rule.getId());
|
|
|
+ express.setId(IDHelper.id());
|
|
|
+ express.setCreateTime(LocalDateTime.now());
|
|
|
+ express.setUpdateBy(SecurityUtils.getUsername());
|
|
|
+ express.setUpdateTime(LocalDateTime.now());
|
|
|
+ appAlarmRuleExpressesList.add(express);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Set<Integer> sourceTypes = new HashSet<>();
|
|
|
+ appAlarmRuleExpressesList.forEach(r->sourceTypes.add(r.getSourceType()));
|
|
|
+ this.saveOrUpdate(rule);
|
|
|
+ //规则先删除原来的然重新插入
|
|
|
+ iotAlarmRuleExpressService.remove(new LambdaQueryWrapper<IotAlarmRuleExpress>().eq(IotAlarmRuleExpress::getRuleId,rule.getId()));
|
|
|
+ iotAlarmRuleExpressService.saveBatch(appAlarmRuleExpressesList);
|
|
|
+ }
|
|
|
+}
|