diff --git a/hzims-service-api/equipment-api/src/main/java/com/hnac/hzims/fdp/vo/FdpFaultMatchVO.java b/hzims-service-api/equipment-api/src/main/java/com/hnac/hzims/fdp/vo/FdpFaultMatchVO.java index 08966a3..2dd215e 100644 --- a/hzims-service-api/equipment-api/src/main/java/com/hnac/hzims/fdp/vo/FdpFaultMatchVO.java +++ b/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; 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() { diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java index d89faf5..fa88d8a 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/database/service/WeaviateService.java +++ b/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); } } + + /** + * 获取查询结果列表 + * @param data 调用weaviate返回数据 + * @param tableName 表名 + * @return 返回列表结果 + */ + public List 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()); + } } diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataInstructController.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataInstructController.java index 999e572..917d30c 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataInstructController.java +++ b/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()); } + @GetMapping("/syncData") + @ApiOperation("同步数据至向量数据库") + @ApiOperationSupport(order = 211) + public R syncData(@RequestParam("ids") String ids) { + return R.data(dataInstructService.syncDataToVectorDb(ids)); + } + + } diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataRecordController.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataRecordController.java index 4276d45..b0117ea 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/controller/DataRecordController.java +++ b/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()); } + // 数据同步向量数据库 + @GetMapping("/syncData") + @ApiOperation("同步数据至向量数据库") + @ApiOperationSupport(order = 211) + public R syncData(@RequestParam("ids") String ids) { + return R.data(dataRecordService.syncDataToVectorDb(ids)); + } } diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceAttrDTO.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceAttrDTO.java index 7057a1a..9351661 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceAttrDTO.java +++ b/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; +import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -10,30 +11,37 @@ public class DeviceAttrDTO { @ApiModelProperty("item") @JsonProperty("item_id") + @JSONField(name = "item_id") private String itemId; @ApiModelProperty("itemName") @JsonProperty("item_name") + @JSONField(name = "item_name") private String itemName; @ApiModelProperty("站点名称") @JsonProperty("station_name") + @JSONField(name = "station_name") private String stationName; @ApiModelProperty("站点ID") @JsonProperty("station_id") + @JSONField(name = "station_id") private String stationId; @ApiModelProperty("遥控的设备名称") @JsonProperty("device_name") + @JSONField(name = "device_name") private String deviceName; @ApiModelProperty("遥控的设备ID") @JsonProperty("device_id") + @JSONField(name = "device_id") private String deviceId; @ApiModelProperty("记录名称") @JsonProperty("record_name") + @JSONField(name = "record_name") private String recordName; @ApiModelProperty("取数规则") diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceFuncDTO.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceFuncDTO.java index 1df8b54..b33ccef 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/dto/DeviceFuncDTO.java +++ b/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; +import com.alibaba.fastjson.annotation.JSONField; import com.fasterxml.jackson.annotation.JsonProperty; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -10,30 +11,37 @@ public class DeviceFuncDTO { @ApiModelProperty("item") @JsonProperty("item_id") + @JSONField(name = "item_id") private String itemId; @ApiModelProperty("itemName") @JsonProperty("item_name") + @JSONField(name = "item_name") private String itemName; @ApiModelProperty("站点名称") @JsonProperty("station_name") + @JSONField(name = "station_name") private String stationName; @ApiModelProperty("站点ID") @JsonProperty("station_id") + @JSONField(name = "station_id") private String stationId; @ApiModelProperty("遥控的设备名称") @JsonProperty("device_name") + @JSONField(name = "device_name") private String deviceName; @ApiModelProperty("遥控的设备ID") @JsonProperty("device_id") + @JSONField(name = "device_id") private String deviceId; @ApiModelProperty("遥控名称") @JsonProperty("yk_name") + @JSONField(name = "yk_name") private String ykName; @ApiModelProperty("遥控名称") diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataInstructService.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataInstructService.java index eb9049f..4cdd8af 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataInstructService.java +++ b/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; +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.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.google.common.collect.Lists; 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.entity.DataInstructEntity; 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 lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springblade.core.mp.base.BaseServiceImpl; import org.springblade.core.tool.utils.BeanUtil; +import org.springblade.core.tool.utils.CollectionUtil; import org.springblade.core.tool.utils.Func; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import java.util.ArrayList; -import java.util.List; +import java.util.*; @Service @Slf4j -@AllArgsConstructor +@RequiredArgsConstructor @DS("hznlm") public class DataInstructService extends BaseServiceImpl { private final VectorDataService vectorDataService; + private final WeaviateService weaviateService; + private final WeaviateProperties weaviateProperties; + @Value("${weaviate.tableName.instruct}") + private String instructTableName; public boolean save(DataInstructEntity entity) { //对遥控名称判重 @@ -82,4 +95,63 @@ public class DataInstructService extends BaseServiceImpl 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 query = new HashMap<>(1); + query.put("item_name", dataInstructEntity.getItemName()); + Object data = weaviateService.query(null, instructTableName, query); + List 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 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; + } } diff --git a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataRecordService.java b/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataRecordService.java index 820dd90..9015585 100644 --- a/hzims-service/gglm-big-model/src/main/java/com/hnac/gglm/bigmodel/maintenance/service/DataRecordService.java +++ b/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; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.dynamic.datasource.annotation.DS; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.google.common.collect.Lists; 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.DeviceFuncDTO; +import com.hnac.gglm.bigmodel.maintenance.entity.DataInstructEntity; import com.hnac.gglm.bigmodel.maintenance.entity.DataRecordEntity; import com.hnac.gglm.bigmodel.maintenance.entity.TableInfoEntity; import com.hnac.gglm.bigmodel.maintenance.mapper.DataRecordMapper; import com.hnac.gglm.bigmodel.maintenance.vo.DataRecordVO; +import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.sdk.v5.device.dto.DeviceAttrInfoDTO; import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; 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.tool.utils.BeanUtil; import org.springblade.core.tool.utils.Func; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @Service @Slf4j -@AllArgsConstructor +@RequiredArgsConstructor @DS("hznlm") public class DataRecordService extends BaseServiceImpl { @@ -37,6 +46,11 @@ public class DataRecordService extends BaseServiceImpl 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 query = new HashMap<>(1); + query.put("item_name", dataRecord.getItemName()); + Object data = weaviateService.query(null, recordTableName, query); + List 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 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; + } } diff --git a/hzims-service/gglm-big-model/src/main/resources/template/template.yml b/hzims-service/gglm-big-model/src/main/resources/template/template.yml index f8d2753..d225f05 100644 --- a/hzims-service/gglm-big-model/src/main/resources/template/template.yml +++ b/hzims-service/gglm-big-model/src/main/resources/template/template.yml @@ -99,3 +99,6 @@ weaviate: host: 192.168.60.16 port: 9992 apiKey: 123 + tableName: + instruct: yk + record: record \ No newline at end of file