Browse Source

add: 数据查询、控制指令同步向量数据库接口

zhongwei
haungxing 2 months ago
parent
commit
eb0e79ac58
  1. 6
      hzims-service-api/equipment-api/src/main/java/com/hnac/hzims/fdp/vo/FdpFaultMatchVO.java
  2. 16
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java
  3. 8
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataInstructController.java
  4. 7
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataRecordController.java
  5. 8
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceAttrDTO.java
  6. 8
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceFuncDTO.java
  7. 78
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataInstructService.java
  8. 62
      hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataRecordService.java
  9. 3
      hzims-service/gglm-big-model/src/main/resources/template/template.yml

6
hzims-service-api/equipment-api/src/main/java/com/hnac/hzims/fdp/vo/FdpFaultMatchVO.java

@ -45,7 +45,11 @@ public class FdpFaultMatchVO implements Serializable {
private String deviceFaultName; private String deviceFaultName;
public void setItemName() { public void setItemName() {
this.itemName = String.format("%s %s", this.deviceName, this.faultName); if(Func.isNotEmpty(this.ord)) {
this.itemName = String.format("%s %s %s", this.stationName, this.ord, this.faultName);
} else {
this.itemName = String.format("%s %s %s", this.stationName, this.deviceName, this.faultName);
}
} }
public void setDeviceFaultName() { public void setDeviceFaultName() {

16
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java

@ -442,4 +442,20 @@ public class WeaviateService {
throw new RuntimeException("Query failed.", e); throw new RuntimeException("Query failed.", e);
} }
} }
/**
* 获取查询结果列表
* @param data 调用weaviate返回数据
* @param tableName 表名
* @return 返回列表结果
*/
public List<JSONObject> getResultList(Object data, String tableName) {
String queryJson = JSON.toJSONString(data);
// 获取到数据的ID
return Optional.ofNullable(queryJson).map(JSONObject::parseObject)
.map(object -> object.getJSONObject("Get"))
.map(object -> object.getJSONArray(weaviateProperties.getDatabase() + "_" + tableName))
.map(array -> array.toJavaList(JSONObject.class))
.orElse(Lists.newArrayList());
}
} }

8
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataInstructController.java

@ -78,4 +78,12 @@ public class DataInstructController {
return R.data(dataInstructService.getDataInstructList()); return R.data(dataInstructService.getDataInstructList());
} }
@GetMapping("/syncData")
@ApiOperation("同步数据至向量数据库")
@ApiOperationSupport(order = 211)
public R syncData(@RequestParam("ids") String ids) {
return R.data(dataInstructService.syncDataToVectorDb(ids));
}
} }

7
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataRecordController.java

@ -67,4 +67,11 @@ public class DataRecordController {
return R.data(dataRecordService.getDataRecordList()); return R.data(dataRecordService.getDataRecordList());
} }
// 数据同步向量数据库
@GetMapping("/syncData")
@ApiOperation("同步数据至向量数据库")
@ApiOperationSupport(order = 211)
public R syncData(@RequestParam("ids") String ids) {
return R.data(dataRecordService.syncDataToVectorDb(ids));
}
} }

8
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceAttrDTO.java

@ -1,5 +1,6 @@
package com.hnac.gglm.bigmodel.maintenance.dto; package com.hnac.gglm.bigmodel.maintenance.dto;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -10,30 +11,37 @@ public class DeviceAttrDTO {
@ApiModelProperty("item") @ApiModelProperty("item")
@JsonProperty("item_id") @JsonProperty("item_id")
@JSONField(name = "item_id")
private String itemId; private String itemId;
@ApiModelProperty("itemName") @ApiModelProperty("itemName")
@JsonProperty("item_name") @JsonProperty("item_name")
@JSONField(name = "item_name")
private String itemName; private String itemName;
@ApiModelProperty("站点名称") @ApiModelProperty("站点名称")
@JsonProperty("station_name") @JsonProperty("station_name")
@JSONField(name = "station_name")
private String stationName; private String stationName;
@ApiModelProperty("站点ID") @ApiModelProperty("站点ID")
@JsonProperty("station_id") @JsonProperty("station_id")
@JSONField(name = "station_id")
private String stationId; private String stationId;
@ApiModelProperty("遥控的设备名称") @ApiModelProperty("遥控的设备名称")
@JsonProperty("device_name") @JsonProperty("device_name")
@JSONField(name = "device_name")
private String deviceName; private String deviceName;
@ApiModelProperty("遥控的设备ID") @ApiModelProperty("遥控的设备ID")
@JsonProperty("device_id") @JsonProperty("device_id")
@JSONField(name = "device_id")
private String deviceId; private String deviceId;
@ApiModelProperty("记录名称") @ApiModelProperty("记录名称")
@JsonProperty("record_name") @JsonProperty("record_name")
@JSONField(name = "record_name")
private String recordName; private String recordName;
@ApiModelProperty("取数规则") @ApiModelProperty("取数规则")

8
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceFuncDTO.java

@ -1,5 +1,6 @@
package com.hnac.gglm.bigmodel.maintenance.dto; package com.hnac.gglm.bigmodel.maintenance.dto;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
@ -10,30 +11,37 @@ public class DeviceFuncDTO {
@ApiModelProperty("item") @ApiModelProperty("item")
@JsonProperty("item_id") @JsonProperty("item_id")
@JSONField(name = "item_id")
private String itemId; private String itemId;
@ApiModelProperty("itemName") @ApiModelProperty("itemName")
@JsonProperty("item_name") @JsonProperty("item_name")
@JSONField(name = "item_name")
private String itemName; private String itemName;
@ApiModelProperty("站点名称") @ApiModelProperty("站点名称")
@JsonProperty("station_name") @JsonProperty("station_name")
@JSONField(name = "station_name")
private String stationName; private String stationName;
@ApiModelProperty("站点ID") @ApiModelProperty("站点ID")
@JsonProperty("station_id") @JsonProperty("station_id")
@JSONField(name = "station_id")
private String stationId; private String stationId;
@ApiModelProperty("遥控的设备名称") @ApiModelProperty("遥控的设备名称")
@JsonProperty("device_name") @JsonProperty("device_name")
@JSONField(name = "device_name")
private String deviceName; private String deviceName;
@ApiModelProperty("遥控的设备ID") @ApiModelProperty("遥控的设备ID")
@JsonProperty("device_id") @JsonProperty("device_id")
@JSONField(name = "device_id")
private String deviceId; private String deviceId;
@ApiModelProperty("遥控名称") @ApiModelProperty("遥控名称")
@JsonProperty("yk_name") @JsonProperty("yk_name")
@JSONField(name = "yk_name")
private String ykName; private String ykName;
@ApiModelProperty("遥控名称") @ApiModelProperty("遥控名称")

78
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataInstructService.java

@ -1,32 +1,45 @@
package com.hnac.gglm.bigmodel.maintenance.service; package com.hnac.gglm.bigmodel.maintenance.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.gglm.bigmodel.business.service.VectorDataService; import com.hnac.gglm.bigmodel.business.service.VectorDataService;
import com.hnac.gglm.bigmodel.configuration.WeaviateProperties;
import com.hnac.gglm.bigmodel.database.service.WeaviateService;
import com.hnac.gglm.bigmodel.maintenance.dto.DeviceFuncDTO; import com.hnac.gglm.bigmodel.maintenance.dto.DeviceFuncDTO;
import com.hnac.gglm.bigmodel.maintenance.entity.DataInstructEntity; import com.hnac.gglm.bigmodel.maintenance.entity.DataInstructEntity;
import com.hnac.gglm.bigmodel.maintenance.mapper.DataInstructMapper; import com.hnac.gglm.bigmodel.maintenance.mapper.DataInstructMapper;
import com.hnac.hzims.fdp.constants.ScheduledConstant;
import com.hnac.hzinfo.exception.HzServiceException;
import com.hnac.hzinfo.sdk.v5.device.dto.DeviceFuncInfoDTO; import com.hnac.hzinfo.sdk.v5.device.dto.DeviceFuncInfoDTO;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import java.util.ArrayList; import java.util.*;
import java.util.List;
@Service @Service
@Slf4j @Slf4j
@AllArgsConstructor @RequiredArgsConstructor
@DS("hznlm") @DS("hznlm")
public class DataInstructService extends BaseServiceImpl<DataInstructMapper, DataInstructEntity> { public class DataInstructService extends BaseServiceImpl<DataInstructMapper, DataInstructEntity> {
private final VectorDataService vectorDataService; private final VectorDataService vectorDataService;
private final WeaviateService weaviateService;
private final WeaviateProperties weaviateProperties;
@Value("${weaviate.tableName.instruct}")
private String instructTableName;
public boolean save(DataInstructEntity entity) { public boolean save(DataInstructEntity entity) {
//对遥控名称判重 //对遥控名称判重
@ -82,4 +95,63 @@ public class DataInstructService extends BaseServiceImpl<DataInstructMapper, Dat
} }
return deviceFuncDTOS; return deviceFuncDTOS;
} }
/**
* 同步数据至向量数据库
* @param ids 控制指令ID 按逗号分隔
* @return 同步结果
*/
public Boolean syncDataToVectorDb(String ids) {
// 根据ID获取控制指令
List<DataInstructEntity> dataInstructList = this.listByIds(Func.toLongList(",", ids));
if(Func.isEmpty(dataInstructList)) {
throw new HzServiceException("根据所传ID未查询到相关数据!");
}
// 根据ItemName获取向量数据库内待同步的数据
dataInstructList.forEach(this::syncDataToVectorDb);
return true;
}
/**
* 同步数据至向量数据库
* @param dataInstructEntity 控制指令
* @return 同步结果
*/
public Boolean syncDataToVectorDb(DataInstructEntity dataInstructEntity) {
Map<String,String> query = new HashMap<>(1);
query.put("item_name", dataInstructEntity.getItemName());
Object data = weaviateService.query(null, instructTableName, query);
List<JSONObject> resultList = weaviateService.getResultList(data, instructTableName);
if(Func.isNotEmpty(resultList)) {
// 若数据条数 > 1 说明有重复数据
if(resultList.size() > 1) {
throw new HzServiceException("数据同步失败,存在重复数据!指令名称为:" + dataInstructEntity.getYkName());
}
// 删除数据后新增
String id = Optional.ofNullable(resultList.get(0))
.map(object -> object.getJSONObject("_additional"))
.map(additional -> additional.getString("id"))
.orElse(null);
if(Func.isNotEmpty(id)) {
log.info("删除id:" + id);
weaviateService.delete(id, instructTableName);
}
}
// 新增数据
Map<String,String> attrsMap = new HashMap<>(2);
attrsMap.put("item_name", "item_name");
attrsMap.put("device_yk_name", "device_name,yk_name");
weaviateService.save(JSONObject.parseObject(JSON.toJSONString(this.convert(dataInstructEntity))), instructTableName, attrsMap);
return true;
}
/**
* 封装数据
* @return 结果
*/
private DeviceFuncDTO convert(DataInstructEntity dataInstructEntity) {
DeviceFuncDTO result = BeanUtil.copyProperties(dataInstructEntity, DeviceFuncDTO.class);
result.setType("数据中心");
return result;
}
} }

62
hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataRecordService.java

@ -1,19 +1,25 @@
package com.hnac.gglm.bigmodel.maintenance.service; package com.hnac.gglm.bigmodel.maintenance.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.hnac.gglm.bigmodel.business.service.VectorDataService; import com.hnac.gglm.bigmodel.business.service.VectorDataService;
import com.hnac.gglm.bigmodel.database.service.WeaviateService;
import com.hnac.gglm.bigmodel.maintenance.dto.DeviceAttrDTO; import com.hnac.gglm.bigmodel.maintenance.dto.DeviceAttrDTO;
import com.hnac.gglm.bigmodel.maintenance.dto.DeviceFuncDTO; import com.hnac.gglm.bigmodel.maintenance.dto.DeviceFuncDTO;
import com.hnac.gglm.bigmodel.maintenance.entity.DataInstructEntity;
import com.hnac.gglm.bigmodel.maintenance.entity.DataRecordEntity; import com.hnac.gglm.bigmodel.maintenance.entity.DataRecordEntity;
import com.hnac.gglm.bigmodel.maintenance.entity.TableInfoEntity; import com.hnac.gglm.bigmodel.maintenance.entity.TableInfoEntity;
import com.hnac.gglm.bigmodel.maintenance.mapper.DataRecordMapper; import com.hnac.gglm.bigmodel.maintenance.mapper.DataRecordMapper;
import com.hnac.gglm.bigmodel.maintenance.vo.DataRecordVO; import com.hnac.gglm.bigmodel.maintenance.vo.DataRecordVO;
import com.hnac.hzinfo.exception.HzServiceException;
import com.hnac.hzinfo.sdk.v5.device.dto.DeviceAttrInfoDTO; import com.hnac.hzinfo.sdk.v5.device.dto.DeviceAttrInfoDTO;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.mp.base.BaseServiceImpl;
@ -21,15 +27,18 @@ import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query; import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.BeanUtil; import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func; import org.springblade.core.tool.utils.Func;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@Slf4j @Slf4j
@AllArgsConstructor @RequiredArgsConstructor
@DS("hznlm") @DS("hznlm")
public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRecordEntity> { public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRecordEntity> {
@ -37,6 +46,11 @@ public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRec
private final TableInfoService tableInfoService; private final TableInfoService tableInfoService;
private final WeaviateService weaviateService;
@Value("${weaviate.tableName.record}")
private String recordTableName;
public boolean save(DataRecordEntity entity) { public boolean save(DataRecordEntity entity) {
splicing(entity); splicing(entity);
return super.save(entity); return super.save(entity);
@ -109,4 +123,50 @@ public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRec
} }
return result; return result;
} }
public Boolean syncDataToVectorDb(String ids) {
// 根据ID获取控制指令
List<DataRecordEntity> recordEntities = this.listByIds(Func.toLongList(",", ids));
if(Func.isEmpty(recordEntities)) {
throw new HzServiceException("根据所传ID未查询到相关数据!");
}
// 根据ItemName获取向量数据库内待同步的数据
recordEntities.forEach(this::syncDataToVectorDb);
return true;
}
public Boolean syncDataToVectorDb(DataRecordEntity dataRecord) {
Map<String,String> query = new HashMap<>(1);
query.put("item_name", dataRecord.getItemName());
Object data = weaviateService.query(null, recordTableName, query);
List<JSONObject> resultList = weaviateService.getResultList(data, recordTableName);
if(Func.isNotEmpty(resultList)) {
// 若数据条数 > 1 说明有重复数据
if(resultList.size() > 1) {
throw new HzServiceException("数据同步失败,存在重复数据!指令名称为:" + dataRecord.getRecordName());
}
// 删除数据后新增
String id = Optional.ofNullable(resultList.get(0))
.map(object -> object.getJSONObject("_additional"))
.map(additional -> additional.getString("id"))
.orElse(null);
if(Func.isNotEmpty(id)) {
log.info("删除id:" + id);
weaviateService.delete(id, recordTableName);
}
}
// 新增数据
Map<String,String> attrsMap = new HashMap<>(2);
attrsMap.put("item_name", "item_name");
attrsMap.put("record_name", "record_name");
attrsMap.put("device_record_name", "device_name,record_name");
weaviateService.save(JSONObject.parseObject(JSON.toJSONString(this.convert(dataRecord))), recordTableName, attrsMap);
return true;
}
private DeviceAttrDTO convert(DataRecordEntity dataRecord) {
DeviceAttrDTO result = BeanUtil.copyProperties(dataRecord, DeviceAttrDTO.class);
result.setType("业务");
return result;
}
} }

3
hzims-service/gglm-big-model/src/main/resources/template/template.yml

@ -99,3 +99,6 @@ weaviate:
host: 192.168.60.16 host: 192.168.60.16
port: 9992 port: 9992
apiKey: 123 apiKey: 123
tableName:
instruct: yk
record: record
Loading…
Cancel
Save