|
|
|
@ -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<DataRecordMapper, DataRecordEntity> { |
|
|
|
|
|
|
|
|
@ -37,6 +46,11 @@ public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRec
|
|
|
|
|
|
|
|
|
|
private final TableInfoService tableInfoService; |
|
|
|
|
|
|
|
|
|
private final WeaviateService weaviateService; |
|
|
|
|
|
|
|
|
|
@Value("${weaviate.tableName.record}") |
|
|
|
|
private String recordTableName; |
|
|
|
|
|
|
|
|
|
public boolean save(DataRecordEntity entity) { |
|
|
|
|
splicing(entity); |
|
|
|
|
return super.save(entity); |
|
|
|
@ -109,4 +123,50 @@ public class DataRecordService extends BaseServiceImpl<DataRecordMapper, DataRec
|
|
|
|
|
} |
|
|
|
|
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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|