Distributed e-commerce project 44: release commodity function - save commodity information

Publish product function - save product information

This chapter compiles the interface for saving commodity information. At the end of publishing commodity function, commodity information needs to be saved, corresponding to the front-end interface:

Corresponding interface document:

19,New products
POST
/product/spuinfo/save
//Request parameters
{
	"spuName": "Apple XR",
	"spuDescription": "Apple XR",
	"catalogId": 225,
	"brandId": 12,
	"weight": 0.048,
	"publishStatus": 0,
	"decript": ["https://gulimall-hello.oss-cn-beijing.aliyuncs.com/2019-11-22//66d30b3f-e02f-48b1-8574-e18fdf454a32_f205d9c99a2b4b01.jpg"],
	"images": ["https://gulimall-hello.oss-cn-beijing.aliyuncs.com/2019-11-22//dcfcaec3-06d8-459b-8759-dbefc247845e_5b5e74d0978360a1.jpg", "https://gulimall-hello.oss-cn-beijing.aliyuncs.com/2019-11-22//5b15e90a-a161-44ff-8e1c-9e2e09929803_749d8efdff062fb0.jpg"],
	"bounds": {
		"buyBounds": 500,
		"growBounds": 6000
	},
	"baseAttrs": [{
		"attrId": 7,
		"attrValues": "aaa;bb",
		"showDesc": 1
	}, {
		"attrId": 8,
		"attrValues": "2019",
		"showDesc": 0
	}],
	"skus": [{
		"attr": [{
			"attrId": 9,
			"attrName": "colour",
			"attrValue": "black"
		}, {
			"attrId": 10,
			"attrName": "Memory",
			"attrValue": "6GB"
		}],
		"skuName": "Apple XR Black 6 GB",
		"price": "1999",
		"skuTitle": "Apple XR Black 6 GB",
		"skuSubtitle": "Apple XR Black 6 GB",
		"images": [{
			"imgUrl": "https://gulimall-hello.oss-cn-beijing.aliyuncs.com/2019-11-22//dcfcaec3-06d8-459b-8759-dbefc247845e_5b5e74d0978360a1.jpg",
			"defaultImg": 1
			}, {
			"imgUrl": "https://gulimall-hello.oss-cn-beijing.aliyuncs.com/2019-11-22//5b15e90a-a161-44ff-8e1c-9e2e09929803_749d8efdff062fb0.jpg",
			"defaultImg": 0
		}],
		"descar": ["black", "6GB"],
		"fullCount": 5,
		"discount": 0.98,
		"countStatus": 1,
		"fullPrice": 1000,
		"reducePrice": 10,
		"priceStatus": 0,
		"memberPrice": [{
			"id": 1,
			"name": "aaa",
			"price": 1998.99
		}]
		}, {
		"attr": [{
			"attrId": 9,
			"attrName": "colour",
			"attrValue": "black"
		}, {
			"attrId": 10,
			"attrName": "Memory",
			"attrValue": "12GB"
		}],
		"skuName": "Apple XR Black 12 GB",
		"price": "2999",
		"skuTitle": "Apple XR Black 12 GB",
		"skuSubtitle": "Apple XR Black 6 GB",
		"images": [{
			"imgUrl": "",
			"defaultImg": 0
		}, {
			"imgUrl": "",
			"defaultImg": 0
		}],
		"descar": ["black", "12GB"],
		"fullCount": 0,
		"discount": 0,
		"countStatus": 0,
		"fullPrice": 0,
		"reducePrice": 0,
		"priceStatus": 0,
		"memberPrice": [{
			"id": 1,
			"name": "aaa",
			"price": 1998.99
		}]
	}, {
		"attr": [{
			"attrId": 9,
			"attrName": "colour",
			"attrValue": "white"
		}, {
			"attrId": 10,
			"attrName": "Memory",
			"attrValue": "6GB"
		}],
		"skuName": "Apple XR White 6 GB",
		"price": "1998",
		"skuTitle": "Apple XR White 6 GB",
		"skuSubtitle": "Apple XR Black 6 GB",
		"images": [{
			"imgUrl": "",
			"defaultImg": 0
		}, {
			"imgUrl": "",
			"defaultImg": 0
		}],
		"descar": ["white", "6GB"],
		"fullCount": 0,
		"discount": 0,
		"countStatus": 0,
		"fullPrice": 0,
		"reducePrice": 0,
		"priceStatus": 0,
		"memberPrice": [{
			"id": 1,
			"name": "aaa",
			"price": 1998.99
		}]
	}, {
		"attr": [{
			"attrId": 9,
			"attrName": "colour",
			"attrValue": "white"
		}, {
			"attrId": 10,
			"attrName": "Memory",
			"attrValue": "12GB"
		}],
		"skuName": "Apple XR White 12 GB",
		"price": "2998",
		"skuTitle": "Apple XR White 12 GB",
		"skuSubtitle": "Apple XR Black 6 GB",
		"images": [{
			"imgUrl": "",
			"defaultImg": 0
		}, {
			"imgUrl": "",
			"defaultImg": 0
		}],
		"descar": ["white", "12GB"],
		"fullCount": 0,
		"discount": 0,
		"countStatus": 0,
		"fullPrice": 0,
		"reducePrice": 0,
		"priceStatus": 0,
		"memberPrice": [{
			"id": 1,
			"name": "aaa",
			"price": 1998.99
		}]
	}]
}
//Paging data

//Response data
{
	"msg": "success",
	"code": 0
}

Because sku of product information can have many combinations, the saved product information is a long list.
It will be troublesome to write VO manually to accept these information, so it is recommended to use json format web site , convert the requested JSON file into a java entity class:
Copy the json file requested by the front end:

After the package name and class name are filled in, the Java entity class can be produced. Then download the code and put it into the VO package:

Code format after import:

/**
  * Copyright 2020 bejson.com 
  */
package com.lastingwar.mall.product.vo;
import java.util.List;

/**
 * Auto-generated: 2020-06-07 14:13:14
 *
 * @author bejson.com (i@bejson.com)
 * @website http://www.bejson.com/java2pojo/
 */
public class SpuSaveVo {

    private String spuName;
    private String spuDescription;
    private int catalogId;
    private int brandId;
    private int weight;
    private int publishStatus;
    private List<String> decript;
    private List<String> images;
    private Bounds bounds;
    private List<BaseAttrs> baseAttrs;
    private List<Skus> skus;
    public void setSpuName(String spuName) {
         this.spuName = spuName;
     }
     public String getSpuName() {
         return spuName;
     }

    public void setSpuDescription(String spuDescription) {
         this.spuDescription = spuDescription;
     }
     public String getSpuDescription() {
         return spuDescription;
     }

    public void setCatalogId(int catalogId) {
         this.catalogId = catalogId;
     }
     public int getCatalogId() {
         return catalogId;
     }

    public void setBrandId(int brandId) {
         this.brandId = brandId;
     }
     public int getBrandId() {
         return brandId;
     }

    public void setWeight(int weight) {
         this.weight = weight;
     }
     public int getWeight() {
         return weight;
     }

    public void setPublishStatus(int publishStatus) {
         this.publishStatus = publishStatus;
     }
     public int getPublishStatus() {
         return publishStatus;
     }

    public void setDecript(List<String> decript) {
         this.decript = decript;
     }
     public List<String> getDecript() {
         return decript;
     }

    public void setImages(List<String> images) {
         this.images = images;
     }
     public List<String> getImages() {
         return images;
     }

    public void setBounds(Bounds bounds) {
         this.bounds = bounds;
     }
     public Bounds getBounds() {
         return bounds;
     }

    public void setBaseAttrs(List<BaseAttrs> baseAttrs) {
         this.baseAttrs = baseAttrs;
     }
     public List<BaseAttrs> getBaseAttrs() {
         return baseAttrs;
     }

    public void setSkus(List<Skus> skus) {
         this.skus = skus;
     }
     public List<Skus> getSkus() {
         return skus;
     }

}

Note that the attributes in this list are the most commonly used by default, which do not meet our requirements. We need to change the ID field to long type, and the one with decimal to BigDecimal to avoid the loss of precision.

Then modify the controller response according to the request address, and change the original / save response to save SpuSaveVo class:

    /**
     * Store product information and carry a lot of information
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo vo){
        //spuInfoService.save(spuInfo);

        spuInfoService.saveSpuInfo(vo);

        return R.ok();
    }

After that, create a method to implement the saveSpuInfo method. Note that this method needs to save a lot of content, so add a transaction annotation to the method:
**First, save the basic information of SPU, and * * corresponds to the database PMS_ spu_ The info table does not have the saveBaseSpuInfo method. It needs to be created and written. Because the content of this method corresponds to the entity class, you can directly use save to:

    @Transactional
    @Override
    public void saveSpuInfo(SpuSaveVo vo) {

        //1. Save basic information of SPU pms_spu_info
        SpuInfoEntity infoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(vo,infoEntity);
        infoEntity.setCreateTime(new Date());
        infoEntity.setUpdateTime(new Date());
        this.saveBaseSpuInfo(infoEntity);

Create and implement method saveBaseSpuInfo:

    @Override
    public void saveBaseSpuInfo(SpuInfoEntity infoEntity) {
        this.baseMapper.insert(infoEntity);
    }

Then save the description picture PMS of Spu_ Spu_ info_ desc

        //2. Save the description picture PMS of Spu_ Spu_ info_ desc
        List<String> decript = vo.getDecript();
        SpuInfoDescEntity descEntity = new SpuInfoDescEntity();
        descEntity.setSpuId(infoEntity.getId());
        descEntity.setDecript(String.join(",", decript));
        spuInfoDescService.saveSpuInfoDesc(descEntity);

You need to create a method to implement saveSpuInfoDesc:

    @Override
    public void saveSpuInfoDesc(SpuInfoDescEntity descEntity) {
        this.baseMapper.insert(descEntity);
    }

Then save the picture set PMS of SPU_ spu_ images

        //3. Save picture set PMS of SPU_ spu_ images
        List<String> images = vo.getImages();
        imagesService.saveImages(infoEntity.getId(), images);

Create and implement method saveImages:

    @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);
        }
    }

Then save the specification parameters of spu; pms_product_attr_value

        //4. Save the specification parameters of 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);


Create the implementation method saveProductAttr:

    @Override
    public void saveProductAttr(List<ProductAttrValueEntity> collect) {
        this.saveBatch(collect);
    }

This step saves all sku information corresponding to the current spu

  //5. Save all sku information corresponding to the current spu;
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) basic information of SKU_ 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 -> {
                    //Return true is required, false is eliminated
                    return !StringUtils.isEmpty(entity.getImgUrl());
                }).collect(Collectors.toList());
                //5.2) picture information of SKU_ sku_ image
                skuImagesService.saveBatch(imagesEntities);
                //TODO does not need to save if there is no picture path

                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) sales attribute information of SKU: pms_sku_sale_attr_value
                skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);
                
                // //5.4) discount and full reduction of SKU_ sms->sms_ sku_ ladder\sms_ sku_ full_ reduction\sms_ member_ price
                // Cross library save required
                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("Remote save sku Offer information failed");
                    }
                }

            });

Later implementation method: saveSkuInfo:

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

Information of SKU's discount and full reduction: gulimall_ sms>sms_ sku_ ladder\sms_ sku_ full_ reduction\sms_ member_ The information of price needs to be saved across libraries (to be completed in the next chapter).

Tags: JSON Java Database Attribute

Posted on Sun, 07 Jun 2020 00:49:12 -0700 by vbzoom.com