FdkkSceneEditService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package com.cdf.service.impl;
  2. import cn.hutool.json.JSONUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.cdf.common.ResultCode;
  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.IHotRelationService;
  17. import com.cdf.util.*;
  18. import com.google.zxing.client.j2se.MatrixToImageWriter;
  19. import com.google.zxing.common.BitMatrix;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.web.multipart.MultipartFile;
  26. import javax.annotation.Resource;
  27. import javax.servlet.http.HttpServletRequest;
  28. import javax.servlet.http.HttpServletResponse;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.*;
  32. @Service
  33. @Slf4j
  34. public class FdkkSceneEditService {
  35. @Resource
  36. private UploadToFdkkOssUtil uploadToFdkkOssUtil;
  37. @Resource
  38. private UploadToCdfOssUtil uploadToCdfOssUtil;
  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. @Value("${fdkk.qr-code-url}")
  46. private String qrCodeUrl;
  47. @Resource
  48. private FdkkClient fdkkClient;
  49. @Resource
  50. private CdfClient cdfClient;
  51. @Autowired
  52. private IHotRelationService hotRelationService;
  53. @Autowired
  54. FdkkSceneService fdkkSceneService;
  55. @Autowired
  56. CdfRolePermService cdfRolePermService;
  57. public JSONObject getAuth(String num, String token) {
  58. cdfRolePermService.checkRole(token);
  59. String fdkkToken = fdkkSceneService.getFdkkToken(token);
  60. HashMap<String,String> map = new HashMap<>();
  61. map.put("num",num);
  62. return fdkkClient.getAuth(map, fdkkToken);
  63. }
  64. public FdkkResponse saveTag(FdkkHotRequest fdkkHotRequest, String token) {
  65. FdkkResponse fdkkResponse = fdkkClient.hotSave(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  66. if(fdkkResponse.getCode() != 0){
  67. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  68. }
  69. List<FdkkHotData> hotDataList = fdkkHotRequest.getHotDataList();
  70. for (FdkkHotData fdkkHotData : hotDataList) {
  71. List<String> relationIds = fdkkHotData.getRelationIds();
  72. String sid = fdkkHotData.getSid();
  73. Integer type = fdkkHotData.getHotType();
  74. if(type == null){
  75. continue;
  76. }
  77. HotRelation hotRelation = hotRelationService.getById(sid);
  78. boolean update = true;
  79. if(hotRelation == null){
  80. hotRelation = new HotRelation();
  81. update = false;
  82. }
  83. hotRelation.setHotId(sid);
  84. hotRelation.setHotType(type);
  85. if(relationIds !=null && relationIds.size() >0){
  86. hotRelation.setRelationIds(JSONArray.toJSONString(relationIds));
  87. }else {
  88. hotRelation.setRelationIds(null);
  89. }
  90. hotRelation.setNum(fdkkHotRequest.getNum());
  91. hotRelation.setContent(fdkkHotData.getHotContent());
  92. if(update){
  93. hotRelationService.updateById(hotRelation);
  94. }else {
  95. hotRelationService.save(hotRelation);
  96. }
  97. }
  98. return fdkkResponse;
  99. }
  100. public void deleteTag(FdkkHotRequest fdkkHotRequest, String token) {
  101. FdkkResponse fdkkResponse = fdkkClient.hotDelete(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  102. if(fdkkResponse.getCode() !=0){
  103. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  104. }
  105. hotRelationService.removeByIds(fdkkHotRequest.getSidList());
  106. }
  107. public JSONObject getTagList(String num, String token) {
  108. FdkkResponse fdkkResponse = fdkkClient.hotList(new SceneRequest(num), fdkkSceneService.getFdkkToken(token));
  109. if(fdkkResponse.getCode() !=0){
  110. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  111. }
  112. JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(fdkkResponse.getData()));
  113. JSONArray tags =jsonObject.getJSONArray("tags");
  114. jsonObject.put("tags",getProductByJsonObj(tags));
  115. return jsonObject;
  116. }
  117. public JSONArray getHotJson(String num) {
  118. String data = uploadToFdkkOssUtil.getObjectContent(String.format(hotPath, num) );
  119. if(StringUtils.isBlank(data)){
  120. throw new BusinessException(ResultCode.NOT_RECORD);
  121. }
  122. JSONArray tags = JSONObject.parseArray(data);
  123. return getProductByJsonObj(tags);
  124. }
  125. public JSONArray getCdfHotJson(String num) {
  126. String data = uploadToCdfOssUtil.getObjectContent(String.format(hotCdfPath, num));
  127. if(StringUtils.isBlank(data)){
  128. data = uploadToFdkkOssUtil.getObjectContent( String.format(hotPath, num));
  129. }
  130. if(StringUtils.isBlank(data)){
  131. throw new BusinessException(ResultCode.NOT_RECORD);
  132. }
  133. return JSONObject.parseArray(data);
  134. }
  135. public void publicScene(FdkkHotRequest fdkkHotRequest, String token) throws Exception {
  136. FdkkResponse fdkkResponse = fdkkClient.scenePublicScene(new SceneRequest(fdkkHotRequest.getNum()),fdkkSceneService.getFdkkToken(token));
  137. if(fdkkResponse.getCode() !=0){
  138. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  139. }
  140. writeHotJson(fdkkHotRequest.getNum());
  141. String sell = "aws s3 sync s3://4dkankan/scene_view_data/"+fdkkHotRequest.getNum()+" " +
  142. " s3://4dage-moderate2/scene_view_data/"+fdkkHotRequest.getNum()+" --profile user1";
  143. log.info("执行脚本:{}",sell);
  144. ReadCmdLine.callShellByExec(sell);
  145. }
  146. public void writeHotJson(String num) throws IOException {
  147. JSONArray hotJson = getHotJson(num);
  148. String path = String.format(hotLocalPath,num)+"/"+"hot.json";
  149. String json = JSONUtil.toJsonStr(hotJson);
  150. FileUtils.writeFile(path,json );
  151. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  152. }
  153. public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile[] files, String token) throws IOException {
  154. List<MultipartFile> multipartFiles = new ArrayList<>();
  155. if(StringUtils.isNotBlank(fdkkUploadRequest.getBase64())){
  156. MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fdkkUploadRequest.getBase64());
  157. multipartFiles.add(file);
  158. }
  159. if(files.length >0){
  160. multipartFiles.addAll(Arrays.asList(files));
  161. }
  162. List<String> paths = new ArrayList<>();
  163. for (MultipartFile file : multipartFiles) {
  164. if(file !=null && file.getSize() >0){
  165. String fileName = file.getOriginalFilename();
  166. assert fileName != null;
  167. String newFilePath = String.format(hotLocalPath,fdkkUploadRequest.getNum()) + "/"+fileName;
  168. File newFiles = new File(newFilePath );
  169. if (!newFiles.getParentFile().exists()) {
  170. newFiles.getParentFile().mkdirs();
  171. }
  172. if (!newFiles.exists()) {
  173. newFiles.createNewFile();
  174. }
  175. file.transferTo(newFiles);
  176. String path = newFiles.getPath();
  177. paths.add(path);
  178. }
  179. }
  180. fdkkUploadRequest.setBase64(null);
  181. FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,paths,fdkkSceneService.getFdkkToken(token));
  182. if(fdkkResponse.getCode() !=0){
  183. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  184. }
  185. return fdkkResponse;
  186. }
  187. private JSONArray getProductByJsonObj(JSONArray tags){
  188. HashMap<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
  189. HashMap<String,CdfProduct> productMap = new HashMap<>(); //productId cdf
  190. JSONArray requestArray = new JSONArray(); //productId all array
  191. List<String> sidsList = new ArrayList<>();
  192. HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
  193. for (Object obj : tags) {
  194. JSONObject tag = (JSONObject) obj;
  195. String sid = tag.getString("sid");
  196. sidsList.add(sid);
  197. }
  198. List<HotRelation> hotRelations = hotRelationService.listByIds(sidsList); //批量查询热点
  199. for (HotRelation hotRelation : hotRelations) {
  200. hotRelationMap.put(hotRelation.getHotId(),hotRelation);
  201. }
  202. for (Object obj : tags) {
  203. JSONObject tag = (JSONObject) obj;
  204. String sid = tag.getString("sid");
  205. HotRelation hotRelation = hotRelationMap.get(sid);
  206. if(hotRelation == null){
  207. continue;
  208. }
  209. tag.put("hotType",hotRelation.getHotType());
  210. //0商品,1优惠劵,2第三方跳转,3瀑布流 ,场景关联
  211. if(hotRelation.getHotType() == null){
  212. continue;
  213. }
  214. if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){
  215. tag.put("hotContent", hotRelation.getContent());
  216. continue;
  217. }
  218. String relationIds = hotRelation.getRelationIds();
  219. if(StringUtils.isBlank(relationIds)){
  220. continue;
  221. }
  222. JSONArray jsonArray = JSONObject.parseArray(relationIds);
  223. if(jsonArray == null || jsonArray.size() <=0){
  224. continue;
  225. }
  226. resultMap.put(sid,jsonArray);
  227. requestArray.addAll(jsonArray);
  228. }
  229. if(requestArray.size() <=0){
  230. return tags;
  231. }
  232. CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray);
  233. CdfProductListByIdsVo vos = cdfClient.getProductListByIds(param);
  234. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  235. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  236. productMap.put(cdfProduct.getId(),cdfProduct);
  237. }
  238. }
  239. for (Object obj : tags) {
  240. JSONObject tag = (JSONObject) obj;
  241. String sid = tag.getString("sid");
  242. JSONArray jsonArray = resultMap.get(sid);
  243. if(jsonArray==null || jsonArray.size()<=0){
  244. continue;
  245. }
  246. List<CdfProduct> cdfProductList = new ArrayList<>();
  247. for (Object o : jsonArray) {
  248. String key = o.toString();
  249. if(productMap.get(key)!=null){
  250. cdfProductList.add(productMap.get(key));
  251. }
  252. }
  253. if(cdfProductList.size() >0){
  254. tag.put("products",cdfProductList);
  255. }
  256. }
  257. return tags;
  258. }
  259. public void downQrCode(HttpServletRequest request, HttpServletResponse response, String num) {
  260. String longUrl;
  261. try {
  262. longUrl = qrCodeUrl +num;
  263. // 生成二维码
  264. BitMatrix qRcodeImg = QRCodeUtil.generateQRCodeStream(longUrl, response);
  265. // 将二维码输出到页面中
  266. MatrixToImageWriter.writeToStream(qRcodeImg, "png", response.getOutputStream());
  267. } catch (Exception e) {
  268. e.printStackTrace();
  269. }
  270. }
  271. }