Commit 5aa497e3 by 赵剑炜

添加了装备类别的模糊查询

parent ef262676
......@@ -4,6 +4,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
@Data
......@@ -22,4 +23,9 @@ public class EquipmentTypeDto {
private Boolean isLeaf;
private List<String> supplierList;
private String photo;
private List<EquipmentTypeDto> children = new ArrayList<>();
public void addChild(EquipmentTypeDto child) {
children.add(child);
}
}
......@@ -12,6 +12,7 @@ public class OrgDto {
private String orgCode;
private Integer isDepartment;
private Long orgParentId;
private String orgParentIds;
private String areaName;
private String levelFlag;
private String dName;
......
......@@ -15,6 +15,7 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@RestController
......@@ -49,7 +50,11 @@ public class EquipmentTypeController {
public ApiRes<List<EquipmentTypeDto>> ShowEquipmentList(@RequestBody QueryEquipmentTypeReq req) {
return ApiRes.success(equipmentTypeService.getEquipmentList(req));
}
@PostMapping("/ShowEquipmentESList")
@ApiOperation("通过ES模糊检索类别列表")
public ApiRes<List<EquipmentTypeDto>> ShowEquipmentESList(@RequestBody QueryEquipmentTypeReq req) throws IOException {
return ApiRes.success(equipmentTypeService.getTypeTreeByEs(req));
}
@PostMapping("/GetEquipmentDetail")
@ApiOperation("查询单个物资信息")
......
package com.junmp.jyzb.entity;
import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
......@@ -17,6 +18,7 @@ import org.springframework.data.elasticsearch.annotations.Document;
@NoArgsConstructor
@TableName("base_equipment_type")
@Document(indexName = "type_pinyin")
@JsonIgnoreProperties(ignoreUnknown = true)
public class EquipmentType implements Serializable {
/**
* 装备类型ID
......
......@@ -25,7 +25,7 @@ public interface EquipmentTypeService extends IService<EquipmentType> {
List<EquipmentTypeDto> getLowType(QueryEquipmentTypeReq orgId);
ResponseResult setTypeParentIds();
List<EquipmentTypeDto> getTypeTreeByEs(QueryOrgReq req) throws IOException;
List<EquipmentTypeDto> getTypeTreeByEs(QueryEquipmentTypeReq req) throws IOException;
List<EquipmentTreeDto> GetTypeTree();
}
......@@ -271,36 +271,71 @@ public class EquipmentTypeServiceImpl extends ServiceImpl<EquipmentTypeMapper, E
return new ResponseResult(HttpStatus.SUCCESS, ReturnMsg.PASS);
}
@Override
public List<EquipmentTypeDto> getTypeTreeByEs(QueryOrgReq req) throws IOException {
return null;
public List<EquipmentTypeDto> getTypeTreeByEs(QueryEquipmentTypeReq req) throws IOException {
ElasticsearchUtil es=new ElasticsearchUtil<>(client);
// 先从ES中拿到检索数据
List<EquipmentType> searchResults = es.searchEntities("type_pinyin","name",0, 10, req.getTypeName(),EquipmentType.class);
LambdaQueryWrapper<EquipmentType> wp = this.createWrapper(req);
// 获取所有组织机构数据
List<EquipmentType> allTypes = this.list(wp);
List<EquipmentTypeDto> treeResult= buildTypeTree(searchResults,allTypes);
return treeResult;
}
public List<EquipmentTypeDto> buildTypeTree(List<EquipmentType> searchResults, List<EquipmentType> allTypes) {
Map<String, EquipmentTypeDto> typeDtoMap = new HashMap<>();
public List<EquipmentTypeDto> getTypeTreeByEs(QueryEquipmentTypeReq req) throws IOException {
// LambdaQueryWrapper<EquipmentType> wp = this.createWrapper(req);
// ElasticsearchUtil esUtil=new ElasticsearchUtil<>(client);
// // 1. 从 Elasticsearch 中搜索组织机构数据
// List<EquipmentType> searchResults = esUtil.searchEntities("name","type_pinyin",0, 20, req.getTypeName(),EquipmentType.class);
// // 2. 从数据库中获取完整的组织机构数据
// List<EquipmentType> fullOrgData = this.list(wp);
//
// // 3. 构建树状结构
// List<OrgDto> tree = new ArrayList<>();
// for (PubOrg searchResult : searchResults) {
// // 在完整数据中查找匹配的组织机构
// PubOrg fullOrg = findOrgById(fullOrgData, searchResult.getOrgId());
//
// // 将完整数据转换为前端需要的 DTO
// OrgDto orgDto = convertToDto(fullOrg, fullOrgData);
//
// // TODO: 根据需求决定是否加入树状结构
// tree.add(orgDto);
// }
// return tree;
return null;
// 将所有组织机构转换为OrgDto并放入map中
for (EquipmentType type : allTypes) {
EquipmentTypeDto typeDto = new EquipmentTypeDto();
BeanPlusUtil.copyProperties(type, typeDto);
// 其他字段的映射...
// 将OrgDto放入map中,以orgId为键
typeDtoMap.put(typeDto.getId(), typeDto);
}
List<EquipmentTypeDto> tree = new ArrayList<>();
// 遍历所有搜索到的组织机构数据,构建树
for (EquipmentType searchResult : searchResults) {
EquipmentTypeDto typeDto = typeDtoMap.get(searchResult.getId());
// 如果找到对应的OrgDto
if (typeDto != null && !tree.contains(typeDto)) {
// 从当前节点开始,依次向上查找上级节点,直到最顶层
String parentId = typeDto.getParentId();
// 将当前节点及其相关节点加入树中
while (parentId != null) {
EquipmentTypeDto parentDto = typeDtoMap.get(parentId);
if (parentDto != null) {
// 将当前节点添加到上级节点的children中,确保不重复添加
if (!parentDto.getChildren().contains(typeDto)) {
parentDto.getChildren().add(typeDto);
}
// 如果上级节点是最顶层,将上级节点添加到树中,确保不重复添加
if (parentDto.getParentId().equals("00000000-0000-0000-0000-000000000000")
&&parentDto.getId()!="00000000-0000-0000-0000-000000000000" && !tree.contains(parentDto)) {
tree.add(parentDto);
}
// 将上级节点设为当前节点,继续向上查找
typeDto = parentDto;
parentId = typeDto.getParentId();
} else {
parentId = null; // 上级节点不存在,跳出循环
}
}
}
}
return tree;
}
//获取整棵type树
public List<EquipmentTreeDto> GetTypeTree() {
List<EquipmentType> list = list(new LambdaQueryWrapper<EquipmentType>()
......
......@@ -394,11 +394,11 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
// 添加 MatchQueryBuilder 作为 must 条件
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("orgName", keyword).operator(Operator.AND);
// MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("orgName.pinyin", keyword).operator(Operator.AND);
MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery("orgName.pinyin", keyword).operator(Operator.AND);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(matchQueryBuilder);
// boolQueryBuilder.should(matchQueryBuilder2);
boolQueryBuilder.should(matchQueryBuilder2);
// 添加过滤条件,只获取 delFlag 为 1 的数据
builder.postFilter(QueryBuilders.termQuery("delFlag", 1));
......@@ -447,6 +447,7 @@ public class PubOrgServiceImpl extends ServiceImpl<PubOrgMapper, PubOrg> implem
orgDto.setLevelFlag(String.valueOf(org.getLevelFlag()));
orgDto.setStatusFlag(org.getStatusFlag());
orgDto.setDName(org.getDName());
orgDto.setOrgParentIds(org.getOrgParentIds());
// 其他字段的映射...
// 将OrgDto放入map中,以orgId为键
......
......@@ -28,14 +28,14 @@ public class ElasticsearchUtil<T> {
public List<T> searchEntities(String typeName,String index, Integer pageNum, Integer pageSize, String keyword, Class<T> entityClass) throws IOException {
if (pageNum < 0) pageNum = 0;
SearchRequest request = new SearchRequest(index);
SearchRequest request = new SearchRequest(typeName);
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.from(pageNum);
builder.size(pageSize);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(typeName, keyword).operator(Operator.AND);
MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery(typeName+".pinyin", keyword).operator(Operator.AND);
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(index, keyword).operator(Operator.AND);
MatchQueryBuilder matchQueryBuilder2 = QueryBuilders.matchQuery(index+".pinyin", keyword).operator(Operator.AND);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(matchQueryBuilder);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论