FdkkSceneEditService.java 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package com.cdf.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.cdf.common.ResultCode;
  5. import com.cdf.entity.FdkkUser;
  6. import com.cdf.entity.HotRelation;
  7. import com.cdf.exception.BusinessException;
  8. import com.cdf.httpClient.client.CdfClient;
  9. import com.cdf.httpClient.client.FdkkClient;
  10. import com.cdf.httpClient.request.FdkkHotData;
  11. import com.cdf.httpClient.request.FdkkHotRequest;
  12. import com.cdf.httpClient.request.FdkkUploadRequest;
  13. import com.cdf.httpClient.request.SceneRequest;
  14. import com.cdf.httpClient.response.FdkkResponse;
  15. import com.cdf.httpClient.response.cdf.*;
  16. import com.cdf.service.IFdkkUserService;
  17. import com.cdf.service.IHotRelationService;
  18. import com.cdf.util.FileUtils;
  19. import com.cdf.util.JwtUtil;
  20. import com.cdf.util.UploadToOssUtil;
  21. import lombok.extern.slf4j.Slf4j;
  22. import org.apache.commons.lang3.StringUtils;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.web.multipart.MultipartFile;
  27. import javax.annotation.Resource;
  28. import java.io.File;
  29. import java.io.IOException;
  30. import java.util.*;
  31. import java.util.stream.Collectors;
  32. @Service
  33. @Slf4j
  34. public class FdkkSceneEditService {
  35. @Resource
  36. private UploadToOssUtil uploadToOssUtil;
  37. @Value("${upload.bucket}")
  38. private String bucket;
  39. @Value("${fdkk.hot-path}")
  40. private String hotPath;
  41. @Value("${fdkk.hot-cdf-path}")
  42. private String hotCdfPath;
  43. @Value("${fdkk.hot-local-path}")
  44. private String hotLocalPath;
  45. @Resource
  46. private FdkkClient fdkkClient;
  47. @Resource
  48. private CdfClient cdfClient;
  49. @Autowired
  50. private IHotRelationService hotRelationService;
  51. @Autowired
  52. FdkkSceneService fdkkSceneService;
  53. public FdkkResponse saveTag(FdkkHotRequest fdkkHotRequest, String token) {
  54. FdkkResponse fdkkResponse = fdkkClient.hotSave(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  55. if(fdkkResponse.getCode() != 0){
  56. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  57. }
  58. List<FdkkHotData> hotDataList = fdkkHotRequest.getHotDataList();
  59. for (FdkkHotData fdkkHotData : hotDataList) {
  60. List<String> relationIds = fdkkHotData.getRelationIds();
  61. String sid = fdkkHotData.getSid();
  62. Integer type = fdkkHotData.getHotType();
  63. if(type == null){
  64. continue;
  65. }
  66. HotRelation hotRelation = new HotRelation();
  67. hotRelation.setHotId(sid);
  68. hotRelation.setHotType(type);
  69. if(relationIds !=null && relationIds.size() >0){
  70. hotRelation.setRelationIds(JSONArray.toJSONString(relationIds));
  71. }
  72. hotRelation.setNum(fdkkHotRequest.getNum());
  73. hotRelation.setContent(fdkkHotData.getHotContent());
  74. hotRelationService.saveOrUpdate(hotRelation);
  75. }
  76. return fdkkResponse;
  77. }
  78. public void deleteTag(FdkkHotRequest fdkkHotRequest, String token) {
  79. FdkkResponse fdkkResponse = fdkkClient.hotDelete(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  80. if(fdkkResponse.getCode() !=0){
  81. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  82. }
  83. hotRelationService.removeByIds(fdkkHotRequest.getSidList());
  84. }
  85. public JSONObject getTagList(String num, String token) {
  86. FdkkResponse fdkkResponse = fdkkClient.hotList(new SceneRequest(num), fdkkSceneService.getFdkkToken(token));
  87. if(fdkkResponse.getCode() !=0){
  88. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  89. }
  90. JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(fdkkResponse.getData()));
  91. JSONArray tags =jsonObject.getJSONArray("tags");
  92. jsonObject.put("tags",getProductByJsonObj(tags));
  93. return jsonObject;
  94. }
  95. public JSONArray getHotJson(String num) {
  96. String data = uploadToOssUtil.getObjectContent(bucket, String.format(hotPath, num));
  97. if(StringUtils.isBlank(data)){
  98. throw new BusinessException(ResultCode.NOT_RECORD);
  99. }
  100. JSONArray tags = JSONObject.parseArray(data);
  101. return getProductByJsonObj(tags);
  102. }
  103. public JSONArray getCdfHotJson(String num) {
  104. String data = uploadToOssUtil.getObjectContent(bucket, String.format(hotCdfPath, num));
  105. if(StringUtils.isBlank(data)){
  106. data = uploadToOssUtil.getObjectContent(bucket, String.format(hotPath, num));
  107. }
  108. if(StringUtils.isBlank(data)){
  109. throw new BusinessException(ResultCode.NOT_RECORD);
  110. }
  111. return JSONObject.parseArray(data);
  112. }
  113. public void publicScene(FdkkHotRequest fdkkHotRequest, String token) throws Exception {
  114. FdkkResponse fdkkResponse = fdkkClient.scenePublicScene(new SceneRequest(fdkkHotRequest.getNum()),fdkkSceneService.getFdkkToken(token));
  115. if(fdkkResponse.getCode() !=0){
  116. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  117. }
  118. JSONArray hotJson = getHotJson(fdkkHotRequest.getNum());
  119. String path = String.format(hotLocalPath, fdkkHotRequest.getNum());
  120. FileUtils.writeFile(path, hotJson.toJSONString());
  121. uploadToOssUtil.upload(path,String.format(hotCdfPath, fdkkHotRequest.getNum()));
  122. }
  123. public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile files, String token) throws IOException {
  124. String path = null;
  125. if(files !=null && files.getSize() >0){
  126. String fileName = files.getOriginalFilename();
  127. assert fileName != null;
  128. String prefix = fileName.substring(fileName.lastIndexOf("."));
  129. File newFile = File.createTempFile(UUID.randomUUID().toString() ,prefix);
  130. files.transferTo(newFile);
  131. path = newFile.getPath();
  132. }
  133. FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,path,fdkkSceneService.getFdkkToken(token));
  134. if(fdkkResponse.getCode() !=0){
  135. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  136. }
  137. return fdkkResponse;
  138. }
  139. private JSONArray getProductByJsonObj(JSONArray tags){
  140. HashMap<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
  141. HashMap<Object,CdfProduct> productMap = new HashMap<>(); //productId cdf
  142. JSONArray requestArray = new JSONArray(); //productId all array
  143. List<String> sidsList = new ArrayList<>();
  144. HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
  145. for (Object obj : tags) {
  146. JSONObject tag = (JSONObject) obj;
  147. String sid = tag.getString("sid");
  148. sidsList.add(sid);
  149. }
  150. List<HotRelation> hotRelations = hotRelationService.listByIds(sidsList); //批量查询热点
  151. for (HotRelation hotRelation : hotRelations) {
  152. hotRelationMap.put(hotRelation.getHotId(),hotRelation);
  153. }
  154. for (Object obj : tags) {
  155. JSONObject tag = (JSONObject) obj;
  156. String sid = tag.getString("sid");
  157. HotRelation hotRelation = hotRelationMap.get(sid);
  158. if(hotRelation == null){
  159. continue;
  160. }
  161. tag.put("hotType",hotRelation.getHotType());
  162. //0商品,1优惠劵,2第三方跳转,3瀑布流 ,场景关联
  163. if(hotRelation.getHotType() == null){
  164. continue;
  165. }
  166. if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){
  167. tag.put("hotContent", hotRelation.getContent());
  168. continue;
  169. }
  170. String relationIds = hotRelation.getRelationIds();
  171. if(StringUtils.isBlank(relationIds)){
  172. continue;
  173. }
  174. JSONArray jsonArray = JSONObject.parseArray(relationIds);
  175. if(jsonArray == null || jsonArray.size() <=0){
  176. continue;
  177. }
  178. resultMap.put(sid,jsonArray);
  179. requestArray.addAll(jsonArray);
  180. }
  181. if(requestArray.size() <=0){
  182. return tags;
  183. }
  184. CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray);
  185. CdfProductListByIdsVo vos = cdfClient.getProductListByIds(param);
  186. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  187. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  188. productMap.put(cdfProduct.getId(),cdfProduct);
  189. }
  190. }
  191. for (Object obj : tags) {
  192. JSONObject tag = (JSONObject) obj;
  193. String sid = tag.getString("sid");
  194. JSONArray jsonArray = resultMap.get(sid);
  195. if(jsonArray==null || jsonArray.size()<=0){
  196. continue;
  197. }
  198. List<CdfProduct> cdfProductList = new ArrayList<>();
  199. for (Object o : jsonArray) {
  200. cdfProductList.add(productMap.get(o));
  201. }
  202. if(cdfProductList.size() >0){
  203. tag.put("products",cdfProductList);
  204. }
  205. }
  206. return tags;
  207. }
  208. }