|
|
@@ -0,0 +1,298 @@
|
|
|
+package com.xunmei.mediator.api.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.IdWorker;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.xunmei.common.core.constant.CacheConstants;
|
|
|
+import com.xunmei.common.core.domain.iot.domain.IotUps;
|
|
|
+import com.xunmei.common.core.domain.iot.domain.IotUpsAttr;
|
|
|
+import com.xunmei.common.core.enums.iot.UpsStatus;
|
|
|
+import com.xunmei.common.core.utils.DateUtils;
|
|
|
+import com.xunmei.common.redis.utils.RedisUtils;
|
|
|
+import com.xunmei.mediator.api.mapper.IotUpsAttrMapper;
|
|
|
+import com.xunmei.mediator.api.mapper.IotUpsMapper;
|
|
|
+import com.xunmei.mediator.api.service.IIotUpsService;
|
|
|
+import com.xunmei.mediator.api.service.NorthErrorService;
|
|
|
+import com.xunmei.mediator.api.service.NorthStatisticsSyncService;
|
|
|
+import com.xunmei.mediator.domain.dto.ups.UpsDataVo;
|
|
|
+import com.xunmei.mediator.domain.dto.ups.UpsDeviceDataDto;
|
|
|
+import com.xunmei.mediator.domain.dto.ups.UpsDeviceDto;
|
|
|
+import com.xunmei.mediator.util.CheckDataUtil;
|
|
|
+import com.xunmei.mediator.util.RedisCheckRepeatDataUtil;
|
|
|
+import com.xunmei.system.api.domain.north.NorthError;
|
|
|
+import com.xunmei.system.api.dto.DataPageDto;
|
|
|
+import com.xunmei.system.api.dto.protection.ReceiveErrorDto;
|
|
|
+import com.xunmei.system.api.enums.DataType;
|
|
|
+import com.xunmei.system.api.util.LogUtils;
|
|
|
+import com.xunmei.system.api.vo.SysOrgVO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * ups表信息 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author jingYuanChao
|
|
|
+ * @since 2024-06-18
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class IotUpsServiceImpl extends ServiceImpl<IotUpsMapper, IotUps> implements IIotUpsService {
|
|
|
+ @Autowired
|
|
|
+ private NorthStatisticsSyncService northStatisticsSyncService;
|
|
|
+ @Resource
|
|
|
+ NorthErrorService northErrorService;
|
|
|
+ @Resource
|
|
|
+ IotUpsAttrMapper upsAttrMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReceiveErrorDto saveUpsDevice(DataPageDto<UpsDeviceDto> request, String msgId) throws IllegalAccessException {
|
|
|
+ //验证基础分页信息
|
|
|
+ ReceiveErrorDto dto = CheckDataUtil.checkObjFieldIsNull(request);
|
|
|
+ if (dto.getSuccess()) {
|
|
|
+ List<UpsDeviceDto> upsDeviceDtoLists = RedisCheckRepeatDataUtil.isCompleted(request, UpsDeviceDto.class);
|
|
|
+ LogUtils.BASE_INFO_UPS_DEVICE.info("判断是否获取UPS同步全部信息分页数据:{}", !upsDeviceDtoLists.isEmpty());
|
|
|
+ if (!upsDeviceDtoLists.isEmpty()) {
|
|
|
+ Map<String, Object> map = this.checkUpsInfo(upsDeviceDtoLists, msgId);
|
|
|
+ this.saveAll(map, upsDeviceDtoLists);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ReceiveErrorDto saveUpsDeviceData(UpsDeviceDataDto request, String msgId) throws IllegalAccessException {
|
|
|
+ String value = DigestUtils.md5Hex(request.getInfos());
|
|
|
+ String key = request.getOrgCode() + request.getDeviceCode() + "upsdata";
|
|
|
+ //验证是否在段时间内收到重复的数据
|
|
|
+ if (!RedisCheckRepeatDataUtil.checkData(key, value)) {
|
|
|
+ return ReceiveErrorDto.success("数据已接受,短时间内收到相同的ups数据,处理结果忽略:拒绝处理该数据");
|
|
|
+ }
|
|
|
+
|
|
|
+ //验证参数是否齐全
|
|
|
+ ReceiveErrorDto dto = checkUpsDataInfo(request);
|
|
|
+
|
|
|
+ if (!dto.getSuccess()) {
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+ //处理数据
|
|
|
+ Map<String, Object> map = this.buildUpsAndData(request);
|
|
|
+ dto = (ReceiveErrorDto) map.get("errorDto");
|
|
|
+ if (!dto.getSuccess()) {
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+ IotUps upsDevice = (IotUps) map.get("upsDevice");
|
|
|
+ try {
|
|
|
+ baseMapper.updateById(upsDevice);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("保存upsDevice失败:{}", e.getMessage());
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> buildUpsAndData(UpsDeviceDataDto request) {
|
|
|
+ IotUps upsDevice = baseMapper.selectByUniqueCode(IotUps.getUniqueCode(request.getOrgCode(), request.getHostCode(), request.getDeviceCode()));
|
|
|
+ ReceiveErrorDto errorDto = ReceiveErrorDto.success();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ if (upsDevice == null) {
|
|
|
+ errorDto.setSuccess(false);
|
|
|
+ errorDto.setErrorMsg("ups不存在!");
|
|
|
+ map.put("errorDto", errorDto);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+ upsDevice.setUpdateTime(DateUtils.toLocalDateTime(request.getUpdateTime()));
|
|
|
+ //List<UpsDataVo> infos = buildInfos(request.getInfos());
|
|
|
+ if (request.getInfos() != null) {
|
|
|
+ List<UpsDataVo> list = JSON.parseArray(request.getInfos(), UpsDataVo.class);
|
|
|
+ for (UpsDataVo vo : list) {
|
|
|
+ if (StringUtils.isNotEmpty(vo.getName()) && vo.getName().contains("UPS市电状态")) {
|
|
|
+ upsDevice.setMainsElectricityStatus(Double.valueOf(vo.getVal()).intValue());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(vo.getName()) && vo.getName().contains("UPS电池电压低告警")) {
|
|
|
+ upsDevice.setLowPressureStatus(Double.valueOf(vo.getVal()).intValue());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ map.put("errorDto", errorDto);
|
|
|
+ map.put("upsDevice", upsDevice);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<UpsDataVo> buildInfos(String infos) {
|
|
|
+ List<IotUpsAttr> rules = upsAttrMapper.findUpsAttribute();
|
|
|
+ List<UpsDataVo> list = JSON.parseArray(infos, UpsDataVo.class);
|
|
|
+ List<UpsDataVo> listNew = new ArrayList<>();
|
|
|
+ for (UpsDataVo vo : list) {
|
|
|
+ Optional<IotUpsAttr> attrOptional = rules.stream().filter(r -> r.getRuleKey().equals(vo.getName())).findFirst();
|
|
|
+ if (!attrOptional.isPresent()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ IotUpsAttr rule = attrOptional.get();
|
|
|
+ UpsDataVo dataVo = new UpsDataVo();
|
|
|
+ dataVo.setName(rule.getRuleKey());
|
|
|
+ dataVo.setStatus(0);
|
|
|
+ if (rule.getRuleValue() == null) {
|
|
|
+ dataVo.setVal(String.format("%.2f", Double.valueOf(vo.getVal())) + rule.getUnit());
|
|
|
+ } else {
|
|
|
+ switch (rule.getRuleKey()) {
|
|
|
+ case "UPS类型":
|
|
|
+ if (Double.valueOf(vo.getVal()).intValue() == rule.getRuleValue()) {
|
|
|
+ dataVo.setVal("在线式");
|
|
|
+ } else {
|
|
|
+ dataVo.setVal("后备式");
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ case "UPS旁路状态":
|
|
|
+ if (Double.valueOf(vo.getVal()).intValue() == rule.getRuleValue()) {
|
|
|
+ dataVo.setVal("逆变/直接输出");
|
|
|
+ } else {
|
|
|
+ dataVo.setVal("旁路输出");
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ case "UPS测试状态":
|
|
|
+ if (Double.valueOf(vo.getVal()).intValue() == rule.getRuleValue()) {
|
|
|
+ dataVo.setVal("没有在测试");
|
|
|
+ } else {
|
|
|
+ dataVo.setVal("正在进行测试");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ if (Double.valueOf(vo.getVal()).intValue() == rule.getRuleValue()) {
|
|
|
+ dataVo.setVal("正常");
|
|
|
+ } else {
|
|
|
+ dataVo.setVal("异常");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ listNew.add(dataVo);
|
|
|
+ }
|
|
|
+ return listNew;
|
|
|
+ }
|
|
|
+
|
|
|
+ private ReceiveErrorDto checkUpsDataInfo(UpsDeviceDataDto request) throws IllegalAccessException {
|
|
|
+ ReceiveErrorDto result = new ReceiveErrorDto();
|
|
|
+ result = CheckDataUtil.checkObjFieldIsNull(request, null);
|
|
|
+ if (result.getSuccess()) {
|
|
|
+ List<SysOrgVO> cacheList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
|
|
|
+ boolean b = cacheList.stream().anyMatch(r -> ObjectUtil.equal(r.getCode(), request.getOrgCode()));
|
|
|
+ if (!b) {
|
|
|
+ result.setSuccess(false);
|
|
|
+ result.setErrorMsg("参数非法:" + request.getOrgCode() + "不正确");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveAll(Map<String, Object> map, List<UpsDeviceDto> upsDeviceDtoLists) {
|
|
|
+ List<NorthError> errors = (List<NorthError>) map.get("errors");
|
|
|
+ List<IotUps> upsDevices = (List<IotUps>) map.get("upsDevices");
|
|
|
+ List<IotUps> upsDeviceOlds = (List<IotUps>) map.get("upsDeviceOlds");
|
|
|
+ List<Long> orgIdList = (List<Long>) map.get("orgIdList");
|
|
|
+ this.northStatisticsSyncService.saveOrUpdateBaseCountByDataType(DataType.upsDeviceCount.getIndex(), upsDeviceDtoLists.size(), false);
|
|
|
+
|
|
|
+ try {
|
|
|
+ northErrorService.saveErrorData(errors);
|
|
|
+ this.updateDel(orgIdList);
|
|
|
+ if (ObjectUtil.isNotEmpty(upsDevices)) {
|
|
|
+ baseMapper.batchInsert(upsDevices);
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(upsDeviceOlds)) {
|
|
|
+ baseMapper.batchUpdate(upsDeviceOlds);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("批量保存ups失败:{}", e.getMessage());
|
|
|
+ LogUtils.BASE_INFO_UPS_DEVICE.error("批量保存ups失败,{}", e);
|
|
|
+ throw new RuntimeException(e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void updateDel(List<Long> orgIdList) {
|
|
|
+ LambdaUpdateWrapper<IotUps> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(IotUps::getDeleted, 0);
|
|
|
+ updateWrapper.in(IotUps::getOrgId, orgIdList);
|
|
|
+ baseMapper.update(null, updateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, Object> checkUpsInfo(List<UpsDeviceDto> upsDeviceDtoLists, String msgId) throws IllegalAccessException {
|
|
|
+ List<SysOrgVO> orgList = RedisUtils.getCacheList(CacheConstants.ORG_CACHE_LIST_KEY);
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
+ List<NorthError> errors = new ArrayList<>();
|
|
|
+ List<IotUps> upsDevices = new ArrayList<>();
|
|
|
+ List<IotUps> upsDeviceOlds = new ArrayList<>();
|
|
|
+ List<IotUps> upsDeviceAllOlds = baseMapper.selectList(new QueryWrapper<>());
|
|
|
+ NorthError error = null;
|
|
|
+ //获取机构列表
|
|
|
+ List<Long> list = new ArrayList<>();
|
|
|
+ for (UpsDeviceDto ups : upsDeviceDtoLists) {
|
|
|
+ SysOrgVO org = null;
|
|
|
+ error = CheckDataUtil.checkObjFieldIsNull(ups, msgId, "/api/data/upsDeviceList", null);
|
|
|
+ if (error != null) {
|
|
|
+ errors.add(error);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Optional<SysOrgVO> orgOptional = orgList.stream().filter(r -> r.getCode().equals(ups.getOrgCode())).findFirst();
|
|
|
+ if (!orgOptional.isPresent()) {
|
|
|
+ error = new NorthError(msgId, "/api/data/upsDeviceList", ups, "orgCode:" + ups.getOrgCode() + "不正确");
|
|
|
+ errors.add(error);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ org = orgOptional.get();
|
|
|
+ if (!list.contains(org.getId())) {
|
|
|
+ list.add(org.getId());
|
|
|
+ }
|
|
|
+ IotUps upsDevice = this.buildUpsDevice(ups, org);
|
|
|
+ //判断老数据中是否有新数据
|
|
|
+ Optional<IotUps> upsOptional = upsDeviceAllOlds.stream().filter(r -> r.getUpsUniqueCode().equals(upsDevice.getUpsUniqueCode())).findAny();
|
|
|
+ if (upsOptional.isPresent()) {
|
|
|
+ IotUps upsDeviceOld = upsOptional.get();
|
|
|
+ BeanUtils.copyProperties(upsDeviceOld, upsDevice);
|
|
|
+ upsDeviceOlds.add(upsDeviceOld);
|
|
|
+ } else {
|
|
|
+ upsDevice.setId(IdWorker.getId());
|
|
|
+ upsDevices.add(upsDevice);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ map.put("errors", errors);
|
|
|
+ //需要插入的数据
|
|
|
+ map.put("upsDevices", upsDevices);
|
|
|
+ //需要修改的数据
|
|
|
+ map.put("upsDeviceOlds", upsDeviceOlds);
|
|
|
+ map.put("orgIdList", list);
|
|
|
+ return map;
|
|
|
+ }
|
|
|
+
|
|
|
+ private IotUps buildUpsDevice(UpsDeviceDto ups, SysOrgVO org) {
|
|
|
+ return IotUps.builder()
|
|
|
+ .deviceCode(ups.getDeviceCode())
|
|
|
+ .deviceName(ups.getDeviceName())
|
|
|
+ .deviceType(ups.getDeviceType())
|
|
|
+ .hostCode(ups.getHostCode())
|
|
|
+ .orgId(org.getId())
|
|
|
+ .orgName(org.getName())
|
|
|
+ .orgPath(org.getPath())
|
|
|
+ .upsUniqueCode(IotUps.getUniqueCode(org.getCode(), ups.getHostCode(), ups.getDeviceCode()))
|
|
|
+ .deleted(0)
|
|
|
+ .status(UpsStatus.UN_KNOW.getStatus())
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+}
|