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

同步专栏

parent 64dd0519
...@@ -30,7 +30,7 @@ public class CreateThemeReq { ...@@ -30,7 +30,7 @@ public class CreateThemeReq {
@ApiModelProperty(value = "修改,则传入正在编辑的ThemeId") @ApiModelProperty(value = "修改,则传入正在编辑的ThemeId")
private String editThemeId = ""; private String editThemeId = "";
@ApiModelProperty("是否同步到社区 0为不同步 1为同步 默认不同步") @ApiModelProperty("是否同步到社区 0为同步 1为不同步 不传也为同步")
private Integer syncToNewComm = 0; 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; package com.tanpu.community.manager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import com.tanpu.biz.common.enums.RelTypeEnum; import com.tanpu.biz.common.enums.RelTypeEnum;
...@@ -47,14 +49,26 @@ import com.tanpu.community.util.TimeUtils; ...@@ -47,14 +49,26 @@ import com.tanpu.community.util.TimeUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils; import org.apache.commons.collections4.ListUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; 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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; 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 javax.annotation.Resource;
import java.io.File;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -63,6 +77,10 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.*; ...@@ -63,6 +77,10 @@ import static com.tanpu.community.api.constants.RedisKeyConstant.*;
@Slf4j @Slf4j
@Service @Service
public class ThemeManager { public class ThemeManager {
@Value("${tmpfile.dir:/data/tmp/}")
private String tmpDir;
@Resource @Resource
private ThemeService themeService; private ThemeService themeService;
...@@ -105,6 +123,9 @@ public class ThemeManager { ...@@ -105,6 +123,9 @@ public class ThemeManager {
@Autowired @Autowired
private RecommendService recommendService; private RecommendService recommendService;
@Resource
private RestTemplate restTemplate;
public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, String ident, String userId) { public ThemeFullSearchResp themeFullSearch(String keyword, Integer pageNo, Integer pageSize, String ident, String userId) {
List<String> excludeIds; List<String> excludeIds;
if (pageNo > 1) { if (pageNo > 1) {
...@@ -150,7 +171,6 @@ public class ThemeManager { ...@@ -150,7 +171,6 @@ public class ThemeManager {
} }
/** /**
* 发表主题(修改) * 发表主题(修改)
*/ */
...@@ -160,7 +180,7 @@ public class ThemeManager { ...@@ -160,7 +180,7 @@ public class ThemeManager {
// 校验参数 // 校验参数
checkAttachment(req.getContent()); checkAttachment(req.getContent());
// 转播权限校验 // 转播权限校验
liveRelayCheck(userId,req.getContent()); liveRelayCheck(userId, req.getContent());
// 保存主题表 // 保存主题表
ThemeEntity themeEntity = new ThemeEntity(); ThemeEntity themeEntity = new ThemeEntity();
...@@ -175,7 +195,10 @@ public class ThemeManager { ...@@ -175,7 +195,10 @@ public class ThemeManager {
// 新建 // 新建
themeService.insertTheme(themeEntity); themeService.insertTheme(themeEntity);
// 同步到专栏 // 同步到专栏
convertToNewsFeed(req, themeEntity.getThemeId()); if (0 == req.getSyncToNewComm()) {
convertToNewsFeed(req, themeEntity.getThemeId(), userId);
}
} else { } else {
// 修改 // 修改
themeService.update(themeEntity, req.getEditThemeId()); themeService.update(themeEntity, req.getEditThemeId());
...@@ -201,27 +224,25 @@ public class ThemeManager { ...@@ -201,27 +224,25 @@ public class ThemeManager {
return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build(); return CreateThemeResp.builder().themeId(themeEntity.getThemeId()).build();
} }
private void convertToNewsFeed(CreateThemeReq req, String themeId) { private void convertToNewsFeed(CreateThemeReq req, String themeId, String userId) {
if (!ThemeTypeEnum.DISCUSSION.getCode().equals(req.getThemeType())){ if (!ThemeTypeEnum.DISCUSSION.getCode().equals(req.getThemeType())) {
// 只有讨论类型才能同步专栏 // 只有讨论类型才能同步专栏
throw new BizException("长文类型无法同步专栏"); throw new BizException("长文类型无法同步专栏");
} }
NewsFeedSave4NewCommReq convertReq = new NewsFeedSave4NewCommReq(); NewsFeedSave4NewCommReq convertReq = new NewsFeedSave4NewCommReq();
convertReq.setNewsFeedId(CommunityConstant.THEME_PREFIX+themeId); convertReq.setNewsFeedId(CommunityConstant.THEME_PREFIX + themeId);
ArrayList<NewsFeedResReq> feedList = new ArrayList<>(); ArrayList<NewsFeedResReq> feedList = new ArrayList<>();
for (ThemeContentReq themeContentReq : req.getContent()) { for (ThemeContentReq themeContentReq : req.getContent()) {
// 文字内容添加到content // 文字内容添加到content
if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())){ if (RelTypeEnum.TEXT.type.equals(themeContentReq.getType())) {
convertReq.setContent(themeContentReq.getValue()); 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(); List<ImagesDTO> imgList = themeContentReq.getImgList();
imgList.forEach(img->feedList.add(NewsFeedResReq.builder() imgList.forEach(img -> {
.remark(img.getRemark()) feedList.add(convertImg(img, userId));
.relType(Integer.parseInt(RelTypeEnum.IMAGE_FILE.type)) });
.relId(img.getRelId()) //todo 上传文件并替换id } else {
.build()));
}else {
//其他类型的附件 //其他类型的附件
feedList.add(NewsFeedResReq.builder().relType(Integer.parseInt(themeContentReq.getType())) feedList.add(NewsFeedResReq.builder().relType(Integer.parseInt(themeContentReq.getType()))
.relId(themeContentReq.getValue()) .relId(themeContentReq.getValue())
...@@ -235,19 +256,64 @@ public class ThemeManager { ...@@ -235,19 +256,64 @@ public class ThemeManager {
} }
private NewsFeedResReq convertImg(ImagesDTO img, String userId) {
private void convertImg(ImagesDTO img,String userId){ ResponseEntity<byte[]> resp = restTemplate.getForEntity(img.getRemark(), byte[].class);
HashMap map = new HashMap<>(); byte[] rst = resp.getBody();
// todo 图片类型 String fileName = tmpDir + System.currentTimeMillis() + ".tmp";
map.put("filetype","jpg"); File f = new File(fileName);
map.put("refid",img.getRelId()); try {
map.put("mode",0); FileUtils.writeByteArrayToFile(f, rst);
map.put("userId",userId);
} // 调用对方接口
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 图片类型
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 { ...@@ -604,14 +670,14 @@ public class ThemeManager {
// 校验参数 // 校验参数
checkAttachment(req.getContent()); checkAttachment(req.getContent());
// 转播权限校验 // 转播权限校验
liveRelayCheck(userId,req.getContent()); liveRelayCheck(userId, req.getContent());
// 保存主题表 // 保存主题表
ThemeEntity themeEntity = new ThemeEntity(); ThemeEntity themeEntity = new ThemeEntity();
BeanUtils.copyProperties(req, themeEntity); BeanUtils.copyProperties(req, themeEntity);
themeEntity.setAuthorId(userId); themeEntity.setAuthorId(userId);
themeEntity.setContent(JsonUtil.toJson(req.getContent())); themeEntity.setContent(JsonUtil.toJson(req.getContent()));
themeEntity.setThemeId(CommunityConstant.THEME_PREFIX+req.getEditThemeId()); themeEntity.setThemeId(CommunityConstant.THEME_PREFIX + req.getEditThemeId());
themeService.insertTheme(themeEntity); 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