Controller三个注释(获取值)
@RequestBody请求体中的JSON
@RequestParam是url?后跟的键值对
@PathVariable是REST风格url中的{}变量
Object划分
配置服务内存
终于你也有今天啊老师,内存终于不够了吧
RunConfigurations -> xxx application -> environment -> Vm opinion -> 设置-Xmx100m,最大100m
平台属性—规格参数(后端) 新增 AttrRequestVo
使用自定义的VO对象用于封装数据
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 package com.atguigu.gulimall.product.vo;import lombok.Data;@Data public class AttrVo { private Long attrId; private String attrName; private Integer searchType; private String icon; private String valueSelect; private Integer attrType; private Long enable; private Long catelogId; private Integer showDesc; private Long attrGroupId; }
AttrController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RestController @RequestMapping("product/attr") public class AttrController { @Autowired private AttrService attrService; @RequestMapping("/save") public R save (@RequestBody AttrVo attr) { attrService.saveAttr(attr); return R.ok(); } }
AttrServiceImpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Service("attrService") public class AttrServiceImpl extends ServiceImpl <AttrDao , AttrEntity > implements AttrService { @Autowired AttrAttrgroupRelationDao relationDao; @Override @Transactional public void saveAttr (AttrVo attr) { AttrEntity attrEntity = new AttrEntity(); BeanUtils.copyProperties(attr, attrEntity); this .save(attrEntity); AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); relationEntity.setAttrId(attrEntity.getAttrId()); relationEntity.setAttrGroupId(attr.getAttrGroupId()); relationDao.insert(relationEntity); } }
查询分页 AttrResponseVo
原来可以使用继承的写法,学到了
1 2 3 4 5 6 7 8 9 10 11 @Data public class AttrResponseVo extends AttrEntity { private String catelogName; private String groupName; }
AttrController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RestController @RequestMapping("product/attr") public class AttrController { @Autowired private AttrService attrService; @RequestMapping("/base/list/{catelogId}") public R baseAttrList (@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) { PageUtils page = attrService.queryBaseAttrPage(params, catelogId); return R.ok().put("page" , page); } }
AttrServiceImpl
带新增的两个属性
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 @Service("attrService") public class AttrServiceImpl extends ServiceImpl <AttrDao , AttrEntity > implements AttrService { @Autowired AttrAttrgroupRelationDao relationDao; @Override public PageUtils queryBaseAttrPage (Map<String, Object> params, Long catelogId) { QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<>(); if (catelogId != 0 ) { queryWrapper.eq("catelog_id" , catelogId); } String key = (String) params.get("key" ); if (!StringUtils.isEmpty(key)) { queryWrapper.and((wrapper) -> { wrapper.eq("attr_id" , key).or().like("attr_name" , key); }); } IPage<AttrEntity> page = this .page( new Query<AttrEntity>().getPage(params), queryWrapper ); PageUtils pageUtils = new PageUtils(page); List<AttrEntity> records = page.getRecords(); List<AttrResponseVo> responseVoList = records.stream().map((attrEntity) -> { AttrResponseVo responseVo = new AttrResponseVo(); BeanUtils.copyProperties(attrEntity, responseVo); CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId()); if (categoryEntity != null ) { responseVo.setCatelogName(categoryEntity.getName()); } AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id" , attrEntity.getAttrId())); if (relationEntity != null ) { responseVo.setGroupName(attrGroupDao.selectById(relationEntity.getAttrGroupId()).getAttrGroupName()); } return responseVo; }).collect(Collectors.toList()); pageUtils.setList(responseVoList); return pageUtils; } }
查询回显 AttrResoponseVo 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Data public class AttrResponseVo extends AttrEntity { private String catelogName; private String groupName; private Long[] catelogPath; private Long attrGroupId; }
AttrController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("product/attr") public class AttrController { @Autowired private AttrService attrService; @RequestMapping("/info/{attrId}") public R attrResponseInfo (@PathVariable("attrId") Long attrId) { AttrResponseVo attr = attrService.getResponseAttrInfo(attrId); return R.ok().put("attr" , attr); }
AttrServiceImpl 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 @Service("attrService") public class AttrServiceImpl extends ServiceImpl <AttrDao , AttrEntity > implements AttrService { @Override public AttrResponseVo getResponseAttrInfo (Long attrId) { AttrResponseVo responseVo = new AttrResponseVo(); AttrEntity attrEntity = this .getById(attrId); BeanUtils.copyProperties(attrEntity, responseVo); AttrAttrgroupRelationEntity relationEntity = relationDao.selectOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id" , attrEntity.getAttrId())); if (relationEntity != null ) { responseVo.setAttrGroupId(relationEntity.getAttrGroupId()); AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(relationEntity.getAttrGroupId()); if (attrGroupEntity != null ) { responseVo.setGroupName(attrGroupEntity.getAttrGroupName()); } } Long[] categoryPath = categoryService.findCategoryPath(attrEntity.getCatelogId()); responseVo.setCatelogPath(categoryPath); CategoryEntity categoryEntity = categoryDao.selectById(attrEntity.getCatelogId()); if (categoryEntity != null ) { responseVo.setCatelogName(categoryEntity.getName()); } return responseVo; } }
修改 AttrController
需要用AttrRequestVo
1 2 3 4 5 6 7 8 @RequestMapping("/update") public R update (@RequestBody AttrRequestVo attr) { attrService.updateAttr(attr); return R.ok(); }
AttrServiceImpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @Override @Transactional public void updateAttr (AttrRequestVo attr) { AttrEntity attrEntity = new AttrEntity(); BeanUtils.copyProperties(attr, attrEntity); this .updateById(attrEntity); AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); relationEntity.setAttrGroupId(attr.getAttrGroupId()); relationEntity.setAttrId(attr.getAttrId()); Integer count = relationDao.selectCount(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id" , attr.getAttrId())); if (count > 0 ) { relationDao.update(relationEntity, new UpdateWrapper<AttrAttrgroupRelationEntity>().eq("attr_id" , attr.getAttrId())); } else { relationDao.insert(relationEntity); } }
平台属性—销售属性(后端)
规格参数和销售属性其实就是attr属性中attrType的不同,所以controller还是同一个,只是请求地址中片段不同,只需要在requestMapping中获得参数判断一下即可,
常量类
在common模块中写一个类,里面写各种枚举
1 2 3 4 5 6 7 8 9 10 11 public class ProductConstant { public enum AttrEnum { ATTR_TYPE_BASE(1 , "基本属性" ), ATTR_TYPE_SALE(0 , "销售属性" ); private int code; private String msg; AttrEnum(int code, String msg) { this .code = code; this .msg = msg; } } }
AttrController 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 @RestController @RequestMapping("product/attr") public class AttrController { @Autowired private AttrService attrService; @RequestMapping("/info/{attrId}") public R attrResponseInfo (@PathVariable("attrId") Long attrId) { AttrResponseVo attr = attrService.getResponseAttrInfo(attrId); return R.ok().put("attr" , attr); } @GetMapping("/{attrType}/list/{catelogId}") public R attrResponseList (@RequestParam Map<String, Object> params, @PathVariable("attrType") String attrType, @PathVariable("catelogId") Long catelogId) { PageUtils page = attrService.queryBaseAttrPage(params, catelogId,attrType); return R.ok().put("page" , page); } }
AttrService 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 @Override public PageUtils queryBaseAttrPage (Map<String, Object> params, Long catelogId, String attrType) { QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("attr_type" , "base" .equalsIgnoreCase(attrType) ? 1 : 0 ); if (catelogId != 0 ) { queryWrapper.eq("catelog_id" , catelogId); } @Override @Transactional public void saveAttr (AttrRequestVo attr) { AttrEntity attrEntity = new AttrEntity(); BeanUtils.copyProperties(attr, attrEntity); this .save(attrEntity); if (attr.getAttrType() == ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode()) { AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); relationEntity.setAttrId(attrEntity.getAttrId()); relationEntity.setAttrGroupId(attr.getAttrGroupId()); relationDao.insert(relationEntity); } } }
平台属性—属性分组(后端) 查询关联
分组和属性多对多,需要关联表
AttrGroupController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 @RestController @RequestMapping("product/attrgroup") public class AttrGroupController { @Autowired private AttrGroupService attrGroupService; @Autowired private CategoryService categoryService; @Autowired private AttrService attrService; @GetMapping("/{attrgroupId}/attr/relation") public R attrRelationList (@PathVariable("attrgroupId") String attrgroupId) { List<AttrEntity> list = attrService.getRelationAttr(attrgroupId); return R.ok().put("data" , list); } }
AttrService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Override public List<AttrEntity> getRelationAttr (String attrgroupId) { List<AttrAttrgroupRelationEntity> relationList = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id" , attrgroupId)); List<Long> idList = relationList.stream().map((relationEntity) -> { return relationEntity.getAttrId(); }).collect(Collectors.toList()); Collection<AttrEntity> attrEntities = this .listByIds(idList); return (List<AttrEntity>) attrEntities; }
删除关联 AttrGroupRelationVo 1 2 3 4 5 @Data public class AttrGroupRelationVo { private Long attrId; private Long attrGroupId; }
AttrGroupController 1 2 3 4 5 6 7 8 @RequestMapping("/attr/relation/delete") public R deleteRelation (AttrGroupRelationVo[] vos) { attrService.deleteRelation(vos); return R.ok(); }
AttrGroupService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override public void deleteRelation (AttrGroupRelationVo[] vos) { List<AttrAttrgroupRelationEntity> collect = Arrays.asList(vos).stream().map((vo) -> { AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); BeanUtils.copyProperties(vo, relationEntity); return relationEntity; }).collect(Collectors.toList()); relationDao.deleteBatchRelation(collect); }
AttrAttrGroupRelationDao 1 2 3 4 5 @Mapper public interface AttrAttrgroupRelationDao extends BaseMapper <AttrAttrgroupRelationEntity > { void deleteBatchRelation (@Param("entities") List<AttrAttrgroupRelationEntity> entities) ; }
1 2 3 4 5 6 7 8 <delete id ="deleteBatchRelation" > DELETE FROM gulimall_pms.pms_attr_attrgroup_relation WHERE <foreach collection ="entities" item ="item" separator =" OR " > (attr_id = #{item.attrId} AND attr_group_id = #{item.attrGroupId}) </foreach > </delete >
查询未关联属性 AttrGroupController 1 2 3 4 5 6 7 8 9 10 11 @GetMapping("/{attrgroupId}/noattr/relation") public R attrNoRelationList (@PathVariable("attrgroupId") String attrgroupId, @RequestParam Map<String, Object> params) { PageUtils pageUtils = attrService.getNoRelationAttr(params, attrgroupId); return R.ok().put("page" , pageUtils); }
AttrService 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 @Override public PageUtils getNoRelationAttr (Map<String, Object> params, String attrgroupId) { AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId); Long catelogId = attrGroupEntity.getCatelogId(); List<AttrGroupEntity> groupEntities = attrGroupDao.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id" , catelogId).ne("attr_group_id" , attrgroupId)); List<Long> groupIdList = groupEntities.stream().map((item) -> { return item.getAttrGroupId(); }).collect(Collectors.toList()); List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_group_id" , groupIdList)); List<Long> attrIdList = relationEntities.stream().map((item) -> { return item.getAttrId(); }).collect(Collectors.toList()); QueryWrapper<AttrEntity> queryWrapper = new QueryWrapper<AttrEntity>().eq("catelog_id" , catelogId).notIn("attr_id" , attrIdList); String key = (String) params.get("key" ); if (!StringUtils.isEmpty(key)) { queryWrapper.and((wrapper) -> { wrapper.eq("attr_id" , key).or().like("attr_name" , key); }); } IPage<AttrEntity> iPage = this .page(new Query<AttrEntity>().getPage(params), queryWrapper); PageUtils pageUtils = new PageUtils(iPage); return pageUtils; }
TODO:这里有个很奇怪的地方,我修改一下关联的话,属性中的所属分组也会改变?
哦我傻了,这俩个就是对应的功能
添加关联 AttrGroupController 1 2 3 4 5 6 7 8 9 10 11 @PostMapping("/attr/relation") public R addRelation (@RequestBody List<AttrGroupRelationVo> vos) { relationService.saveBatch(vos); return R.ok(); }
AttrGroupService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Override public void saveBatch (List<AttrGroupRelationVo> vos) { List<AttrAttrgroupRelationEntity> relationEntityList = vos.stream().map((vo) -> { AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity(); BeanUtils.copyProperties(vo, relationEntity); return relationEntity; }).collect(Collectors.toList()); this .saveBatch(relationEntityList); }
商品维护—发布商品(后端) 获取用户等级 配置网关application.yml
详见文档,需要member模块中的功能,之前还没有配过该模块的网关配置,配置一下即可
1 2 3 4 5 6 7 8 9 10 11 12 spring: cloud: gateway: routes: - id: member_route uri: lb://gulimall-member predicates: - Path=/api/member/** filters: - RewritePath=/api/(?<segment>.*),/$\{segment}
页面模板
原本renren-generator生成的时候就有了,直接复制到前端src文件夹下即可
然后就好了,基本的增删改查
获得分类关联的品牌 BrandVo 1 2 3 4 5 @Data public class BrandVo { private Long brandId; private String brandName; }
CategoryBrandRelationController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 @RestController @RequestMapping("product/categorybrandrelation") public class CategoryBrandRelationController { @Autowired private CategoryBrandRelationService relationService; @GetMapping("/brand/list") public R relationListByBrand (@RequestParam("catId") Long catId) { List<BrandEntity> brandEntities = relationService.getBrandsByCatId(catId); List<BrandVo> list = 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" , list); } }
为什么这里不直接使用relation对象的原因是,为了重用,如果后面还有业务需要获得更多的brand信息,需要用到brandEntity
CategoryBrandRelationServiceImpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @Override public List<BrandEntity> getBrandsByCatId (Long catId) { List<CategoryBrandRelationEntity> relationList = baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id" , catId)); List<BrandEntity> collect = relationList.stream().map(relationEntity -> { Long brandId = relationEntity.getBrandId(); return brandService.getById(brandId); }).collect(Collectors.toList()); return collect; }
获取分类关联的分组和属性
分类:手机手表……
分组:主体,基本信息,其他信息
属性:尺寸,颜色
属性从属于分组,分组从属于分类(多对一),分类和品牌多对多
AttrGroupWithAttrsVo 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 @Data public class AttrGroupWithAttrsVo { private Long attrGroupId; private String attrGroupName; private Integer sort; private String descript; private String icon; private Long catelogId; private List<AttrEntity> attrs; }
AttrGroupController 1 2 3 4 5 6 7 8 9 10 11 public class AttrGroupController { @GetMapping("/{catelogId}/withattr") public R getAttrGroupWithAttr (@PathVariable("catelogId") Long catelogId, @RequestParam Map<String, Object> params) { List<AttrGroupWithAttrsVo> list = attrGroupService.getWithAttrByCatelogId(catelogId); return R.ok().put("data" , list); }
AttrGroupService 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Override public List<AttrGroupWithAttrsVo> getWithAttrByCatelogId (Long catelogId) { List<AttrGroupEntity> attrGroupList = this .list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id" , catelogId)); List<AttrGroupWithAttrsVo> collect = attrGroupList.stream().map(attrGroupEntity -> { AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo(); BeanUtils.copyProperties(attrGroupEntity, attrGroupWithAttrsVo); List<AttrEntity> attrList = attrService.getRelationAttr(attrGroupEntity.getAttrGroupId()); attrGroupWithAttrsVo.setAttrs(attrList); return attrGroupWithAttrsVo; }).collect(Collectors.toList()); return collect; }
新增商品 Vo
准备一个Vo接收JSON数据,使用BEJSON网站下的转java实体类工具,然后下载即可
真不戳!
SpuInfoController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @RestController @RequestMapping("product/spuinfo") public class SpuInfoController { @Autowired private SpuInfoService spuInfoService; @RequestMapping("/save") public R save (@RequestBody SpuSaveVo spuSaveVo) { spuInfoService.saveSpuVo(spuSaveVo); return R.ok(); } }
远程服务
因为涉及到coupon模块,需要远程调用
开启服务
@EnableDiscoveryClient // 被调用服务application上,开启nacos注册 @EnableFeignClients(basePackages = “com.atguigu.gulimall.product.feign”) //feign
TO : 由于两个服务之间需要公用一种对象,使用TO传输对象,可以写在common公共模块中
编写接口
远程服务接口 1 2 3 4 5 6 7 8 9 10 11 12 @FeignClient("gulimall-coupon") public interface CouponFeignService { @PostMapping("/coupon/spubounds/save") R saveSpuBound (@RequestBody SpuBoundTo spuBoundTo) ; }
SpuInfoServiceImpl
因为这个商品Vo涉及表众多,还有关联到远程调用,所以代码量较长。
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 @Service("spuInfoService") public class SpuInfoServiceImpl extends ServiceImpl <SpuInfoDao , SpuInfoEntity > implements SpuInfoService { @Autowired SpuInfoDescService spuInfoDescService; @Autowired SpuImagesService spuImagesService; @Autowired ProductAttrValueService productAttrValueService; @Autowired AttrService attrService; @Autowired SkuInfoService skuInfoService; @Autowired SkuImagesService skuImagesService; @Autowired SkuSaleAttrValueService skuSaleAttrValueService; @Autowired CouponFeignService couponFeignService; @Transactional @Override public void saveSpuVoInfo (SpuSaveVo spuSaveVo) { SpuInfoEntity spuInfoEntity = new SpuInfoEntity(); BeanUtils.copyProperties(spuSaveVo, spuInfoEntity); spuInfoEntity.setCreateTime(new Date()); spuInfoEntity.setUpdateTime(new Date()); this .save(spuInfoEntity); List<String> decrypts = spuSaveVo.getDecript(); SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity(); spuInfoDescEntity.setSpuId(spuInfoEntity.getId()); spuInfoDescEntity.setDecript(String.join("," , decrypts)); spuInfoDescService.save(spuInfoDescEntity); List<String> images = spuSaveVo.getImages(); List<SpuImagesEntity> spuImagesList = images.stream().map(image -> { SpuImagesEntity spuImagesEntity = new SpuImagesEntity(); spuImagesEntity.setSpuId(spuInfoEntity.getId()); spuImagesEntity.setImgUrl(image); return spuImagesEntity; }).collect(Collectors.toList()); spuImagesService.saveBatch(spuImagesList); List<BaseAttrs> baseAttrs = spuSaveVo.getBaseAttrs(); List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> { ProductAttrValueEntity attrValueEntity = new ProductAttrValueEntity(); attrValueEntity.setAttrId(attr.getAttrId()); attrValueEntity.setSpuId(spuInfoEntity.getId()); attrValueEntity.setAttrName(attrService.getById(attr.getAttrId()).getAttrName()); attrValueEntity.setAttrValue(attr.getAttrValues()); attrValueEntity.setQuickShow(attr.getShowDesc()); return attrValueEntity; }).collect(Collectors.toList()); productAttrValueService.saveBatch(collect); List<Skus> skus = spuSaveVo.getSkus(); if (skus != null && skus.size() > 0 ) { skus.forEach(sku -> { SkuInfoEntity skuInfoEntity = new SkuInfoEntity(); BeanUtils.copyProperties(sku, skuInfoEntity); skuInfoEntity.setBrandId(spuInfoEntity.getBrandId()); skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId()); skuInfoEntity.setSaleCount(0L ); skuInfoEntity.setSpuId(spuInfoEntity.getId()); String defaultImg = "" ; for (Images img : sku.getImages()) { if (img.getDefaultImg() == 1 ) { defaultImg = img.getImgUrl(); } } skuInfoEntity.setSkuDefaultImg(defaultImg); skuInfoService.save(skuInfoEntity); Long skuId = skuInfoEntity.getSkuId(); List<SkuImagesEntity> skuImagesList = sku.getImages().stream().map(img -> { SkuImagesEntity skuImagesEntity = new SkuImagesEntity(); skuImagesEntity.setSkuId(skuId); skuImagesEntity.setImgUrl(img.getImgUrl()); skuImagesEntity.setDefaultImg(img.getDefaultImg()); return skuImagesEntity; }).collect(Collectors.toList()); skuImagesService.saveBatch(skuImagesList); List<Attr> attrList = sku.getAttr(); List<SkuSaleAttrValueEntity> skuSaleAttrValueList = attrList.stream().map(attr -> { SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity(); BeanUtils.copyProperties(attr, skuSaleAttrValueEntity); skuSaleAttrValueEntity.setSkuId(skuId); return skuSaleAttrValueEntity; }).collect(Collectors.toList()); skuSaleAttrValueService.saveBatch(skuSaleAttrValueList); Bounds bounds = spuSaveVo.getBounds(); SpuBoundTo spuBoundTo = new SpuBoundTo(); BeanUtils.copyProperties(bounds, spuBoundTo); spuBoundTo.setSpuId(spuInfoEntity.getId()); R saveSpuBound = couponFeignService.saveSpuBound(spuBoundTo); if (saveSpuBound.getCode() != 0 ) { log.error("远程保存spu积分信息失败" ); } SkuReductionTo skuReductionTo = new SkuReductionTo(); BeanUtils.copyProperties(sku, skuReductionTo); skuReductionTo.setSkuId(skuId); R saveSkuReduction = couponFeignService.saveSkuReduction(skuReductionTo); if (saveSkuReduction.getCode() != 0 ) { log.error("远程保存spu积满减信息失败" ); } }); } } }
SkuFullReductionServiceImpl 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 @Service("skuFullReductionService") public class SkuFullReductionServiceImpl extends ServiceImpl <SkuFullReductionDao , SkuFullReductionEntity > implements SkuFullReductionService { @Autowired SkuLadderService skuLadderService; @Autowired MemberPriceService memberPriceService; @Override public void saveSkuReduction (SkuReductionTo skuReductionTo) { SkuLadderEntity skuLadderEntity = new SkuLadderEntity(); skuLadderEntity.setSkuId(skuReductionTo.getSkuId()); skuLadderEntity.setFullCount(skuReductionTo.getFullCount()); skuLadderEntity.setDiscount(skuReductionTo.getDiscount()); skuLadderEntity.setAddOther(skuReductionTo.getCountStatus()); skuLadderService.save(skuLadderEntity); SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity(); BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity); this .save(skuFullReductionEntity); List<MemberPriceTo> memberPriceToList = skuReductionTo.getMemberPrice(); List<MemberPriceEntity> memberPriceList = memberPriceToList.stream().map(memberPriceTo -> { MemberPriceEntity memberPriceEntity = new MemberPriceEntity(); memberPriceEntity.setSkuId(skuReductionTo.getSkuId()); memberPriceEntity.setMemberLevelId(memberPriceTo.getId()); memberPriceEntity.setMemberLevelName(memberPriceTo.getName()); memberPriceEntity.setMemberPrice(memberPriceTo.getPrice()); memberPriceEntity.setAddOther(1 ); return memberPriceEntity; }).collect(Collectors.toList()); memberPriceService.saveBatch(memberPriceList); } }
debug & test
妈耶一次成功有点开心
TODO: 失败的情况没有编写
商品维护—SPU管理(后端) 时间戳格式化 1 2 3 4 spring: jackson: date-format: yyyy-MM-dd HH:mm:ss
SPU待条件检索 1 2 3 4 5 6 7 8 9 @RequestMapping("/list") public R list (@RequestParam Map<String, Object> params) { PageUtils page = spuInfoService.queryPageByCondition(params); return R.ok().put("page" , page); }
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 @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); }); } 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)) { wrapper.eq("brand_id" , brandId); } String catelogId = (String) params.get("catelogId" ); if (!StringUtils.isEmpty(catelogId)) { wrapper.eq("catalog_id" , catelogId); } IPage<SpuInfoEntity> page = this .page( new Query<SpuInfoEntity>().getPage(params), wrapper ); return new PageUtils(page); }
SKU检索 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("product/skuinfo") public class SkuInfoController { @Autowired private SkuInfoService skuInfoService; @RequestMapping("/list") public R list (@RequestParam Map<String, Object> params) { PageUtils page = skuInfoService.queryPageByCondition(params); return R.ok().put("page" , page); }
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 @Override public PageUtils queryPageByCondition (Map<String, Object> params) { QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>(); String key = (String) params.get("key" ); if (!StringUtils.isEmpty(key)) { wrapper.and((w) -> { w.eq("sku_id" , key).or().like("sku_name" , key); }); } String brandId = (String) params.get("brandId" ); if (!StringUtils.isEmpty(brandId)) { wrapper.eq("brand_id" , brandId); } String catelogId = (String) params.get("catelogId" ); if (!StringUtils.isEmpty(catelogId)) { wrapper.eq("catalog_id" , catelogId); } String min = (String) params.get("min" ); if (!StringUtils.isEmpty(min)) { wrapper.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 ) { wrapper.le("price" , max); } } catch (Exception e) { } } IPage<SkuInfoEntity> page = this .page( new Query<SkuInfoEntity>().getPage(params), wrapper ); return new PageUtils(page); }
库存系统—仓库维护(后端)
仓库相关表:gulimall_wms_xxx
服务:ware
服务基础搭建
application.yml中配置nacos、application name
注解开启nacos(@EnableDiscoveryClient)(com.alibaba.nacos.api.exception.NacosException: java.lang.reflect.InvocationTargetException这个报错是因为没有配置bootstrap)
配置日志
1 2 3 4 logging: level: com.atguigu: debug
配置网关
配置mybatis分页插件
获得仓库列表带条件
修改一下原来的queryPage即可,添加上condition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @Override public PageUtils queryPageByCondition (Map<String, Object> params) { QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>(); String key = (String) params.get("key" ); if (!StringUtils.isEmpty(key)) { wrapper.eq("id" , key).or().like("name" , key).or().like("address" , key); } IPage<WareInfoEntity> page = this .page( new Query<WareInfoEntity>().getPage(params), wrapper ); return new PageUtils(page); }
库存系统—商品库存(后端) 获得库存列表带条件
同样
库存系统—采购需求(后端) 获得需求列表带条件
同样
获得未领取的采购单列表
将多个采购需求合并到一个采购单当中(并且该采购单未被领取)
PurchaseController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @RestController @RequestMapping("ware/purchase") public class PurchaseController { @Autowired private PurchaseService purchaseService; @RequestMapping("/unreceive/list") public R unreceiveList (@RequestParam Map<String, Object> params) { PageUtils page = purchaseService.queryPageUnreceive(params); return R.ok().put("page" , page); }
PurchaseServiceImpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Service("purchaseService") public class PurchaseServiceImpl extends ServiceImpl <PurchaseDao , PurchaseEntity > implements PurchaseService { @Override public PageUtils queryPageUnreceive (Map<String, Object> params) { QueryWrapper<PurchaseEntity> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("status" , 0 ).or().eq("status" , 1 ); IPage<PurchaseEntity> page = this .page( new Query<PurchaseEntity>().getPage(params), queryWrapper ); return new PageUtils(page); }
合并采购单
如果前端没有传递采购单的id那么就新建一个采购单
WareConstant 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 public class WareConstant { public enum PurchaseStatusEnum { CREATED(0 , "新建" ), ASSIGN(1 , "已分配" ), RECEIVE(2 , "已领取" ), FINISHED(3 , "已完成" ), HASERROR(4 , "异常" ); private int code; private String msg; PurchaseStatusEnum(int code, String msg) { this .code = code; this .msg = msg; } public int getCode () { return code; } public String getMsg () { return msg; } } public enum PurchaseDetailStatusEnum { CREATED(0 , "新建" ), ASSIGN(1 , "已分配" ), BUYYING(2 , "正在采购" ), FINISHED(3 , "已完成" ), HASERROR(4 , "异常" ); private int code; private String msg; PurchaseDetailStatusEnum(int code, String msg) { this .code = code; this .msg = msg; } public int getCode () { return code; } public String getMsg () { return msg; } } }
MergeVo 1 2 3 4 5 6 7 8 @Data public class MergeVo { private Long purchaseId; private List<Long> items; }
PurchaseController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("ware/purchase") public class PurchaseController { @Autowired private PurchaseService purchaseService; @PostMapping("/merge") public R merge (@RequestBody MergeVo mergeVo) { purchaseService.mergePurchase(mergeVo); return R.ok() }
PurchaseServiceImpl 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 @Transactional @Override public void mergePurchase (MergeVo mergeVo) { Long purchaseId = mergeVo.getPurchaseId(); if (purchaseId == null ) { PurchaseEntity purchaseEntity = new PurchaseEntity(); purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode()); this .save(purchaseEntity); purchaseId = purchaseEntity.getId(); } List<Long> items = mergeVo.getItems(); Long finalPurchaseId = purchaseId; List<PurchaseDetailEntity> detailEntities = items.stream().map(detailId -> { PurchaseDetailEntity detailEntity = new PurchaseDetailEntity(); detailEntity.setId(detailId); detailEntity.setPurchaseId(finalPurchaseId); detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGN.getCode()); return detailEntity; }).collect(Collectors.toList()); detailService.updateBatchById(detailEntities); }
领取采购单 PurchaseController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("ware/purchase") public class PurchaseController { @Autowired private PurchaseService purchaseService; @PostMapping("/received") public R received (@RequestBody List<Long> ids) { purchaseService.receivedByIds(ids); return R.ok(); }
PurchaseServiceImpl 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 @Override public void receivedByIds (List<Long> ids) { List<PurchaseEntity> purchaseEntityList = ids.stream().map(id -> { return this .getById(id); }).filter(item -> { Integer status = item.getStatus(); return (status == WareConstant.PurchaseStatusEnum.ASSIGN.getCode()) || status == WareConstant.PurchaseStatusEnum.CREATED.getCode(); }).map(purchaseEntity -> { purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode()); return purchaseEntity; }).collect(Collectors.toList()); this .updateBatchById(purchaseEntityList); purchaseEntityList.forEach(purchaseEntity -> { List<PurchaseDetailEntity> detailEntityList = detailService.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id" , purchaseEntity.getId())); List<PurchaseDetailEntity> list = detailEntityList.stream().map(detail -> { PurchaseDetailEntity detailEntity = new PurchaseDetailEntity(); detailEntity.setId(detail.getId()); detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGN.getCode()); return detailEntity; }).collect(Collectors.toList()); detailService.updateBatchById(list); }); }
完成采购
自动修改对应采购内容的库存数
PurchaseDoneItemVo 1 2 3 4 5 6 7 8 9 @Data public class PurchaseDoneItemVo { private Long itemId; private Integer status; private String reason; }
PurchaseFinishedVo 1 2 3 4 5 6 7 8 9 10 11 12 @Data public class PurchaseFinishedVo { @NotNull private Long id; private List<PurchaseDoneItemVo> items; }
PurchaseController 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @RestController @RequestMapping("ware/purchase") public class PurchaseController { @Autowired private PurchaseService purchaseService; @PostMapping("/done") public R finish (@RequestBody PurchaseFinishedVo finishedVo) { purchaseService.finishPurchase(finishedVo); return R.ok(); }
PurchaseServiceImpl 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 @Override public void finishPurchase (PurchaseFinishedVo finishedVo) { boolean purchaseFinished = true ; List<PurchaseDetailEntity> updatedDetailList = new ArrayList<>(); List<PurchaseDoneItemVo> doneItemVoList = finishedVo.getItems(); for (PurchaseDoneItemVo doneItemVo : doneItemVoList) { PurchaseDetailEntity detailEntity = new PurchaseDetailEntity(); if (doneItemVo.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()) { purchaseFinished = false ; } else { PurchaseDetailEntity detail = detailService.getById(doneItemVo.getItemId()); wareSkuService.addStock(detail.getSkuId(), detail.getWareId(), detail.getSkuNum()); } detailEntity.setStatus(doneItemVo.getStatus()); detailEntity.setId(doneItemVo.getItemId()); updatedDetailList.add(detailEntity); } detailService.updateBatchById(updatedDetailList); PurchaseEntity purchaseEntity = new PurchaseEntity(); purchaseEntity.setId(finishedVo.getId()); if (purchaseFinished) { purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.FINISHED.getCode()); } else { purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.HASERROR.getCode()); } this .updateById(purchaseEntity); }
WareSkuServiceImpl 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Override public void addStock (Long skuId, Long wareId, Integer skuNum) { Integer count = baseMapper.selectCount(new QueryWrapper<WareSkuEntity>().eq("sku_id" , skuId).eq("ware_id" , wareId)); if (count == 0 ) { WareSkuEntity wareSkuEntity = new WareSkuEntity(); wareSkuEntity.setSkuId(skuId); wareSkuEntity.setWareId(wareId); wareSkuEntity.setStock(skuNum); baseMapper.insert(wareSkuEntity); } else baseMapper.addStock(skuId, wareId, skuNum); }
WareSkuDao 1 2 3 4 5 6 7 <update id ="addStock" > UPDATE gulimall_wms.wms_ware_sku SET stock = stock + #{skuNum} WHERE sku_id = #{skuId} AND ware_id = #{wareId} </update >
商品维护—SKU规格维护(后端)
这是一个按钮触发的页面,具体解决方案如下
谷粒商城踩坑汇总(分布式基础(全栈开发篇)) - 大厨无盐煮 - 博客园 (cnblogs.com)
获取 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @RestController @RequestMapping("product/attr") public class AttrController { @Autowired private AttrService attrService; @Autowired private ProductAttrValueService valueService; @GetMapping("/base/listforspu/{spuId}") public R baseAttrListForSpu (@PathVariable("spuId") Long spuId) { List<ProductAttrValueEntity> data = valueService.baseAttrListForSpu(spuId); return R.ok().put("data" , data); }
1 2 3 4 5 6 @Service("productAttrValueService") public class ProductAttrValueServiceImpl extends ServiceImpl <ProductAttrValueDao , ProductAttrValueEntity > implements ProductAttrValueService { @Override public List<ProductAttrValueEntity> baseAttrListForSpu (Long spuId) { return baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id" , spuId)); }
修改 1 2 3 4 5 6 7 8 9 10 11 12 @RestController @RequestMapping("product/attr") public class AttrController { @PostMapping("/update/{spuId}") public R updateSpuAttr (@PathVariable("spuId") String spuId, @RequestBody List<ProductAttrValueEntity> entities) { valueService.updateSpuAttr(spuId, entities); return R.ok(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 @Transactional @Override public void updateSpuAttr (Long spuId, List<ProductAttrValueEntity> entities) { baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id" , spuId)); List<ProductAttrValueEntity> collect = entities.stream().map(item -> { item.setSpuId(spuId); return item; }).collect(Collectors.toList()); this .saveBatch(collect); }
分布式基础篇—完结撒花🎉