123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- 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<FdkkHotData> hotDataList = fdkkHotRequest.getHotDataList();
- for (FdkkHotData fdkkHotData : hotDataList) {
- List<String> 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<String,JSONArray> resultMap = new HashMap<>(); //sid, productId array
- HashMap<Object,CdfProduct> productMap = new HashMap<>(); //productId cdf
- JSONArray requestArray = new JSONArray(); //productId all array
- List<String> sidsList = new ArrayList<>();
- HashMap<String,HotRelation> hotRelationMap = new HashMap<>();
- for (Object obj : tags) {
- JSONObject tag = (JSONObject) obj;
- String sid = tag.getString("sid");
- sidsList.add(sid);
- }
- List<HotRelation> 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<CdfProduct> cdfProductList = new ArrayList<>();
- for (Object o : jsonArray) {
- cdfProductList.add(productMap.get(o));
- }
- if(cdfProductList.size() >0){
- tag.put("products",cdfProductList);
- }
- }
- return tags;
- }
- }
|