FdkkSceneEditService.java 16 KB

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