package com.cdf.service.impl; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.cdf.common.ResultCode; import com.cdf.entity.FdkkUser; import com.cdf.entity.HotRelation; import com.cdf.exception.BusinessException; import com.cdf.httpClient.client.CdfClient; import com.cdf.httpClient.client.FdkkClient; import com.cdf.httpClient.request.FdkkHotData; import com.cdf.httpClient.request.FdkkHotRequest; import com.cdf.httpClient.request.FdkkUploadRequest; import com.cdf.httpClient.request.SceneRequest; import com.cdf.httpClient.response.FdkkResponse; import com.cdf.httpClient.response.cdf.*; import com.cdf.service.IFdkkUserService; import com.cdf.service.IHotRelationService; import com.cdf.util.FileUtils; import com.cdf.util.JwtUtil; import com.cdf.util.UploadToOssUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.util.*; import java.util.stream.Collectors; @Service @Slf4j public class FdkkSceneEditService { @Resource private UploadToOssUtil uploadToOssUtil; @Value("${upload.bucket}") private String bucket; @Value("${fdkk.hot-path}") private String hotPath; @Value("${fdkk.hot-cdf-path}") private String hotCdfPath; @Value("${fdkk.hot-local-path}") private String hotLocalPath; @Resource private FdkkClient fdkkClient; @Resource private CdfClient cdfClient; @Autowired private IHotRelationService hotRelationService; @Autowired FdkkSceneService fdkkSceneService; public FdkkResponse saveTag(FdkkHotRequest fdkkHotRequest, String token) { FdkkResponse fdkkResponse = fdkkClient.hotSave(fdkkHotRequest,fdkkSceneService.getFdkkToken(token)); if(fdkkResponse.getCode() != 0){ throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg()); } List hotDataList = fdkkHotRequest.getHotDataList(); for (FdkkHotData fdkkHotData : hotDataList) { List relationIds = fdkkHotData.getRelationIds(); String sid = fdkkHotData.getSid(); Integer type = fdkkHotData.getHotType(); if(type == null){ continue; } HotRelation hotRelation = new HotRelation(); hotRelation.setHotId(sid); hotRelation.setHotType(type); if(relationIds !=null && relationIds.size() >0){ hotRelation.setRelationIds(JSONArray.toJSONString(relationIds)); } hotRelation.setNum(fdkkHotRequest.getNum()); hotRelation.setContent(fdkkHotData.getHotContent()); hotRelationService.saveOrUpdate(hotRelation); } return fdkkResponse; } public void deleteTag(FdkkHotRequest fdkkHotRequest, String token) { FdkkResponse fdkkResponse = fdkkClient.hotDelete(fdkkHotRequest,fdkkSceneService.getFdkkToken(token)); if(fdkkResponse.getCode() !=0){ throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg()); } hotRelationService.removeByIds(fdkkHotRequest.getSidList()); } public JSONObject getTagList(String num, String token) { FdkkResponse fdkkResponse = fdkkClient.hotList(new SceneRequest(num), fdkkSceneService.getFdkkToken(token)); if(fdkkResponse.getCode() !=0){ throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg()); } JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(fdkkResponse.getData())); JSONArray tags =jsonObject.getJSONArray("tags"); jsonObject.put("tags",getProductByJsonObj(tags)); return jsonObject; } public JSONArray getHotJson(String num) { String data = uploadToOssUtil.getObjectContent(bucket, String.format(hotPath, num)); if(StringUtils.isBlank(data)){ throw new BusinessException(ResultCode.NOT_RECORD); } JSONArray tags = JSONObject.parseArray(data); return getProductByJsonObj(tags); } public JSONArray getCdfHotJson(String num) { String data = uploadToOssUtil.getObjectContent(bucket, String.format(hotCdfPath, num)); if(StringUtils.isBlank(data)){ data = uploadToOssUtil.getObjectContent(bucket, String.format(hotPath, num)); } if(StringUtils.isBlank(data)){ throw new BusinessException(ResultCode.NOT_RECORD); } return JSONObject.parseArray(data); } public void publicScene(FdkkHotRequest fdkkHotRequest, String token) throws Exception { FdkkResponse fdkkResponse = fdkkClient.scenePublicScene(new SceneRequest(fdkkHotRequest.getNum()),fdkkSceneService.getFdkkToken(token)); if(fdkkResponse.getCode() !=0){ throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg()); } JSONArray hotJson = getHotJson(fdkkHotRequest.getNum()); String path = String.format(hotLocalPath, fdkkHotRequest.getNum()); FileUtils.writeFile(path, hotJson.toJSONString()); uploadToOssUtil.upload(path,String.format(hotCdfPath, fdkkHotRequest.getNum())); } public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile files, String token) throws IOException { String path = null; if(files !=null && files.getSize() >0){ String fileName = files.getOriginalFilename(); assert fileName != null; String prefix = fileName.substring(fileName.lastIndexOf(".")); File newFile = File.createTempFile(UUID.randomUUID().toString() ,prefix); files.transferTo(newFile); path = newFile.getPath(); } FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,path,fdkkSceneService.getFdkkToken(token)); if(fdkkResponse.getCode() !=0){ throw new BusinessException(fdkkResponse.getCode(),fdkkResponse.getMsg()); } return fdkkResponse; } private JSONArray getProductByJsonObj(JSONArray tags){ HashMap resultMap = new HashMap<>(); //sid, productId array HashMap productMap = new HashMap<>(); //productId cdf JSONArray requestArray = new JSONArray(); //productId all array List sidsList = new ArrayList<>(); HashMap hotRelationMap = new HashMap<>(); for (Object obj : tags) { JSONObject tag = (JSONObject) obj; String sid = tag.getString("sid"); sidsList.add(sid); } List hotRelations = hotRelationService.listByIds(sidsList); //批量查询热点 for (HotRelation hotRelation : hotRelations) { hotRelationMap.put(hotRelation.getHotId(),hotRelation); } for (Object obj : tags) { JSONObject tag = (JSONObject) obj; String sid = tag.getString("sid"); HotRelation hotRelation = hotRelationMap.get(sid); if(hotRelation == null){ continue; } tag.put("hotType",hotRelation.getHotType()); //0商品,1优惠劵,2第三方跳转,3瀑布流 ,场景关联 if(hotRelation.getHotType() == null){ continue; } if(hotRelation.getHotType() == 1 || hotRelation.getHotType() == 2){ tag.put("hotContent", hotRelation.getContent()); continue; } String relationIds = hotRelation.getRelationIds(); if(StringUtils.isBlank(relationIds)){ continue; } JSONArray jsonArray = JSONObject.parseArray(relationIds); if(jsonArray == null || jsonArray.size() <=0){ continue; } resultMap.put(sid,jsonArray); requestArray.addAll(jsonArray); } if(requestArray.size() <=0){ return tags; } CdfProductListByIdsRequest param = new CdfProductListByIdsRequest(requestArray); CdfProductListByIdsVo vos = cdfClient.getProductListByIds(param); if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){ for (CdfProduct cdfProduct : vos.getProductCardList()) { productMap.put(cdfProduct.getId(),cdfProduct); } } for (Object obj : tags) { JSONObject tag = (JSONObject) obj; String sid = tag.getString("sid"); JSONArray jsonArray = resultMap.get(sid); if(jsonArray==null || jsonArray.size()<=0){ continue; } List cdfProductList = new ArrayList<>(); for (Object o : jsonArray) { cdfProductList.add(productMap.get(o)); } if(cdfProductList.size() >0){ tag.put("products",cdfProductList); } } return tags; } }