ExcelService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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,ProductSource> productSourceHashMap = new HashMap<>();
  218. HashMap<String, List<String>> productSourceProductMap = new HashMap<>();
  219. HashMap<String, List<String>> resultPMap = new HashMap<>();
  220. HashMap<String, List<String>> numMap = new HashMap<>();
  221. for (HashMap<Integer, String> map : excelRowList) {
  222. String sourceId = map.get(3);
  223. if(StringUtils.isNotBlank(sourceId)){
  224. ProductSource byMchId = productSourceService.getByMchId(sourceId);
  225. productSourceHashMap.put(sourceId,byMchId);
  226. }
  227. String pid = map.get(4);
  228. if(StringUtils.isNotBlank(pid)){
  229. List<String> pidList = productSourceProductMap.computeIfAbsent(sourceId, k -> new ArrayList<>());
  230. pidList.add(pid);
  231. }
  232. }
  233. for (String mchId : productSourceProductMap.keySet()) {
  234. ProductSource productSource = productSourceHashMap.get(mchId);
  235. JSONArray ids = new JSONArray();
  236. ids.addAll(productSourceProductMap.get(mchId));
  237. CdfProductListByIdsRequest idsRequest = new CdfProductListByIdsRequest(ids);
  238. CdfProductListByIdsVo vos = cdfHKClient.getProductListByIds(productSource.getCdfHost(), productSource.getCdfMchId(), idsRequest);
  239. if(vos.getProductCardList()!=null && vos.getProductCardList().size() >0){
  240. for (CdfProduct cdfProduct : vos.getProductCardList()) {
  241. List<String> pidList = resultPMap.computeIfAbsent(mchId, k -> new ArrayList<>());
  242. pidList.add(cdfProduct.getId());
  243. }
  244. }
  245. }
  246. for (HashMap<Integer, String> map : excelRowList) {
  247. colum ++;
  248. String sceneNum = map.get(0);
  249. String sid = map.get(1);
  250. String title = map.get(2);
  251. String mchId = map.get(3);
  252. String pid = map.get(4);
  253. if(StringUtils.isBlank(sceneNum) || StringUtils.isBlank(sid) || StringUtils.isBlank(pid) || StringUtils.isBlank(mchId)){
  254. errorList.add(colum.toString());
  255. continue;
  256. }
  257. if(numMap.get(sceneNum) == null){
  258. List<HotRelation> hotRelations = hotRelationService.getByNum(sceneNum);
  259. List<String> sidList = hotRelations.stream().map(HotRelation::getHotId).collect(Collectors.toList());
  260. numMap.put(sceneNum,sidList);
  261. }
  262. if(!numMap.get(sceneNum).contains(sid)){
  263. errorList.add(colum.toString());
  264. continue;
  265. }
  266. if(!productSourceHashMap.containsKey(mchId)){
  267. errorList.add(colum.toString());
  268. continue;
  269. }
  270. List<String> cdfProductList = resultPMap.get(mchId);
  271. if(cdfProductList.isEmpty()){
  272. errorList.add(colum.toString());
  273. continue;
  274. }
  275. if(!cdfProductList.contains(pid)){
  276. errorList.add(colum.toString());
  277. }
  278. }
  279. }
  280. private void checkHot(File file, List<String> errorList) {
  281. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  282. Integer colum = 1;
  283. List<HotUploadTemplate> list = null;
  284. for (HashMap<Integer, String> map : excelRowList) {
  285. colum ++;
  286. String sceneNum = map.get(0);
  287. String sid = map.get(1);
  288. String title = map.get(2);
  289. if(StringUtils.isBlank(sceneNum) || StringUtils.isBlank(sid) ){
  290. errorList.add(colum.toString());
  291. continue;
  292. }
  293. if(list == null){
  294. list = getHotsList(sceneNum);
  295. }
  296. if(list .isEmpty()){
  297. errorList.add(colum.toString());
  298. continue;
  299. }
  300. List<String> collect = list.stream().map(HotUploadTemplate::getSid).collect(Collectors.toList());
  301. if(!collect.contains(sid)){
  302. errorList.add(colum.toString());
  303. }
  304. }
  305. }
  306. private List<HotUploadTemplate> getHotsList(String sceneNum){
  307. JSONArray hotJson = fdkkSceneEditService.getCdfHotJson(sceneNum);
  308. List<HotUploadTemplate> list = new ArrayList<>();
  309. for (Object obj : hotJson) {
  310. JSONObject tag = (JSONObject) obj;
  311. String sid = tag.getString("sid");
  312. String title = tag.getString("title");
  313. HotUploadTemplate hotExcelVo = new HotUploadTemplate(sceneNum,sid,title);
  314. list.add(hotExcelVo);
  315. }
  316. return list;
  317. }
  318. public void uploadExcel(UploadHotsParam param) {
  319. if(StringUtils.isBlank(param.getFilePath()) || param.getType()== null){
  320. throw new BusinessException(ResultCode.PARAM_MISS);
  321. }
  322. String awsKey = param.getFilePath().replace(queryPath, "");
  323. if(!uploadToCdfOssUtil.existKey(awsKey)){
  324. throw new BusinessException(ResultCode.UPLOAD_FILE_NO_EXIST);
  325. }
  326. String localPath = String.format(CacheUtil.localFilePath, CacheUtil.activeYaml);
  327. String localFile = localPath + awsKey;
  328. File file = new File(localFile);
  329. if(!file.exists()){
  330. throw new BusinessException(ResultCode.UPLOAD_EXCEL_CHECK_ERROR);
  331. }
  332. switch (param.getType()){
  333. case 0:
  334. uploadOutline(file);
  335. break;
  336. case 1:
  337. uploadProduct(file);
  338. break;
  339. case 2 :case 3 :
  340. uploadHot(file,param.getType());
  341. break;
  342. default: throw new BusinessException(ResultCode.PARAM_MISS);
  343. }
  344. }
  345. private void uploadOutline(File file) {
  346. List<String> errorList = new ArrayList<>();
  347. this.checkUploadOutline(file,errorList);
  348. Integer colum = 1;
  349. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  350. for (HashMap<Integer, String> map : excelRowList) {
  351. colum ++;
  352. if(errorList.contains(colum.toString())){
  353. continue;
  354. }
  355. String outlineId = map.get(0);
  356. String brandId = map.get(1);
  357. if(StringUtils.isNotBlank(outlineId) && StringUtils.isNotBlank(brandId)){
  358. brandService.updateBrandOutline(brandId,outlineId);
  359. }
  360. }
  361. }
  362. private void uploadProduct(File file) {
  363. List<String> errorList = new ArrayList<>();
  364. this.checkUploadProduct(file,errorList);
  365. Integer colum = 1;
  366. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  367. HashMap<String,HashMap<String,HashSet<String>>> numMap = new HashMap<>();
  368. for (HashMap<Integer, String> map : excelRowList) {
  369. colum ++;
  370. if(errorList.contains(colum.toString())){
  371. continue;
  372. }
  373. String sceneNum = map.get(0);
  374. String sid = map.get(1);
  375. String title = map.get(2);
  376. String mchId = map.get(3);
  377. String pid = map.get(4);
  378. if(StringUtils.isBlank(mchId) || StringUtils.isBlank(sceneNum) || StringUtils.isBlank(pid) || StringUtils.isBlank(sid)){
  379. continue;
  380. }
  381. numMap.computeIfAbsent(sceneNum, k -> new HashMap<>());
  382. numMap.get(sceneNum).computeIfAbsent(sid,k -> new HashSet<>());
  383. numMap.get(sceneNum).get(sid).add(pid);
  384. }
  385. List<HotRelation> updateList = new ArrayList<>();
  386. for (String num : numMap.keySet()) {
  387. HashMap<String, HashSet<String>> sidMap = numMap.get(num);
  388. for (String sid : sidMap.keySet()) {
  389. HotRelation hotRelation = hotRelationService.getById(sid);
  390. if(hotRelation == null){
  391. continue;
  392. }
  393. HashSet<String> pidSet = sidMap.get(sid);
  394. hotRelation.setRelationIds(JSONArray.toJSONString(pidSet));
  395. updateList.add(hotRelation);
  396. }
  397. }
  398. if(!updateList.isEmpty()){
  399. hotRelationService.updateBatchById(updateList);
  400. }
  401. for (String num : numMap.keySet()) {
  402. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  403. fdkkSceneEditService.writeHotJson(num,numRegion.getRegion());
  404. }
  405. }
  406. private void uploadHot(File file,Integer type) {
  407. List<String> errorList = new ArrayList<>();
  408. this.checkHot(file,errorList);
  409. Integer colum = 1;
  410. List<HashMap<Integer, String>> excelRowList = ExcelUtil.getExcelRowList(file);
  411. HashMap<String,HashSet<String>> numMap = new HashMap<>();
  412. for (HashMap<Integer, String> map : excelRowList) {
  413. colum ++;
  414. if(errorList.contains(colum.toString())){
  415. continue;
  416. }
  417. String sceneNum = map.get(0);
  418. String sid = map.get(1);
  419. String title = map.get(2);
  420. if(StringUtils.isBlank(sid) || StringUtils.isBlank(sceneNum)){
  421. continue;
  422. }
  423. numMap.computeIfAbsent(sceneNum, k -> new HashSet<>());
  424. numMap.get(sceneNum).add(sid);
  425. HotRecommend recommend = hotRecommendService.getByNumAndSid(sceneNum,sid);
  426. if(recommend == null){
  427. recommend = new HotRecommend();
  428. recommend.setSceneNum(sceneNum);
  429. recommend.setSid(sid);
  430. }
  431. if(type == 2){
  432. recommend.setRecommendSelection(1);
  433. }
  434. if(type == 3){
  435. recommend.setRecommendBrand(1);
  436. }
  437. hotRecommendService.saveOrUpdate(recommend);
  438. }
  439. List<Integer> delIds = new ArrayList<>();
  440. for (String num : numMap.keySet()) {
  441. List<HotRecommend> list = hotRecommendService.getByNum(num);
  442. HashSet<String> strings = numMap.get(num);
  443. for (HotRecommend recommend : list) {
  444. if(!strings.contains(recommend.getSid())){
  445. delIds.add(recommend.getId());
  446. }
  447. }
  448. }
  449. if(!delIds.isEmpty()){
  450. hotRecommendService.removeByIds(delIds);
  451. }
  452. for (String num : numMap.keySet()) {
  453. NumRegion numRegion = fdkkUserService.getRegionByNum(num);
  454. fdkkSceneEditService.writeHotJson(num,numRegion.getRegion());
  455. }
  456. }
  457. public void downBrandExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
  458. LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
  459. wrapper.orderByDesc(Brand::getCdfBrandId);
  460. List<Brand> list = brandService.list(wrapper);
  461. List<BrandExcelVo> voList = new ArrayList<>();
  462. for (Brand brand : list) {
  463. BrandExcelVo vo = new BrandExcelVo();
  464. BeanUtil.copyProperties(brand,vo);
  465. voList.add(vo);
  466. }
  467. this.commonExport(request,response,"品牌信息",voList, BrandExcelVo.class);
  468. }
  469. }