FdkkSceneEditService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. }else {
  104. if(hotRelation.getHotType() == 0 && !hotRelation.getProductSourceId().equals(fdkkHotData.getProductSourceId())){
  105. throw new BusinessException(ResultCode.HOT_PRODUCT_SOURCE_ERROR);
  106. }
  107. }
  108. hotRelation.setHotId(sid);
  109. hotRelation.setHotType(type);
  110. if(relationIds !=null && relationIds.size() >0){
  111. hotRelation.setRelationIds(JSONArray.toJSONString(relationIds));
  112. }else {
  113. hotRelation.setRelationIds(null);
  114. }
  115. hotRelation.setNum(fdkkHotRequest.getNum());
  116. hotRelation.setContent(fdkkHotData.getHotContent());
  117. hotRelation.setProductSourceId(fdkkHotData.getProductSourceId());
  118. hotRelation.setBrandId(fdkkHotData.getBrandId());
  119. if(update){
  120. hotRelationService.updateById(hotRelation);
  121. }else {
  122. hotRelationService.save(hotRelation);
  123. }
  124. }
  125. return fdkkResponse;
  126. }
  127. public void deleteTag(FdkkHotRequest fdkkHotRequest, String token) {
  128. FdkkResponse fdkkResponse = fdkkClient.hotDelete(fdkkHotRequest,fdkkSceneService.getFdkkToken(token));
  129. if(fdkkResponse.getCode() !=0){
  130. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  131. }
  132. hotRelationService.removeByIds(fdkkHotRequest.getSidList());
  133. }
  134. public JSONObject getTagList(String num, String token,String language) {
  135. FdkkResponse fdkkResponse = fdkkClient.hotList(new SceneRequest(num), fdkkSceneService.getFdkkToken(token));
  136. if(fdkkResponse.getCode() !=0){
  137. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  138. }
  139. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  140. JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(fdkkResponse.getData()));
  141. JSONArray tags =jsonObject.getJSONArray("tags");
  142. jsonObject.put("tags",getProductByJsonObj(tags,numRegion.getRegion(),language));
  143. return jsonObject;
  144. }
  145. public JSONArray getHotJson(String num,String pType,String language) {
  146. String data = uploadToFdkkOssUtil.getObjectContent(String.format(hotPath, num) );
  147. if(StringUtils.isBlank(data)){
  148. throw new BusinessException(ResultCode.NOT_RECORD);
  149. }
  150. JSONArray tags = JSONObject.parseArray(data);
  151. return getProductByJsonObj(tags,pType,language);
  152. }
  153. public Boolean cdfHasHotJson(String num) {
  154. return uploadToCdfOssUtil.existKey(String.format(hotCdfPath, num));
  155. }
  156. public JSONArray getCdfHotJson(String num) {
  157. String data = uploadToCdfOssUtil.getObjectContent(String.format(hotCdfPath, num));
  158. if(StringUtils.isBlank(data)){
  159. data = uploadToFdkkOssUtil.getObjectContent( String.format(hotPath, num));
  160. }
  161. if(StringUtils.isBlank(data)){
  162. throw new BusinessException(ResultCode.NOT_RECORD);
  163. }
  164. return JSONObject.parseArray(data);
  165. }
  166. public void publicScene(FdkkHotRequest fdkkHotRequest, String token) throws Exception {
  167. FdkkResponse fdkkResponse = fdkkClient.scenePublicScene(new SceneRequest(fdkkHotRequest.getNum()),fdkkSceneService.getFdkkToken(token));
  168. if(fdkkResponse.getCode() !=0){
  169. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  170. }
  171. NumRegion numRegion = fdkkUserService.getRegionByNum(fdkkHotRequest.getNum());
  172. writeHotJson(fdkkHotRequest.getNum(),numRegion.getRegion());
  173. String res1 = "scene_view_data/%s/images/vision.modeldata";
  174. String res2 = "scene_view_data/%s/images/vision2.modeldata";
  175. updateModelData(fdkkHotRequest.getNum(),res1);
  176. updateModelData(fdkkHotRequest.getNum(),res2);
  177. String sell1 = "aws s3 sync s3://4dkankan/scene_view_data/"+fdkkHotRequest.getNum()+"/user " +
  178. " s3://4dage-moderate2/scene_view_data/"+fdkkHotRequest.getNum()+"/user --profile user1";
  179. log.info("执行脚本:{}",sell1);
  180. ReadCmdLine.callShellByExec(sell1);
  181. }
  182. private void updateModelData(String num,String res) {
  183. String localPath = "/home/cdf/"+String.format(res, num);
  184. String s3PathKey = String.format(res, num);
  185. uploadToFdkkOssUtil.downFromS3(s3PathKey,localPath);
  186. uploadToCdfOssUtil.upload(localPath,s3PathKey);
  187. }
  188. public void writeHotJson(String num,String pType) {
  189. try {
  190. if(pType.equals("HK")){
  191. writeHotJsonHk(num,"eshop_en");
  192. writeHotJsonHk(num,"eshop_zh");
  193. writeHotJsonHk(num,"eshop_cn");
  194. return;
  195. }
  196. JSONArray hotJson = getHotJson(num,pType,"all");
  197. String path = String.format(hotLocalPath,num)+"/"+"hot.json";
  198. String json = JSONUtil.toJsonStr(hotJson);
  199. FileUtils.writeFile(path,json );
  200. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  201. }catch (Exception e){
  202. log.error("writeHotJson",e);
  203. }
  204. }
  205. public void writeHotJsonHk(String num,String lang){
  206. try {
  207. JSONArray hotJson = getHotJson(num,"HK",lang);
  208. String name = "hot_" +lang +".json";
  209. String path = String.format(hotLocalPath,num)+"/"+name;
  210. String json = JSONUtil.toJsonStr(hotJson);
  211. FileUtils.writeFile(path,json );
  212. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num).replace("hot.json",name));
  213. if("eshop_zh".equals(lang)){
  214. uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
  215. }
  216. }catch (Exception e){
  217. e.printStackTrace();
  218. }
  219. }
  220. public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile[] files, String token) throws IOException {
  221. List<MultipartFile> multipartFiles = new ArrayList<>();
  222. if(StringUtils.isNotBlank(fdkkUploadRequest.getBase64())){
  223. MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fdkkUploadRequest.getBase64());
  224. multipartFiles.add(file);
  225. }
  226. if(files.length >0){
  227. multipartFiles.addAll(Arrays.asList(files));
  228. }
  229. List<String> paths = new ArrayList<>();
  230. for (MultipartFile file : multipartFiles) {
  231. if(file !=null && file.getSize() >0){
  232. String fileName = file.getOriginalFilename();
  233. assert fileName != null;
  234. String newFilePath = String.format(hotLocalPath,fdkkUploadRequest.getNum()) + "/"+fileName;
  235. File newFiles = new File(newFilePath );
  236. if (!newFiles.getParentFile().exists()) {
  237. newFiles.getParentFile().mkdirs();
  238. }
  239. if (!newFiles.exists()) {
  240. newFiles.createNewFile();
  241. }
  242. file.transferTo(newFiles);
  243. String path = newFiles.getPath();
  244. paths.add(path);
  245. }
  246. }
  247. fdkkUploadRequest.setBase64(null);
  248. FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,paths,fdkkSceneService.getFdkkToken(token));
  249. if(fdkkResponse.getCode() !=0){
  250. throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg());
  251. }
  252. return fdkkResponse;
  253. }
  254. private JSONArray getProductByJsonObj(JSONArray tags,String pType,String language){
  255. HashMap<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
  256. HashMap<String,CdfProduct> productMap = new HashMap<>(); //productId cdf
  257. JSONArray requestArray = new JSONArray(); //productId all array
  258. List<String> sidsList = new ArrayList<>();
  259. HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
  260. HashMap<String,HotRecommend> hotRecommendHashMap = new HashMap<>();
  261. HashMap<Integer,ProductSource> productSourceMap = new HashMap<>();
  262. HashMap<String, BrandApiVo> brandMap = new HashMap<>();
  263. for (Object obj : tags) {
  264. JSONObject tag = (JSONObject) obj;
  265. String sid = tag.getString("sid");
  266. sidsList.add(sid);
  267. }
  268. List<ProductSource> productSources = new ArrayList<>();
  269. if(sidsList.size() >0){
  270. List<HotRelation> hotRelations = hotRelationService.listByIds(sidsList); //批量查询熱點
  271. for (HotRelation hotRelation : hotRelations) {
  272. hotRelationMap.put(hotRelation.getHotId(),hotRelation);
  273. }
  274. hotRecommendHashMap = hotRecommendService.getMapBySids(sidsList);
  275. Set<Integer> productSourceIds = hotRelations.stream().map(HotRelation::getProductSourceId).collect(Collectors.toSet());
  276. if(productSourceIds.size() >0){
  277. productSources = productSourceService.listByIds(productSourceIds);
  278. for (ProductSource productSource : productSources) {
  279. productSourceMap.put(productSource.getId(),productSource);
  280. }
  281. }
  282. Set<String> brandIds = new HashSet<>();
  283. for (HotRelation hotRelation : hotRelations) {
  284. if(StringUtils.isNotBlank(hotRelation.getBrandId())){
  285. brandIds.add(hotRelation.getBrandId());
  286. }
  287. }
  288. brandMap = brandService.getMapByIds(brandIds);
  289. }
  290. for (Object obj : tags) {
  291. JSONObject tag = (JSONObject) obj;
  292. String sid = tag.getString("sid");
  293. HotRecommend recommend = hotRecommendHashMap.get(sid);
  294. if(recommend != null){
  295. tag.put("recommendSelection",recommend.getRecommendSelection());
  296. tag.put("recommendBrand",recommend.getRecommendBrand());
  297. }
  298. HotRelation hotRelation = hotRelationMap.get(sid);
  299. if(hotRelation == null){
  300. continue;
  301. }
  302. if(hotRelation.getBrandId() != null){
  303. tag.put("brand",brandMap.get(hotRelation.getBrandId()));
  304. }
  305. tag.put("hotType",hotRelation.getHotType());
  306. //0商品,1优惠劵,2第三方跳转,3瀑布流 ,場景关联
  307. if(hotRelation.getHotType() == null){
  308. continue;
  309. }
  310. if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){
  311. tag.put("hotContent", hotRelation.getContent());
  312. continue;
  313. }
  314. String relationIds = hotRelation.getRelationIds();
  315. if(StringUtils.isBlank(relationIds)){
  316. continue;
  317. }
  318. JSONArray jsonArray = JSONObject.parseArray(relationIds);
  319. if(jsonArray == null || jsonArray.size() <=0){
  320. continue;
  321. }
  322. if(hotRelation.getProductSourceId() != null){
  323. tag.put("productSource",productSourceMap.get(hotRelation.getProductSourceId()));
  324. }
  325. resultMap.put(sid,jsonArray);
  326. requestArray.addAll(jsonArray);
  327. }
  328. if(requestArray.size() <=0){
  329. return tags;
  330. }
  331. for (ProductSource productSource : productSources) {
  332. if(productSource.getMchType() == 1){
  333. List<ProductHk> list = productHkService.getListByIds(requestArray);
  334. List<CdfProduct> convert = productHkService.convert(list, language);
  335. for (CdfProduct cdfProduct : convert) {
  336. productMap.put(productSource.getId() + "_"+ cdfProduct.getId(),cdfProduct);
  337. }
  338. }
  339. if(productSource.getMchType() == 0){
  340. CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray);
  341. CdfProductListByIdsVo vos = cdfHKClient.getProductListByIds(productSource.getCdfHost(),productSource.getCdfMchId(),param);
  342. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  343. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  344. productMap.put(productSource.getId() + "_"+ cdfProduct.getId(),cdfProduct);
  345. }
  346. }
  347. }
  348. }
  349. for (Object obj : tags) {
  350. JSONObject tag = (JSONObject) obj;
  351. String sid = tag.getString("sid");
  352. JSONObject productSource = tag.getJSONObject("productSource");
  353. String keyPre = "";
  354. if(productSource != null){
  355. Integer id = productSource.getInteger("id");
  356. keyPre = id +"_";
  357. }
  358. JSONArray jsonArray = resultMap.get(sid);
  359. if(jsonArray==null || jsonArray.size()<=0){
  360. continue;
  361. }
  362. List<CdfProduct> cdfProductList = new ArrayList<>();
  363. for (Object o : jsonArray) {
  364. String key = keyPre + o.toString();
  365. if(productMap.get( key)!=null){
  366. cdfProductList.add(productMap.get( key));
  367. }
  368. }
  369. if(cdfProductList.size() >0){
  370. tag.put("products",cdfProductList);
  371. }
  372. }
  373. return tags;
  374. }
  375. public void downQrCode(HttpServletRequest request, HttpServletResponse response, String num) {
  376. String longUrl;
  377. try {
  378. longUrl = qrCodeUrl +num;
  379. // 生成二维码
  380. BitMatrix qRcodeImg = QRCodeUtil.generateQRCodeStream(longUrl, response);
  381. // 将二维码输出到页面中
  382. MatrixToImageWriter.writeToStream(qRcodeImg, "png", response.getOutputStream());
  383. } catch (Exception e) {
  384. e.printStackTrace();
  385. }
  386. }
  387. public String getHotTitle(List<FdkkHotData> hotDataList) {
  388. try {
  389. StringBuilder title = new StringBuilder();
  390. for (FdkkHotData fdkkHotData : hotDataList) {
  391. String hotData = fdkkHotData.getHotData();
  392. String s1 = StringEscapeUtils.unescapeJava(hotData);
  393. JSONObject jsonObject = JSONObject.parseObject(s1);
  394. title.append(jsonObject.getString("title"));
  395. title.append(",");
  396. }
  397. if(title.length()>0 && title.toString().contains(",")){
  398. title.delete(title.lastIndexOf(",") ,title.length() );
  399. }
  400. return title.toString();
  401. }catch (Exception e){
  402. e.printStackTrace();
  403. }
  404. return "";
  405. }
  406. public String getSceneName(String num) {
  407. try {
  408. HashMap<String, String> map = new HashMap<>();
  409. map.put("num",num);
  410. JSONObject info = fdkkClient.getInfo(map);
  411. return info.getJSONObject("data").getString("title");
  412. }catch (Exception e){
  413. e.printStackTrace();
  414. }
  415. return num;
  416. }
  417. }