BrandServiceImpl.java 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package com.cdf.service.impl;
  2. import cn.hutool.core.bean.BeanUtil;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.cdf.common.PageInfo;
  7. import com.cdf.entity.Brand;
  8. import com.cdf.entity.HotOutline;
  9. import com.cdf.httpClient.util.CdfOpenApiUtil;
  10. import com.cdf.httpClient.vo.CdfBrand;
  11. import com.cdf.mapper.IBrandMapper;
  12. import com.cdf.request.BrandApiParam;
  13. import com.cdf.response.BrandApiVo;
  14. import com.cdf.service.IBrandService;
  15. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  16. import com.cdf.service.IHotOutlineService;
  17. import com.cdf.util.MyStringUtils;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. /**
  25. * <p>
  26. * 服务实现类
  27. * </p>
  28. *
  29. * @author
  30. * @since 2023-12-06
  31. */
  32. @Service
  33. @Slf4j
  34. public class BrandServiceImpl extends ServiceImpl<IBrandMapper, Brand> implements IBrandService {
  35. @Autowired
  36. IHotOutlineService hotOutlineService;
  37. @Override
  38. public void updateBrandInfo() throws Exception {
  39. Long pageNum = 1L;
  40. Long pageSize = 100L;
  41. List<CdfBrand> allBrand = new ArrayList<>();
  42. PageInfo<List<CdfBrand>> pageInfo = CdfOpenApiUtil.getBrandList(pageNum,pageSize);
  43. if(pageInfo == null){
  44. return;
  45. }
  46. Long size = 0L;
  47. if(pageInfo.getTotal() % pageSize == 0){
  48. size = pageInfo.getTotal() /pageSize;
  49. }else {
  50. size = pageInfo.getTotal() / pageSize +1;
  51. }
  52. allBrand.addAll(pageInfo.getList());
  53. log.info("cdf品牌总数为{},一共为{}页",pageInfo.getTotal(),size);
  54. for (long i = 2 ; i<= size;i++){
  55. Thread.sleep(100L);
  56. pageInfo = CdfOpenApiUtil.getBrandList(i,pageSize);
  57. allBrand.addAll(pageInfo.getList());
  58. }
  59. List<Brand> list = this.list();
  60. HashMap<String,Brand> brandHashMap = new HashMap<>();
  61. for (Brand brand : list) {
  62. brandHashMap.put(brand.getCdfBrandId(),brand);
  63. }
  64. List<Brand> saveList = new ArrayList<>();
  65. HashMap<String,CdfBrand> cdfBrandHashMap = new HashMap<>();
  66. for (CdfBrand cdfBrand : allBrand) {
  67. cdfBrandHashMap.put(cdfBrand.getBrand_id(),cdfBrand);
  68. }
  69. for (String brandId : cdfBrandHashMap.keySet()) {
  70. CdfBrand cdfBrand = cdfBrandHashMap.get(brandId);
  71. if(cdfBrand == null){
  72. continue;
  73. }
  74. Brand brand = brandHashMap.get(cdfBrand.getBrand_id());
  75. if(brand != null &&
  76. MyStringUtils.eqStr(brand.getZhName(),cdfBrand.getBrand_name()) &&
  77. MyStringUtils.eqStr(brand.getFtName(),cdfBrand.getBrand_tc_name()) &&
  78. MyStringUtils.eqStr(brand.getEnName(),cdfBrand.getBrand_en_name()) &&
  79. MyStringUtils.eqStr(brand.getBrandLogo(),cdfBrand.getLogo())){
  80. continue;
  81. }
  82. if(brand == null){
  83. brand = new Brand();
  84. brand.setCdfBrandId(cdfBrand.getBrand_id());
  85. }
  86. brand.setZhName(cdfBrand.getBrand_name());
  87. brand.setFtName(cdfBrand.getBrand_tc_name());
  88. brand.setEnName(cdfBrand.getBrand_en_name());
  89. brand.setBrandLogo(cdfBrand.getLogo());
  90. brand.setUpdateTime(null);
  91. saveList.add(brand);
  92. }
  93. this.saveOrUpdateBatch(saveList);
  94. List<String> delList = new ArrayList<>();
  95. Set<String> cdfBrandIds = allBrand.stream().map(CdfBrand::getBrand_id).collect(Collectors.toSet());
  96. Set<String> dbBrandIds = list.stream().map(Brand::getCdfBrandId).collect(Collectors.toSet());
  97. for (String dbBrandId : dbBrandIds) {
  98. if(!cdfBrandIds.contains(dbBrandId)){
  99. delList.add(dbBrandId);
  100. }
  101. }
  102. if(delList.size() >0){
  103. LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
  104. wrapper.in(Brand::getCdfBrandId,delList);
  105. this.remove(wrapper);
  106. }
  107. }
  108. @Override
  109. public Object pageList(BrandApiParam param) {
  110. LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
  111. if(StringUtils.isNotBlank(param.getKeyword())){
  112. wrapper.like(Brand::getZhName,param.getKeyword())
  113. .or()
  114. .like(Brand::getFtName,param.getKeyword())
  115. .or()
  116. .like(Brand::getEnName,param.getKeyword());
  117. }
  118. wrapper.orderByDesc(Brand::getCdfBrandId);
  119. Page<Brand> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  120. List<Integer> outLineIds = page.getRecords().stream().map(Brand::getOutlineId).filter(Objects::nonNull).collect(Collectors.toList());
  121. HashMap<Integer, HotOutline> map = hotOutlineService.getMapByIds(outLineIds);
  122. Page<BrandApiVo> pageVo = new Page<>(param.getPageNum(), param.getPageSize());
  123. List<BrandApiVo> listVo = new ArrayList<>();
  124. for (Brand record : page.getRecords()) {
  125. BrandApiVo vo = new BrandApiVo();
  126. BeanUtil.copyProperties(record,vo);
  127. HotOutline hotOutline = map.get(record.getOutlineId());
  128. if(hotOutline != null){
  129. vo.setOutlineName(hotOutline.getOutlineName());
  130. vo.setOutlineImage(hotOutline.getOutlineImage());
  131. }
  132. listVo.add(vo);
  133. }
  134. pageVo.setTotal(page.getTotal());
  135. pageVo.setRecords(listVo);
  136. return PageInfo.PageInfo(pageVo);
  137. }
  138. @Override
  139. public HashMap<String, BrandApiVo> getMapByIds(Set<String> brandIds) {
  140. HashMap<String, BrandApiVo> map = new HashMap<>();
  141. if(!brandIds.isEmpty()){
  142. List<Brand> brands = this.getByCdfBrandIds(new ArrayList<>(brandIds));
  143. List<Integer> outLineIds = brands.stream().map(Brand::getOutlineId).filter(Objects::nonNull).collect(Collectors.toList());
  144. HashMap<Integer, HotOutline> hotOutlineHashMap = hotOutlineService.getMapByIds(outLineIds);
  145. for (Brand brand : brands) {
  146. log.info("brand:{}",brand);
  147. BrandApiVo vo = new BrandApiVo();
  148. BeanUtil.copyProperties(brand,vo);
  149. HotOutline hotOutline = hotOutlineHashMap.get(brand.getOutlineId());
  150. if(hotOutline != null){
  151. vo.setOutlineName(hotOutline.getOutlineName());
  152. vo.setOutlineImage(hotOutline.getOutlineImage());
  153. }
  154. map.put(vo.getCdfBrandId(),vo);
  155. }
  156. }
  157. return map;
  158. }
  159. @Override
  160. public List<Brand> getByCdfBrandIds(List<String> brandIds) {
  161. if(brandIds != null && brandIds.size() >0){
  162. LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
  163. wrapper.in(Brand::getCdfBrandId,brandIds);
  164. return this.list(wrapper);
  165. }
  166. return null;
  167. }
  168. public static void main(String[] args) {
  169. Long pageNum = 1L;
  170. Long pageSize = 100L;
  171. List<CdfBrand> allBrand = new ArrayList<>();
  172. PageInfo<List<CdfBrand>> pageInfo =CdfOpenApiUtil.getBrandList(pageNum,pageSize);
  173. if(pageInfo == null){
  174. return;
  175. }
  176. Long size = 0L;
  177. if(pageInfo.getTotal() % pageSize == 0){
  178. size = pageInfo.getTotal() /pageSize;
  179. }else {
  180. size = pageInfo.getTotal() / pageSize +1;
  181. }
  182. log.info("品牌总数为{},一共为{}页",pageInfo.getTotal(),size);
  183. for (long i = 1 ; i<= size;i++){
  184. allBrand.addAll(pageInfo.getList());
  185. pageInfo = CdfOpenApiUtil.getBrandList(i,pageSize);
  186. }
  187. System.out.println(allBrand.size());
  188. }
  189. @Override
  190. public void updateBrandOutline(String brandId, String outlineId) {
  191. LambdaUpdateWrapper<Brand> wrapper = new LambdaUpdateWrapper<>();
  192. wrapper.eq(Brand::getCdfBrandId,brandId);
  193. wrapper.set(Brand::getOutlineId,outlineId);
  194. this.update(wrapper);
  195. }
  196. @Override
  197. public List<Brand> getByHotOutlineId(Integer outlineId) {
  198. LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
  199. wrapper.eq(Brand::getOutlineId,outlineId);
  200. return this.list(wrapper);
  201. }
  202. }