提交 31fb8907 作者: yanxin

资讯手动打标逻辑优化

上级 0857edbd
...@@ -269,6 +269,20 @@ public class InformationController { ...@@ -269,6 +269,20 @@ public class InformationController {
} }
/** /**
* 删除指定标签
* @param id
* @param relationId
* @param index
* @return
*/
@GetMapping("/deleteLabel")
public Result<?> delLabel(@RequestParam("id") String id,
@RequestParam("relationId") String relationId,
@RequestParam("index") String index) {
informationService.removeLabelById(index,id,relationId);
return Result.OK("删除成功");
}
/**
* 批量删除标签 * 批量删除标签
* *
* @param searchCondition 筛选条件 * @param searchCondition 筛选条件
...@@ -380,9 +394,15 @@ public class InformationController { ...@@ -380,9 +394,15 @@ public class InformationController {
*/ */
@PostMapping(value = "/modifyLabel") @PostMapping(value = "/modifyLabel")
public Result<?> modifyLabel(@RequestBody DataBindLabelFrom bindLabelFrom) { public Result<?> modifyLabel(@RequestBody DataBindLabelFrom bindLabelFrom) {
List<DictVO> nodes = subjectDictMapService.boundList(bindLabelFrom.getSubjectId()); if(bindLabelFrom.getBindList()==null || bindLabelFrom.getBindList().isEmpty()){
informationService.modifyLabel(bindLabelFrom,nodes); List<DictVO> boundList = subjectDictMapService.boundList(bindLabelFrom.getSubjectId());
return Result.OK(); bindLabelFrom.setBindList(boundList);
}
if(bindLabelFrom.getBindList()==null || bindLabelFrom.getBindList().isEmpty()){
return Result.FAIL("专题未绑定标签,不可编辑");
}
informationService.modifyLabel(bindLabelFrom);
return Result.OK("编辑成功");
} }
/** /**
......
...@@ -18,6 +18,7 @@ import com.zzsn.event.service.*; ...@@ -18,6 +18,7 @@ import com.zzsn.event.service.*;
import com.zzsn.event.util.DateUtil; import com.zzsn.event.util.DateUtil;
import com.zzsn.event.util.EsDateUtil; import com.zzsn.event.util.EsDateUtil;
import com.zzsn.event.util.EsIndexUtil; import com.zzsn.event.util.EsIndexUtil;
import com.zzsn.event.util.EsOpUtil;
import com.zzsn.event.util.tree.Node; import com.zzsn.event.util.tree.Node;
import com.zzsn.event.vo.*; import com.zzsn.event.vo.*;
import com.zzsn.event.vo.es.DisplayInfo; import com.zzsn.event.vo.es.DisplayInfo;
...@@ -27,8 +28,11 @@ import lombok.extern.slf4j.Slf4j; ...@@ -27,8 +28,11 @@ import lombok.extern.slf4j.Slf4j;
import lombok.val; import lombok.val;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.join.ScoreMode; import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory; import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.RestHighLevelClient;
...@@ -51,6 +55,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilde ...@@ -51,6 +55,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilde
import org.elasticsearch.search.aggregations.metrics.Cardinality; import org.elasticsearch.search.aggregations.metrics.Cardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder; import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -92,6 +97,8 @@ public class EsService { ...@@ -92,6 +97,8 @@ public class EsService {
private ISubjectKeywordsMapService subjectKeywordsMapService; private ISubjectKeywordsMapService subjectKeywordsMapService;
@Autowired @Autowired
private CommonService commonService; private CommonService commonService;
@Autowired
private EsOpUtil esOpUtil;
/** /**
* 获取专题下的资讯 * 获取专题下的资讯
...@@ -2569,4 +2576,36 @@ public class EsService { ...@@ -2569,4 +2576,36 @@ public class EsService {
} }
return fullTextBoolQuery; return fullTextBoolQuery;
} }
/**
* 根据id查询标签列表
* @param index
* @param id
* @return
*/
public List<Label> getLabelsById(String index, String id) {
try {
GetRequest request = new GetRequest(index, id);
request.fetchSourceContext(new FetchSourceContext(true, new String[]{"labels","title"}, null));
request.realtime(true);
GetResponse documentFields = client.get(request, RequestOptions.DEFAULT);
String labelsStr = JSONArray.toJSONString(documentFields.getSourceAsMap().get("labels"));
return JSONArray.parseArray(labelsStr, Label.class);
} catch (Exception e) {
log.error("根据数据id获取标签失败,id:{},e:{}", id, e.getMessage());
}
return null;
}
/**
* 根据id更新标签列表
* @param index
* @param id
* @param labels
*/
public void updateLabelsById(String index, String id, List<Label> labels) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("labels", labels);
esOpUtil.docUpdateById(index,id,jsonObject.toString());
}
} }
...@@ -138,11 +138,10 @@ public interface InformationService { ...@@ -138,11 +138,10 @@ public interface InformationService {
* 资讯绑定标签 * 资讯绑定标签
* *
* @param dataBindLabelFrom 绑定参数 * @param dataBindLabelFrom 绑定参数
* @param boundList 专题绑定的标签(数据字典)集合
* @author lkg * @author lkg
* @date 2025/3/31 * @date 2025/3/31
*/ */
void modifyLabel(DataBindLabelFrom dataBindLabelFrom, List<DictVO> boundList); void modifyLabel(DataBindLabelFrom dataBindLabelFrom);
/** /**
* 审核 * 审核
...@@ -260,4 +259,6 @@ public interface InformationService { ...@@ -260,4 +259,6 @@ public interface InformationService {
* @date 2025/2/28 * @date 2025/2/28
*/ */
List<SpecialInformation> informationAllList(InfoDataSearchCondition searchCondition); List<SpecialInformation> informationAllList(InfoDataSearchCondition searchCondition);
void removeLabelById(String index, String id, String relationId);
} }
...@@ -651,39 +651,44 @@ public class InformationServiceImpl implements InformationService { ...@@ -651,39 +651,44 @@ public class InformationServiceImpl implements InformationService {
} }
@Override @Override
public void modifyLabel(DataBindLabelFrom dataBindLabelFrom, List<DictVO> boundList) { public void modifyLabel(DataBindLabelFrom dataBindLabelFrom) {
DisplayInfo info = (DisplayInfo) esOpUtil.getInfoById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId(), DisplayInfo.class); List<DictVO> boundList = dataBindLabelFrom.getBindList();
if (info != null) { List<String> labelMarks = new ArrayList<>();
List<Label> newLabels = new ArrayList<>(); for (DictVO dictVO : boundList) {
List<SysDictItem> dictItemList = dataBindLabelFrom.getDictItemList(); if(StringUtils.isNotEmpty(dictVO.getCode())){
for (SysDictItem sysDictItem : dictItemList) { labelMarks.add(dictVO.getCode());
Label label = new Label(); }else{
label.setRelationId(sysDictItem.getId()); labelMarks.add(dictVO.getLabelMark());
label.setLabelMark(sysDictItem.getDictCode());
label.setRelationName(sysDictItem.getItemText());
newLabels.add(label);
} }
List<Label> labels = info.getLabels(); }
if (CollectionUtils.isNotEmpty(labels)) { List<Label> dbLabels = esService.getLabelsById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId());
List<String> dictCodes = new ArrayList<>(); List<Label> newLabels = new ArrayList<>();
boundList.forEach(node -> dictCodes.add(node.getCode())); List<SysDictItem> dictItemList = dataBindLabelFrom.getDictItemList();
List<Label> collect = labels.stream().filter(label -> dictCodes.contains(label.getLabelMark())).collect(Collectors.toList()); for (SysDictItem sysDictItem : dictItemList) {
if (CollectionUtils.isNotEmpty(collect)) { Label label = new Label();
List<String> dictItemIds = new ArrayList<>(); label.setRelationId(sysDictItem.getId());
dictItemList.forEach(sysDictItem -> dictItemIds.add(sysDictItem.getId())); label.setLabelMark(sysDictItem.getDictCode());
for (Label label : collect) { label.setRelationName(sysDictItem.getItemText());
if (dictItemIds.contains(label.getRelationId())) { newLabels.add(label);
newLabels.add(label); }
}
if (CollectionUtils.isNotEmpty(dbLabels)) {
List<Label> collect = dbLabels.stream().filter(label -> labelMarks.contains(label.getLabelMark())).collect(Collectors.toList());
if (CollectionUtils.isNotEmpty(collect)) {
List<String> dictItemIds = new ArrayList<>();
dictItemList.forEach(sysDictItem -> dictItemIds.add(sysDictItem.getId()));
for (Label label : collect) {
if (dictItemIds.contains(label.getRelationId())) {
//数据库中已存在的标签以当前存储为主,不做更新
newLabels.removeIf(item -> item.getRelationId().equals(label.getRelationId()));
newLabels.add(label);
} }
} }
List<Label> otherCollect = labels.stream().filter(label -> !dictCodes.contains(label.getLabelMark())).collect(Collectors.toList());
newLabels.addAll(otherCollect);
} }
List<Label> finalList = newLabels.stream().collect(Collectors.collectingAndThen(Collectors.toMap(Label::getRelationId, p -> p, (p1, p2) -> p1), map -> new ArrayList<>(map.values()))); List<Label> otherCollect = dbLabels.stream().filter(label -> !labelMarks.contains(label.getLabelMark())).collect(Collectors.toList());
info.setLabels(finalList); newLabels.addAll(otherCollect);
esOpUtil.docUpdateById(dataBindLabelFrom.getIndex(), info.getId(), JSON.toJSONString(info));
} }
esService.updateLabelsById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId(), newLabels);
} }
@Override @Override
...@@ -1128,6 +1133,16 @@ public class InformationServiceImpl implements InformationService { ...@@ -1128,6 +1133,16 @@ public class InformationServiceImpl implements InformationService {
return informationList; return informationList;
} }
@Override
public void removeLabelById(String index, String id, String relationId) {
List<Label> labels = esService.getLabelsById(index,id);
if(labels == null){
return;
}
labels.removeIf(e -> e.getRelationId().equals(relationId));
esService.updateLabelsById(index,id,labels);
}
private void formatLabel(List<LabelModelVo> labelModelVos, DisplayInfo info) { private void formatLabel(List<LabelModelVo> labelModelVos, DisplayInfo info) {
if (CollectionUtils.isNotEmpty(labelModelVos)) { if (CollectionUtils.isNotEmpty(labelModelVos)) {
List<Label> list = info.getLabels(); List<Label> list = info.getLabels();
......
...@@ -22,4 +22,6 @@ public class DataBindLabelFrom { ...@@ -22,4 +22,6 @@ public class DataBindLabelFrom {
private String subjectId; private String subjectId;
/**字典id*/ /**字典id*/
private List<SysDictItem> dictItemList; private List<SysDictItem> dictItemList;
/**专题绑定的标签类型*/
private List<DictVO> bindList;
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论