目录
新增商品
1.上线会员服务
2. 获取分类关联的品牌
3.获取选定分类下的属性分组和属性
4.新增商品vo
5.保存商品信息
6.Spu检索
7.Sku商品检索
新增商品
1.上线会员服务
将会员服务注册到nacos注册中心,启用服务注册发现@EnableDiscoveryClient。
同时新增会员等级。
新增商品时,要设置会员价,需要调用会员服务的api。
2. 获取分类关联的品牌
发布商品时,要选择商品的分类和品牌。品牌应在分类选定后,从指定分类下的品牌列表中选择。
前端将分类id传给服务器,请求需要的品牌id和品牌name
/*** /product/categorybrandrelation/brands/list* 获取分类关联的品牌*/@GetMapping("/brands/list")public R brandsList(@RequestParam("catId") Long catId){List<BrandEntity> brandEntities=categoryBrandRelationService.getBrandsByCatId(catId);List<BrandVo> collect = brandEntities.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);}@Overridepublic List<BrandEntity> getBrandsByCatId(Long catId) {List<CategoryBrandRelationEntity> relationEntities=categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));List<Long> brandIds=relationEntities.stream().map(item->{return item.getBrandId();}).collect(Collectors.toList());return brandDao.selectBatchIds(brandIds);}
3.获取选定分类下的属性分组和属性
获取属性分组,并将基本属性封装进属性分组中,即AttrGroupWithAttrsVo
/*** 获取分类下所有分组&关联属性* /product/attrgroup/{catelogId}/withattr*/@GetMapping("/{catelogId}/withattr")public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsByCatId(catelogId);return R.ok().put("data",vos);}@Overridepublic List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatId(Long catelogId) {List<AttrGroupEntity> attrGroupEntities=list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> {AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();BeanUtils.copyProperties(group, attrGroupWithAttrsVo);attrGroupWithAttrsVo.setAttrs(attrService.getAttrRelation(group.getAttrGroupId()));return attrGroupWithAttrsVo;}).collect(Collectors.toList());return collect;}
4.新增商品vo
利用Json工具 Json生成java实体类,再进行修改微调
5.保存商品信息
主要是将vo拷贝给po实体对象,各实体的spuId或skuId设置好后保存。
其中会员价,满减,积分的保存需要调用远程服务。
Feign中的接口,方法签名一般与远程服务的方一致,但不一致也能够接收到传输过来的Json对象。
某些值为空或 不合法,不应插入表中。
由于该业务数据库操作较多,加上事务注解
Feign 是一个声明式的 Web 服务客户端,能够通过注解和接口定义的方式,简化 HTTP 请求的过程。开发者不需要手动编写复杂的 HTTP 请求和响应处理代码,Feign 会“假装”成客户端接口的一部分,自动处理底层的请求和响应。
@FeignClient("gulimall-coupon")
public interface CouponFeignService {@PostMapping("coupon/spubounds/save")R saveSpuBounds(@RequestBody SpuBoundsTo boundsTo);@PostMapping("coupon/skufullreduction/saveInfo")R saveSkuReduction(SkuReductionTo skuReductionTo);
}
/*** 保存*/@RequestMapping("/save")//@RequiresPermissions("product:spuinfo:save")public R save(@RequestBody SpuSaveVo spuSaveVo){spuInfoService.saveSpuInfo(spuSaveVo);return R.ok();}/*** 保存Spu信息* @param spuSaveVo*/@Transactional@Overridepublic void saveSpuInfo(SpuSaveVo spuSaveVo) {//1.保存spu基本信息 pms_spu_infoSpuInfoEntity spuInfoEntity=new SpuInfoEntity();BeanUtils.copyProperties(spuSaveVo,spuInfoEntity);spuInfoEntity.setCreateTime(new Date());spuInfoEntity.setUpdateTime(new Date());this.saveSpuBaseInfo(spuInfoEntity);//2.保存spu描述图片 pms_spu_info_descList<String> descrImgs=spuSaveVo.getDecript();SpuInfoDescEntity spuInfoDescEntity=new SpuInfoDescEntity();spuInfoDescEntity.setSpuId(spuInfoEntity.getId());spuInfoDescEntity.setDecript(String.join(",",descrImgs));spuInfoDescService.saveSpuInfoDesc(spuInfoDescEntity);//3.保存spu图片集 pms_spu_imagesList<String> spuImgs=spuSaveVo.getImages();spuImagesService.saveSpuImages(spuInfoEntity.getId(),spuImgs);//4.保存spu规格参数 pms_product_attr_valueList<BaseAttrs> baseAttrs=spuSaveVo.getBaseAttrs();productAttrValueService.saveSpuBaseAttrs(spuInfoEntity.getId(),baseAttrs);//5.保存spu积分信息 gulimall-sms -> sms_spu_boundsBounds bounds=spuSaveVo.getBounds();SpuBoundsTo boundsTo=new SpuBoundsTo();BeanUtils.copyProperties(bounds,boundsTo);boundsTo.setSpuId(spuInfoEntity.getId());if(boundsTo.getBuyBounds().compareTo(new BigDecimal(0))>0 || boundsTo.getGrowBounds().compareTo(new BigDecimal(0))>0){if(couponFeignService.saveSpuBounds(boundsTo).getCode()!=0){log.error("远程保存spu积分信息失败");}}//6.保存spu对应的所有sku信息List<Skus> skus=spuSaveVo.getSkus();if(skus!=null && !skus.isEmpty()){skus.forEach(sku->{String defaultImg="";for (Images image : sku.getImages()) {if (image.getDefaultImg()==1){defaultImg=image.getImgUrl();break;}}//6.1 sku基本信息 pms_sku_infoSkuInfoEntity skuInfoEntity=new SkuInfoEntity();
// private String skuName;
// private BigDecimal price;
// private String skuTitle;
// private String skuSubtitle;BeanUtils.copyProperties(sku,skuInfoEntity);skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());skuInfoEntity.setSaleCount(0L);skuInfoEntity.setSpuId(spuInfoEntity.getId());skuInfoEntity.setSkuDefaultImg(defaultImg);skuInfoService.saveSkuInfo(skuInfoEntity);Long skuId=skuInfoEntity.getSkuId();//6.2 sku 图片信息 pms_sku_imagesList<SkuImagesEntity> skuImagesEntities = sku.getImages().stream().map(img -> {SkuImagesEntity skuImagesEntity = new SkuImagesEntity();skuImagesEntity.setSkuId(skuId);skuImagesEntity.setImgUrl(img.getImgUrl());skuImagesEntity.setDefaultImg(img.getDefaultImg());return skuImagesEntity;}).filter(item->{return !StringUtils.isEmpty(item.getImgUrl());}).collect(Collectors.toList());skuImagesService.saveBatch(skuImagesEntities);//6.3 sku销售属性信息 pms_sku_sale_attr_valueList<Attr> saleAttrs=sku.getAttr();List<SkuSaleAttrValueEntity> saleAttrValueEntities = saleAttrs.stream().map(attr -> {SkuSaleAttrValueEntity saleAttrValueEntity = new SkuSaleAttrValueEntity();BeanUtils.copyProperties(attr, saleAttrValueEntity);saleAttrValueEntity.setSkuId(skuId);return saleAttrValueEntity;}).collect(Collectors.toList());skuSaleAttrValueService.saveBatch(saleAttrValueEntities);//6.4 sku的优惠、满减等信息: gulimall-sms -> sms_sku_ladder sms_sku_full_reduction sms_member_priceSkuReductionTo skuReductionTo=new SkuReductionTo();BeanUtils.copyProperties(sku,skuReductionTo);skuReductionTo.setSkuId(skuId);if(skuReductionTo.getFullCount()>=0 || skuReductionTo.getFullPrice().compareTo(new BigDecimal(0))>0){if(couponFeignService.saveSkuReduction(skuReductionTo).getCode()!=0){log.error("远程保存sku优惠信息失败");}}});}}
@Overridepublic void saveSkuReduction(SkuReductionTo skuReductionTo) {//sku的优惠、满减等信息: gulimall-sms -> sms_sku_ladder sms_sku_full_reduction sms_member_price//1.满几件,打几折SkuLadderEntity skuLadderEntity=new SkuLadderEntity();BeanUtils.copyProperties(skuReductionTo,skuLadderEntity);skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());if((skuReductionTo.getFullCount()>0)){skuLadderService.save(skuLadderEntity);}//2.满多少钱,减多少钱SkuFullReductionEntity skuFullReductionEntity=new SkuFullReductionEntity();BeanUtils.copyProperties(skuReductionTo,skuFullReductionEntity);skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());if(skuReductionTo.getFullPrice().compareTo(new BigDecimal(0))>0){this.save(skuFullReductionEntity);}//3.会员价List<MemberPrice> memberPrices=skuReductionTo.getMemberPrice();List<MemberPriceEntity> collect = memberPrices.stream().map(item -> {MemberPriceEntity memberPriceEntity = new MemberPriceEntity();memberPriceEntity.setSkuId(skuReductionTo.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))>0;}).collect(Collectors.toList());memberPriceService.saveBatch(collect);}
6.Spu检索
各个检索条件串联,同时忽略大小写,字段值为0时不把该字段加入sql语句,前端初值为0
把key加入到wrapper中时,要用and
这是因为要将该条件作为一个整体,在sql语句中加上括号
防止or关键字影响查询
/*** 列表*/@RequestMapping("/list")//@RequiresPermissions("product:spuinfo:list")public R list(@RequestParam Map<String, Object> params){PageUtils page = spuInfoService.queryPageByCondition(params);return R.ok().put("page", page);}@Overridepublic PageUtils queryPageByCondition(Map<String, Object> params) {QueryWrapper<SpuInfoEntity> wrapper = new QueryWrapper<>();/*** status: 2* key:* brandId: 9* catelogId: 225*/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);}
设置Json中的date格式
spring:jackson:date-format: yyyy-MM-dd HH:mm:ss
7.Sku商品检索
最大值应大于0
/*** 列表*/@RequestMapping("/list")//@RequiresPermissions("product:skuinfo:list")public R list(@RequestParam Map<String, Object> params){PageUtils page = skuInfoService.queryPageByCondition(params);return R.ok().put("page", page);}@Overridepublic void saveSkuInfo(SkuInfoEntity skuInfoEntity) {this.baseMapper.insert(skuInfoEntity);}@Overridepublic PageUtils queryPageByCondition(Map<String, Object> params) {/*** key:* catelogId: 0* brandId: 0* min: 0* max: 0*/QueryWrapper<SkuInfoEntity> wrapper= new QueryWrapper<>();String key=(String) params.get("key");if(!StringUtils.isEmpty(key)){wrapper.and((obj)->{obj.eq("sku_id",key).or().like("sku_name",key);});}String catelogId=(String) params.get("catelogId");if(!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)){wrapper.eq("catalog_id",catelogId);}String brandId=(String) params.get("brandId");if (!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId)){wrapper.eq("brand_id",brandId);}String min=(String) params.get("min");if(!StringUtils.isEmpty(min)){wrapper.ge("price",min);}String max=(String) params.get("max");if(!StringUtils.isEmpty(max)&&!"0".equalsIgnoreCase(max)){try{BigDecimal bigDecimal = new BigDecimal(max);if(bigDecimal.compareTo(new BigDecimal("0"))>0){wrapper.le("price",max);}}catch (Exception e){}}IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params),wrapper);return new PageUtils(page);}