FdkkSceneEditService.java 18 KB

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