haungxing
2 months ago
30 changed files with 588 additions and 5 deletions
@ -0,0 +1,32 @@
|
||||
package com.hnac.gglm.bigmodel.api.dto; |
||||
|
||||
import lombok.Data; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/18 19:50 |
||||
* @Description: Weaviate新增数据对象 |
||||
*/ |
||||
@Data |
||||
public class WeaviateInsertDTO implements Serializable { |
||||
|
||||
/** |
||||
* 实体列表 |
||||
*/ |
||||
private List entities; |
||||
|
||||
/** |
||||
* 类名 |
||||
*/ |
||||
private String className; |
||||
|
||||
/** |
||||
* 属性 |
||||
*/ |
||||
private Map<String,String> attrsMap; |
||||
|
||||
} |
@ -0,0 +1,32 @@
|
||||
package com.hnac.gglm.bigmodel.api.dto; |
||||
|
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/18 19:47 |
||||
*/ |
||||
@Data |
||||
@EqualsAndHashCode |
||||
public class WeaviateQueryDTO implements Serializable { |
||||
|
||||
/** |
||||
* 返回结果字段 |
||||
*/ |
||||
private String resultFields; |
||||
|
||||
/** |
||||
* 类名 |
||||
*/ |
||||
private String className; |
||||
|
||||
/** |
||||
* 查询条件 |
||||
*/ |
||||
private Map<String,String> query; |
||||
|
||||
} |
@ -0,0 +1,45 @@
|
||||
package com.hnac.gglm.bigmodel.api.feign; |
||||
|
||||
import com.hnac.gglm.bigmodel.BigModelConstants; |
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateInsertDTO; |
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateQueryDTO; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springframework.cloud.openfeign.FeignClient; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/18 19:42 |
||||
*/ |
||||
@FeignClient(value = BigModelConstants.APP_NAME) |
||||
public interface IWeaviatesClient { |
||||
|
||||
String PREFIX_API = "/feign/weaviate"; |
||||
String SAVE_BATCH = PREFIX_API + "/saveBatch"; |
||||
String DELETE_BY_IDS = PREFIX_API + "/deleteByIds"; |
||||
String QUERY_LIST = PREFIX_API + "/queryList"; |
||||
/** |
||||
* 删除 |
||||
* @param ids ids,按逗号分隔 |
||||
* @param className 类名 |
||||
*/ |
||||
@DeleteMapping(DELETE_BY_IDS) |
||||
void deleteByIds(@RequestParam("ids") String ids, @RequestParam("className") String className); |
||||
|
||||
/** |
||||
* 查询 |
||||
* @param query 查询参数 |
||||
* @return 查询结果 |
||||
*/ |
||||
@PostMapping(QUERY_LIST) |
||||
R<Object> queryList(@RequestBody WeaviateQueryDTO query); |
||||
|
||||
/** |
||||
* 批量保存 |
||||
* @param insertDTO 插入数据 |
||||
* @return 是否成功 |
||||
*/ |
||||
@PostMapping(SAVE_BATCH) |
||||
R saveBatch(@RequestBody WeaviateInsertDTO insertDTO); |
||||
|
||||
} |
@ -0,0 +1,85 @@
|
||||
package com.hnac.hzims.equipment.scheduled; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.alibaba.fastjson.JSONArray; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateInsertDTO; |
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateQueryDTO; |
||||
import com.hnac.gglm.bigmodel.api.feign.IWeaviatesClient; |
||||
import com.hnac.hzims.equipment.service.IEmInfoService; |
||||
import com.hnac.hzims.equipment.vo.DeviceLedgerVO; |
||||
import com.hnac.hzims.fdp.constants.ScheduledConstant; |
||||
import com.xxl.job.core.biz.model.ReturnT; |
||||
import com.xxl.job.core.handler.annotation.XxlJob; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.utils.DateUtil; |
||||
import org.springblade.core.tool.utils.Func; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import java.time.LocalDate; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Optional; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/18 18:33 |
||||
* @Description: 设备台账定时任务 |
||||
*/ |
||||
@Component |
||||
@AllArgsConstructor |
||||
public class DeviceLedgerScheduledTask { |
||||
|
||||
private final IEmInfoService emInfoService; |
||||
private final IWeaviatesClient weaviateClient; |
||||
|
||||
@XxlJob(ScheduledConstant.DEVICE_LEDGER_DATA_GENERATE) |
||||
public ReturnT<String> execute(String param) throws Exception { |
||||
JSONObject paramJson = JSONObject.parseObject(param); |
||||
String emCode = Optional.ofNullable(paramJson).map(json -> json.getString("emCode")).orElse(""); |
||||
String date = Optional.ofNullable(paramJson).map(json -> json.getString("date")).orElse(LocalDate.now().minusDays(1).format(DateUtil.DATE_FORMATTER)); |
||||
List<DeviceLedgerVO> deviceLedgerList = emInfoService.getDeviceLedgerList(emCode, date); |
||||
// 向量库查询该设备的所有数据
|
||||
WeaviateQueryDTO query = new WeaviateQueryDTO(); |
||||
Map<String,String> queryMap = new HashMap<>(); |
||||
if(Func.isNotEmpty(emCode)) { |
||||
queryMap.put("emCode",emCode); |
||||
} |
||||
queryMap.put("date",date); |
||||
query.setQuery(queryMap); |
||||
query.setClassName(ScheduledConstant.DEVICE_LEDGER_CLASS_NAME); |
||||
R<Object> searchResult = weaviateClient.queryList(query); |
||||
if(searchResult.isSuccess() && Func.isNotEmpty(searchResult.getData())) { |
||||
// 取出数据ID 删除数据
|
||||
JSONObject queryJson = JSONObject.parseObject(searchResult.getData().toString()); |
||||
JSONArray data = Optional.ofNullable(queryJson).map(json -> json.getJSONObject("Get")) |
||||
.map(json -> json.getJSONArray(ScheduledConstant.DEVICE_LEDGER_CLASS_NAME)).get(); |
||||
List<String> ids = data.stream().map(item -> { |
||||
JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(item)); |
||||
return Optional.ofNullable(jsonObject) |
||||
.map(json -> json.getJSONObject("_additional")) |
||||
.map(json -> json.getString("id")) |
||||
.orElse(""); |
||||
}).filter(Func::isNotEmpty).collect(Collectors.toList()); |
||||
if(Func.isNotEmpty(ids)) { |
||||
weaviateClient.deleteByIds(ids.toString(),ScheduledConstant.DEVICE_LEDGER_CLASS_NAME); |
||||
} |
||||
} |
||||
// 批量插入数据
|
||||
WeaviateInsertDTO weaviateInsertDTO = new WeaviateInsertDTO(); |
||||
weaviateInsertDTO.setEntities(deviceLedgerList); |
||||
weaviateInsertDTO.setClassName(ScheduledConstant.DEVICE_LEDGER_CLASS_NAME.replace("Hzn_lm_","")); |
||||
Map<String,String> attrMap = new HashMap<>(); |
||||
attrMap.put("dataSummary","dataSummary"); |
||||
weaviateInsertDTO.setAttrsMap(attrMap); |
||||
R saveResult = weaviateClient.saveBatch(weaviateInsertDTO); |
||||
if(!saveResult.isSuccess()) { |
||||
return new ReturnT<>("FAIL"); |
||||
} |
||||
return new ReturnT<>("SUCCESS"); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,40 @@
|
||||
package com.hnac.hzims.equipment.vo; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/18 18:45 |
||||
*/ |
||||
@Data |
||||
@ApiModel(value = "设备台账VO对象",description = "大模型使用") |
||||
@EqualsAndHashCode |
||||
public class DeviceLedgerVO implements Serializable { |
||||
|
||||
@ApiModelProperty("站点编号") |
||||
private String stationCode; |
||||
|
||||
@ApiModelProperty("站点名称") |
||||
private String stationName; |
||||
|
||||
@ApiModelProperty("设备编号") |
||||
private String deviceCode; |
||||
|
||||
@ApiModelProperty("设备名称") |
||||
private String deviceName; |
||||
|
||||
@ApiModelProperty("日期") |
||||
private String date; |
||||
|
||||
@ApiModelProperty("数据类型") |
||||
private String type; |
||||
|
||||
@ApiModelProperty("数据概括") |
||||
private String dataSummary; |
||||
|
||||
} |
@ -0,0 +1,37 @@
|
||||
package com.hnac.gglm.bigmodel.api.feign; |
||||
|
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateInsertDTO; |
||||
import com.hnac.gglm.bigmodel.api.dto.WeaviateQueryDTO; |
||||
import com.hnac.gglm.bigmodel.database.service.WeaviateService; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
/** |
||||
* @Author: huangxing |
||||
* @Date: 2024/09/19 19:06 |
||||
*/ |
||||
@RestController |
||||
@AllArgsConstructor |
||||
public class WeaviatesClient implements IWeaviatesClient { |
||||
|
||||
private final WeaviateService weaviateService; |
||||
|
||||
@Override |
||||
@DeleteMapping(DELETE_BY_IDS) |
||||
public void deleteByIds(@RequestParam("ids") String ids, @RequestParam("className") String className) { |
||||
weaviateService.delete(ids,className); |
||||
} |
||||
|
||||
@Override |
||||
@PostMapping(QUERY_LIST) |
||||
public R<Object> queryList(@RequestBody WeaviateQueryDTO query) { |
||||
return R.data(weaviateService.query(query.getResultFields(), query.getClassName(), query.getQuery())); |
||||
} |
||||
|
||||
@Override |
||||
@PostMapping(SAVE_BATCH) |
||||
public R saveBatch(@RequestBody WeaviateInsertDTO insertDTO) { |
||||
return R.status(weaviateService.saveBatch(insertDTO.getEntities(), insertDTO.getClassName(), insertDTO.getAttrsMap())); |
||||
} |
||||
} |
Loading…
Reference in new issue