一、新增商品-调试会员等级相关接口
编写路由规则
- id: member_route uri: lb://gulimall-member predicates: - Path=/api/member/** filters: - RewritePath=/api/(?<segment>.*),/$\{segment}
导入你代码生成的vue界面
二、新增商品-获取分类关联的品牌
controller
/** * /product/categorybrandrelation/brands/list * * 1、Controller:处理请求,接受和校验数据 * 2、Service接受controller传来的数据,进行业务处理 * 3、Controller接受Service处理完的数据,封装页面指定的vo */ @GetMapping("/brands/list") public R relationBrandsList(@RequestParam(value = "catId",required = true)Long catId){ //1 接受请求拿到传来的目录Id //2 调用categoryBrandRelationService的自定义方法获取BrandEntity List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId); //3 把BrandEntity进行处理封装成前端需要的BrandVo返回 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); }
service
List<BrandEntity> getBrandsByCatId(Long catId);
serviceImpl
/** * 根据一个分类id查询和他有关的所有品牌实体类 * @param catId * @return */ @Override public List<BrandEntity> getBrandsByCatId(Long catId) { //1 首先根据分类catId查出CategoryBrandRelation表中所有关联数据 List<CategoryBrandRelationEntity> catelogId = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId)); //2 根据CategoryBrandRelationEntity 的 List 查出 brand list List<BrandEntity> collect = catelogId.stream().map(item -> { //2.1 对于每一个CategoryBrandRelationEntity 先拿出其BrandId Long brandId = item.getBrandId(); //2.2 用其BrandId 调用 brandService拿到brand实体类 BrandEntity byId = brandService.getById(brandId); return byId; }).collect(Collectors.toList()); //3 最终返回结果 return collect; }
三、新增商品- 获取分类下所有分组和属性
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); }
service
public interface AttrGroupService extends IService<AttrGroupEntity> { PageUtils queryPage(Map<String, Object> params); PageUtils queryPage(Map<String, Object> params, Long catelogId); List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId); }
serviceimpl
/** * 根据分类id查出所有的分组以及这些组里面的属性 * @param catelogId * @return */ @Override public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatelogId(Long catelogId) { //com.atguigu.gulimall.product.vo //1、查询分组信息(列出所有与目录ID相关的分组) List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId)); //2、查询所有属性 List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(group -> { //创建一个返回VO AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo(); //copy属性 BeanUtils.copyProperties(group,attrsVo); //根据分组ID得到对应的属性函数 List<AttrEntity> attrs = attrService.getRelationAttr(attrsVo.getAttrGroupId()); //把得到的对应属性赋值到VO中 attrsVo.setAttrs(attrs); return attrsVo; }).collect(Collectors.toList()); return collect; }
四、新增商品-新增商品VO抽取
@Data public class BaseAttrs { private Long attrId; private String attrValues; private int showDesc; }
五、新增商品 保存SPU与SKU基本信息
大概思路:stream流处理好数据之后直接调serverce用mybatis-plus进行数据保存
SpuInfoServiceImpl
@Transactional @Override public void saveSpuInfo(SpuSaveVo vo) { //1、保存spu基本信息 pms_spu_info SpuInfoEntity infoEntity = new SpuInfoEntity(); BeanUtils.copyProperties(vo,infoEntity); infoEntity.setCreateTime(new Date()); infoEntity.setUpdateTime(new Date()); 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优惠信息失败"); } } }); }
六、新增商品 调用远程服务保存优惠等信息
feingn定义
CouponFeignService
@FeignClient("gulimall-coupon") public interface CouponFeignService { /** * 1、CouponFeignService.saveSpuBounds(spuBoundTo); * 1)、@RequestBody将这个对象转为json。 * 2)、找到gulimall-coupon服务,给/coupon/spubounds/save发送请求。 * 将上一步转的json放在请求体位置,发送请求; * 3)、对方服务收到请求。请求体里有json数据。 * (@RequestBody SpuBoundsEntity spuBounds);将请求体的json转为SpuBoundsEntity; * 只要json数据模型是兼容的。双方服务无需使用同一个to * @param spuBoundTo * @return */ @PostMapping("/coupon/spubounds/save") R saveSpuBounds(@RequestBody SpuBoundTo spuBoundTo); @PostMapping("/coupon/skufullreduction/saveinfo") R saveSkuReduction(@RequestBody SkuReductionTo skuReductionTo); }
coppon服务
@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); }
SkuFullReductionServiceImpl
@Service("skuFullReductionService") public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService { @Autowired SkuLadderService skuLadderService; @Autowired MemberPriceService memberPriceService; @Override public PageUtils queryPage(Map<String, Object> params) { IPage<SkuFullReductionEntity> page = this.page( new Query<SkuFullReductionEntity>().getPage(params), new QueryWrapper<SkuFullReductionEntity>() ); return new PageUtils(page); } @Override public void saveSkuReduction(SkuReductionTo reductionTo) { //1、// //6.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); } }
七、SPU检索
controller
@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); }
service
PageUtils queryPageByCondition(Map<String, Object> params);
serviceimpl
@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); } /** * status: 2 * key: * brandId: 9 * catelogId: 225 */ IPage<SpuInfoEntity> page = this.page( new Query<SpuInfoEntity>().getPage(params), wrapper ); return new PageUtils(page); }
八、SKU检索
controller
@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); }
service
PageUtils queryPageByCondition(Map<String, Object> params);
serviceimpl
@Override public PageUtils queryPageByCondition(Map<String, Object> params) { QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>(); /** * key: * catelogId: 0 * brandId: 0 * min: 0 * max: 0 */ 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); }