ExcelService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. package com.cdf.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.alibaba.excel.EasyExcel;
  4. import com.alibaba.excel.ExcelWriter;
  5. import com.alibaba.excel.write.metadata.WriteSheet;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.amazonaws.services.ecs.model.Tmpfs;
  9. import com.cdf.common.CacheUtil;
  10. import com.cdf.common.ResultCode;
  11. import com.cdf.entity.*;
  12. import com.cdf.httpClient.client.CdfClient;
  13. import com.cdf.httpClient.client.CdfHKClient;
  14. import com.cdf.httpClient.response.cdf.CdfProduct;
  15. import com.cdf.httpClient.response.cdf.CdfProductListByIdsRequest;
  16. import com.cdf.httpClient.response.cdf.CdfProductListByIdsVo;
  17. import com.cdf.request.UploadHotsParam;
  18. import com.cdf.response.*;
  19. import com.cdf.exception.BusinessException;
  20. import com.cdf.service.*;
  21. import com.cdf.util.ExcelUtil;
  22. import com.cdf.util.UploadToCdfOssUtil;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.beans.factory.annotation.Value;
  27. import org.springframework.stereotype.Service;
  28. import javax.annotation.Resource;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.File;
  32. import java.io.InputStream;
  33. import java.net.URLEncoder;
  34. import java.util.*;
  35. import java.util.stream.Collectors;
  36. @Service
  37. @Slf4j
  38. public class ExcelService {
  39. @Resource
  40. private UploadToCdfOssUtil uploadToCdfOssUtil;
  41. @Value("${upload.file-path}")
  42. private String filePath;
  43. @Value("${upload.query-path}")
  44. private String queryPath;
  45. @Autowired
  46. private FdkkSceneEditService fdkkSceneEditService;
  47. @Autowired
  48. CdfHKClient cdfHKClient;
  49. @Autowired
  50. IProductSourceService productSourceService;
  51. @Autowired
  52. private IHotOutlineService hotOutlineService;
  53. @Autowired
  54. private IBrandService brandService;
  55. @Autowired
  56. private IHotRelationService hotRelationService;
  57. @Autowired
  58. private IHotRecommendService hotRecommendService;
  59. @Autowired
  60. IFdkkUserService fdkkUserService;
  61. @Autowired
  62. INumRegionService numRegionService;
  63. // 0 外框设置 ,1 瀑布流, 2,精选推荐设置 ,3 品牌推荐设置
  64. public void downTemplate(Integer type,String sceneNum,HttpServletRequest req,HttpServletResponse response) {
  65. if(type == null ){
  66. throw new BusinessException(ResultCode.PARAM_MISS);
  67. }
  68. try {
  69. List<HotUploadTemplate> list = new ArrayList<>();
  70. if(type !=0 && StringUtils.isNotBlank(sceneNum)){
  71. list = getHotsList(sceneNum);
  72. }
  73. String fileName = "";
  74. switch (type){
  75. case 0 :
  76. fileName ="导入外框模版";
  77. List<HotOutline> list1 = hotOutlineService.list();
  78. List<OutlineUploadTemplate> OutlineUploadTemplateList = new ArrayList<>();
  79. for (HotOutline hotOutline : list1) {
  80. OutlineUploadTemplate template = new OutlineUploadTemplate(hotOutline.getId().toString());
  81. OutlineUploadTemplateList.add(template);
  82. }
  83. this.commonExport(req,response,fileName,OutlineUploadTemplateList, OutlineUploadTemplate.class);
  84. break;
  85. case 1 :
  86. HashMap<String,HotUploadTemplate> sidMap = new HashMap<>();
  87. for (HotUploadTemplate hotUploadTemplate : list) {
  88. sidMap.put(hotUploadTemplate.getSid(),hotUploadTemplate);
  89. }
  90. List<HotRelation> hotRelations = hotRelationService.getByNum(sceneNum);
  91. List<String> sids = hotRelations.stream().filter(e -> e.getHotType() == 3).map(HotRelation::getHotId).collect(Collectors.toList());
  92. List<ProductUploadTemplate> resultList = new ArrayList<>();
  93. for (String sid : sids) {
  94. HotUploadTemplate hotUploadTemplate = sidMap.get(sid);
  95. if(hotUploadTemplate == null){
  96. continue;
  97. }
  98. ProductUploadTemplate template = new ProductUploadTemplate();
  99. template.setSceneNum(sceneNum);
  100. template.setSid(sid);
  101. template.setHotTitle(hotUploadTemplate.getHotTitle());
  102. resultList.add(template);
  103. }
  104. fileName ="导入商品瀑布流模版";
  105. this.commonExport(req,response,fileName,resultList, ProductUploadTemplate.class);
  106. break;
  107. case 2 :
  108. fileName ="导入精选推荐模版";
  109. this.commonExport(req,response,fileName,list, HotUploadTemplate.class);
  110. break;
  111. case 3 :
  112. fileName ="导入品牌推荐模版";
  113. this.commonExport(req,response,fileName,list, HotUploadTemplate.class);
  114. break;
  115. default: throw new BusinessException(ResultCode.PARAM_MISS);
  116. }
  117. }catch (Exception e){
  118. log.info("导出热点列表出错",e);
  119. }
  120. }
  121. public void commonExport(HttpServletRequest request, HttpServletResponse response,String name,List<?> result,Class<?> clz) throws Exception {
  122. ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), clz).build();
  123. response.setContentType("application/vnd.ms-excel");
  124. response.setCharacterEncoding("utf-8");
  125. String fileName = name + ".xlsx";
  126. fileName = URLEncoder.encode(fileName, "UTF-8");
  127. response.setHeader("Content-disposition", "attachment;filename=" + fileName);
  128. WriteSheet writeSheet = EasyExcel.writerSheet(name).build();
  129. excelWriter.write(result, writeSheet);
  130. excelWriter.finish();
  131. }
  132. public List<String> checkFile(UploadHotsParam param) {
  133. if(StringUtils.isBlank(param.getFilePath()) || param.getType() == null){
  134. throw new BusinessException(ResultCode.PARAM_MISS);
  135. }
  136. String awsKey = param.getFilePath().replace(queryPath, "");
  137. if(!uploadToCdfOssUtil.existKey(awsKey)){
  138. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  139. }
  140. String localPath = String.format(CacheUtil.localFilePath, CacheUtil.activeYaml);
  141. String localFile = localPath + awsKey;
  142. File file = new File(localFile);
  143. if (! file.getParentFile().exists()) {
  144. file.getParentFile().mkdirs();
  145. }
  146. log.info("checkFile:{}",localFile);
  147. uploadToCdfOssUtil.downFromS3(awsKey,localFile);
  148. if(!file.exists()){
  149. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  150. }
  151. List<String> errorList = new ArrayList<>();
  152. String resultError = null;
  153. switch (param.getType()){
  154. case 0:
  155. checkUploadOutline(file,errorList);
  156. break;
  157. case 1:
  158. checkUploadProduct(file,errorList);
  159. break;
  160. case 2 :case 3 :
  161. checkHot(file,errorList);
  162. break;
  163. default: throw new BusinessException(ResultCode.PARAM_MISS);
  164. }
  165. return errorList;
  166. }
  167. private void checkUploadOutline(File file, List<String> errorList) {
  168. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  169. Integer colum = 1;
  170. List<String> outlineIds = new ArrayList<>();
  171. List<String> outlineIdsDb = new ArrayList<>();
  172. List<String> brandIds = new ArrayList<>();
  173. List<String> brandIdsDb = new ArrayList<>();
  174. for (HashMap<Integer, String> map : excelRowList) {
  175. String outlineId = map.get(0);
  176. String brandId = map.get(1);
  177. if(StringUtils.isNotBlank(outlineId)){
  178. outlineIds.add(outlineId);
  179. }
  180. if(StringUtils.isNotBlank(brandId)){
  181. brandIds.add(brandId);
  182. }
  183. }
  184. if(outlineIds.size() >0){
  185. List<HotOutline> hotOutlines = hotOutlineService.listByIds(outlineIds);
  186. for (HotOutline hotOutline : hotOutlines) {
  187. outlineIdsDb.add(hotOutline.getId().toString());
  188. }
  189. }
  190. if(brandIds.size() >0){
  191. List<Brand> brands = brandService.getByCdfBrandIds(brandIds);
  192. for (Brand brand : brands) {
  193. brandIdsDb.add(brand.getCdfBrandId().toString());
  194. }
  195. }
  196. for (HashMap<Integer, String> map : excelRowList) {
  197. colum ++;
  198. String outlineId = map.get(0);
  199. String brandId = map.get(1);
  200. if(StringUtils.isBlank(outlineId) || StringUtils.isBlank(brandId)){
  201. errorList.add(colum.toString());
  202. continue;
  203. }
  204. if(!outlineIdsDb.contains(outlineId)){
  205. errorList.add(colum.toString());
  206. continue;
  207. }
  208. if(!brandIdsDb.contains(brandId)){
  209. errorList.add(colum.toString());
  210. }
  211. }
  212. }
  213. private void checkUploadProduct(File file, List<String> errorList){
  214. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  215. Integer colum = 1;
  216. HashMap<String,ProductSource> productSourceHashMap = new HashMap<>();
  217. HashMap<String, List<String>> productSourceProductMap = new HashMap<>();
  218. HashMap<String, List<String>> resultPMap = new HashMap<>();
  219. HashMap<String, List<String>> numMap = new HashMap<>();
  220. for (HashMap<Integer, String> map : excelRowList) {
  221. String sourceId = map.get(3);
  222. if(StringUtils.isNotBlank(sourceId)){
  223. ProductSource byMchId = productSourceService.getByMchId(sourceId);
  224. productSourceHashMap.put(sourceId,byMchId);
  225. }
  226. String pid = map.get(4);
  227. if(StringUtils.isNotBlank(pid)){
  228. List<String> pidList = productSourceProductMap.computeIfAbsent(sourceId, k -> new ArrayList<>());
  229. pidList.add(pid);
  230. }
  231. }
  232. for (String mchId : productSourceProductMap.keySet()) {
  233. ProductSource productSource = productSourceHashMap.get(mchId);
  234. JSONArray ids = new JSONArray();
  235. ids.addAll(productSourceProductMap.get(mchId));
  236. CdfProductListByIdsRequest idsRequest = new CdfProductListByIdsRequest(ids);
  237. CdfProductListByIdsVo vos = cdfHKClient.getProductListByIds(productSource.getCdfHost(), productSource.getCdfMchId(), idsRequest);
  238. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  239. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  240. List<String> pidList = resultPMap.computeIfAbsent(mchId, k -> new ArrayList<>());
  241. pidList.add(cdfProduct.getId());
  242. }
  243. }
  244. }
  245. for (HashMap<Integer, String> map : excelRowList) {
  246. colum ++;
  247. String sceneNum = map.get(0);
  248. String sid = map.get(1);
  249. String title = map.get(2);
  250. String mchId = map.get(3);
  251. String pid = map.get(4);
  252. if(StringUtils.isBlank(sceneNum) || StringUtils.isBlank(sid) || StringUtils.isBlank(pid) || StringUtils.isBlank(mchId)){
  253. errorList.add(colum.toString());
  254. continue;
  255. }
  256. if(numMap.get(sceneNum) == null){
  257. List<HotRelation> hotRelations = hotRelationService.getByNum(sceneNum);
  258. List<String> sidList = hotRelations.stream().map(HotRelation::getHotId).collect(Collectors.toList());
  259. numMap.put(sceneNum,sidList);
  260. }
  261. if(!numMap.get(sceneNum).contains(sid)){
  262. errorList.add(colum.toString());
  263. continue;
  264. }
  265. if(!productSourceHashMap.containsKey(mchId)){
  266. errorList.add(colum.toString());
  267. continue;
  268. }
  269. List<String> cdfProductList = resultPMap.get(mchId);
  270. if(cdfProductList.isEmpty()){
  271. errorList.add(colum.toString());
  272. continue;
  273. }
  274. if(!cdfProductList.contains(pid)){
  275. errorList.add(colum.toString());
  276. }
  277. }
  278. }
  279. private void checkHot(File file, List<String> errorList) {
  280. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  281. Integer colum = 1;
  282. List<HotUploadTemplate> list = null;
  283. for (HashMap<Integer, String> map : excelRowList) {
  284. colum ++;
  285. String sceneNum = map.get(0);
  286. String sid = map.get(1);
  287. String title = map.get(2);
  288. if(StringUtils.isBlank(sceneNum) || StringUtils.isBlank(sid) ){
  289. errorList.add(colum.toString());
  290. continue;
  291. }
  292. if(list == null){
  293. list = getHotsList(sceneNum);
  294. }
  295. if(list .isEmpty()){
  296. errorList.add(colum.toString());
  297. continue;
  298. }
  299. List<String> collect = list.stream().map(HotUploadTemplate::getSid).collect(Collectors.toList());
  300. if(!collect.contains(sid)){
  301. errorList.add(colum.toString());
  302. }
  303. }
  304. }
  305. private List<HotUploadTemplate> getHotsList(String sceneNum){
  306. JSONArray hotJson = fdkkSceneEditService.getCdfHotJson(sceneNum);
  307. List<HotUploadTemplate> list = new ArrayList<>();
  308. for (Object obj : hotJson) {
  309. JSONObject tag = (JSONObject) obj;
  310. String sid = tag.getString("sid");
  311. String title = tag.getString("title");
  312. HotUploadTemplate hotExcelVo = new HotUploadTemplate(sceneNum,sid,title);
  313. list.add(hotExcelVo);
  314. }
  315. return list;
  316. }
  317. public void uploadExcel(UploadHotsParam param) {
  318. if(StringUtils.isBlank(param.getFilePath()) || param.getType()== null){
  319. throw new BusinessException(ResultCode.PARAM_MISS);
  320. }
  321. String awsKey = param.getFilePath().replace(queryPath, "");
  322. if(!uploadToCdfOssUtil.existKey(awsKey)){
  323. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  324. }
  325. String localPath = String.format(CacheUtil.localFilePath, CacheUtil.activeYaml);
  326. String localFile = localPath + awsKey;
  327. File file = new File(localFile);
  328. if(!file.exists()){
  329. throw new BusinessException(ResultCode.UPLOAD_EXCEL_CHECK_ERROR);
  330. }
  331. switch (param.getType()){
  332. case 0:
  333. uploadOutline(file);
  334. break;
  335. case 1:
  336. uploadProduct(file);
  337. break;
  338. case 2 :case 3 :
  339. uploadHot(file,param.getType());
  340. break;
  341. default: throw new BusinessException(ResultCode.PARAM_MISS);
  342. }
  343. }
  344. private void uploadOutline(File file) {
  345. List<String> errorList = new ArrayList<>();
  346. this.checkUploadOutline(file,errorList);
  347. Integer colum = 1;
  348. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  349. for (HashMap<Integer, String> map : excelRowList) {
  350. colum ++;
  351. if(errorList.contains(colum.toString())){
  352. continue;
  353. }
  354. String outlineId = map.get(0);
  355. String brandId = map.get(1);
  356. if(StringUtils.isNotBlank(outlineId) && StringUtils.isNotBlank(brandId)){
  357. brandService.updateBrandOutline(brandId,outlineId);
  358. }
  359. }
  360. }
  361. private void uploadProduct(File file) {
  362. List<String> errorList = new ArrayList<>();
  363. this.checkUploadProduct(file,errorList);
  364. Integer colum = 1;
  365. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  366. HashMap<String,HashMap<String,HashSet<String>>> numMap = new HashMap<>();
  367. for (HashMap<Integer, String> map : excelRowList) {
  368. colum ++;
  369. if(errorList.contains(colum.toString())){
  370. continue;
  371. }
  372. String sceneNum = map.get(0);
  373. String sid = map.get(1);
  374. String title = map.get(2);
  375. String mchId = map.get(3);
  376. String pid = map.get(4);
  377. if(StringUtils.isBlank(mchId) || StringUtils.isBlank(sceneNum) || StringUtils.isBlank(pid) || StringUtils.isBlank(sid)){
  378. continue;
  379. }
  380. numMap.computeIfAbsent(sceneNum, k -> new HashMap<>());
  381. numMap.get(sceneNum).computeIfAbsent(sid,k -> new HashSet<>());
  382. numMap.get(sceneNum).get(sid).add(pid);
  383. }
  384. List<HotRelation> updateList = new ArrayList<>();
  385. for (String num : numMap.keySet()) {
  386. HashMap<String, HashSet<String>> sidMap = numMap.get(num);
  387. for (String sid : sidMap.keySet()) {
  388. HotRelation hotRelation = hotRelationService.getById(sid);
  389. if(hotRelation == null){
  390. continue;
  391. }
  392. HashSet<String> pidSet = sidMap.get(sid);
  393. hotRelation.setRelationIds(JSONArray.toJSONString(pidSet));
  394. updateList.add(hotRelation);
  395. }
  396. }
  397. if(!updateList.isEmpty()){
  398. hotRelationService.updateBatchById(updateList);
  399. }
  400. for (String num : numMap.keySet()) {
  401. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  402. fdkkSceneEditService.writeHotJson(num,numRegion.getRegion());
  403. }
  404. }
  405. private void uploadHot(File file,Integer type) {
  406. List<String> errorList = new ArrayList<>();
  407. this.checkHot(file,errorList);
  408. Integer colum = 1;
  409. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  410. HashMap<String,HashSet<String>> numMap = new HashMap<>();
  411. for (HashMap<Integer, String> map : excelRowList) {
  412. colum ++;
  413. if(errorList.contains(colum.toString())){
  414. continue;
  415. }
  416. String sceneNum = map.get(0);
  417. String sid = map.get(1);
  418. String title = map.get(2);
  419. if(StringUtils.isBlank(sid) || StringUtils.isBlank(sceneNum)){
  420. continue;
  421. }
  422. numMap.computeIfAbsent(sceneNum, k -> new HashSet<>());
  423. numMap.get(sceneNum).add(sid);
  424. HotRecommend recommend = hotRecommendService.getByNumAndSid(sceneNum,sid);
  425. if(recommend == null){
  426. recommend = new HotRecommend();
  427. recommend.setSceneNum(sceneNum);
  428. recommend.setSid(sid);
  429. }
  430. if(type == 2){
  431. recommend.setRecommendSelection(1);
  432. }
  433. if(type == 3){
  434. recommend.setRecommendBrand(1);
  435. }
  436. hotRecommendService.saveOrUpdate(recommend);
  437. }
  438. List<Integer> delIds = new ArrayList<>();
  439. for (String num : numMap.keySet()) {
  440. List<HotRecommend> list = hotRecommendService.getByNum(num);
  441. HashSet<String> strings = numMap.get(num);
  442. for (HotRecommend recommend : list) {
  443. if(!strings.contains(recommend.getSid())){
  444. delIds.add(recommend.getId());
  445. }
  446. }
  447. }
  448. if(!delIds.isEmpty()){
  449. hotRecommendService.removeByIds(delIds);
  450. }
  451. for (String num : numMap.keySet()) {
  452. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  453. fdkkSceneEditService.writeHotJson(num,numRegion.getRegion());
  454. }
  455. }
  456. public void downBrandExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
  457. List<Brand> list = brandService.list();
  458. List<BrandExcelVo> voList = new ArrayList<>();
  459. for (Brand brand : list) {
  460. BrandExcelVo vo = new BrandExcelVo();
  461. BeanUtil.copyProperties(brand,vo);
  462. voList.add(vo);
  463. }
  464. this.commonExport(request,response,"品牌信息",voList, BrandExcelVo.class);
  465. }
  466. }