• gulimall基础篇回顾Day-13


    前言

    本文继续记录B站谷粒商城项目视频 P87-92 的内容,做到知识点的梳理和总结的作用,接口文档地址:gulimall接口文档

    一、商品新增VO

    1.1 商品信息VO

    @Data
    public class SpuSaveVo {
        //商品名称
        private String spuName;
        //商品名称
        private String spuDescription;
        //选择分类
        private Long catalogId;
        //选择品牌
        private Long brandId;
        //商品重量
        private BigDecimal weight;
        //发布状态
        private int publishStatus;
        //商品介绍
        private List<String> decript;
        //商品图集
        private List<String> images;
        //设置积分
        private Bounds bounds;
        //基本信息(入网型号,上市年份,机身长度等信息)
        private List<BaseAttrs> baseAttrs;
        //sku信息
        private List<Skus> skus;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    @Data
    public class Skus {
        //基本属性
        private List<Attr> attr;
        //sku名称
        private String skuName;
        //价格
        private BigDecimal price;
        //标题
        private String skuTitle;
        //副标题
        private String skuSubtitle;
        //图集信息
        private List<Images> images;
        //笛卡尔(如星河银,8GB + 128GB)
        private List<String> descar;
        //满减信息
        private int fullCount;
        //打折
        private BigDecimal discount;
        //满件优惠,满多少件打多少折(1可以叠加,0不可以叠加)
        private int countStatus;
        //fullPrice reducePrice 满10000 减50
        private BigDecimal fullPrice;
        private BigDecimal reducePrice;
        //满减叠加优惠(1可以叠加,0不可以叠加)
        private int priceStatus;
        //会员价格信息
        private List<MemberPrice> memberPrice;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    @Data
    public class Bounds {
        //购物积分(金币)
        private BigDecimal buyBounds;
        //成长积分(成长值)
        private BigDecimal growBounds;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Data
    public class MemberPrice {
        //会员id
        private Long id;
        //会员名称(银牌会员,铜牌会员)
        private String name;
        //会员价格
        private BigDecimal price;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Data
    public class Images {
        //图片url
        private String imgUrl;
        //是否默认图片(0-不是 1-是)
        private int defaultImg;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    @Data
    public class BaseAttrs {
        //属性id
        private Long attrId;
        //属性值
        private String attrValues;
        //是否快速展示(0-否 1-是)
        private int showDesc;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    @Data
    public class Attr {
        private Long attrId;
        private String attrName;
        private String attrValue;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、保存spu与sku基本信息

    2.1 SpuInfoController 层

    /**
     * 接收界面发送的数据,保存商品发布信息
     */
    @RequestMapping("/save")
    public R save(@RequestBody SpuSaveVo vo){
        //spuInfoService.save(vo);
        spuInfoService.saveSpuInfo(vo);
        return R.ok();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2.2 SpuInfoService 层

    public interface SpuInfoService extends IService<SpuInfoEntity> {
    
        PageUtils queryPage(Map<String, Object> params);
    
        void saveSpuInfo(SpuSaveVo vo);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.3 SpuInfoServiceImpl 层

    @Service("spuInfoService")
    public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {
        @Autowired
        SpuInfoDescService spuInfoDescService;
    
        @Autowired
        SpuImagesService imagesService;
    
        @Autowired
        AttrService attrService;
    
        @Autowired
        ProductAttrValueService attrValueService;
    
        @Autowired
        SkuInfoService skuInfoService;
        @Autowired
        SkuImagesService skuImagesService;
    
        @Autowired
        SkuSaleAttrValueService skuSaleAttrValueService;
    
        @Autowired
        CouponFeignService couponFeignService;
        
    	@Transactional
    	@Override
    	public void saveSpuInfo(SpuSaveVo vo) {
    	
    	    //1、保存spu基本信息 pms_spu_info
    	    SpuInfoEntity vo = new SpuInfoEntity();
    	    //infoEntity属性字段与vo属性字段相同的可以进行属性对拷
    	    BeanUtils.copyProperties(vo,infoEntity);
    	    infoEntity.setCreateTime(new Date());
    	    infoEntity.setUpdateTime(new Date());
    	    //这个我觉得可以直接用this.save(infoEntity);
    	    this.saveBaseSpuInfo(infoEntity);
    	
    	    //2、保存Spu的描述图片 pms_spu_info_desc
    	    List<String> decript = vo.getDecript();
    	    SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
    	    descEntity.setSpuId(infoEntity.getId());
    	    descEntity.setDecript(String.join(",",decript));
    	    spuInfoDescService.saveSpuInfoDesc(descEntity);
    	
    	    //3、保存spu的图片集 pms_spu_images
    	    List<String> images = vo.getImages();
    	    imagesService.saveImages(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的积分信息;gulimall_sms->sms_spu_bounds
    	    Bounds bounds = vo.getBounds();
    	    SpuBoundTo spuBoundTo = new SpuBoundTo();
    	    BeanUtils.copyProperties(bounds,spuBoundTo);
    	    spuBoundTo.setSpuId(infoEntity.getId());
    	    R r = couponFeignService.saveSpuBounds(spuBoundTo);
    	    if(r.getCode() != 0){
    	        log.error("远程保存spu积分信息失败");
    	    }
    	
    	    //5、保存当前spu对应的所有sku信息;
    	    List<Skus> skus = vo.getSkus();
    	    if(skus!=null && skus.size()>0){
    	        skus.forEach(item->{
    	            String defaultImg = "";
    	            for (Images image : item.getImages()) {
    	                if(image.getDefaultImg() == 1){
    	                    defaultImg = image.getImgUrl();
    	                }
    	            }
    	            //    private String skuName;
    	            //    private BigDecimal price;
    	            //    private String skuTitle;
    	            //    private String skuSubtitle;
    	            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);
    	
    	            Long skuId = skuInfoEntity.getSkuId();
    	
    	            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 没有图片路径的无需保存
    	
    	            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优惠信息失败");
    	                }
    	            }
    	        });
    	    }
    	}
    }	
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139

    spuInfoService层,保存SpuInfoEntity实体类对象

     @Override
     public void saveBaseSpuInfo(SpuInfoEntity infoEntity) {
         this.baseMapper.insert(infoEntity);
     }
    
    • 1
    • 2
    • 3
    • 4

    spuInfoDescService层,保存SpuInfoDescEntity实体类对象

    @Override
    public void saveSpuInfoDesc(SpuInfoDescEntity descEntity) {
        this.baseMapper.insert(descEntity);
    }
    
    • 1
    • 2
    • 3
    • 4

    spuImagesService层,保存SpuImagesEntity实体类对象

    @Override
    public void saveImages(Long id, List<String> images) {
        if(images == null || images.size() == 0){
    
        }else{
            List<SpuImagesEntity> collect = images.stream().map(img -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(id);
                spuImagesEntity.setImgUrl(img);
    
                return spuImagesEntity;
            }).collect(Collectors.toList());
    
            this.saveBatch(collect);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    productAttrValueService层,保存ProductAttrValueEntity实体类对象

    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }
    
    • 1
    • 2
    • 3
    • 4

    skuInfoService层,保存SkuInfoEntity实体类对象

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }
    
    • 1
    • 2
    • 3
    • 4

    三、远程调用服务保存优惠等信息

    3.1 远程调用保证三点

    1.远程服务必须上线,保存在注册中心中

    #优惠卷服务yaml配置
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
      application:
        name: gulimall-coupon
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.远程服务必须开启服务注册和发现的功能(@EnableDiscoveryClient),别的服务才能调用

    @SpringBootApplication
    @EnableDiscoveryClient
    @MapperScan("com.atguigu.gulimall.coupon.dao")
    public class GulimallCouponApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GulimallCouponApplication.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.服务主动调用方也必须注册到注册中心中,开启远程调用功能

    @MapperScan("com.atguigu.gulimall.product.dao")
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableFeignClients(basePackages = "com.atguigu.gulimall.product.feign")
    public class GulimallProductApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(GulimallProductApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3.2 远程调用接口声明

    @FeignClient("gulimall-coupon")
    public interface CouponFeignService {
    
        @PostMapping("/coupon/spubounds/save")
        R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo);
    
        @PostMapping("/coupon/skufullreduction/saveinfo")
        R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.3 TO领域对象模型

    TO领域对象模型是用于远程服务方法接口其参数是对象,例如远程服务A调用远程服务B,A接口的参数是对象类型则可以将该对象声明为TO。

    @Data
    public class SpuBoundTo {
        private Long spuId;
        private BigDecimal buyBounds;
        private BigDecimal growBounds;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.3.1 远程gulimall-coupon服务,SpuBoundsController

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody SpuBoundsEntity spuBounds){
        spuBoundsDao.saveSpuBounds(spuBounds);
    
        return R.ok();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.4 满减Controller

    @PostMapping("/saveinfo")
    public R saveInfo(@RequestBody SkuReductionTo reductionTo){
        skuFullReductionService.saveSkuReduction(reductionTo);
        return R.ok();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    @Override
    public void saveSkuReduction(SkuReductionTo reductionTo) {
        //1、// //5.4)、sku的优惠、满减等信息;gulimall_sms->sms_sku_ladder\sms_sku_full_reduction\sms_member_price
        //sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(reductionTo.getSkuId());
        skuLadderEntity.setFullCount(reductionTo.getFullCount());
        skuLadderEntity.setDiscount(reductionTo.getDiscount());
        skuLadderEntity.setAddOther(reductionTo.getCountStatus());
        if(reductionTo.getFullCount() > 0){
            skuLadderService.save(skuLadderEntity);
        }
    
    
        //2、sms_sku_full_reduction
        SkuFullReductionEntity reductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(reductionTo,reductionEntity);
        if(reductionEntity.getFullPrice().compareTo(new BigDecimal("0"))==1){
            this.save(reductionEntity);
        }
    
    
        //3、sms_member_price
        List<MemberPrice> memberPrice = reductionTo.getMemberPrice();
    
        List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
            MemberPriceEntity priceEntity = new MemberPriceEntity();
            priceEntity.setSkuId(reductionTo.getSkuId());
            priceEntity.setMemberLevelId(item.getId());
            priceEntity.setMemberLevelName(item.getName());
            priceEntity.setMemberPrice(item.getPrice());
            priceEntity.setAddOther(1);
            return priceEntity;
        }).filter(item->{
            return item.getMemberPrice().compareTo(new BigDecimal("0")) == 1;
        }).collect(Collectors.toList());
    
        memberPriceService.saveBatch(collect);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
  • 相关阅读:
    脉冲发生器助力雷达系统开发的关键应用
    vSphere6.7创建centos7.9虚拟机及配置网卡和yum仓库
    算法笔记(三)基础提升
    阿里云云主机免费试用三个月
    GIS开发入坑(二)--ArcGIS影像切片并使用GeoServer发布
    第26章 并发:介绍
    Java —— 数组
    最新版:RadSystems Studio:Crack
    将ECharts图表插入到Word文档中
    【SpringBoot】配置文件.properties和.yml
  • 原文地址:https://blog.csdn.net/m0_49692893/article/details/127703109