123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- package com.cdf.service.impl;
- import cn.hutool.json.JSONUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.cdf.common.ResultCode;
- 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.IHotRelationService;
- import com.cdf.util.*;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- import com.google.zxing.common.BitMatrix;
- 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 javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.io.IOException;
- import java.util.*;
- @Service
- @Slf4j
- public class FdkkSceneEditService {
- @Resource
- private UploadToFdkkOssUtil uploadToFdkkOssUtil;
- @Resource
- private UploadToCdfOssUtil uploadToCdfOssUtil;
- @Value("${fdkk.hot-path}")
- private String hotPath;
- @Value("${fdkk.hot-cdf-path}")
- private String hotCdfPath;
- @Value("${fdkk.hot-local-path}")
- private String hotLocalPath;
- @Value("${fdkk.qr-code-url}")
- private String qrCodeUrl;
- @Resource
- private FdkkClient fdkkClient;
- @Resource
- private CdfClient cdfClient;
- @Autowired
- private IHotRelationService hotRelationService;
- @Autowired
- FdkkSceneService fdkkSceneService;
- @Autowired
- CdfRolePermService cdfRolePermService;
- public JSONObject getAuth(String num, String token) {
- cdfRolePermService.checkRole(token);
- String fdkkToken = fdkkSceneService.getFdkkToken(token);
- HashMap<String,String> map = new HashMap<>();
- map.put("num",num);
- return fdkkClient.getAuth(map, fdkkToken);
- }
- 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 = hotRelationService.getById(sid);
- boolean update = true;
- if(hotRelation == null){
- hotRelation = new HotRelation();
- update = false;
- }
- hotRelation.setHotId(sid);
- hotRelation.setHotType(type);
- if(relationIds !=null && relationIds.size() >0){
- hotRelation.setRelationIds(JSONArray.toJSONString(relationIds));
- }else {
- hotRelation.setRelationIds(null);
- }
- hotRelation.setNum(fdkkHotRequest.getNum());
- hotRelation.setContent(fdkkHotData.getHotContent());
- if(update){
- hotRelationService.updateById(hotRelation);
- }else {
- hotRelationService.save(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 = uploadToFdkkOssUtil.getObjectContent(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 = uploadToCdfOssUtil.getObjectContent(String.format(hotCdfPath, num));
- if(StringUtils.isBlank(data)){
- data = uploadToFdkkOssUtil.getObjectContent( 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());
- }
- writeHotJson(fdkkHotRequest.getNum());
- }
- public void writeHotJson(String num) throws IOException {
- JSONArray hotJson = getHotJson(num);
- String path = String.format(hotLocalPath,num)+"/"+"hot.json";
- String json = JSONUtil.toJsonStr(hotJson);
- FileUtils.writeFile(path,json );
- uploadToCdfOssUtil.upload(path,String.format(hotCdfPath, num));
- }
- public FdkkResponse uploadFiles(FdkkUploadRequest fdkkUploadRequest, MultipartFile[] files, String token) throws IOException {
- List<MultipartFile> multipartFiles = new ArrayList<>();
- if(StringUtils.isNotBlank(fdkkUploadRequest.getBase64())){
- MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(fdkkUploadRequest.getBase64());
- multipartFiles.add(file);
- }
- if(files.length >0){
- multipartFiles.addAll(Arrays.asList(files));
- }
- List<String> paths = new ArrayList<>();
- for (MultipartFile file : multipartFiles) {
- if(file !=null && file.getSize() >0){
- String fileName = file.getOriginalFilename();
- assert fileName != null;
- String newFilePath = String.format(hotLocalPath,fdkkUploadRequest.getNum()) + "/"+fileName;
- File newFiles = new File(newFilePath );
- if (!newFiles.getParentFile().exists()) {
- newFiles.getParentFile().mkdirs();
- }
- if (!newFiles.exists()) {
- newFiles.createNewFile();
- }
- file.transferTo(newFiles);
- String path = newFiles.getPath();
- paths.add(path);
- }
- }
- fdkkUploadRequest.setBase64(null);
- FdkkResponse fdkkResponse = fdkkClient.uploadFiles(fdkkUploadRequest,paths,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<String,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) {
- String key = o.toString();
- if(productMap.get(key)!=null){
- cdfProductList.add(productMap.get(key));
- }
- }
- if(cdfProductList.size() >0){
- tag.put("products",cdfProductList);
- }
- }
- return tags;
- }
- public void downQrCode(HttpServletRequest request, HttpServletResponse response, String num) {
- String longUrl;
- try {
- longUrl = qrCodeUrl +num;
- // 生成二维码
- BitMatrix qRcodeImg = QRCodeUtil.generateQRCodeStream(longUrl, response);
- // 将二维码输出到页面中
- MatrixToImageWriter.writeToStream(qRcodeImg, "png", response.getOutputStream());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
|