Commit 44abacfc authored by 刘基明's avatar 刘基明

同步专栏

parent 64dd0519
......@@ -30,7 +30,7 @@ public class CreateThemeReq {
@ApiModelProperty(value = "修改,则传入正在编辑的ThemeId")
private String editThemeId = "";
@ApiModelProperty("是否同步到社区 0为不同步 1为同步 默认不同步")
@ApiModelProperty("是否同步到社区 0为同步 1为不同步 不传也为同步")
private Integer syncToNewComm = 0;
}
package com.tanpu.community.feign.community;
import com.tanpu.common.api.CommonResp;
import com.tanpu.community.api.beans.vo.feign.newsfeed.NewsFeedSave4NewCommReq;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class FeignBackClientForCommunity implements FallbackFactory<FeignClientForCommunity> {
@Override
public FeignClientForCommunity create(Throwable throwable) {
return new FeignClientForCommunity() {
@Override
public CommonResp saveNewsFeed4NewComm(NewsFeedSave4NewCommReq req) {
log.error("请求信息", throwable);
log.error("FeignBackClientForCommunity.saveNewsFeed4NewComm-同步专栏频ids:{}", req.getNewsFeedId());
return null;
}
};
}
}
package com.tanpu.community.feign.community;
import com.tanpu.common.api.CommonResp;
import com.tanpu.community.api.beans.vo.feign.newsfeed.NewsFeedSave4NewCommReq;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
@FeignClient(value = "service-community", contextId = "community", fallbackFactory = FeignBackClientForCommunity.class, url = "http://tp-community-svc", path = "/community")
// @FeignClient(value = "service-community", contextId = "community", fallbackFactory = FeignBackClientForCommunity.class, url = "http://127.0.0.1:8202/community")
public interface FeignClientForCommunity {
@ApiOperation("发布专栏 圈子用feign")
@PostMapping("/newsFeed/save4NewComm")
CommonResp saveNewsFeed4NewComm(NewsFeedSave4NewCommReq req);
}
package com.tanpu.community.manager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Sets;
import com.tanpu.biz.common.enums.RelTypeEnum;
......@@ -47,14 +49,26 @@ import com.tanpu.community.util.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
......@@ -63,6 +77,10 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.*;
@Slf4j
@Service
public class ThemeManager {
@Value("${tmpfile.dir:/data/tmp/}")
private String tmpDir;
@Resource
private ThemeService themeService;
......@@ -105,6 +123,9 @@ public class ThemeManager {
@Autowired
private RecommendService recommendService;
@Resource
private RestTemplate restTemplate;
public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, String ident, String userId) {
List<String> excludeIds;
if (pageNo > 1) {
......@@ -150,7 +171,6 @@ public class ThemeManager {
}
/**
* 发表主题(修改)
*/
......@@ -160,7 +180,7 @@ public class ThemeManager {
// 校验参数
checkAttachment(req.getContent());
// 转播权限校验
liveRelayCheck(userId,req.getContent());
liveRelayCheck(userId, req.getContent());
// 保存主题表
ThemeEntity themeEntity = new ThemeEntity();
......@@ -175,7 +195,10 @@ public class ThemeManager {
// 新建
themeService.insertTheme(themeEntity);
// 同步到专栏
convertToNewsFeed(req, themeEntity.getThemeId());
if (0 == req.getSyncToNewComm()) {
convertToNewsFeed(req, themeEntity.getThemeId(), userId);
}
} else {
// 修改
themeService.update(themeEntity, req.getEditThemeId());
......@@ -201,27 +224,25 @@ public class ThemeManager {
return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
}
private void convertToNewsFeed(CreateThemeReq req, String themeId) {
if (!ThemeTypeEnum.DISCUSSION.getCode().equals(req.getThemeType())){
private void convertToNewsFeed(CreateThemeReq req, String themeId, String userId) {
if (!ThemeTypeEnum.DISCUSSION.getCode().equals(req.getThemeType())) {
// 只有讨论类型才能同步专栏
throw new BizException("长文类型无法同步专栏");
}
NewsFeedSave4NewCommReq convertReq = new NewsFeedSave4NewCommReq();
convertReq.setNewsFeedId(CommunityConstant.THEME_PREFIX+themeId);
convertReq.setNewsFeedId(CommunityConstant.THEME_PREFIX + themeId);
ArrayList<NewsFeedResReq> feedList = new ArrayList<>();
for (ThemeContentReq themeContentReq : req.getContent()) {
// 文字内容添加到content
if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())){
if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())) {
convertReq.setContent(themeContentReq.getValue());
}else if (RelTypeEnum.MULTIPLE_IMAGE.type.equals(themeContentReq.getType())){
} else if (RelTypeEnum.MULTIPLE_IMAGE.type.equals(themeContentReq.getType())) {
List<ImagesDTO> imgList = themeContentReq.getImgList();
imgList.forEach(img->feedList.add(NewsFeedResReq.builder()
.remark(img.getRemark())
.relType(Integer.parseInt(RelTypeEnum.IMAGE_FILE.type))
.relId(img.getRelId()) //todo 上传文件并替换id
.build()));
}else {
imgList.forEach(img -> {
feedList.add(convertImg(img, userId));
});
} else {
//其他类型的附件
feedList.add(NewsFeedResReq.builder().relType(Integer.parseInt(themeContentReq.getType()))
.relId(themeContentReq.getValue())
......@@ -235,19 +256,64 @@ public class ThemeManager {
}
private NewsFeedResReq convertImg(ImagesDTO img, String userId) {
private void convertImg(ImagesDTO img,String userId){
HashMap map = new HashMap<>();
ResponseEntity<byte[]> resp = restTemplate.getForEntity(img.getRemark(), byte[].class);
byte[] rst = resp.getBody();
String fileName = tmpDir + System.currentTimeMillis() + ".tmp";
File f = new File(fileName);
try {
FileUtils.writeByteArrayToFile(f, rst);
// 调用对方接口
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
headers.set("uid", userId);
MultiValueMap<String, Object> params = new LinkedMultiValueMap<String, Object>();
HashMap<String, Object> item = new HashMap<>();
// todo 图片类型
map.put("filetype","jpg");
map.put("refid",img.getRelId());
map.put("mode",0);
map.put("userId",userId);
item.put("filetype", "jpg");
item.put("refid", img.getRelId());
item.put("mode", 0);
item.put("userId", userId);
FileSystemResource br = new FileSystemResource(f);
params.add("file", br);
params.add("item", item);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(params, headers);
// tp-community-svc
ResponseEntity<String> response = restTemplate.exchange("http://127.0.0.1:8090/fatools/h5/rest/common/uploadSingleFile", HttpMethod.POST, requestEntity, String.class);
log.info("new-community uploadThemePic returns {}", JSON.toJSONString(response));
if (StringUtils.isBlank(response.getBody())) {
throw new RuntimeException("response body is blank");
}
CommonResp<JSONObject> ret = JSON.parseObject(response.getBody(), CommonResp.class);
if (!ret.isSuccess()) {
throw new RuntimeException("reponse is not success");
}
JSONObject commonResult = ret.getData();
NewsFeedResReq newsFeedResReq = new NewsFeedResReq();
newsFeedResReq.setRelId(commonResult.getString("fileId"));
newsFeedResReq.setRelType(Integer.parseInt(RelTypeEnum.IMAGE_FILE.type));
newsFeedResReq.setRemark(commonResult.getString("fileurl"));
return newsFeedResReq;
} catch (Exception e) {
log.error("error in handleSyncImg for imgUrl: {}", img.getRemark(), e);
throw new RuntimeException(e);
} finally {
try {
FileUtils.forceDelete(f);
} catch (Exception e) {
// do nothing
}
}
}
/**
......@@ -604,14 +670,14 @@ public class ThemeManager {
// 校验参数
checkAttachment(req.getContent());
// 转播权限校验
liveRelayCheck(userId,req.getContent());
liveRelayCheck(userId, req.getContent());
// 保存主题表
ThemeEntity themeEntity = new ThemeEntity();
BeanUtils.copyProperties(req, themeEntity);
themeEntity.setAuthorId(userId);
themeEntity.setContent(JsonUtil.toJson(req.getContent()));
themeEntity.setThemeId(CommunityConstant.THEME_PREFIX+req.getEditThemeId());
themeEntity.setThemeId(CommunityConstant.THEME_PREFIX + req.getEditThemeId());
themeService.insertTheme(themeEntity);
// 保存附件表
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment