|
@@ -0,0 +1,171 @@
|
|
|
|
+package com.cdf.service.impl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
|
+import com.alibaba.excel.ExcelWriter;
|
|
|
|
+import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.cdf.common.CacheUtil;
|
|
|
|
+import com.cdf.common.ResultCode;
|
|
|
|
+import com.cdf.request.UploadHotsParam;
|
|
|
|
+import com.cdf.response.HotExcelVo;
|
|
|
|
+import com.cdf.exception.BusinessException;
|
|
|
|
+import com.cdf.response.HotUploadTemplate;
|
|
|
|
+import com.cdf.response.OutlineUploadTemplate;
|
|
|
|
+import com.cdf.response.ProductUploadTemplate;
|
|
|
|
+import com.cdf.util.ExcelUtil;
|
|
|
|
+import com.cdf.util.UploadToCdfOssUtil;
|
|
|
|
+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 javax.annotation.Resource;
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+@Service
|
|
|
|
+@Slf4j
|
|
|
|
+public class ExcelService {
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private UploadToCdfOssUtil uploadToCdfOssUtil;
|
|
|
|
+
|
|
|
|
+ @Value("${upload.file-path}")
|
|
|
|
+ private String filePath;
|
|
|
|
+ @Value("${upload.query-path}")
|
|
|
|
+ private String queryPath;
|
|
|
|
+
|
|
|
|
+ @Autowired
|
|
|
|
+ private FdkkSceneEditService fdkkSceneEditService;
|
|
|
|
+ // 0 外框设置 ,1 瀑布流, 2,精选推荐设置 ,3 品牌推荐设置
|
|
|
|
+ public void downTemplate(Integer type,String sceneNum,HttpServletRequest req,HttpServletResponse response) {
|
|
|
|
+ if(type == null || StringUtils.isBlank(sceneNum)){
|
|
|
|
+ throw new BusinessException(ResultCode.PARAM_MISS);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ List<HotUploadTemplate> list = getHotsList(sceneNum,req.getHeader("token"));
|
|
|
|
+
|
|
|
|
+ String fileName = "";
|
|
|
|
+ switch (type){
|
|
|
|
+ case 0 :
|
|
|
|
+ fileName ="导入外框模版";
|
|
|
|
+ this.commonExport(req,response,fileName,list, OutlineUploadTemplate.class);
|
|
|
|
+ break;
|
|
|
|
+ case 1 :
|
|
|
|
+ fileName ="导入商品瀑布流模版";
|
|
|
|
+ this.commonExport(req,response,fileName,list, ProductUploadTemplate.class);
|
|
|
|
+ break;
|
|
|
|
+ case 2 :
|
|
|
|
+ fileName ="导入精选推荐模版";
|
|
|
|
+ this.commonExport(req,response,fileName,list, HotUploadTemplate.class);
|
|
|
|
+ break;
|
|
|
|
+ case 3 :
|
|
|
|
+ fileName ="导入品牌推荐模版";
|
|
|
|
+ this.commonExport(req,response,fileName,list, HotUploadTemplate.class);
|
|
|
|
+ break;
|
|
|
|
+ default: throw new BusinessException(ResultCode.PARAM_MISS);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ log.info("导出热点列表出错",e);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List<?> result,Class<?> clz) throws Exception {
|
|
|
|
+ ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), clz).build();
|
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+ String fileName = name + ".xlsx";
|
|
|
|
+ fileName = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileName);
|
|
|
|
+ WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
|
|
|
|
+ excelWriter.write(result, writeSheet);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void checkFile(UploadHotsParam param) {
|
|
|
|
+ if(StringUtils.isBlank(param.getFilePath()) || param.getType() == null){
|
|
|
|
+ throw new BusinessException(ResultCode.PARAM_MISS);
|
|
|
|
+ }
|
|
|
|
+ String awsKey = param.getFilePath().replace(queryPath, "");
|
|
|
|
+ if(!uploadToCdfOssUtil.existKey(awsKey)){
|
|
|
|
+ throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
|
|
|
|
+ }
|
|
|
|
+ String localPath = String.format(CacheUtil.localFilePath, CacheUtil.activeYaml);
|
|
|
|
+ String localFile = localPath + awsKey;
|
|
|
|
+ File file = new File(localFile);
|
|
|
|
+ if (! file.getParentFile().exists()) {
|
|
|
|
+ file.getParentFile().mkdirs();
|
|
|
|
+ }
|
|
|
|
+ uploadToCdfOssUtil.download(awsKey,localFile);
|
|
|
|
+
|
|
|
|
+ List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
|
|
|
|
+ List<HotUploadTemplate> list = null;
|
|
|
|
+ List<Integer> errorList = new ArrayList<>();
|
|
|
|
+ Integer colum = 0;
|
|
|
|
+ List<String> pidList = new ArrayList<>();
|
|
|
|
+ for (HashMap<Integer, String> map : excelRowList) {
|
|
|
|
+ String pid = map.get(3);
|
|
|
|
+ if(StringUtils.isNotBlank(pid)){
|
|
|
|
+ pidList.add(pid);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (HashMap<Integer, String> map : excelRowList) {
|
|
|
|
+ colum ++;
|
|
|
|
+ String sceneNum = map.get(0);
|
|
|
|
+ String sid = map.get(1);
|
|
|
|
+ String title = map.get(2);
|
|
|
|
+ String pid = map.get(3);
|
|
|
|
+ if(StringUtils.isBlank(sceneNum) || StringUtils.isBlank(sid) || StringUtils.isBlank(pid)){
|
|
|
|
+ errorList.add(colum);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if(list == null){
|
|
|
|
+ list = getHotsList(sceneNum,param.getToken());
|
|
|
|
+ }
|
|
|
|
+ if(list .isEmpty()){
|
|
|
|
+ errorList.add(colum);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ List<String> collect = list.stream().map(HotUploadTemplate::getSid).collect(Collectors.toList());
|
|
|
|
+ if(!collect.contains(sid)){
|
|
|
|
+ errorList.add(colum);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private List<HotUploadTemplate> getHotsList(String sceneNum,String token){
|
|
|
|
+ JSONObject tagList = fdkkSceneEditService.getTagList(sceneNum, token, "eshop_cn");
|
|
|
|
+ JSONArray tags =tagList.getJSONArray("tags");
|
|
|
|
+ List<HotUploadTemplate> list = new ArrayList<>();
|
|
|
|
+ for (Object obj : tags) {
|
|
|
|
+ JSONObject tag = (JSONObject) obj;
|
|
|
|
+ String sid = tag.getString("sid");
|
|
|
|
+ String title = tag.getString("title");
|
|
|
|
+ HotUploadTemplate hotExcelVo = new HotUploadTemplate(sceneNum,sid,title);
|
|
|
|
+ list.add(hotExcelVo);
|
|
|
|
+ }
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void uploadExcel(UploadHotsParam param) {
|
|
|
|
+ }
|
|
|
|
+}
|