FdkkSceneEditService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. }
  142. public void writeHotJson(String num) throws IOException {
  143. JSONArray hotJson = getHotJson(num);
  144. String path = String.format(hotLocalPath,num)+"/"+"hot.json";
  145. String json = JSONUtil.toJsonStr(hotJson);
  146. FileUtils.writeFile(path,json );
  147. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  148. }
  149. public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile[] files, String token) throws IOException {
  150. List<MultipartFile> multipartFiles = new ArrayList<>();
  151. if(StringUtils.isNotBlank(fdkkUploadRequest.getBase64())){
  152. MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fdkkUploadRequest.getBase64());
  153. multipartFiles.add(file);
  154. }
  155. if(files.length >0){
  156. multipartFiles.addAll(Arrays.asList(files));
  157. }
  158. List<String> paths = new ArrayList<>();
  159. for (MultipartFile file : multipartFiles) {
  160. if(file !=null && file.getSize() >0){
  161. String fileName = file.getOriginalFilename();
  162. assert fileName != null;
  163. String newFilePath = String.format(hotLocalPath,fdkkUploadRequest.getNum()) + "/"+fileName;
  164. File newFiles = new File(newFilePath );
  165. if (!newFiles.getParentFile().exists()) {
  166. newFiles.getParentFile().mkdirs();
  167. }
  168. if (!newFiles.exists()) {
  169. newFiles.createNewFile();
  170. }
  171. file.transferTo(newFiles);
  172. String path = newFiles.getPath();
  173. paths.add(path);
  174. }
  175. }
  176. fdkkUploadRequest.setBase64(null);
  177. FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,paths,fdkkSceneService.getFdkkToken(token));
  178. if(fdkkResponse.getCode() !=0){
  179. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  180. }
  181. return fdkkResponse;
  182. }
  183. private JSONArray getProductByJsonObj(JSONArray tags){
  184. HashMap<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
  185. HashMap<String,CdfProduct> productMap = new HashMap<>(); //productId cdf
  186. JSONArray requestArray = new JSONArray(); //productId all array
  187. List<String> sidsList = new ArrayList<>();
  188. HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
  189. for (Object obj : tags) {
  190. JSONObject tag = (JSONObject) obj;
  191. String sid = tag.getString("sid");
  192. sidsList.add(sid);
  193. }
  194. List<HotRelation> hotRelations = hotRelationService.listByIds(sidsList); //批量查询热点
  195. for (HotRelation hotRelation : hotRelations) {
  196. hotRelationMap.put(hotRelation.getHotId(),hotRelation);
  197. }
  198. for (Object obj : tags) {
  199. JSONObject tag = (JSONObject) obj;
  200. String sid = tag.getString("sid");
  201. HotRelation hotRelation = hotRelationMap.get(sid);
  202. if(hotRelation == null){
  203. continue;
  204. }
  205. tag.put("hotType",hotRelation.getHotType());
  206. //0商品,1优惠劵,2第三方跳转,3瀑布流 ,场景关联
  207. if(hotRelation.getHotType() == null){
  208. continue;
  209. }
  210. if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){
  211. tag.put("hotContent", hotRelation.getContent());
  212. continue;
  213. }
  214. String relationIds = hotRelation.getRelationIds();
  215. if(StringUtils.isBlank(relationIds)){
  216. continue;
  217. }
  218. JSONArray jsonArray = JSONObject.parseArray(relationIds);
  219. if(jsonArray == null || jsonArray.size() <=0){
  220. continue;
  221. }
  222. resultMap.put(sid,jsonArray);
  223. requestArray.addAll(jsonArray);
  224. }
  225. if(requestArray.size() <=0){
  226. return tags;
  227. }
  228. CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray);
  229. CdfProductListByIdsVo vos = cdfClient.getProductListByIds(param);
  230. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  231. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  232. productMap.put(cdfProduct.getId(),cdfProduct);
  233. }
  234. }
  235. for (Object obj : tags) {
  236. JSONObject tag = (JSONObject) obj;
  237. String sid = tag.getString("sid");
  238. JSONArray jsonArray = resultMap.get(sid);
  239. if(jsonArray==null || jsonArray.size()<=0){
  240. continue;
  241. }
  242. List<CdfProduct> cdfProductList = new ArrayList<>();
  243. for (Object o : jsonArray) {
  244. String key = o.toString();
  245. if(productMap.get(key)!=null){
  246. cdfProductList.add(productMap.get(key));
  247. }
  248. }
  249. if(cdfProductList.size() >0){
  250. tag.put("products",cdfProductList);
  251. }
  252. }
  253. return tags;
  254. }
  255. public void downQrCode(HttpServletRequest request, HttpServletResponse response, String num) {
  256. String longUrl;
  257. try {
  258. longUrl = qrCodeUrl +num;
  259. // 生成二维码
  260. BitMatrix qRcodeImg = QRCodeUtil.generateQRCodeStream(longUrl, response);
  261. // 将二维码输出到页面中
  262. MatrixToImageWriter.writeToStream(qRcodeImg, "png", response.getOutputStream());
  263. } catch (Exception e) {
  264. e.printStackTrace();
  265. }
  266. }
  267. }