提交 31fb8907 作者: yanxin

资讯手动打标逻辑优化

上级 0857edbd
......@@ -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 筛选条件
......@@ -380,9 +394,15 @@ public class InformationController {
*/
@PostMapping(value = "/modifyLabel")
public Result<?> modifyLabel(@RequestBody DataBindLabelFrom bindLabelFrom) {
List<DictVO> nodes = subjectDictMapService.boundList(bindLabelFrom.getSubjectId());
informationService.modifyLabel(bindLabelFrom,nodes);
return Result.OK();
if(bindLabelFrom.getBindList()==null || bindLabelFrom.getBindList().isEmpty()){
List<DictVO> boundList = subjectDictMapService.boundList(bindLabelFrom.getSubjectId());
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.*;
import com.zzsn.event.util.DateUtil;
import com.zzsn.event.util.EsDateUtil;
import com.zzsn.event.util.EsIndexUtil;
import com.zzsn.event.util.EsOpUtil;
import com.zzsn.event.util.tree.Node;
import com.zzsn.event.vo.*;
import com.zzsn.event.vo.es.DisplayInfo;
......@@ -27,8 +28,11 @@ import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
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.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
......@@ -51,6 +55,7 @@ import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilde
import org.elasticsearch.search.aggregations.metrics.Cardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -92,6 +97,8 @@ public class EsService {
private ISubjectKeywordsMapService subjectKeywordsMapService;
@Autowired
private CommonService commonService;
@Autowired
private EsOpUtil esOpUtil;
/**
* 获取专题下的资讯
......@@ -2569,4 +2576,36 @@ public class EsService {
}
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 {
* 资讯绑定标签
*
* @param dataBindLabelFrom 绑定参数
* @param boundList 专题绑定的标签(数据字典)集合
* @author lkg
* @date 2025/3/31
*/
void modifyLabel(DataBindLabelFrom dataBindLabelFrom, List<DictVO> boundList);
void modifyLabel(DataBindLabelFrom dataBindLabelFrom);
/**
* 审核
......@@ -260,4 +259,6 @@ public interface InformationService {
* @date 2025/2/28
*/
List<SpecialInformation> informationAllList(InfoDataSearchCondition searchCondition);
void removeLabelById(String index, String id, String relationId);
}
......@@ -651,39 +651,44 @@ public class InformationServiceImpl implements InformationService {
}
@Override
public void modifyLabel(DataBindLabelFrom dataBindLabelFrom, List<DictVO> boundList) {
DisplayInfo info = (DisplayInfo) esOpUtil.getInfoById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId(), DisplayInfo.class);
if (info != null) {
List<Label> newLabels = new ArrayList<>();
List<SysDictItem> dictItemList = dataBindLabelFrom.getDictItemList();
for (SysDictItem sysDictItem : dictItemList) {
Label label = new Label();
label.setRelationId(sysDictItem.getId());
label.setLabelMark(sysDictItem.getDictCode());
label.setRelationName(sysDictItem.getItemText());
newLabels.add(label);
public void modifyLabel(DataBindLabelFrom dataBindLabelFrom) {
List<DictVO> boundList = dataBindLabelFrom.getBindList();
List<String> labelMarks = new ArrayList<>();
for (DictVO dictVO : boundList) {
if(StringUtils.isNotEmpty(dictVO.getCode())){
labelMarks.add(dictVO.getCode());
}else{
labelMarks.add(dictVO.getLabelMark());
}
List<Label> labels = info.getLabels();
if (CollectionUtils.isNotEmpty(labels)) {
List<String> dictCodes = new ArrayList<>();
boundList.forEach(node -> dictCodes.add(node.getCode()));
List<Label> collect = labels.stream().filter(label -> dictCodes.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.add(label);
}
}
List<Label> dbLabels = esService.getLabelsById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId());
List<Label> newLabels = new ArrayList<>();
List<SysDictItem> dictItemList = dataBindLabelFrom.getDictItemList();
for (SysDictItem sysDictItem : dictItemList) {
Label label = new Label();
label.setRelationId(sysDictItem.getId());
label.setLabelMark(sysDictItem.getDictCode());
label.setRelationName(sysDictItem.getItemText());
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())));
info.setLabels(finalList);
esOpUtil.docUpdateById(dataBindLabelFrom.getIndex(), info.getId(), JSON.toJSONString(info));
List<Label> otherCollect = dbLabels.stream().filter(label -> !labelMarks.contains(label.getLabelMark())).collect(Collectors.toList());
newLabels.addAll(otherCollect);
}
esService.updateLabelsById(dataBindLabelFrom.getIndex(), dataBindLabelFrom.getId(), newLabels);
}
@Override
......@@ -1128,6 +1133,16 @@ public class InformationServiceImpl implements InformationService {
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) {
if (CollectionUtils.isNotEmpty(labelModelVos)) {
List<Label> list = info.getLabels();
......
......@@ -22,4 +22,6 @@ public class DataBindLabelFrom {
private String subjectId;
/**字典id*/
private List<SysDictItem> dictItemList;
/**专题绑定的标签类型*/
private List<DictVO> bindList;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论