【谷粒商城基础篇】商品服务:商品维护

在这里插入图片描述

谷粒商城笔记合集

分布式基础篇分布式高级篇高可用集群篇
===简介&环境搭建===
项目简介与分布式概念(第一、二章)
基础环境搭建(第三章)
===整合SpringCloud===
整合SpringCloud、SpringCloud alibaba(第四、五章)
===前端知识===
前端开发基础知识(第六章)
===商品服务开发===
商品服务开发:基础概念、三级分类(第七、八章)
商品服务开发:品牌管理(第九章)
商品服务开发:属性分组、平台属性(第十、十一章)
商品服务:商品维护(第十二、十三章)
===仓储服务开发===
仓储服务:仓库维护(第十四章)
基础篇总结(第十五章)

十二、商品服务&商品维护&发布商品⚠️

12.1 后端开发:会员服务⚠️

  • 网关服务 中配置会员服务的路由信息并重启:商品发布需要加载会员等级信息

    spring:
      cloud:
        gateway:
          routes:
            - id: member_route
              uri: lb://bilimall-member
              predicates:
                - Path=/api/member/**
              filters:
                - RewritePath=/api/?(?<segment>.*),/$\{segment}
    
  • 启动 会员服务商品发布需要加载会员等级信息

12.2 API:查询 分类下的可选品牌

发布商品的基本信息填写中,选择分类后需要选择分类下的品牌

在这里插入图片描述

  1. 创建响应数据的BrandVo:cn/lzwei/bilimall/product/vo/BrandVo.java

    @Data
    public class BrandVo {
        //品牌id
        private Long brandId;
        //品牌名
        private String brandName;
    }
    
  2. CategoryBrandRelationController:查询分类下的可供选择品牌,用于用户商品发布的基本信息填写

    @RestController
    @RequestMapping("product/categorybrandrelation")
    public class CategoryBrandRelationController {
        @Autowired
        private CategoryBrandRelationService categoryBrandRelationService;
    
        /**
         * 查询分类下的可供选择品牌,用于用户商品发布的基本信息填写
         */
        @RequestMapping("/brands/list")
        public R brandsList(@RequestParam(name = "catId") Long catId){
            List<CategoryBrandRelationEntity> brands= categoryBrandRelationService.getCategoryRelationBrands(catId);
            List<BrandVo> brandVos = brands.stream().map(item -> {
                BrandVo brandVo = new BrandVo();
                BeanUtils.copyProperties(item,brandVo);
                return brandVo;
            }).collect(Collectors.toList());
            return R.ok().put("data", brandVos);
        }
    }
    
  3. CategoryBrandRelationService:查询分类下的可供选择品牌,用于用户商品发布的基本信息填写

    public interface CategoryBrandRelationService extends IService<CategoryBrandRelationEntity> {
        /**
         * 查询分类下的可供选择品牌,用于用户商品发布的基本信息填写
         * @return
         */
        List<CategoryBrandRelationEntity> getCategoryRelationBrands(Long catId);
    }
    
  4. CategoryBrandRelationServiceImpl:查询分类下的可供选择品牌,用于用户商品发布的基本信息填写

    @Service("categoryBrandRelationService")
    public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
        /**
         * 查询分类下的可供选择品牌,用于用户商品发布的基本信息填写
         * @return
         */
        @Override
        public List<CategoryBrandRelationEntity> getCategoryRelationBrands(Long catId) {
            //通过分类id关联查询出品牌信息
            List<CategoryBrandRelationEntity> entities = this.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
            return entities;
        }
    }
    

12.3 API:查询 分类下的属性分组&属性

发布商品的基本信息填写后下一步设置规格参数,需要查询出分类下的属性分组及关联属性

在这里插入图片描述

  1. 创建响应数据的AttrGroupAttrVo:cn/lzwei/bilimall/product/vo/AttrGroupAttrVo.java

    @Data
    public class AttrGroupAttrVo {
        /**
         * 分组id
         */
        private Long attrGroupId;
        /**
         * 组名
         */
        private String attrGroupName;
        /**
         * 排序
         */
        private Integer sort;
        /**
         * 描述
         */
        private String descript;
        /**
         * 组图标
         */
        private String icon;
        /**
         * 所属分类id
         */
        private Long catelogId;
    
        /**
         * 属性分组下的所有基本属性
         */
        private List<AttrEntity> attrs;
    }
    
  2. AttrGroupController:查询出分类下的属性分组及关联属性

    @RestController
    @RequestMapping("product/attrgroup")
    public class AttrGroupController {
        @Autowired
        private AttrGroupService attrGroupService;
    
        /**
         * 获取分类下的属性分组以及每个分组下的基本属性:用于商品发布的规格参数设置
         */
        @GetMapping("/{catelogId}/withattr")
        public R getAttrGroupAndAttrsByCategory(@PathVariable(value = "catelogId") Long catelogId){
            List<AttrGroupAttrVo> attrGroupAttrVos=attrGroupService.getAttrGroupAndAttrsByCategory(catelogId);
            return R.ok().put("data",attrGroupAttrVos);
        }
    }
    
  3. AttrGroupService:查询出分类下的属性分组及关联属性

    public interface AttrGroupService extends IService<AttrGroupEntity> {
        /**
         * 获取分类下的属性分组以及每个分组下的基本属性:用于商品发布的规格参数设置
         */
        List<AttrGroupAttrVo> getAttrGroupAndAttrsByCategory(Long catelogId);
    }
    
  4. AttrGroupServiceImpl:查询出分类下的属性分组及关联属性

    @Service("attrGroupService")
    public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
        @Resource
        AttrService attrService;
    
        /**
         * 获取分类下的属性分组以及每个分组下的基本属性:用于商品发布的规格参数设置
         */
        @Override
        public List<AttrGroupAttrVo> getAttrGroupAndAttrsByCategory(Long catelogId) {
            //1.获取分类下的所有属性分组
            List<AttrGroupEntity> attrGroups = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
            //2.遍历分组集合,对每个分组下的所有基本属性进行封装
            List<AttrGroupAttrVo> collect = attrGroups.stream().map(item -> {
                AttrGroupAttrVo attrGroupAttrVo = new AttrGroupAttrVo();
                //2.1 将group的基本属性添加到vo中
                BeanUtils.copyProperties(item,attrGroupAttrVo);
                //2.2 设置vo中的attr信息
                List<AttrEntity> attrEntities = attrService.attrGroupRelation(attrGroupAttrVo.getAttrGroupId());
                attrGroupAttrVo.setAttrs(attrEntities);
                return attrGroupAttrVo;
            }).collect(Collectors.toList());
            return collect;
        }
    }
    

12.4 前后端联调:生成spu的VO类

  1. 获取sku相关的json数据

    在这里插入图片描述

  2. 利用json数据生成java代码

    在这里插入图片描述

  3. 解压生成的代码,将代码拷贝到 bilimall-product/src/main/java/cn/lzwei/bilimall/product/vo 中

    • SpuSaveVo.java:Spu

      @Data
      public class SpuSaveVo {
      
          private String spuName;
          private String spuDescription;
          private Long catalogId;
          private Long brandId;
          private BigDecimal weight;
          private Integer publishStatus;
          private List<String> decript;
          private List<String> images;
          private Bounds bounds;
          private List<BaseAttrs> baseAttrs;
          private List<Skus> skus;
      }
      
    • Bounds.java:积分

      @Data
      public class Bounds {
      
          private BigDecimal buyBounds;
          private BigDecimal growBounds;
      }
      
    • BaseAttrs.java:基础属性

      @Data
      public class BaseAttrs {
      
          private Long attrId;
          private String attrValues;
          private int showDesc;
      }
      
    • Skus.java:Sku

      @Data
      public class Skus {
      
          private List<Attr> attr;
          private String skuName;
          private BigDecimal price;
          private String skuTitle;
          private String skuSubtitle;
          private List<Images> images;
          private List<String> descar;
          private int fullCount;
          private BigDecimal discount;
          private int countStatus;
          private BigDecimal fullPrice;
          private BigDecimal reducePrice;
          private int priceStatus;
          private List<MemberPrice> memberPrice;
      }
      
    • MemberPrice.java:sku会员价格

      @Data
      public class MemberPrice {
      
          private Long id;
          private String name;
          private BigDecimal price;
      }
      
    • Images.java:sku图片

      @Data
      public class Images {
      
          private String imgUrl;
          private int defaultImg;
      }
      
    • Attr.java:销售属性

      @Data
      public class Attr {
      
          private Long attrId;
          private String attrName;
          private String attrValue;
      }
      

12.5 API:保存 商品信息⚠️

在这里插入图片描述

在这里插入图片描述

12.5.1 公共服务开发⚠️

  1. 新增远程调用TO:cn/lzwei/common/to/BoundsTo.java

    //积分信息
    @Data
    public class BoundsTo {
    
        private Long id;
    
        private Long spuId;
        /**
         * 成长积分
         */
        private BigDecimal growBounds;
        /**
         * 购物积分
         */
        private BigDecimal buyBounds;
        /**
         * 优惠生效情况[1111(四个状态位,从右到左);0 - 无优惠,成长积分是否赠送;1 - 无优惠,购物积分是否赠送;2 - 有优惠,成长积分是否赠送;3 - 有优惠,购物积分是否赠送【状态位0:不赠送,1:赠送】]
         */
        private Integer work;
    }
    
  2. 新增远程调用TO:cn/lzwei/common/to/FullReductionTo.java

    //打折、满减、会员优惠信息
    @Data
    public class FullReductionTo {
        private Long skuId;
        private int fullCount;
        private BigDecimal discount;
        private int countStatus;
        private BigDecimal fullPrice;
        private BigDecimal reducePrice;
        private int priceStatus;
        private List<MemberPrice> memberPrice;
    }
    
  3. 新增远程调用TO:cn/lzwei/common/to/MemberPrice.java

    //会员优惠信息
    @Data
    public class MemberPrice {
    
        private Long id;
        private String name;
        private BigDecimal price;
    }
    
  4. 修改响应包装类,添加响应码获取方法,用于远程调用判断:cn/lzwei/common/utils/R.java

    public class R extends HashMap<String, Object> {
        ...
    	public Integer getCode(){
    		return (Integer) get("code");
    	}
    }
    

12.5.2 优惠服务开发⚠️

  1. SpuBoundsController:修改API /bilimall-coupon/coupon/spubounds/save ,直接进行远程调用

    @RestController
    @RequestMapping("coupon/spubounds")
    public class SpuBoundsController {
        @Autowired
        private SpuBoundsService spuBoundsService;
        /**
         * 保存
         */
        @PostMapping("/save")
        public R save(@RequestBody SpuBoundsEntity spuBounds){
    		spuBoundsService.save(spuBounds);
            return R.ok();
        }
    }
    
  2. SkuFullReductionController:为新增商品保存优惠信息时远程调用提供专门的API

    @RestController
    @RequestMapping("coupon/skufullreduction")
    public class SkuFullReductionController {
        @Autowired
        private SkuFullReductionService skuFullReductionService;
    
        /**
         * 批量保存:保存打折、满减、会员优惠信息
         */
        @PostMapping("/saveTotal")
        public R saveTotal(@RequestBody FullReductionTo fullReductionTo){
            skuFullReductionService.saveTotal(fullReductionTo);
            return R.ok();
        }
    }
    
  3. SkuFullReductionService:为新增商品保存优惠信息时远程调用提供专门的API

    public interface SkuFullReductionService extends IService<SkuFullReductionEntity> {
        /**
         * 批量保存:保存打折、满减、会员优惠信息
         */
        void saveTotal(FullReductionTo fullReductionTo);
    }
    
  4. SkuFullReductionServiceImpl:为新增商品保存优惠信息时远程调用提供专门的API

    @Service("skuFullReductionService")
    public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
        @Resource
        SkuLadderService skuLadderService;
        @Resource
        SkuFullReductionService skuFullReductionService;
        @Resource
        MemberPriceService memberPriceService;
    
        /**
         * 批量保存:保存打折、满减、会员优惠信息
         */
        @Override
        @Transactional
        public void saveTotal(FullReductionTo fullReductionTo) {
            //保存打折、满减、会员优惠信息 (远程服务) sms_sku_ladder、sms_sku_full_reduction、sms_member_price
            //1.保存打折信息 sms_sku_ladder:    id  sku_id  full_count  discount  price   add_other
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(fullReductionTo,skuLadderEntity);
            skuLadderEntity.setAddOther(1);
            //存在打折优惠才保存
            if (skuLadderEntity.getFullCount()>0){
                skuLadderService.save(skuLadderEntity);
            }
            //2.保存满减信息 sms_sku_full_reduction:    id  sku_id  full_price  reduce_price  add_other
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(fullReductionTo,skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(1);
            //存在满减优惠才保存
            if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1){
                this.save(skuFullReductionEntity);
            }
            //3.保存会员优惠信息 sms_member_price:    id  sku_id  member_level_id  member_level_name  member_price  add_other
            List<MemberPrice> memberPrice = fullReductionTo.getMemberPrice();
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(fullReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);
             return memberPriceEntity;
            }).filter(item->{
                //存在会员优惠才保存
                return item.getMemberPrice().compareTo(new BigDecimal(0))==1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }
    

12.5.3 商品服务开发💡

  1. 进行远程调用相关相关配置:配置注册中心,开启服务注册发现功能,开启远程调用功能并指定远程调用接口所在包等…

  2. 创建远程调用接口:cn/lzwei/bilimall/product/feign/CouponFeignService.java

    @FeignClient(name = "bilimall-coupon")
    public interface CouponFeignService {
    
        //保存积分信息
        @PostMapping("coupon/spubounds/save")
        R saveBounds(@RequestBody BoundsTo boundsTo);
        //保存打折、满减、会员优惠信息
        @PostMapping("coupon/skufullreduction/saveTotal")
        R saveTotal(@RequestBody FullReductionTo fullReductionTo);
    }
    
  3. SpuInfoDescEntity:将spu描述信息的特殊id设置为非自增

    @Data
    @TableName("pms_spu_info_desc")
    public class SpuInfoDescEntity implements Serializable {
    	/**
    	 * 商品id
    	 */
    	@TableId(type = IdType.INPUT)
    	private Long spuId;
    }
    
  4. SpuInfoController:保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品

    @RestController
    @RequestMapping("product/spuinfo")
    public class SpuInfoController {
        @Autowired
        private SpuInfoService spuInfoService;
    
        /**
         * 保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品
         */
        @RequestMapping("/save")
        public R save(@RequestBody SpuSaveVo spuSaveVo){
            spuInfoService.saveSpuInfo(spuSaveVo);
            return R.ok();
        }
    }
    
  5. SpuInfoService:保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品

    public interface SpuInfoService extends IService<SpuInfoEntity> {
        /**
         * 保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品
         */
        void saveSpuInfo(SpuSaveVo spuSaveVo);
    }
    
  6. SpuInfoServiceImpl:保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品

    @Service("spuInfoService")
    public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
        @Resource
        SpuInfoDescService spuInfoDescService;
        @Resource
        SpuImagesService spuImagesService;
        @Resource
        ProductAttrValueService productAttrValueService;
        @Resource
        AttrService attrService;
        @Resource
        SkuInfoService skuInfoService;
        @Resource
        SkuSaleAttrValueService skuSaleAttrValueService;
        @Resource
        SkuImagesService skuImagesService;
        @Resource
        CouponFeignService couponFeignService;
    
        /**
         * TODO 高级部分完善
         * 保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品
         */
        @Transactional
        @Override
        public void saveSpuInfo(SpuSaveVo spuSaveVo) {
            //1.保存spu基本信息 pms_spu_info:id  spu_name  spu_description  catalog_id  brand_id  weight  publish_status  create_time  update_time
            SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
            BeanUtils.copyProperties(spuSaveVo,spuInfoEntity);
            spuInfoEntity.setCreateTime(new Date());
            spuInfoEntity.setUpdateTime(new Date());
            this.save(spuInfoEntity);
            Long spuId = spuInfoEntity.getId();
    
            //2.保存spu介绍 pms_spu_info_desc: spu_id  decript
            SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
            spuInfoDescEntity.setSpuId(spuId);
            List<String> decript = spuSaveVo.getDecript();
            spuInfoDescEntity.setDecript(String.join(",",decript));
            spuInfoDescService.save(spuInfoDescEntity);
    
            //3.保存spu图集 pms_spu_images:  id  spu_id  img_name  img_url  img_sort  default_img
            List<String> images = spuSaveVo.getImages();
            List<SpuImagesEntity> spuImagesEntities = images.stream().map(item -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuId);
                spuImagesEntity.setImgUrl(item);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            spuImagesService.saveBatch(spuImagesEntities);
    
            //4.保存积分信息 (远程服务)sms_spu_bounds: id  spu_id  grow_bounds  buy_bounds    work
            Bounds bounds = spuSaveVo.getBounds();
            BoundsTo boundsTo = new BoundsTo();
            BeanUtils.copyProperties(bounds,boundsTo);
            boundsTo.setSpuId(spuId);
            R r = couponFeignService.saveBounds(boundsTo);
            if (r.getCode()!=0){
                log.error("远程调用失败!");
            }
    
            //5.保存规格参数 pms_product_attr_value:    id  spu_id  attr_id  attr_name  attr_value  attr_sort  quick_show
            List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
            List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream().map(item -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(spuId);
                productAttrValueEntity.setAttrId(item.getAttrId());
                AttrEntity attrEntity = attrService.getById(item.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setAttrValue(item.getAttrValues());
                productAttrValueEntity.setQuickShow(item.getShowDesc());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            productAttrValueService.saveBatch(productAttrValueEntities);
    
            //6.sku信息:
            List<Skus> skus = spuSaveVo.getSkus();
            for (Skus sku : skus) {
                //6.1、保存sku基本信息 pms_sku_info:sku_id  spu_id  sku_name  sku_desc  catalog_id  brand_id  sku_default_img  sku_title  sku_subtitle  price   sale_count
                String defaultImg="";
                for (Images image : sku.getImages()) {
                    if (image.getDefaultImg()==1) defaultImg=image.getImgUrl();
                }
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(sku,skuInfoEntity);
                skuInfoEntity.setSpuId(spuId);
                List<String> descar = sku.getDescar();
                skuInfoEntity.setSkuDesc(String.join(",",descar));
                skuInfoEntity.setCatalogId(spuSaveVo.getCatalogId());
                skuInfoEntity.setBrandId(spuSaveVo.getBrandId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                skuInfoEntity.setSaleCount(0l);
                skuInfoService.save(skuInfoEntity);
                Long skuId = skuInfoEntity.getSkuId();
    
                //6.2、保存sku销售属性 pms_sku_sale_attr_value:   id  sku_id  attr_id  attr_name  attr_value  attr_sort
                List<Attr> attrs = sku.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attrs.stream().map(item -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                    skuSaleAttrValueEntity.setSkuId(skuId);
                    BeanUtils.copyProperties(item,skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setAttrSort(0);
                    return skuSaleAttrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
    
                //6.3、保存sku图集 pms_sku_images:    id  sku_id  img_url  img_sort  default_img
                List<Images> imgs = sku.getImages();
                List<SkuImagesEntity> skuImagesEntities = imgs.stream().map(item -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    BeanUtils.copyProperties(item,skuImagesEntity);
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgSort(0);
                    return skuImagesEntity;
                }).filter(item->{
                    //过滤空图片
                    return !StringUtils.isNullOrEmpty(item.getImgUrl());
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(skuImagesEntities);
    
                //6.4、保存打折、满减、会员优惠信息 (远程服务) sms_sku_ladder、sms_sku_full_reduction、sms_member_price
                FullReductionTo fullReductionTo = new FullReductionTo();
                fullReductionTo.setSkuId(skuId);
                BeanUtils.copyProperties(sku,fullReductionTo);
                //如果存在打折优惠或者满减优惠再进行保存
                if(sku.getFullCount()>0 || sku.getFullPrice().compareTo(new BigDecimal(0))==1){
                    R r1 = couponFeignService.saveTotal(fullReductionTo);
                    if(r1.getCode()!=0){
                        log.error("远程调用失败!");
                    }
             }
            }
        }
    }
    

12.5.4 Debug优化 商品信息数据插入异常

由于 SpuInfoDescEntity 的主键字段特殊(pms_spu_info_desc: spu_id decript),需要设置为非自增:否则mybatis插入数据时没有添加主键导致抛异常

@Data
@TableName("pms_spu_info_desc")
public class SpuInfoDescEntity implements Serializable {
	/**
	 * 商品id
	 */
	@TableId(type = IdType.INPUT)
	private Long spuId;
}

在这里插入图片描述

12.5.5 优化 过滤无用数据

在这里插入图片描述

  • SpuInfoServiceImpl

    @Service("skuFullReductionService")
    public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
        @Resource
        SkuLadderService skuLadderService;
        @Resource
        SkuFullReductionService skuFullReductionService;
        @Resource
        MemberPriceService memberPriceService;
    
        /**
         * 批量保存:保存打折、满减、会员优惠信息
         */
        @Override
        @Transactional
        public void saveTotal(FullReductionTo fullReductionTo) {
            //保存打折、满减、会员优惠信息 (远程服务) sms_sku_ladder、sms_sku_full_reduction、sms_member_price
            //1.保存打折信息 sms_sku_ladder:    id  sku_id  full_count  discount  price   add_other
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(fullReductionTo,skuLadderEntity);
            skuLadderEntity.setAddOther(1);
            //存在打折优惠才保存
            if (skuLadderEntity.getFullCount()>0){
                skuLadderService.save(skuLadderEntity);
            }
            //2.保存满减信息 sms_sku_full_reduction:    id  sku_id  full_price  reduce_price  add_other
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(fullReductionTo,skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(1);
            //存在满减优惠才保存
            if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0))==1){
                this.save(skuFullReductionEntity);
            }
            //3.保存会员优惠信息 sms_member_price:    id  sku_id  member_level_id  member_level_name  member_price  add_other
            List<MemberPrice> memberPrice = fullReductionTo.getMemberPrice();
            List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(fullReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(item->{
                //存在会员优惠才保存
                return item.getMemberPrice().compareTo(new BigDecimal(0))==1;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(memberPriceEntities);
        }
    }
    
  • SkuFullReductionServiceImpl

    @Service("spuInfoService")
    public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
        @Resource
        SpuInfoDescService spuInfoDescService;
        @Resource
        SpuImagesService spuImagesService;
        @Resource
        ProductAttrValueService productAttrValueService;
        @Resource
        AttrService attrService;
        @Resource
        SkuInfoService skuInfoService;
        @Resource
        SkuSaleAttrValueService skuSaleAttrValueService;
        @Resource
        SkuImagesService skuImagesService;
        @Resource
        CouponFeignService couponFeignService;
    
        /**
         * TODO 高级部分完善
         * 保存spu、sku信息,跨服务保存打折、满减、会员优惠信息:新增商品
         */
        @Transactional
        @Override
        public void saveSpuInfo(SpuSaveVo spuSaveVo) {
            //1.保存spu基本信息 pms_spu_info:id  spu_name  spu_description  catalog_id  brand_id  weight  publish_status  create_time  update_time
            SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
            BeanUtils.copyProperties(spuSaveVo,spuInfoEntity);
            spuInfoEntity.setCreateTime(new Date());
            spuInfoEntity.setUpdateTime(new Date());
            this.save(spuInfoEntity);
            Long spuId = spuInfoEntity.getId();
    
            //2.保存spu介绍 pms_spu_info_desc: spu_id  decript
            SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
            spuInfoDescEntity.setSpuId(spuId);
            List<String> decript = spuSaveVo.getDecript();
            spuInfoDescEntity.setDecript(String.join(",",decript));
            spuInfoDescService.save(spuInfoDescEntity);
    
            //3.保存spu图集 pms_spu_images:  id  spu_id  img_name  img_url  img_sort  default_img
            List<String> images = spuSaveVo.getImages();
            List<SpuImagesEntity> spuImagesEntities = images.stream().map(item -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuId);
                spuImagesEntity.setImgUrl(item);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            spuImagesService.saveBatch(spuImagesEntities);
    
            //4.保存积分信息 (远程服务)sms_spu_bounds: id  spu_id  grow_bounds  buy_bounds    work
            Bounds bounds = spuSaveVo.getBounds();
            BoundsTo boundsTo = new BoundsTo();
            BeanUtils.copyProperties(bounds,boundsTo);
            boundsTo.setSpuId(spuId);
            R r = couponFeignService.saveBounds(boundsTo);
            if (r.getCode()!=0){
                log.error("远程调用失败!");
            }
    
            //5.保存规格参数 pms_product_attr_value:    id  spu_id  attr_id  attr_name  attr_value  attr_sort  quick_show
            List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs();
            List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream().map(item -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(spuId);
                productAttrValueEntity.setAttrId(item.getAttrId());
                AttrEntity attrEntity = attrService.getById(item.getAttrId());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                productAttrValueEntity.setAttrValue(item.getAttrValues());
                productAttrValueEntity.setQuickShow(item.getShowDesc());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            productAttrValueService.saveBatch(productAttrValueEntities);
    
            //6.sku信息:
            List<Skus> skus = spuSaveVo.getSkus();
            for (Skus sku : skus) {
                //6.1、保存sku基本信息 pms_sku_info:sku_id  spu_id  sku_name  sku_desc  catalog_id  brand_id  sku_default_img  sku_title  sku_subtitle  price   sale_count
                String defaultImg="";
                for (Images image : sku.getImages()) {
                    if (image.getDefaultImg()==1) defaultImg=image.getImgUrl();
                }
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                BeanUtils.copyProperties(sku,skuInfoEntity);
                skuInfoEntity.setSpuId(spuId);
                List<String> descar = sku.getDescar();
                skuInfoEntity.setSkuDesc(String.join(",",descar));
                skuInfoEntity.setCatalogId(spuSaveVo.getCatalogId());
                skuInfoEntity.setBrandId(spuSaveVo.getBrandId());
                skuInfoEntity.setSkuDefaultImg(defaultImg);
                skuInfoEntity.setSaleCount(0l);
                skuInfoService.save(skuInfoEntity);
                Long skuId = skuInfoEntity.getSkuId();
    
                //6.2、保存sku销售属性 pms_sku_sale_attr_value:   id  sku_id  attr_id  attr_name  attr_value  attr_sort
                List<Attr> attrs = sku.getAttr();
                List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attrs.stream().map(item -> {
                    SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                    skuSaleAttrValueEntity.setSkuId(skuId);
                    BeanUtils.copyProperties(item,skuSaleAttrValueEntity);
                    skuSaleAttrValueEntity.setAttrSort(0);
                    return skuSaleAttrValueEntity;
                }).collect(Collectors.toList());
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
    
                //6.3、保存sku图集 pms_sku_images:    id  sku_id  img_url  img_sort  default_img
                List<Images> imgs = sku.getImages();
                List<SkuImagesEntity> skuImagesEntities = imgs.stream().map(item -> {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    BeanUtils.copyProperties(item,skuImagesEntity);
                    skuImagesEntity.setSkuId(skuId);
                    skuImagesEntity.setImgSort(0);
                    return skuImagesEntity;
                }).filter(item->{
                    //过滤空图片
                    return !StringUtils.isNullOrEmpty(item.getImgUrl());
                }).collect(Collectors.toList());
                skuImagesService.saveBatch(skuImagesEntities);
    
                //6.4、保存打折、满减、会员优惠信息 (远程服务) sms_sku_ladder、sms_sku_full_reduction、sms_member_price
                FullReductionTo fullReductionTo = new FullReductionTo();
                fullReductionTo.setSkuId(skuId);
                BeanUtils.copyProperties(sku,fullReductionTo);
                //如果存在打折优惠或者满减优惠再进行保存
                if(sku.getFullCount()>0 || sku.getFullPrice().compareTo(new BigDecimal(0))==1){
                    R r1 = couponFeignService.saveTotal(fullReductionTo);
                    if(r1.getCode()!=0){
                        log.error("远程调用失败!");
                    }
                }
            }
        }
    }
    

十三、商品服务&商品维护&管理

13.1 API:SPU检索

在这里插入图片描述

  1. 配置时间格式化规则:application.yaml

    spring:
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
    
  2. SpuInfoController:spu检索:模糊查询、分类、品牌、状态

    @RestController
    @RequestMapping("product/spuinfo")
    public class SpuInfoController {
        @Autowired
        private SpuInfoService spuInfoService;
        /**
         * spu检索列表:模糊查询、分类、品牌、状态
         */
        @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
            PageUtils page = spuInfoService.queryPageByCondition(params);
    
            return R.ok().put("page", page);
        }
    }
    
  3. SpuInfoService:spu检索:模糊查询、分类、品牌、状态

    public interface SpuInfoService extends IService<SpuInfoEntity> {
        /**
         * spu检索列表:模糊查询、分类、品牌、状态
         */
        PageUtils queryPageByCondition(Map<String, Object> params);
    }
    
  4. SpuInfoServiceImpl:spu检索:模糊查询、分类、品牌、状态

    @Service("spuInfoService")
    public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
        /**
         * spu检索列表:模糊查询、分类、品牌、状态
         */
        @Override
        public PageUtils queryPageByCondition(Map<String, Object> params) {
            QueryWrapper<SpuInfoEntity> queryWrapper = new QueryWrapper<>();
            //1.状态
            String status = (String) params.get("status");
            if (!StringUtils.isNullOrEmpty(status)){
                queryWrapper.eq("publish_status",status);
            }
            //2.模糊查询
            String key = (String) params.get("key");
            if (!StringUtils.isNullOrEmpty(key)){
                queryWrapper.and(item->{
                    item.eq("id",key).or().like("spu_name",key);
                });
            }
            //3.品牌
            String brandId = (String) params.get("brandId");
            if (!StringUtils.isNullOrEmpty(brandId) && !"0".equals(brandId)){
                queryWrapper.eq("brand_id",brandId);
            }
            //4.分类
            String catelogId = (String) params.get("catelogId");
            if (!StringUtils.isNullOrEmpty(catelogId) && !"0".equals(catelogId)){
                queryWrapper.eq("catalog_id",catelogId);
            }
            IPage<SpuInfoEntity> page = this.page(
                    new Query<SpuInfoEntity>().getPage(params),
                    queryWrapper
            );
    
            return new PageUtils(page);
        }
    
    }
    

13.2 API:SKU检索

在这里插入图片描述

  1. SkuInfoController:检索sku:模糊查询、分类、品牌、价格区间

    @RestController
    @RequestMapping("product/skuinfo")
    public class SkuInfoController {
        @Autowired
        private SkuInfoService skuInfoService;
    
        /**
         * 检索sku:模糊查询、分类、品牌、价格区间
         */
        @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params){
            PageUtils page = skuInfoService.queryPageByCondition(params);
    
            return R.ok().put("page", page);
        }
    }
    
  2. SkuInfoService:检索sku:模糊查询、分类、品牌、价格区间

    public interface SkuInfoService extends IService<SkuInfoEntity> {
        /**
         * 检索sku:模糊查询、分类、品牌、价格区间
         */
        PageUtils queryPageByCondition(Map<String, Object> params);
    }
    
  3. SkuInfoServiceImpl:检索sku:模糊查询、分类、品牌、价格区间

    @Service("skuInfoService")
    public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
        /**
         * 检索sku:模糊查询、分类、品牌、价格区间
         */
        @Override
        public PageUtils queryPageByCondition(Map<String, Object> params) {
            QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();
            //1.模糊查询
            String key = (String) params.get("key");
            if(!StringUtils.isNullOrEmpty(key)){
                queryWrapper.and(item->{
                    item.eq("sku_id",key).or().like("sku_name",key);
                });
            }
            //2.分类
            String catelogId = (String) params.get("catelogId");
            if(!StringUtils.isNullOrEmpty(catelogId) && !"0".equals(catelogId)){
                queryWrapper.eq("catalog_id",catelogId);
            }
            //3.品牌
            String brandId = (String) params.get("brandId");
            if(!StringUtils.isNullOrEmpty(brandId) && !"0".equals(brandId)){
                queryWrapper.eq("brand_id",brandId);
            }
            //4.价格区间
            String min = (String) params.get("min");
            if(!StringUtils.isNullOrEmpty(min)){
                queryWrapper.ge("price",min);
            }
            String max = (String) params.get("max");
            if(!StringUtils.isNullOrEmpty(max)){
                int value = new BigDecimal(max).compareTo(BigDecimal.valueOf(0));
                if (value>0){
                    queryWrapper.le("price",max);
                }
            }
            IPage<SkuInfoEntity> page = this.page(
                    new Query<SkuInfoEntity>().getPage(params),
                    queryWrapper
            );
    
            return new PageUtils(page);
        }
    }
    

13.3 API:获取spu规格

在这里插入图片描述

  • AttrController:获取spu规格

    @RestController
    @RequestMapping("product/attr")
    public class AttrController {
        @Resource
        private ProductAttrValueService productAttrValueService;
    
        /**
         * 获取spu规格
         */
        @GetMapping("/base/listforspu/{spuId}")
        public R listForSpuAttr(@PathVariable(value = "spuId") Long spuId){
            QueryWrapper<ProductAttrValueEntity> queryWrapper = new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId);
            List<ProductAttrValueEntity> entityList=productAttrValueService.list(queryWrapper);
            return R.ok().put("data",entityList);
        }
    }
    

13.4 API:修改spu规格

在这里插入图片描述

  1. AttrController:修改商品规格

    @RestController
    @RequestMapping("product/attr")
    public class AttrController {
        @Resource
        private ProductAttrValueService productAttrValueService;
    
        /**
         * 商品管理:修改商品规格
         */
        @PostMapping("/update/{spuId}")
        public R updateBySpu(@RequestBody List<ProductAttrValueEntity> productAttrValue,@PathVariable Long spuId){
            productAttrValueService.updateBySpu(productAttrValue,spuId);
    
            return R.ok();
        }
    }
    
  2. ProductAttrValueService:修改商品规格

    public interface ProductAttrValueService extends IService<ProductAttrValueEntity> {
        /**
         * 商品管理:修改商品规格
         */
        void updateBySpu(List<ProductAttrValueEntity> productAttrValue, Long spuId);
    }
    
  3. ProductAttrValueServiceImpl:修改商品规格

    @Service("productAttrValueService")
    public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {
        /**
         * 商品管理:修改商品规格
         */
        @Transactional
        @Override
        public void updateBySpu(List<ProductAttrValueEntity> productAttrValue, Long spuId) {
            //1.删除该商品的所有规格参数
            UpdateWrapper<ProductAttrValueEntity> wrapper = new UpdateWrapper<ProductAttrValueEntity>().eq("spu_id",spuId);
            this.remove(wrapper);
            //2.将传过来的规格重新插入
            List<ProductAttrValueEntity> collect = productAttrValue.stream().map(item -> {
                item.setSpuId(spuId);
                return item;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    
    }
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
gulimall_pms 商品 drop table if exists pms_attr; drop table if exists pms_attr_attrgroup_relation; drop table if exists pms_attr_group; drop table if exists pms_brand; drop table if exists pms_category; drop table if exists pms_category_brand_relation; drop table if exists pms_comment_replay; drop table if exists pms_product_attr_value; drop table if exists pms_sku_images; drop table if exists pms_sku_info; drop table if exists pms_sku_sale_attr_value; drop table if exists pms_spu_comment; drop table if exists pms_spu_images; drop table if exists pms_spu_info; drop table if exists pms_spu_info_desc; /*==============================================================*/ /* Table: pms_attr */ /*==============================================================*/ create table pms_attr ( attr_id bigint not null auto_increment comment '属性id', attr_name char(30) comment '属性名', search_type tinyint comment '是否需要检索[0-不需要,1-需要]', icon varchar(255) comment '属性图标', value_select char(255) comment '可选值列表[用逗号分隔]', attr_type tinyint comment '属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]', enable bigint comment '启用状态[0 - 禁用,1 - 启用]', catelog_id bigint comment '所属分类', show_desc tinyint comment '快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整', primary key (attr_id) ); alter table pms_attr comment '商品属性'; /*==============================================================*/ /* Table: pms_attr_attrgroup_relation */ /*==============================================================*/ create table pms_attr_attrgroup_relation ( id bigint not null auto_increment comment 'id', attr_id bigint comment '属性id', attr_group_id bigint comment '属性分组id', attr_sort int comment '属性组内排序', primary key (id) ); alter table pms_attr_attrgroup_relation comment '属性&

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

愿你满腹经纶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值