ExcelService.java 21 KB

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