123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package com.cdf.service.impl;
- import cn.hutool.core.bean.BeanUtil;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.cdf.common.PageInfo;
- import com.cdf.entity.Brand;
- import com.cdf.entity.HotOutline;
- import com.cdf.httpClient.util.CdfOpenApiUtil;
- import com.cdf.httpClient.vo.CdfBrand;
- import com.cdf.mapper.IBrandMapper;
- import com.cdf.request.BrandApiParam;
- import com.cdf.response.BrandApiVo;
- import com.cdf.service.IBrandService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.cdf.service.IHotOutlineService;
- import com.cdf.util.MyStringUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 服务实现类
- * </p>
- *
- * @author
- * @since 2023-12-06
- */
- @Service
- @Slf4j
- public class BrandServiceImpl extends ServiceImpl<IBrandMapper, Brand> implements IBrandService {
- @Autowired
- IHotOutlineService hotOutlineService;
- @Override
- public void updateBrandInfo() throws Exception {
- Long pageNum = 1L;
- Long pageSize = 100L;
- List<CdfBrand> allBrand = new ArrayList<>();
- PageInfo<List<CdfBrand>> pageInfo = CdfOpenApiUtil.getBrandList(pageNum,pageSize);
- if(pageInfo == null){
- return;
- }
- Long size = 0L;
- if(pageInfo.getTotal() % pageSize == 0){
- size = pageInfo.getTotal() /pageSize;
- }else {
- size = pageInfo.getTotal() / pageSize +1;
- }
- allBrand.addAll(pageInfo.getList());
- log.info("cdf品牌总数为{},一共为{}页",pageInfo.getTotal(),size);
- for (long i = 2 ; i<= size;i++){
- Thread.sleep(100L);
- pageInfo = CdfOpenApiUtil.getBrandList(i,pageSize);
- allBrand.addAll(pageInfo.getList());
- }
- List<Brand> list = this.list();
- HashMap<String,Brand> brandHashMap = new HashMap<>();
- for (Brand brand : list) {
- brandHashMap.put(brand.getCdfBrandId(),brand);
- }
- List<Brand> saveList = new ArrayList<>();
- HashMap<String,CdfBrand> cdfBrandHashMap = new HashMap<>();
- for (CdfBrand cdfBrand : allBrand) {
- cdfBrandHashMap.put(cdfBrand.getBrand_id(),cdfBrand);
- }
- for (String brandId : cdfBrandHashMap.keySet()) {
- CdfBrand cdfBrand = cdfBrandHashMap.get(brandId);
- if(cdfBrand == null){
- continue;
- }
- Brand brand = brandHashMap.get(cdfBrand.getBrand_id());
- if(brand != null &&
- MyStringUtils.eqStr(brand.getZhName(),cdfBrand.getBrand_name()) &&
- MyStringUtils.eqStr(brand.getFtName(),cdfBrand.getBrand_tc_name()) &&
- MyStringUtils.eqStr(brand.getEnName(),cdfBrand.getBrand_en_name()) &&
- MyStringUtils.eqStr(brand.getBrandLogo(),cdfBrand.getLogo())){
- continue;
- }
- if(brand == null){
- brand = new Brand();
- brand.setCdfBrandId(cdfBrand.getBrand_id());
- }
- brand.setZhName(cdfBrand.getBrand_name());
- brand.setFtName(cdfBrand.getBrand_tc_name());
- brand.setEnName(cdfBrand.getBrand_en_name());
- brand.setBrandLogo(cdfBrand.getLogo());
- brand.setUpdateTime(null);
- saveList.add(brand);
- }
- this.saveOrUpdateBatch(saveList);
- List<String> delList = new ArrayList<>();
- Set<String> cdfBrandIds = allBrand.stream().map(CdfBrand::getBrand_id).collect(Collectors.toSet());
- Set<String> dbBrandIds = list.stream().map(Brand::getCdfBrandId).collect(Collectors.toSet());
- for (String dbBrandId : dbBrandIds) {
- if(!cdfBrandIds.contains(dbBrandId)){
- delList.add(dbBrandId);
- }
- }
- if(delList.size() >0){
- LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Brand::getCdfBrandId,delList);
- this.remove(wrapper);
- }
- }
- @Override
- public Object pageList(BrandApiParam param) {
- LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
- if(StringUtils.isNotBlank(param.getKeyword())){
- wrapper.like(Brand::getZhName,param.getKeyword())
- .or()
- .like(Brand::getFtName,param.getKeyword())
- .or()
- .like(Brand::getEnName,param.getKeyword());
- }
- wrapper.orderByDesc(Brand::getCdfBrandId);
- Page<Brand> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
- List<Integer> outLineIds = page.getRecords().stream().map(Brand::getOutlineId).filter(Objects::nonNull).collect(Collectors.toList());
- HashMap<Integer, HotOutline> map = hotOutlineService.getMapByIds(outLineIds);
- Page<BrandApiVo> pageVo = new Page<>(param.getPageNum(), param.getPageSize());
- List<BrandApiVo> listVo = new ArrayList<>();
- for (Brand record : page.getRecords()) {
- BrandApiVo vo = new BrandApiVo();
- BeanUtil.copyProperties(record,vo);
- HotOutline hotOutline = map.get(record.getOutlineId());
- if(hotOutline != null){
- vo.setOutlineName(hotOutline.getOutlineName());
- vo.setOutlineImage(hotOutline.getOutlineImage());
- }
- listVo.add(vo);
- }
- pageVo.setTotal(page.getTotal());
- pageVo.setRecords(listVo);
- return PageInfo.PageInfo(pageVo);
- }
- @Override
- public HashMap<String, BrandApiVo> getMapByIds(Set<String> brandIds) {
- HashMap<String, BrandApiVo> map = new HashMap<>();
- if(!brandIds.isEmpty()){
- List<Brand> brands = this.getByCdfBrandIds(new ArrayList<>(brandIds));
- List<Integer> outLineIds = brands.stream().map(Brand::getOutlineId).filter(Objects::nonNull).collect(Collectors.toList());
- HashMap<Integer, HotOutline> hotOutlineHashMap = hotOutlineService.getMapByIds(outLineIds);
- for (Brand brand : brands) {
- BrandApiVo vo = new BrandApiVo();
- BeanUtil.copyProperties(brand,vo);
- HotOutline hotOutline = hotOutlineHashMap.get(brand.getOutlineId());
- if(hotOutline != null){
- vo.setOutlineName(hotOutline.getOutlineName());
- vo.setOutlineImage(hotOutline.getOutlineImage());
- }
- map.put(vo.getCdfBrandId(),vo);
- }
- }
- return map;
- }
- @Override
- public List<Brand> getByCdfBrandIds(List<String> brandIds) {
- if(brandIds != null && brandIds.size() >0){
- LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
- wrapper.in(Brand::getCdfBrandId,brandIds);
- return this.list(wrapper);
- }
- return null;
- }
- public static void main(String[] args) {
- Long pageNum = 1L;
- Long pageSize = 100L;
- List<CdfBrand> allBrand = new ArrayList<>();
- PageInfo<List<CdfBrand>> pageInfo =CdfOpenApiUtil.getBrandList(pageNum,pageSize);
- if(pageInfo == null){
- return;
- }
- Long size = 0L;
- if(pageInfo.getTotal() % pageSize == 0){
- size = pageInfo.getTotal() /pageSize;
- }else {
- size = pageInfo.getTotal() / pageSize +1;
- }
- log.info("品牌总数为{},一共为{}页",pageInfo.getTotal(),size);
- for (long i = 1 ; i<= size;i++){
- allBrand.addAll(pageInfo.getList());
- pageInfo = CdfOpenApiUtil.getBrandList(i,pageSize);
- }
- System.out.println(allBrand.size());
- }
- @Override
- public void updateBrandOutline(String brandId, String outlineId) {
- LambdaUpdateWrapper<Brand> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(Brand::getCdfBrandId,brandId);
- wrapper.set(Brand::getOutlineId,outlineId);
- this.update(wrapper);
- }
- @Override
- public List<Brand> getByHotOutlineId(Integer outlineId) {
- LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
- wrapper.eq(Brand::getOutlineId,outlineId);
- return this.list(wrapper);
- }
- }
|