Browse Source

feat: 1.当文章进行新增及修改时,同步新增文章数据至es. 2.文章进行下线时,根据下线范围选择更新数据或删除es数据

national_dev
coral 2 years ago
parent
commit
cdfa7cab51
  1. 4
      code/smart-community/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/ActWithoutProofService.java
  2. 74
      code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java
  3. 59
      code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchArticleDTO.java
  4. 129
      code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java

4
code/smart-community/epmet-module/gov-org/gov-org-server/src/main/java/com/epmet/service/ActWithoutProofService.java

@ -27,7 +27,6 @@ public interface ActWithoutProofService {
/** /**
* 根据居民身份证获取证明 * 根据居民身份证获取证明
* *
* @param token
* @param idCard 身份证号码 * @param idCard 身份证号码
* @param certType 证件类型 001身份证 002统一信用代码证 * @param certType 证件类型 001身份证 002统一信用代码证
* @param qrcode 营业执照必填 * @param qrcode 营业执照必填
@ -39,7 +38,6 @@ public interface ActWithoutProofService {
/** /**
* 重新开具证明 * 重新开具证明
* *
* @param token
* @param idCard * @param idCard
* @param zmtaskguid * @param zmtaskguid
* @return * @return
@ -53,5 +51,5 @@ public interface ActWithoutProofService {
* @param refresh 是否刷新token(true刷新false不刷新) * @param refresh 是否刷新token(true刷新false不刷新)
* @return * @return
*/ */
String getToken(Boolean refresh) throws IOException;; String getToken(Boolean refresh) throws IOException;
} }

74
code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/service/impl/ArticleServiceImpl.java

@ -55,6 +55,8 @@ import com.epmet.feign.EpmetUserOpenFeignClient;
import com.epmet.feign.GovOrgOpenFeignClient; import com.epmet.feign.GovOrgOpenFeignClient;
import com.epmet.redis.TagRedis; import com.epmet.redis.TagRedis;
import com.epmet.service.*; import com.epmet.service.*;
import com.epmet.utils.ElasticsearchArticleDTO;
import com.epmet.utils.ElasticsearchUtils;
import com.epmet.utils.ModuleConstant; import com.epmet.utils.ModuleConstant;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
@ -130,6 +132,8 @@ public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao, ArticleEntit
private ArticleTagsService articleTagsService; private ArticleTagsService articleTagsService;
@Autowired @Autowired
private DraftPublishRangeService draftPublishRangeService; private DraftPublishRangeService draftPublishRangeService;
@Autowired
private ElasticsearchUtils elasticsearchUtils;
@Value("${openapi.scan.server.url}") @Value("${openapi.scan.server.url}")
private String scanApiUrl; private String scanApiUrl;
@ -550,11 +554,47 @@ public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao, ArticleEntit
publishRangeDesc.append(publishRangeDesc.length() > NumConstant.ZERO ? StrConstant.COMMA_ZH : ""); publishRangeDesc.append(publishRangeDesc.length() > NumConstant.ZERO ? StrConstant.COMMA_ZH : "");
publishRangeDesc.append(range.getAgencyGridName()); publishRangeDesc.append(range.getAgencyGridName());
}); });
//文章下线后范围为空,删除es中的文章
try {
if (elasticsearchUtils.deleteArticle(Integer.parseInt(articleEntity.getId()))){
//删除成功
log.info("文章添加,更新es成功");
}else{
//删除失败
log.info("文章添加,更新es失败");
}
}catch (Exception e) {
log.error("scanAllPassPublishArticle delete es exception", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
} else { } else {
publishedList.forEach(pub -> { publishedList.forEach(pub -> {
publishRangeDesc.append(publishRangeDesc.length() > NumConstant.ZERO ? StrConstant.COMMA_ZH : ""); publishRangeDesc.append(publishRangeDesc.length() > NumConstant.ZERO ? StrConstant.COMMA_ZH : "");
publishRangeDesc.append(pub.getAgencyGridName()); publishRangeDesc.append(pub.getAgencyGridName());
}); });
//文章下线后范围不为空,更新es中的文章的范围
try {
ArticleCoverEntity articleCoverEntity = articleCoverDao.selectByArticleId(formDTO.getArticleId());
ArticleContentEntity articleContentEntity = articleContentDao.selectByArticleId(formDTO.getArticleId());
ElasticsearchArticleDTO elasticsearchArticleDTO = ElasticsearchArticleDTO.builder().id(articleEntity.getId()).author(articleEntity.getPublisherName())
.abstracts(articleEntity.getPreviewContent()).category("ARTICLE").title(articleEntity.getTitle())
.content(articleContentEntity.getContent()).source(articleEntity.getPublisherName()).updateDate(String.valueOf(articleEntity.getUpdatedTime()))
.thumbnail(articleCoverEntity.getImgUrl()).uri("/aritcle/"+articleEntity.getId()).gridIdList(formDTO.getGridIdList()).build();
if (elasticsearchUtils.addArticle(elasticsearchArticleDTO)){
//删除成功
log.info("文章添加,更新es成功");
}else{
//删除失败
log.info("文章添加,更新es失败");
}
} catch (Exception e) {
log.error("scanAllPassPublishArticle update es exception", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
} }
entity.setPublishRangeDesc(publishRangeDesc.toString()); entity.setPublishRangeDesc(publishRangeDesc.toString());
baseDao.updateById(entity); baseDao.updateById(entity);
@ -1613,6 +1653,23 @@ public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao, ArticleEntit
log.error("scanAllPassPublishArticle update redis exception", e); log.error("scanAllPassPublishArticle update redis exception", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
} }
// 9.更新es
try {
ElasticsearchArticleDTO elasticsearchArticleDTO = ElasticsearchArticleDTO.builder().id(article.getId()).author(article.getPublisherName())
.abstracts(article.getPreviewContent()).category("ARTICLE").title(article.getTitle())
.content(articleContent.getContent()).source(article.getPublisherName()).updateDate(String.valueOf(article.getUpdatedTime()))
.thumbnail(formDTO.getImgUrl()).uri("/aritcle/"+article.getId()).gridIdList(formDTO.getGridIdList()).build();
if (elasticsearchUtils.addArticle(elasticsearchArticleDTO)){
//删除成功
log.info("文章添加,更新es成功");
}else{
//删除失败
log.info("文章添加,更新es失败");
}
} catch (Exception e) {
log.error("scanAllPassPublishArticle update es exception", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
}else {// 存草稿 }else {// 存草稿
// 1.草稿 // 1.草稿
DraftEntity draft = ConvertUtils.sourceToTarget(formDTO, DraftEntity.class); DraftEntity draft = ConvertUtils.sourceToTarget(formDTO, DraftEntity.class);
@ -1976,6 +2033,23 @@ public class ArticleServiceImpl extends BaseServiceImpl<ArticleDao, ArticleEntit
log.warn("updateArticle update redis exception", e); log.warn("updateArticle update redis exception", e);
// throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg()); // throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
} }
// 9.更新es
try {
ElasticsearchArticleDTO elasticsearchArticleDTO = ElasticsearchArticleDTO.builder().id(articleEntity.getId()).author(articleEntity.getPublisherName())
.abstracts(articleEntity.getPreviewContent()).category("ARTICLE").title(articleEntity.getTitle())
.content(articleContent.getContent()).source(articleEntity.getPublisherName()).updateDate(String.valueOf(articleEntity.getUpdatedTime()))
.thumbnail(formDTO.getImgUrl()).uri("/aritcle/"+articleEntity.getId()).gridIdList(formDTO.getGridIdList()).build();
if (elasticsearchUtils.updateArticle(elasticsearchArticleDTO)){
//删除成功
log.info("文章更新,更新es成功");
}else{
//删除失败
log.info("文章更新,更新es失败");
}
} catch (Exception e) {
log.error("scanAllPassPublishArticle update es exception", e);
throw new RenException(EpmetErrorCode.SERVER_ERROR.getCode(), EpmetErrorCode.SERVER_ERROR.getMsg());
}
} }

59
code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchArticleDTO.java

@ -0,0 +1,59 @@
package com.epmet.utils;
import lombok.Builder;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
@Builder
public class ElasticsearchArticleDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 文章摘要
*/
private String abstracts;
/**
* 文章作者
*/
private String author;
/**
* 资源类型 APPLICATION应用SERVICE服务, ARTICLE文章资讯, PRODUCT产品
*/
private String category;
/**
* 文章内容
*/
private String content;
/**
* ID
*/
private String id;
/**
* 资源来源 SELF系统本身, HUI_SHENG_HUO惠生活WU_YE物业, ZHAO_PIN招聘 , SHE_QU社区
*/
private String source;
/**
* 缩略图
*/
private String thumbnail;
/**
* 文章标题
*/
private String title;
/**
* 更新日期
*/
private String updateDate;
/**
* 资源定位符
*/
private String uri;
/**
* 发布范围IDs
*/
private List<String> gridIdList;
}

129
code/smart-community/epmet-module/gov-voice/gov-voice-server/src/main/java/com/epmet/utils/ElasticsearchUtils.java

@ -0,0 +1,129 @@
package com.epmet.utils;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ElasticsearchUtils {
private static final String ElasticsearchTestURL = "http://172.16.9.20:9000";
private static final String ElasticsearchProdURL = "http://172.16.9.20:9000";
private static final String ElasticsearchURL = ElasticsearchTestURL;
private static final String ElasticsearchArticleDelete = "/SysElasticsearch/article/delete/";
private static final String ElasticsearchArticleAdd = "/SysElasticsearch/article/add";
private static final String ElasticsearchArticleUpdate = "/SysElasticsearch/article/update";
@SneakyThrows
public Boolean addArticle(ElasticsearchArticleDTO bean) {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(bean);
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(ElasticsearchURL+ElasticsearchArticleAdd)
.method("POST", body)
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
// 获取响应的状态码
int statusCode = response.code();
if (response.isSuccessful()) {
// 请求成功,状态码为2xx
if (responseBody != null) {
// 处理响应内容
String responseString = responseBody.string();
log.info("接口请求成功,响应内容:" + responseString);
responseBody.close();
return true;
}
} else {
// 请求失败,状态码不是2xx
log.info("接口请求失败,状态码:" + statusCode);
return false;
}
return false;
}
@SneakyThrows
public Boolean updateArticle(ElasticsearchArticleDTO bean) {
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(bean);
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, json);
Request request = new Request.Builder()
.url(ElasticsearchURL+ElasticsearchArticleUpdate)
.method("POST", body)
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
.addHeader("Content-Type", "application/json")
.build();
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
// 获取响应的状态码
int statusCode = response.code();
if (response.isSuccessful()) {
// 请求成功,状态码为2xx
if (responseBody != null) {
// 处理响应内容
String responseString = responseBody.string();
log.info("接口请求成功,响应内容:" + responseString);
responseBody.close();
return true;
}
} else {
// 请求失败,状态码不是2xx
log.info("接口请求失败,状态码:" + statusCode);
return false;
}
return false;
}
@SneakyThrows
public Boolean deleteArticle(int id) {
OkHttpClient client = new OkHttpClient().newBuilder()
.build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url(ElasticsearchURL+ElasticsearchArticleDelete+id)
.method("DELETE", body)
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
.build();
Response response = client.newCall(request).execute();
ResponseBody responseBody = response.body();
// 获取响应的状态码
int statusCode = response.code();
if (response.isSuccessful()) {
// 请求成功,状态码为2xx
if (responseBody != null) {
// 处理响应内容
String responseString = responseBody.string();
log.info("接口请求成功,响应内容:" + responseString);
responseBody.close();
return true;
}
} else {
// 请求失败,状态码不是2xx
log.info("接口请求失败,状态码:" + statusCode);
return false;
}
return false;
}
}
Loading…
Cancel
Save