|  | @@ -1,19 +1,36 @@
 | 
											
												
													
														|  |  package com.xunmei.iot.service.impl;
 |  |  package com.xunmei.iot.service.impl;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import cn.hutool.core.util.ObjectUtil;
 |  |  import cn.hutool.core.util.ObjectUtil;
 | 
											
												
													
														|  | 
 |  | +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 | 
											
												
													
														|  | 
 |  | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
											
												
													
														|  | 
 |  | +import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
											
												
													
														|  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.common.core.domain.iot.domain.IotSensor;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.common.core.domain.mediator.domain.IotAlarmRule;
 | 
											
												
													
														|  |  import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleSource;
 |  |  import com.xunmei.common.core.domain.mediator.domain.IotAlarmRuleSource;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.common.core.utils.IDHelper;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.common.core.utils.StringUtils;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDetailDto;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDeviceBatchJoin;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDeviceDto;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.dto.alarm.IotAlarmRuleSourceDeviceRuleDto;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.enums.ProductTypeEnum;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.enums.ValueTypeEnum;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.mapper.IotAlarmRuleExpressMapper;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.mapper.IotAlarmRuleMapper;
 | 
											
												
													
														|  |  import com.xunmei.iot.mapper.IotAlarmRuleSourceMapper;
 |  |  import com.xunmei.iot.mapper.IotAlarmRuleSourceMapper;
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.mapper.IotCommonSensorMapper;
 | 
											
												
													
														|  |  import com.xunmei.iot.service.IotAlarmRuleSourceService;
 |  |  import com.xunmei.iot.service.IotAlarmRuleSourceService;
 | 
											
												
													
														|  | -import com.xunmei.iot.vo.alarm.IotAlarmRuleSourceDetailVo;
 |  | 
 | 
											
												
													
														|  | -import com.xunmei.iot.vo.alarm.ProductTypeDataVo;
 |  | 
 | 
											
												
													
														|  | 
 |  | +import com.xunmei.iot.vo.alarm.*;
 | 
											
												
													
														|  |  import org.springframework.beans.factory.annotation.Autowired;
 |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
											
												
													
														|  |  import org.springframework.stereotype.Service;
 |  |  import org.springframework.stereotype.Service;
 | 
											
												
													
														|  | 
 |  | +import org.springframework.transaction.annotation.Transactional;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  import java.util.ArrayList;
 |  |  import java.util.ArrayList;
 | 
											
												
													
														|  | -import java.util.Comparator;
 |  | 
 | 
											
												
													
														|  | 
 |  | +import java.util.HashMap;
 | 
											
												
													
														|  |  import java.util.List;
 |  |  import java.util.List;
 | 
											
												
													
														|  |  import java.util.Map;
 |  |  import java.util.Map;
 | 
											
												
													
														|  | 
 |  | +import java.util.function.Function;
 | 
											
												
													
														|  |  import java.util.stream.Collectors;
 |  |  import java.util.stream.Collectors;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  @Service
 |  |  @Service
 | 
											
										
											
												
													
														|  | @@ -22,21 +39,162 @@ public class IotAlarmRuleSourceServiceImpl extends ServiceImpl<IotAlarmRuleSourc
 | 
											
												
													
														|  |      @Autowired
 |  |      @Autowired
 | 
											
												
													
														|  |      private IotAlarmRuleSourceMapper iotAlarmRuleSourceMapper;
 |  |      private IotAlarmRuleSourceMapper iotAlarmRuleSourceMapper;
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +    @Autowired
 | 
											
												
													
														|  | 
 |  | +    private IotAlarmRuleMapper iotAlarmRuleMapper;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Autowired
 | 
											
												
													
														|  | 
 |  | +    private IotAlarmRuleExpressMapper iotAlarmRuleExpressMapper;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Autowired
 | 
											
												
													
														|  | 
 |  | +    private IotCommonSensorMapper iotCommonSensorMapper;
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Override
 | 
											
												
													
														|  | 
 |  | +    public IotAlarmRuleSourceDetailVo detail(IotAlarmRuleSourceDetailDto detailVo) throws Exception {
 | 
											
												
													
														|  | 
 |  | +        IotAlarmRuleSourceDetailVo iotAlarmRuleSourceDetailVo = new IotAlarmRuleSourceDetailVo();
 | 
											
												
													
														|  | 
 |  | +        //1.获取告警规则数据
 | 
											
												
													
														|  | 
 |  | +        IotAlarmRule iotAlarmRule = iotAlarmRuleMapper.selectById(detailVo.getRuleId());
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceDetailVo.setRuleId(iotAlarmRule.getId());
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceDetailVo.setRuleName(iotAlarmRule.getName());
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceDetailVo.setRuleType(iotAlarmRule.getType());
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceDetailVo.setEnable(iotAlarmRule.getEnabled());
 | 
											
												
													
														|  | 
 |  | +        //2.获取告警规则对应有哪些设备的告警属性规则。
 | 
											
												
													
														|  | 
 |  | +        List<ProductTypeDataVo> productTypeDataVos = iotAlarmRuleSourceMapper.selectRuleDevice(detailVo.getRuleId());
 | 
											
												
													
														|  | 
 |  | +        //3.处理其他sql获取不了的数据
 | 
											
												
													
														|  | 
 |  | +        for (ProductTypeDataVo pdv : productTypeDataVos) {
 | 
											
												
													
														|  | 
 |  | +            pdv.setTypeText(ProductTypeEnum.getName(pdv.getType()));
 | 
											
												
													
														|  | 
 |  | +            dealDevice(pdv);
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceDetailVo.setTypeDataList(productTypeDataVos);
 | 
											
												
													
														|  | 
 |  | +        return iotAlarmRuleSourceDetailVo;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    @Override
 | 
											
												
													
														|  | 
 |  | +    public Page<IotAlarmRuleSourceDeviceVo> selectDeviceList(IotAlarmRuleSourceDeviceDto param)throws Exception {
 | 
											
												
													
														|  | 
 |  | +        Page<IotAlarmRuleSourceDeviceVo> page = new Page<>();
 | 
											
												
													
														|  | 
 |  | +        page.setSize(param.getSize());
 | 
											
												
													
														|  | 
 |  | +        page.setCurrent(param.getPage());
 | 
											
												
													
														|  | 
 |  | +        Page<IotAlarmRuleSourceDeviceVo> result = iotAlarmRuleSourceMapper.selectDeviceList(page, param);
 | 
											
												
													
														|  | 
 |  | +        return result;
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |      @Override
 |  |      @Override
 | 
											
												
													
														|  | -    public IotAlarmRuleSourceDetailVo detail(IotAlarmRuleSourceDetailVo detailVo) throws Exception {
 |  | 
 | 
											
												
													
														|  | -        //根据规则id获取,规则数据
 |  | 
 | 
											
												
													
														|  | -        List<IotAlarmRuleSourceDetailVo> detailVoList = iotAlarmRuleSourceMapper.selectDetailData(detailVo.getRuleId());
 |  | 
 | 
											
												
													
														|  | -        if(ObjectUtil.isEmpty(detailVoList)){
 |  | 
 | 
											
												
													
														|  | -            return new IotAlarmRuleSourceDetailVo();
 |  | 
 | 
											
												
													
														|  | 
 |  | +    @Transactional(rollbackFor = Exception.class)
 | 
											
												
													
														|  | 
 |  | +    public void updateRuleAndProductDevice(IotAlarmRuleSourceDetailVo detailDto) throws Exception {
 | 
											
												
													
														|  | 
 |  | +        //删除设备绑定数据
 | 
											
												
													
														|  | 
 |  | +        LambdaQueryWrapper<IotAlarmRuleSource> qw = new LambdaQueryWrapper<IotAlarmRuleSource>();
 | 
											
												
													
														|  | 
 |  | +        qw.eq(IotAlarmRuleSource::getRuleId,detailDto.getRuleId());
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceMapper.delete(qw);
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        //重新绑定设置
 | 
											
												
													
														|  | 
 |  | +        List<ProductTypeDataVo> deviceData = detailDto.getTypeDataList();
 | 
											
												
													
														|  | 
 |  | +        if(ObjectUtil.isEmpty(deviceData)){
 | 
											
												
													
														|  | 
 |  | +            return;
 | 
											
												
													
														|  |          }
 |  |          }
 | 
											
												
													
														|  | 
 |  | +        IotAlarmRuleSource ruleSource = null;
 | 
											
												
													
														|  | 
 |  | +        for (ProductTypeDataVo productType : deviceData) {
 | 
											
												
													
														|  | 
 |  | +            ruleSource = new IotAlarmRuleSource();
 | 
											
												
													
														|  | 
 |  | +            ruleSource.setRuleId(detailDto.getRuleId());
 | 
											
												
													
														|  | 
 |  | +            if (ValueTypeEnum.AllDevice.getText().equals(productType.getValueType()) && productType.getIsAllDeviceEnable()) {
 | 
											
												
													
														|  | 
 |  | +                ruleSource.setId(IDHelper.id());
 | 
											
												
													
														|  | 
 |  | +                ruleSource.setOrgId(null);
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        IotAlarmRuleSourceDetailVo iotAlarmRuleSourceDetailVo = detailVoList.get(0);
 |  | 
 | 
											
												
													
														|  | 
 |  | +                ruleSource.setSourceType(productType.getType());
 | 
											
												
													
														|  | 
 |  | +                ruleSource.setValue(null);
 | 
											
												
													
														|  | 
 |  | +                ruleSource.setValueType(ValueTypeEnum.AllDevice.getText());
 | 
											
												
													
														|  | 
 |  | +                iotAlarmRuleSourceMapper.insert(ruleSource);
 | 
											
												
													
														|  | 
 |  | +                continue;
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +            if (productType.getDeviceTempList() != null) {
 | 
											
												
													
														|  | 
 |  | +                for (ProductDeviceTempVo deviceTempVo : productType.getDeviceTempList()) {
 | 
											
												
													
														|  | 
 |  | +                    ruleSource.setId(IDHelper.id());
 | 
											
												
													
														|  | 
 |  | +                    ruleSource.setOrgId(deviceTempVo.getOrgId());
 | 
											
												
													
														|  | 
 |  | +                    ruleSource.setSourceType(productType.getType());
 | 
											
												
													
														|  | 
 |  | +                    ruleSource.setValue(deviceTempVo.getDeviceId());
 | 
											
												
													
														|  | 
 |  | +                    ruleSource.setValueType(ValueTypeEnum.Device.getText());
 | 
											
												
													
														|  | 
 |  | +                    iotAlarmRuleSourceMapper.insert(ruleSource);
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        List<ProductTypeDataVo> list = new ArrayList<>();
 |  | 
 | 
											
												
													
														|  | -        Map<Integer, List<IotAlarmRuleSourceDetailVo>> listMap =
 |  | 
 | 
											
												
													
														|  | -                detailVoList.stream().sorted(Comparator.comparing(IotAlarmRuleSourceDetailVo::getSourceType)).collect(Collectors.groupingBy(IotAlarmRuleSourceDetailVo::getSourceType));
 |  | 
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +    @Override
 | 
											
												
													
														|  | 
 |  | +    @Transactional(rollbackFor = Exception.class)
 | 
											
												
													
														|  | 
 |  | +    public void batchJoin(IotAlarmRuleSourceDeviceBatchJoin batchJoinDto) throws Exception {
 | 
											
												
													
														|  | 
 |  | +        final List<Long> ruleIdList = batchJoinDto.getRuleIdList();
 | 
											
												
													
														|  | 
 |  | +        final List<String> deviceIdList = batchJoinDto.getDeviceIdList();
 | 
											
												
													
														|  | 
 |  | +        Map<Object, Object> hashMap = new HashMap<>();
 | 
											
												
													
														|  | 
 |  | +        iotAlarmRuleSourceMapper.delete(new LambdaQueryWrapper<IotAlarmRuleSource>()
 | 
											
												
													
														|  | 
 |  | +                .in(IotAlarmRuleSource::getValue, deviceIdList));
 | 
											
												
													
														|  | 
 |  | +        for (Long ruleId : ruleIdList) {
 | 
											
												
													
														|  | 
 |  | +            for (String deviceId : deviceIdList) {
 | 
											
												
													
														|  | 
 |  | +                IotSensor productDevice = (IotSensor) hashMap.get(deviceId);
 | 
											
												
													
														|  | 
 |  | +                if (productDevice == null) {
 | 
											
												
													
														|  | 
 |  | +                    QueryWrapper<IotSensor> qw = new QueryWrapper<>();
 | 
											
												
													
														|  | 
 |  | +                    qw.lambda().eq(IotSensor::getDeviceCode,deviceId).eq(IotSensor::getDeleted,0);
 | 
											
												
													
														|  | 
 |  | +                    hashMap.put(deviceId, productDevice);
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +                String code = productDevice.getDeviceType();
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | -        return null;
 |  | 
 | 
											
												
													
														|  | 
 |  | +                boolean isAllDevice = false;
 | 
											
												
													
														|  | 
 |  | +                final List<IotAlarmRuleSource> ruleSourceList = iotAlarmRuleSourceMapper.selectList(new LambdaQueryWrapper<IotAlarmRuleSource>()
 | 
											
												
													
														|  | 
 |  | +                        .eq(IotAlarmRuleSource::getRuleId, ruleId)
 | 
											
												
													
														|  | 
 |  | +                        .eq(IotAlarmRuleSource::getSourceType, Integer.parseInt(code)));
 | 
											
												
													
														|  | 
 |  | +                isAllDevice = ruleSourceList.stream().map(IotAlarmRuleSource::getValueType).anyMatch(ValueTypeEnum.AllDevice.getText()::equals);
 | 
											
												
													
														|  | 
 |  | +                if (isAllDevice) {
 | 
											
												
													
														|  | 
 |  | +                    //如果此前已经设置了 产品类型=AllDevice的  本次就无需再添加
 | 
											
												
													
														|  | 
 |  | +                    continue;
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +                //isAllDevice = false 说明当前规则与当前的产品类型没有设置需要报警的设备信息,或者说是仅指定了具体的设备需要报警
 | 
											
												
													
														|  | 
 |  | +                //如果是仅指定了具体的设备需要报警,本次重新设置 需要覆盖之前的数据
 | 
											
												
													
														|  | 
 |  | +                IotAlarmRuleSource source = new IotAlarmRuleSource();
 | 
											
												
													
														|  | 
 |  | +                source.setId(IDHelper.id());
 | 
											
												
													
														|  | 
 |  | +                source.setOrgId(productDevice.getOrgId());
 | 
											
												
													
														|  | 
 |  | +                source.setRuleId(ruleId);
 | 
											
												
													
														|  | 
 |  | +                source.setSourceType(Integer.parseInt(code));
 | 
											
												
													
														|  | 
 |  | +                source.setValue(deviceId);
 | 
											
												
													
														|  | 
 |  | +                source.setValueType(ValueTypeEnum.Device.getText());
 | 
											
												
													
														|  | 
 |  | +                iotAlarmRuleSourceMapper.insert(source);
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +    /**
 | 
											
												
													
														|  | 
 |  | +     * 处理每种类型的数据
 | 
											
												
													
														|  | 
 |  | +     * @param pdv
 | 
											
												
													
														|  | 
 |  | +     * @return
 | 
											
												
													
														|  | 
 |  | +     */
 | 
											
												
													
														|  | 
 |  | +    private void dealDevice(ProductTypeDataVo pdv){
 | 
											
												
													
														|  | 
 |  | +        String valueType = pdv.getValueType();
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        if(StringUtils.isEmpty(valueType)){
 | 
											
												
													
														|  | 
 |  | +            //未配置设备关联时
 | 
											
												
													
														|  | 
 |  | +            pdv.setIsAllDeviceEnable(false);
 | 
											
												
													
														|  | 
 |  | +            pdv.setValueType(ValueTypeEnum.AllDevice.getText());
 | 
											
												
													
														|  | 
 |  | +        }else{
 | 
											
												
													
														|  | 
 |  | +            if(ValueTypeEnum.AllDevice.getText().equals(valueType)){
 | 
											
												
													
														|  | 
 |  | +                //配置按设备类型关联时
 | 
											
												
													
														|  | 
 |  | +                pdv.setIsAllDeviceEnable(true);
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +            if(ValueTypeEnum.Device.getText().equals(valueType)){
 | 
											
												
													
														|  | 
 |  | +                //配置具体设备关联时
 | 
											
												
													
														|  | 
 |  | +                List<ProductDeviceTempVo> deviceTempList = pdv.getDeviceTempList();
 | 
											
												
													
														|  | 
 |  | +                //获取对应的设备id
 | 
											
												
													
														|  | 
 |  | +                List<String> deviceIds = deviceTempList.stream().map(ProductDeviceTempVo::getDeviceId).collect(Collectors.toList());
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +                List<IotSensor> iotSensors = iotCommonSensorMapper.selectByIds(deviceIds);
 | 
											
												
													
														|  | 
 |  | +                Map<String, IotSensor> deviceIdAndName = iotSensors.stream().collect(Collectors.toMap(IotSensor::getDeviceCode, Function.identity()));
 | 
											
												
													
														|  | 
 |  | +                for (ProductDeviceTempVo ptv : deviceTempList) {
 | 
											
												
													
														|  | 
 |  | +                    IotSensor iotSensor = deviceIdAndName.get(ptv.getDeviceId());
 | 
											
												
													
														|  | 
 |  | +                    if( iotSensor!= null){
 | 
											
												
													
														|  | 
 |  | +                        ptv.setDeviceName(iotSensor.getDeviceName());
 | 
											
												
													
														|  | 
 |  | +                        ptv.setOrgName(iotSensor.getOrgName());
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +                }
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  |      }
 |  |      }
 | 
											
												
													
														|  |  }
 |  |  }
 |