FdkkSceneEditService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. package com.cdf.service.impl;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.json.JSONUtil;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.cdf.common.ResultCode;
  7. import com.cdf.entity.*;
  8. import com.cdf.exception.BusinessException;
  9. import com.cdf.httpClient.client.CdfClient;
  10. import com.cdf.httpClient.client.CdfHKClient;
  11. import com.cdf.httpClient.client.FdkkClient;
  12. import com.cdf.httpClient.request.FdkkHotData;
  13. import com.cdf.httpClient.request.FdkkHotRequest;
  14. import com.cdf.httpClient.request.FdkkUploadRequest;
  15. import com.cdf.httpClient.request.SceneRequest;
  16. import com.cdf.httpClient.response.FdkkResponse;
  17. import com.cdf.httpClient.response.cdf.*;
  18. import com.cdf.response.BrandApiVo;
  19. import com.cdf.service.*;
  20. import com.cdf.util.*;
  21. import com.google.zxing.client.j2se.MatrixToImageWriter;
  22. import com.google.zxing.common.BitMatrix;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringEscapeUtils;
  25. import org.apache.commons.lang3.StringUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.beans.factory.annotation.Value;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.web.multipart.MultipartFile;
  30. import javax.annotation.Resource;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.io.File;
  34. import java.io.IOException;
  35. import java.util.*;
  36. import java.util.stream.Collectors;
  37. @Service
  38. @Slf4j
  39. public class FdkkSceneEditService {
  40. @Resource
  41. private UploadToFdkkOssUtil uploadToFdkkOssUtil;
  42. @Resource
  43. private UploadToCdfOssUtil uploadToCdfOssUtil;
  44. @Value("${fdkk.hot-path}")
  45. private String hotPath;
  46. @Value("${fdkk.hot-cdf-path}")
  47. private String hotCdfPath;
  48. @Value("${fdkk.hot-local-path}")
  49. private String hotLocalPath;
  50. @Value("${fdkk.qr-code-url}")
  51. private String qrCodeUrl;
  52. @Resource
  53. private FdkkClient fdkkClient;
  54. @Resource
  55. private CdfHKClient cdfHKClient;
  56. @Autowired
  57. private IHotRelationService hotRelationService;
  58. @Autowired
  59. FdkkSceneService fdkkSceneService;
  60. @Autowired
  61. CdfRolePermService cdfRolePermService;
  62. @Autowired
  63. IProductHkService productHkService;
  64. @Autowired
  65. IFdkkUserService fdkkUserService;
  66. @Autowired
  67. IProductSourceService productSourceService;
  68. @Autowired
  69. IBrandService brandService;
  70. @Autowired
  71. IHotRecommendService hotRecommendService;
  72. public JSONObject getAuth(String num, String token) {
  73. String fdkkToken = fdkkSceneService.getFdkkToken(token);
  74. HashMap<String,String> map = new HashMap<>();
  75. map.put("num",num);
  76. JSONObject auth = fdkkClient.getAuth(map, fdkkToken);
  77. Integer code = auth.getInteger("code");
  78. if(code == 0){
  79. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  80. JSONObject data = auth.getJSONObject("data");
  81. data.put("cdfRegionData",numRegion);
  82. }
  83. return auth;
  84. }
  85. public FdkkResponse saveTag(FdkkHotRequest fdkkHotRequest, String token) {
  86. FdkkResponse fdkkResponse = fdkkClient.hotSave(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  87. if(fdkkResponse.getCode() != 0){
  88. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  89. }
  90. List<FdkkHotData> hotDataList = fdkkHotRequest.getHotDataList();
  91. for (FdkkHotData fdkkHotData : hotDataList) {
  92. List<String> relationIds = fdkkHotData.getRelationIds();
  93. String sid = fdkkHotData.getSid();
  94. Integer type = fdkkHotData.getHotType();
  95. if(type == null){
  96. continue;
  97. }
  98. HotRelation hotRelation = hotRelationService.getById(sid);
  99. boolean update = true;
  100. if(hotRelation == null){
  101. hotRelation = new HotRelation();
  102. update = false;
  103. }
  104. hotRelation.setHotId(sid);
  105. hotRelation.setHotType(type);
  106. if(relationIds !=null && relationIds.size() >0){
  107. hotRelation.setRelationIds(JSONArray.toJSONString(relationIds));
  108. }else {
  109. hotRelation.setRelationIds(null);
  110. }
  111. hotRelation.setNum(fdkkHotRequest.getNum());
  112. hotRelation.setContent(fdkkHotData.getHotContent());
  113. hotRelation.setProductSourceId(fdkkHotData.getProductSourceId());
  114. hotRelation.setBrandId(fdkkHotData.getBrandId());
  115. if(update){
  116. hotRelationService.updateById(hotRelation);
  117. }else {
  118. hotRelationService.save(hotRelation);
  119. }
  120. }
  121. return fdkkResponse;
  122. }
  123. public void deleteTag(FdkkHotRequest fdkkHotRequest, String token) {
  124. FdkkResponse fdkkResponse = fdkkClient.hotDelete(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  125. if(fdkkResponse.getCode() !=0){
  126. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  127. }
  128. hotRelationService.removeByIds(fdkkHotRequest.getSidList());
  129. }
  130. public JSONObject getTagList(String num, String token,String language) {
  131. FdkkResponse fdkkResponse = fdkkClient.hotList(new SceneRequest(num), fdkkSceneService.getFdkkToken(token));
  132. if(fdkkResponse.getCode() !=0){
  133. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  134. }
  135. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  136. JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(fdkkResponse.getData()));
  137. JSONArray tags =jsonObject.getJSONArray("tags");
  138. jsonObject.put("tags",getProductByJsonObj(tags,numRegion.getRegion(),language));
  139. return jsonObject;
  140. }
  141. public JSONArray getHotJson(String num,String pType,String language) {
  142. String data = uploadToFdkkOssUtil.getObjectContent(String.format(hotPath, num) );
  143. if(StringUtils.isBlank(data)){
  144. throw new BusinessException(ResultCode.NOT_RECORD);
  145. }
  146. JSONArray tags = JSONObject.parseArray(data);
  147. return getProductByJsonObj(tags,pType,language);
  148. }
  149. public Boolean cdfHasHotJson(String num) {
  150. return uploadToCdfOssUtil.existKey(String.format(hotCdfPath, num));
  151. }
  152. public JSONArray getCdfHotJson(String num) {
  153. String data = uploadToCdfOssUtil.getObjectContent(String.format(hotCdfPath, num));
  154. if(StringUtils.isBlank(data)){
  155. data = uploadToFdkkOssUtil.getObjectContent( String.format(hotPath, num));
  156. }
  157. if(StringUtils.isBlank(data)){
  158. throw new BusinessException(ResultCode.NOT_RECORD);
  159. }
  160. return JSONObject.parseArray(data);
  161. }
  162. public void publicScene(FdkkHotRequest fdkkHotRequest, String token) throws Exception {
  163. FdkkResponse fdkkResponse = fdkkClient.scenePublicScene(new SceneRequest(fdkkHotRequest.getNum()),fdkkSceneService.getFdkkToken(token));
  164. if(fdkkResponse.getCode() !=0){
  165. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  166. }
  167. NumRegion numRegion = fdkkUserService.getRegionByNum(fdkkHotRequest.getNum());
  168. writeHotJson(fdkkHotRequest.getNum(),numRegion.getRegion());
  169. String res1 = "scene_view_data/%s/images/vision.modeldata";
  170. String res2 = "scene_view_data/%s/images/vision2.modeldata";
  171. updateModelData(fdkkHotRequest.getNum(),res1);
  172. updateModelData(fdkkHotRequest.getNum(),res2);
  173. String sell1 = "aws s3 sync s3://4dkankan/scene_view_data/"+fdkkHotRequest.getNum()+"/user " +
  174. " s3://4dage-moderate2/scene_view_data/"+fdkkHotRequest.getNum()+"/user --profile user1";
  175. log.info("执行脚本:{}",sell1);
  176. ReadCmdLine.callShellByExec(sell1);
  177. }
  178. private void updateModelData(String num,String res) {
  179. String localPath = "/home/cdf/"+String.format(res, num);
  180. String s3PathKey = String.format(res, num);
  181. uploadToFdkkOssUtil.downFromS3(s3PathKey,localPath);
  182. uploadToCdfOssUtil.upload(localPath,s3PathKey);
  183. }
  184. public void writeHotJson(String num,String pType) {
  185. try {
  186. if(pType.equals("HK")){
  187. writeHotJsonHk(num,"eshop_en");
  188. writeHotJsonHk(num,"eshop_zh");
  189. writeHotJsonHk(num,"eshop_cn");
  190. return;
  191. }
  192. JSONArray hotJson = getHotJson(num,pType,"all");
  193. String path = String.format(hotLocalPath,num)+"/"+"hot.json";
  194. String json = JSONUtil.toJsonStr(hotJson);
  195. FileUtils.writeFile(path,json );
  196. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  197. }catch (Exception e){
  198. log.error("writeHotJson",e);
  199. }
  200. }
  201. public void writeHotJsonHk(String num,String lang){
  202. try {
  203. JSONArray hotJson = getHotJson(num,"HK",lang);
  204. String name = "hot_" +lang +".json";
  205. String path = String.format(hotLocalPath,num)+"/"+name;
  206. String json = JSONUtil.toJsonStr(hotJson);
  207. FileUtils.writeFile(path,json );
  208. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num).replace("hot.json",name));
  209. if("eshop_zh".equals(lang)){
  210. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  211. }
  212. }catch (Exception e){
  213. e.printStackTrace();
  214. }
  215. }
  216. public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile[] files, String token) throws IOException {
  217. List<MultipartFile> multipartFiles = new ArrayList<>();
  218. if(StringUtils.isNotBlank(fdkkUploadRequest.getBase64())){
  219. MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fdkkUploadRequest.getBase64());
  220. multipartFiles.add(file);
  221. }
  222. if(files.length >0){
  223. multipartFiles.addAll(Arrays.asList(files));
  224. }
  225. List<String> paths = new ArrayList<>();
  226. for (MultipartFile file : multipartFiles) {
  227. if(file !=null && file.getSize() >0){
  228. String fileName = file.getOriginalFilename();
  229. assert fileName != null;
  230. String newFilePath = String.format(hotLocalPath,fdkkUploadRequest.getNum()) + "/"+fileName;
  231. File newFiles = new File(newFilePath );
  232. if (!newFiles.getParentFile().exists()) {
  233. newFiles.getParentFile().mkdirs();
  234. }
  235. if (!newFiles.exists()) {
  236. newFiles.createNewFile();
  237. }
  238. file.transferTo(newFiles);
  239. String path = newFiles.getPath();
  240. paths.add(path);
  241. }
  242. }
  243. fdkkUploadRequest.setBase64(null);
  244. FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,paths,fdkkSceneService.getFdkkToken(token));
  245. if(fdkkResponse.getCode() !=0){
  246. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  247. }
  248. return fdkkResponse;
  249. }
  250. private JSONArray getProductByJsonObj(JSONArray tags,String pType,String language){
  251. HashMap<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
  252. HashMap<String,CdfProduct> productMap = new HashMap<>(); //productId cdf
  253. JSONArray requestArray = new JSONArray(); //productId all array
  254. List<String> sidsList = new ArrayList<>();
  255. HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
  256. HashMap<String,HotRecommend> hotRecommendHashMap = new HashMap<>();
  257. HashMap<Integer,ProductSource> productSourceMap = new HashMap<>();
  258. HashMap<String, BrandApiVo> brandMap = new HashMap<>();
  259. for (Object obj : tags) {
  260. JSONObject tag = (JSONObject) obj;
  261. String sid = tag.getString("sid");
  262. sidsList.add(sid);
  263. }
  264. List<ProductSource> productSources = new ArrayList<>();
  265. if(sidsList.size() >0){
  266. List<HotRelation> hotRelations = hotRelationService.listByIds(sidsList); //批量查询熱點
  267. for (HotRelation hotRelation : hotRelations) {
  268. hotRelationMap.put(hotRelation.getHotId(),hotRelation);
  269. }
  270. hotRecommendHashMap = hotRecommendService.getMapBySids(sidsList);
  271. Set<Integer> productSourceIds = hotRelations.stream().map(HotRelation::getProductSourceId).collect(Collectors.toSet());
  272. if(productSourceIds.size() >0){
  273. productSources = productSourceService.listByIds(productSourceIds);
  274. for (ProductSource productSource : productSources) {
  275. productSourceMap.put(productSource.getId(),productSource);
  276. }
  277. }
  278. Set<String> brandIds = new HashSet<>();
  279. for (HotRelation hotRelation : hotRelations) {
  280. if(StringUtils.isNotBlank(hotRelation.getBrandId())){
  281. brandIds.add(hotRelation.getBrandId());
  282. }
  283. }
  284. brandMap = brandService.getMapByIds(brandIds);
  285. }
  286. for (Object obj : tags) {
  287. JSONObject tag = (JSONObject) obj;
  288. String sid = tag.getString("sid");
  289. HotRecommend recommend = hotRecommendHashMap.get(sid);
  290. if(recommend != null){
  291. tag.put("recommendSelection",recommend.getRecommendSelection());
  292. tag.put("recommendBrand",recommend.getRecommendBrand());
  293. }
  294. HotRelation hotRelation = hotRelationMap.get(sid);
  295. if(hotRelation == null){
  296. continue;
  297. }
  298. if(hotRelation.getBrandId() != null){
  299. tag.put("brand",brandMap.get(hotRelation.getBrandId()));
  300. }
  301. tag.put("hotType",hotRelation.getHotType());
  302. //0商品,1优惠劵,2第三方跳转,3瀑布流 ,場景关联
  303. if(hotRelation.getHotType() == null){
  304. continue;
  305. }
  306. if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){
  307. tag.put("hotContent", hotRelation.getContent());
  308. continue;
  309. }
  310. String relationIds = hotRelation.getRelationIds();
  311. if(StringUtils.isBlank(relationIds)){
  312. continue;
  313. }
  314. JSONArray jsonArray = JSONObject.parseArray(relationIds);
  315. if(jsonArray == null || jsonArray.size() <=0){
  316. continue;
  317. }
  318. if(hotRelation.getProductSourceId() != null){
  319. tag.put("productSource",productSourceMap.get(hotRelation.getProductSourceId()));
  320. }
  321. resultMap.put(sid,jsonArray);
  322. requestArray.addAll(jsonArray);
  323. }
  324. if(requestArray.size() <=0){
  325. return tags;
  326. }
  327. for (ProductSource productSource : productSources) {
  328. if(productSource.getMchType() == 1){
  329. List<ProductHk> list = productHkService.getListByIds(requestArray);
  330. List<CdfProduct> convert = productHkService.convert(list, language);
  331. for (CdfProduct cdfProduct : convert) {
  332. productMap.put(productSource.getId() + "_"+ cdfProduct.getId(),cdfProduct);
  333. }
  334. }
  335. if(productSource.getMchType() == 0){
  336. CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray);
  337. CdfProductListByIdsVo vos = cdfHKClient.getProductListByIds(productSource.getCdfHost(),productSource.getCdfMchId(),param);
  338. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  339. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  340. productMap.put(productSource.getId() + "_"+ cdfProduct.getId(),cdfProduct);
  341. }
  342. }
  343. }
  344. }
  345. for (Object obj : tags) {
  346. JSONObject tag = (JSONObject) obj;
  347. String sid = tag.getString("sid");
  348. JSONObject productSource = tag.getJSONObject("productSource");
  349. String keyPre = "";
  350. if(productSource != null){
  351. Integer id = productSource.getInteger("id");
  352. keyPre = id +"_";
  353. }
  354. JSONArray jsonArray = resultMap.get(sid);
  355. if(jsonArray==null || jsonArray.size()<=0){
  356. continue;
  357. }
  358. List<CdfProduct> cdfProductList = new ArrayList<>();
  359. for (Object o : jsonArray) {
  360. String key = keyPre + o.toString();
  361. if(productMap.get( key)!=null){
  362. cdfProductList.add(productMap.get( key));
  363. }
  364. }
  365. if(cdfProductList.size() >0){
  366. tag.put("products",cdfProductList);
  367. }
  368. }
  369. return tags;
  370. }
  371. public void downQrCode(HttpServletRequest request, HttpServletResponse response, String num) {
  372. String longUrl;
  373. try {
  374. longUrl = qrCodeUrl +num;
  375. // 生成二维码
  376. BitMatrix qRcodeImg = QRCodeUtil.generateQRCodeStream(longUrl, response);
  377. // 将二维码输出到页面中
  378. MatrixToImageWriter.writeToStream(qRcodeImg, "png", response.getOutputStream());
  379. } catch (Exception e) {
  380. e.printStackTrace();
  381. }
  382. }
  383. public String getHotTitle(List<FdkkHotData> hotDataList) {
  384. try {
  385. StringBuilder title = new StringBuilder();
  386. for (FdkkHotData fdkkHotData : hotDataList) {
  387. String hotData = fdkkHotData.getHotData();
  388. String s1 = StringEscapeUtils.unescapeJava(hotData);
  389. JSONObject jsonObject = JSONObject.parseObject(s1);
  390. title.append(jsonObject.getString("title"));
  391. title.append(",");
  392. }
  393. if(title.length()>0 && title.toString().contains(",")){
  394. title.delete(title.lastIndexOf(",") ,title.length() );
  395. }
  396. return title.toString();
  397. }catch (Exception e){
  398. e.printStackTrace();
  399. }
  400. return "";
  401. }
  402. public String getSceneName(String num) {
  403. try {
  404. HashMap<String, String> map = new HashMap<>();
  405. map.put("num",num);
  406. JSONObject info = fdkkClient.getInfo(map);
  407. return info.getJSONObject("data").getString("title");
  408. }catch (Exception e){
  409. e.printStackTrace();
  410. }
  411. return num;
  412. }
  413. }