|
|
package com.yohomars.search.index.builder.impls;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.search.dal.ContentMapper;
|
|
|
import com.yoho.search.dal.model.Content;
|
|
|
import com.yoho.search.dal.model.ContentTag;
|
|
|
import com.yohomars.search.index.builder.IndexBuilder;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.LinkedHashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author gris.wang
|
|
|
* @since 2018/3/8
|
|
|
**/
|
|
|
@Component
|
|
|
public class ContentIndexBuilder implements IndexBuilder{
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
private ContentMapper contentMapper;
|
|
|
|
|
|
@Override
|
|
|
public int getTotalCount() throws Exception {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<?> getPageLists(int offset, int limit) throws Exception {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Integer getMinId() throws Exception {
|
|
|
return contentMapper.selectMinId();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Integer getMaxId() throws Exception {
|
|
|
return contentMapper.selectMaxId();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public List<?> getListByRange(int min, int max) throws Exception {
|
|
|
List<Content> list = contentMapper.selectListByRange(min,max);
|
|
|
List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
|
|
|
if(CollectionUtils.isNotEmpty(list)) {
|
|
|
List<Integer> contentIds = new ArrayList<>(list.size());
|
|
|
for (Content content : list) {
|
|
|
String title = content.getTitle();
|
|
|
String cover = content.getCover();
|
|
|
String summary = content.getSummary();
|
|
|
try {
|
|
|
if(StringUtils.isNotBlank(title) && title.startsWith("{")) {
|
|
|
JSONObject obj = JSON.parseObject(title);
|
|
|
title = obj.getString("s");
|
|
|
}
|
|
|
if(StringUtils.isNotBlank(cover) && cover.startsWith("{")) {
|
|
|
JSONObject obj = JSON.parseObject(cover);
|
|
|
cover = obj.getString("pic");
|
|
|
}
|
|
|
if(StringUtils.isNotBlank(summary) && summary.startsWith("{")) {
|
|
|
JSONObject obj = JSON.parseObject(summary);
|
|
|
summary = obj.getString("s");
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
// ignore
|
|
|
}
|
|
|
content.setTitle(title);
|
|
|
content.setCover(cover);
|
|
|
content.setSummary(summary);
|
|
|
contentIds.add(content.getCid());
|
|
|
}
|
|
|
if(CollectionUtils.isNotEmpty(contentIds)) {
|
|
|
List<ContentTag> contentTags = contentMapper.selectTagsByIds(contentIds);
|
|
|
if(CollectionUtils.isNotEmpty(contentTags)) {
|
|
|
for (Content content : list) {
|
|
|
for (ContentTag tag : contentTags) {
|
|
|
if(tag.getCid().equals(content.getId())){
|
|
|
content.addTagId(tag.getTagId());
|
|
|
content.addTagName(tag.getTagName());
|
|
|
content.increaseTagCount(1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
for (Content content : list) {
|
|
|
dataList.add(beanToMap(content));
|
|
|
}
|
|
|
}
|
|
|
return dataList;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getId(Object object) {
|
|
|
return ((Map)object).get("id").toString();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 拼装对象数据为map
|
|
|
* @param content
|
|
|
* @return
|
|
|
*/
|
|
|
private Map<String, Object> beanToMap(Content content) {
|
|
|
Map<String, Object> map = new LinkedHashMap<>();
|
|
|
map.put("id", content.getId());
|
|
|
map.put("cid", content.getCid());
|
|
|
map.put("type", content.getType());
|
|
|
map.put("app", content.getApp());
|
|
|
map.put("title",content.getTitle());
|
|
|
map.put("cover",content.getCover());
|
|
|
map.put("summary",content.getSummary());
|
|
|
map.put("relate_account",content.getRelateAccount());
|
|
|
map.put("tag_ids",content.getTagIds());
|
|
|
map.put("tag_names",content.getTagNames());
|
|
|
map.put("tag_count",content.getTagCount());
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|