Grain Mall - Basics - Product Service 6 - Product Maintenance (P83-P92)


Commodity service-commodity maintenance

1. Commodity service-API-commodity maintenance

insert image description here


1. Publish products

Publishing products involves many services, such as member, coupon, third-party, gateway, nacos, product

insert image description here


(1) Debug membership level-related interfaces

  • gulimall-memberedit code in service
  • Modify the configuration file, register to nacos, and enable the service registration and discovery function
  • Configure routing rules in the gateway gateway
  • start service

Modify the file in gulimall-gatewayand add the route for member

 #member服务路由
        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            # 把/api/* 去掉,剩下的留下来
            - RewritePath=/api/(?<segment>.*),/$\{
    
    segment}

insert image description here


(2) Obtain the brand associated with the category

ask:http://localhost:88/api/product/categorybrandrelation/brands/list

  • After selecting a category, the associated brand information will be obtained

insert image description here

CategoryBrandRelationController

  /**
     * 获取分类关联的品牌
     * /product/categorybrandrelation/brands/list
     *
     * @param catId
     * @return
     */
    @GetMapping("/brands/list")
    public R relationBrandsList(@RequestParam(value = "catId", required = true) Long catId) {
    
    

        List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);

        List<BrandVo> collect = vos.stream().map((item) -> {
    
    
            BrandVo brandVo = new BrandVo();
            brandVo.setBrandId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());

        return R.ok().put("data", collect);

    }

CategoryBrandRelationServiceImpl

 @Override
    public List<BrandEntity> getBrandsByCatId(Long catId) {
    
    

        // 1.在关联表中根据分类id查询关联的品牌
        List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        // 2.封装品牌详细信息
        List<BrandEntity> collect = catelogId.stream().map((item) -> {
    
    
            // 2.1 获取品牌id
            Long brandId = item.getBrandId();
            // 2.1 根据品牌id查询品牌
            BrandEntity byId = brandService.getById(brandId);
            return byId;
        }).collect(Collectors.toList());

        return collect;
    }

(3) Obtain all groups and attributes under the category

insert image description here

  • Add a new one AttrGroupWithAttrsVoto return data

AttrGroupController

  /**
     * 获取分类下所有分组及属性
     * /product/attrgroup/{catelogId}/withattr
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithattrs(@PathVariable("catelogId") Long catelogId) {
    
    
        //1、查出当前分类下的所有属性分组,
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);

    }

AttrGroupServiceImpl

 /**
     * 根据分类id查出所有的分组以及这些组里面的属性
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
    
    
        // 1.查出分组信息
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));

        //2、查询所有属性
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {
    
    
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(group, attrsVo);

            // 获取所有关联的属性
            List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId());
            attrsVo.setAttrs(attrs);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }


(4) New vo extraction for products

  • Copy the json after filling in the product information, and use the online json tool to convert the json into a java entity class
  • All generated Vo, copied to the vo directory

insert image description here

2. New business for commodities

Business Process Analysis:

	1、保存spu基本信息 pms_spu_info

    2、保存Spu的描述图片 pms_spu_info_desc

    3、保存spu的图片集 pms_spu_images

    4、保存spu的规格参数;pms_product_attr_value

    5、保存spu的积分信息;gulimall_sms->sms_spu_bounds

    6、保存当前spu对应的所有sku信息;

        6.1)、sku的基本信息;pms_sku_info
       
        6.2)、sku的图片信息;pms_sku_image
       
        6.3)、sku的销售属性信息:pms_sku_sale_attr_value

        6.4)、sku的优惠、满减等信息;gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price

(1) Save the basic information of the spu

Edit code in product service

SpuInfoServiceImpl

// spu信息保存
    @Transactional
    @Override
    public void savaSpuInfo(SpuSaveVo vo) {
    
    
        //1、保存spu基本信息 pms_spu_info
        SpuInfoEntity infoEntity = new SpuInfoEntity();
        // 1.1 拷贝基本信息
        BeanUtils.copyProperties(vo, infoEntity);
        // 1.2 处理没有的信息
        infoEntity.setCreateTime(new Date());
        infoEntity.setUpdateTime(new Date());
        // 1.3 保存
        this.saveBaseSpuInfo(infoEntity);

        //2、保存Spu的描述图片 pms_spu_info_desc
        // 2.1 获取描述信息
        List<String> decript = vo.getDecript();
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        // 2.2 处理信息
        descEntity.setSpuId(infoEntity.getId());
        descEntity.setDecript(String.join(",", decript));
        // 2.3 保存
        spuInfoDescService.savaSpuInfoDesc(descEntity);

        //3、保存spu的图片集 pms_spu_images
        List<String> images = vo.getImages();
        imagesService.savaImages(infoEntity.getId(), images);

        //4、保存spu的规格参数;pms_product_attr_value
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
    
    
            ProductAttrValueEntity valueEntity = new ProductAttrValueEntity();
            valueEntity.setAttrId(attr.getAttrId());

            AttrEntity id = attrService.getById(attr.getAttrId());
            valueEntity.setAttrName(id.getAttrName());

            valueEntity.setAttrValue(attr.getAttrValues());
            valueEntity.setQuickShow(attr.getShowDesc());
            valueEntity.setSpuId(infoEntity.getId());

            return valueEntity;
        }).collect(Collectors.toList());

        attrValueService.saveProductAttr(collect);

        //5、保存当前spu对应的所有sku信息;
        List<Skus> skus = vo.getSkus();
        // 1-sku 信息处理
        if (skus != null && skus.size() > 0) {
    
    
            skus.forEach(item -> {
    
    
                // 获取默认图片
                String defaultImg = "";
                for (Images image : item.getImages()) {
    
    
                    if (image.getDefaultImg() == 1) {
    
    
                        defaultImg = image.getImgUrl();
                    }
                }
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(item, skuInfoEntity);
                // 设置基本属性
                skuInfoEntity.setBrandId(infoEntity.getBrandId());
                skuInfoEntity.setCatalogId(infoEntity.getCatalogId());
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setSpuId(infoEntity.getId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);

                //5.1)、保存sku的基本信息;pms_sku_info
                skuInfoService.saveSkuInfo(skuInfoEntity);

                // pms_sku_info 保存后获取sku_id
                Long skuId = skuInfoEntity.getSkuId();
                // 2-图片信息处理
                List<SkuImagesEntity> imagesEntities = item.getImages().stream().map(img -> {
    
    
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();

                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    skuImagesEntity.setDefaultImg(img.getDefaultImg());
                    return skuImagesEntity;
                }).filter(entity -> {
    
    
                    //返回true就是需要,false就是剔除
                    return !StringUtils.isEmpty(entity.getImgUrl());
                }).collect(Collectors.toList());

                //5.2)、sku的图片信息;pms_sku_image
                skuImagesService.saveBatch(imagesEntities);
                //TODO 没有图片路径的无需保存

                // 3-attr信息处理
                List<Attr> attr = item.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attr.stream().map(a -> {
    
    
                    SkuSaleAttrValueEntity attrValueEntity = new SkuSaleAttrValueEntity();
                    BeanUtils.copyProperties(a, attrValueEntity);
                    attrValueEntity.setSkuId(skuId);

                    return attrValueEntity;
                }).collect(Collectors.toList());

                //5.3)、sku的销售属性信息:pms_sku_sale_attr_value
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);

                //5.4)、sku的优惠、满减等信息;gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
                SkuReductionTo skuReductionTo = new SkuReductionTo();
                BeanUtils.copyProperties(item, skuReductionTo);
                skuReductionTo.setSkuId(skuId);

                if (skuReductionTo.getFullCount() > 0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
    
    
                    R r1 = couponFeignService.saveSkuReduction(skuReductionTo);
                    if (r1.getCode() != 0) {
    
    
                        log.error("远程保存sku优惠信息失败");
                    }
                }

            });
        }

        //6、保存spu的积分信息;gulimall_sms->sms_spu_bounds
        // 6.1 获取积分信息
        Bounds bounds = vo.getBounds();
        // 6.2 信息处理
        SpuBoundTo spuBoundTo = new SpuBoundTo();
        BeanUtils.copyProperties(bounds, spuBoundTo);
        spuBoundTo.setSpuId(infoEntity.getId());

        R r = couponFeignService.saveSpuBounds(spuBoundTo);
        if (r.getCode() != 0) {
    
    
            log.error("远程保存spu积分信息失败");
        }
    }


(2) Call remote service to save discount and other information

productThe service is feigncalled . Create a new package in the service coupon服务
for remote invocation. Remote data transmission uses TO, and create a new to package under the service.productfeign
common

  1. Create a new CouponFeignServiceremote call coupon service
// 指定要调用的服务
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    
    

    @PostMapping("/coupon/spubounds/save")
    R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
}

  1. Remote call logic
    insert image description here

3. Commodity management


(1) SPU search


    @Override
    public PageUtils queryPageBycondition(Map<String, Object> params) {
    
    


        QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
    
    
            wrapper.and((w) -> {
    
    
                w.eq("id", key).or().like("spu_name", key);
            });
        }
        // status=1 and (id=1 or spu_name like xxx)
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
    
    
            wrapper.eq("publish_status", status);
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
    
    
            wrapper.eq("brand_id", brandId);
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
    
    
            wrapper.eq("catalog_id", catelogId);
        }

        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

Time formatting:

spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss


(2) sku search


    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
    
    

        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
    
    
            queryWrapper.and((wrapper) -> {
    
    
                wrapper.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
    
    

            queryWrapper.eq("catalog_id", catelogId);
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(catelogId)) {
    
    
            queryWrapper.eq("brand_id", brandId);
        }

        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)) {
    
    
            queryWrapper.ge("price", min);
        }

        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
    
    
            try {
    
    
                BigDecimal bigDecimal = new BigDecimal(max);

                if (bigDecimal.compareTo(new BigDecimal("0")) == 1) {
    
    
                    queryWrapper.le("price", max);
                }
            } catch (Exception e) {
    
    

            }

        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

4. Spu management (P100)


(1) Obtain spu specifications

  • That is, some basic attributes (specification parameters) were specified when the sku was created before the echo
  • pms_product_attr_valuedata in table
  • Request path:GET:/product/attr/base/listforspu/{spuId}

insert image description here


(2) Modify the spu specification

  • URL:/product/attr/update/{spuId}
@Transactional
    @Override
    public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
    
    
        // 1、删除这个spuid对应的原来的属性
        this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));

        // 2、提交修改后的属性
        List<ProductAttrValueEntity> collect = entities.stream().map(item -> {
    
    
            item.setSpuId(spuId);
            return item;
        }).collect(Collectors.toList());

        this.saveBatch(collect);

    }

Guess you like

Origin blog.csdn.net/ljn1046016768/article/details/124784653