|
@@ -3,10 +3,14 @@ package com.zcxk.water.web.service.impl;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.zcxk.core.common.enums.StatusEnum;
|
|
|
import com.zcxk.core.common.exception.BusinessException;
|
|
|
import com.zcxk.core.common.util.BeanCopyUtils;
|
|
|
import com.zcxk.core.oauth2.pojo.LoginUser;
|
|
|
import com.zcxk.core.oauth2.util.UserUtil;
|
|
|
+import com.zcxk.iot.sync.dto.AlarmDataDTO;
|
|
|
+import com.zcxk.iot.sync.dto.AlarmOfflineDTO;
|
|
|
+import com.zcxk.iot.sync.service.SyncAlarmService;
|
|
|
import com.zcxk.water.api.dto.AlarmRuleDTO;
|
|
|
import com.zcxk.water.api.dto.AlarmTypeDTO;
|
|
|
import com.zcxk.water.api.dto.AlarmTypePageDto;
|
|
@@ -17,9 +21,11 @@ import com.zcxk.water.core.dao.AlarmRuleMapper;
|
|
|
import com.zcxk.water.core.dao.AlarmTypeMapper;
|
|
|
import com.zcxk.water.core.entity.AlarmRule;
|
|
|
import com.zcxk.water.core.entity.AlarmType;
|
|
|
+import com.zcxk.water.web.enums.AlarmCategoryEnum;
|
|
|
import com.zcxk.water.web.service.AlarmTypeService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
@@ -27,6 +33,7 @@ import org.springframework.util.CollectionUtils;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -43,8 +50,8 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
@Resource
|
|
|
private AlarmRuleMapper alarmRuleMapper;
|
|
|
|
|
|
- //@Autowired
|
|
|
- //private SyncAlarmService syncAlarmService;
|
|
|
+ @Autowired
|
|
|
+ private SyncAlarmService syncAlarmService;
|
|
|
|
|
|
@Override
|
|
|
public int insertSelective(AlarmType record) {
|
|
@@ -76,20 +83,29 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public void add(AlarmTypeDTO dto) {
|
|
|
- Long iotAlarmId;
|
|
|
log.info("begin addAlarmType alarmType:" + JSON.toJSONString(dto));
|
|
|
+ Long iotAlarmId = null;
|
|
|
LoginUser loginUser = UserUtil.getCurrentUser();
|
|
|
AlarmType alarmType = BeanCopyUtils.copy(dto, AlarmType.class);
|
|
|
setUserInfo(loginUser, alarmType, dto.getRules());
|
|
|
// 设备告警
|
|
|
- /*if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
|
|
|
- iotAlarmId = syncAlarmService.addDataWaringConfig(null);
|
|
|
+ if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
|
|
|
+ AlarmDataDTO dataDTO = BeanCopyUtils.copy(dto, AlarmDataDTO.class);
|
|
|
+ dataDTO.setCustomerId(loginUser.getCustomerId());
|
|
|
+ dataDTO.setDeviceId(dto.getIotDeviceId());
|
|
|
+ dataDTO.setProductId(dto.getIotProductId());
|
|
|
+ iotAlarmId = syncAlarmService.addDataWaringConfig(dataDTO);
|
|
|
} else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == dto.getAlarmCategory()) {
|
|
|
- iotAlarmId = syncAlarmService.addOfflineConfig(null);
|
|
|
- } else {
|
|
|
- throw BusinessException.builder(500, "未匹配");
|
|
|
+ AlarmOfflineDTO dataDTO = BeanCopyUtils.copy(dto, AlarmOfflineDTO.class);
|
|
|
+ dataDTO.setCustomerId(loginUser.getCustomerId());
|
|
|
+ dataDTO.setDeviceId(dto.getIotDeviceId());
|
|
|
+ dataDTO.setProductId(dto.getIotProductId());
|
|
|
+ iotAlarmId = syncAlarmService.addOfflineConfig(dataDTO);
|
|
|
+ }
|
|
|
+ if (iotAlarmId == null || iotAlarmId == 0) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_ADD_ALARM_ERROR);
|
|
|
}
|
|
|
- alarmType.setIotAlarmId(iotAlarmId);*/
|
|
|
+ alarmType.setIotAlarmId(iotAlarmId);
|
|
|
int result = this.insertSelective(alarmType);
|
|
|
// 批量加入告警规则
|
|
|
batchInsertAlarmRule(alarmType, dto.getRules());
|
|
@@ -102,16 +118,39 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
public void edit(AlarmTypeDTO dto) {
|
|
|
log.info("begin editAlarmType alarmType:" + JSON.toJSONString(dto));
|
|
|
LoginUser loginUser = UserUtil.getCurrentUser();
|
|
|
+ Long iotAlarmId = null;
|
|
|
+ AlarmType alarm = alarmTypeMapper.findById(dto.getId());
|
|
|
+ if (alarm == null) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
|
|
|
+ }
|
|
|
if (!nameUnique(dto.getId(), loginUser.getTenantId(), dto.getName())){
|
|
|
throw BusinessException.builder(WaterErrorEnum.ALARM_NAME_ALREADY_EXISTS);
|
|
|
}
|
|
|
AlarmType alarmType = BeanCopyUtils.copy(dto, AlarmType.class);
|
|
|
if (dto.getRules() != null){
|
|
|
alarmType.setAlarmRuleNum(dto.getRules().size());
|
|
|
- }else {
|
|
|
+ } else {
|
|
|
alarmType.setAlarmRuleNum(0);
|
|
|
}
|
|
|
- alarmType.setUpdateDate(LocalDateTime.now());
|
|
|
+ if (AlarmCategoryEnum.DATA_ALARM.getType() == dto.getAlarmCategory()) {
|
|
|
+ AlarmDataDTO dataDTO = BeanCopyUtils.copy(dto, AlarmDataDTO.class);
|
|
|
+ dataDTO.setCustomerId(loginUser.getCustomerId());
|
|
|
+ dataDTO.setDeviceId(dto.getIotDeviceId());
|
|
|
+ dataDTO.setProductId(dto.getIotProductId());
|
|
|
+ dataDTO.setRuleId(alarm.getIotAlarmId());
|
|
|
+ iotAlarmId = syncAlarmService.updateDataWaringConfig(dataDTO);
|
|
|
+ } else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == dto.getAlarmCategory()) {
|
|
|
+ AlarmOfflineDTO dataDTO = BeanCopyUtils.copy(dto, AlarmOfflineDTO.class);
|
|
|
+ dataDTO.setCustomerId(loginUser.getCustomerId());
|
|
|
+ dataDTO.setDeviceId(dto.getIotDeviceId());
|
|
|
+ dataDTO.setProductId(dto.getIotProductId());
|
|
|
+ dataDTO.setRuleId(alarm.getIotAlarmId());
|
|
|
+ iotAlarmId = syncAlarmService.updateOfflineConfig(dataDTO);
|
|
|
+ }
|
|
|
+ if (iotAlarmId == null || iotAlarmId == 0) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_UPDATE_ALARM_ERROR);
|
|
|
+ }
|
|
|
+ alarmType.setUpdateDate(new Date());
|
|
|
alarmType.setUpdateBy(loginUser.getUsername());
|
|
|
int result = this.updateByPrimaryKeySelective(alarmType);
|
|
|
// 告警规则
|
|
@@ -125,9 +164,20 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
public void delete(Integer id) {
|
|
|
log.info("begin deleteAlarmType id=" + id);
|
|
|
AlarmType alarmType = alarmTypeMapper.findById(id);
|
|
|
+ if (alarmType == null) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
|
|
|
+ }
|
|
|
alarmType.setId(id);
|
|
|
alarmType.setStatus(false);
|
|
|
-
|
|
|
+ Long iotAlarmId = null;
|
|
|
+ if (AlarmCategoryEnum.DATA_ALARM.getType() == alarmType.getAlarmCategory()) {
|
|
|
+ iotAlarmId = syncAlarmService.deleteDataWaringConfig(alarmType.getIotAlarmId());
|
|
|
+ } else if (AlarmCategoryEnum.OFFLINE_ALARM.getType() == alarmType.getAlarmCategory()) {
|
|
|
+ iotAlarmId = syncAlarmService.deleteOfflineConfig(alarmType.getIotAlarmId());
|
|
|
+ }
|
|
|
+ if (iotAlarmId == null || iotAlarmId == 0) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_DELETE_ALARM_ERROR);
|
|
|
+ }
|
|
|
int result = this.updateByPrimaryKeySelective(alarmType);
|
|
|
AlarmType dto = new AlarmType();
|
|
|
dto.setId(id);
|
|
@@ -141,8 +191,11 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
public void updateEnabled(Integer id) {
|
|
|
AlarmType alarmType = alarmTypeMapper.findById(id);
|
|
|
if (alarmType == null) {
|
|
|
- log.info("异常类型不存在");
|
|
|
- return;
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.NOT_FOUND);
|
|
|
+ }
|
|
|
+ Long data = syncAlarmService.enable(alarmType.getIotAlarmId());
|
|
|
+ if (data == null || data == 0) {
|
|
|
+ throw BusinessException.builder(WaterErrorEnum.IOT_SYNC_ENABLED_ALARM_ERROR);
|
|
|
}
|
|
|
alarmType.setId(id);
|
|
|
alarmType.setEnabled(!alarmType.getEnabled());
|
|
@@ -209,6 +262,7 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
}
|
|
|
alarmRule.setExpression(expression);
|
|
|
AlarmRule rule = BeanCopyUtils.copy(alarmRule, AlarmRule.class);
|
|
|
+ rule.setStatus(true);
|
|
|
alarmRuleList.add(rule);
|
|
|
}
|
|
|
alarmRuleMapper.batchInsert(alarmRuleList);
|
|
@@ -236,9 +290,9 @@ public class AlarmTypeServiceImpl implements AlarmTypeService {
|
|
|
alarmType.setDeptOrgId(loginUser.getDepartmentId());
|
|
|
alarmType.setStatus(true);
|
|
|
alarmType.setEnabled(true);
|
|
|
- alarmType.setCreateDate(LocalDateTime.now());
|
|
|
+ alarmType.setCreateDate(new Date());
|
|
|
alarmType.setCreateBy(loginUser.getUsername());
|
|
|
- alarmType.setUpdateDate(LocalDateTime.now());
|
|
|
+ alarmType.setUpdateDate(new Date());
|
|
|
alarmType.setUpdateBy(loginUser.getUsername());
|
|
|
}
|
|
|
|