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