当前位置:   article > 正文

谷粒商城笔记+踩坑(8)——仓库管理_谷粒商城获取spu规格

谷粒商城获取spu规格

导航:

谷粒商城笔记+踩坑汇总篇

目录

仓库管理

0、预览

1、数据表的说明

2、整合仓库服务

3、查询库存的模糊查询

4、采购需求的模糊查询

5、合并采购流程

6、查询未领取的采购单

7、合并采购需求

8、领取采购单

9、完成采购

10、获取spu规格

11、修改商品规格


仓库管理

0、预览

商家先在采购需求界面新增需求,新增时查询了商品、仓库等列表便于选择,然后根据需求新增采购单,采购完毕后将采购单和采购需求的状态字段设为“已完成”对应字典库的代码。

“仓库维护”界面展示仓库的名字、地址,“商品库存”界面展示sku和库存的关系,每个sku所在的每个仓库、以及该仓库下sku数量。

采购单维护-采购需求: 

采购单维护-采购单:  

仓库维护:

库存工作单:

商品库存:

1、数据表的说明

gulimall_wms数据库 

表wms_ware_info,表示仓库信息

image-20211008202953452

wms_ware_sku,绑定仓库id和sku_id的关系

2、整合仓库服务

1、要整合仓库服务,首先把仓库服务注册到nacos中

image-20211008203205685

2、配置网关

  1. - id: ware_route
  2. uri: lb://gulimall-ware
  3. predicates:
  4. - Path=/api/ware/**
  5. filters:
  6. - RewritePath=/api/(?<segment>.*),/$\{segment}

3、配置后测试仓库维护

image-20211008203531424

4、实现仓库模糊查询功能

点击查询,查看url

http://localhost:88/api/ware/wareinfo/list?t=1633696575331&page=1&limit=10&key=

WareInfoController.java

  1. @RequestMapping("/list")
  2. //@RequiresPermissions("ware:wareinfo:list")
  3. public R list(@RequestParam Map<String, Object> params){
  4. PageUtils page = wareInfoService.queryPageByCondition(params);
  5. return R.ok().put("page", page);
  6. }

WareInfoServiceImpl.java

  1. @Override
  2. public PageUtils queryPageByCondition(Map<String, Object> params) {
  3. QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
  4. String key = (String) params.get("key");
  5. if (!StringUtils.isNullOrEmpty(key)){
  6. wrapper.eq("id", key).or().like("name", key).or().like("address", key).or().like("areacode", key);
  7. }
  8. IPage<WareInfoEntity> page = this.page(
  9. new Query<WareInfoEntity>().getPage(params),
  10. wrapper
  11. );
  12. return new PageUtils(page);
  13. }

设置日志输出级别,方便查看sql语句

  1. logging:
  2. level:
  3. com.xmh: debug

测试成功

image-20211008205923395

3、查询库存的模糊查询

1、库存系统02,url:/ware/waresku/list

2、实现库存模糊查询功能,WareSkuServiceImpl.java

  1. @Override
  2. public PageUtils queryPage(Map<String, Object> params) {
  3. QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
  4. String wareId = (String) params.get("wareId");
  5. if (!StringUtils.isNullOrEmpty(wareId)){
  6. wrapper.eq("ware_id", wareId);
  7. }
  8. String skuId = (String) params.get("skuId");
  9. if (!StringUtils.isNullOrEmpty(skuId)){
  10. wrapper.eq("sku_id", skuId);
  11. }
  12. IPage<WareSkuEntity> page = this.page(
  13. new Query<WareSkuEntity>().getPage(params),
  14. wrapper
  15. );
  16. return new PageUtils(page);
  17. }

4、采购需求的模糊查询

1、库存系统03,url:/ware/purchasedetail/list

2、PurchaseDetailServiceImpl.java

  1. @Override
  2. public PageUtils queryPage(Map<String, Object> params) {
  3. QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
  4. String key = (String) params.get("key");
  5. if (!StringUtils.isNullOrEmpty(key)){
  6. wrapper.and(w -> {
  7. w.eq("sku_id", key).or().eq("purchase_id", key);
  8. });
  9. }
  10. String status = (String) params.get("status");
  11. if (!StringUtils.isNullOrEmpty(status)){
  12. wrapper.eq("status", status);
  13. }
  14. String wareId = (String) params.get("wareId");
  15. if (!StringUtils.isNullOrEmpty(wareId)){
  16. wrapper.eq("ware_id", wareId);
  17. }
  18. IPage<PurchaseDetailEntity> page = this.page(
  19. new Query<PurchaseDetailEntity>().getPage(params),
  20. wrapper
  21. );
  22. return new PageUtils(page);
  23. }

5、合并采购流程

1、采购逻辑,新建采购需求后还要可以提供合并采购单,比如一个仓库的东西可以合并到一起,让采购人员一趟采购完

image-20211009140714639

新建采购需求后还要可以提供合并采购单,比如一个仓库的东西可以合并到一起,让采购人员一趟采购完

image-20211009142628012

新建采购单,可以在采购单后面分配给员工,员工可以在系统管理->管理员列表中新建

image-20211009142748552

6、查询未领取的采购单

1、库存系统05、url:/ware/purchase/unreceive/list, 查询未领取的采购单

image-20211009142933952

2、PurchaseController.java

  1. @RequestMapping("/unreceive/list")
  2. //@RequiresPermissions("ware:purchase:list")
  3. public R unreceiveList(@RequestParam Map<String, Object> params){
  4. PageUtils page = purchaseService.queryPageUnreceive(params);
  5. return R.ok().put("page", page);
  6. }

3、新建常量枚举类constant.WareConstant

  1. public class WareConstant {
  2. /** 采购单状态枚举 */
  3. public enum PurchaseStatusEnum{
  4. CREATED(0,"新建"),ASSIGNED(1,"已分配"),
  5. RECEIVE(2,"已领取"),FINISH(3,"已完成"),
  6. HASERROR(4,"有异常");
  7. private int code;
  8. private String msg;
  9. PurchaseStatusEnum(int code, String msg){
  10. this.code = code;
  11. this.msg = msg;
  12. }
  13. public int getCode(){
  14. return code;
  15. }
  16. public String getMsg(){
  17. return msg;
  18. }
  19. }
  20. /** 采购需求枚举 */
  21. public enum PurchaseDetailStatusEnum{
  22. CREATED(0,"新建"),ASSIGNED(1,"已分配"),
  23. BUYING(2,"正在采购"),FINISH(3,"已完成"),
  24. HASERROR(4,"采购失败");
  25. private int code;
  26. private String msg;
  27. PurchaseDetailStatusEnum(int code,String msg){
  28. this.code = code;
  29. this.msg = msg;
  30. }
  31. public int getCode() {
  32. return code;
  33. }
  34. public String getMsg() {
  35. return msg;
  36. }
  37. }
  38. }

4、queryPageUnreceive.java

  1. @Override
  2. public PageUtils queryPageUnreceive(Map<String, Object> params) {
  3. QueryWrapper<PurchaseEntity> wrapper = new QueryWrapper<>();
  4. wrapper.eq("status", WareConstant.PurchaseStatusEnum.CREATED.getCode()).or().eq("status", WareConstant.PurchaseStatusEnum.ASSIGNED.getCode());
  5. IPage<PurchaseEntity> page = this.page(
  6. new Query<PurchaseEntity>().getPage(params),
  7. wrapper
  8. );
  9. return new PageUtils(page);
  10. }

5、测试成功

image-20211009143734631

7、合并采购需求

1、库存系统04,url:/ware/purchase/merge

选择要合并的采购需求,然后合并到整单

image-20211009143734631

如果不选择整单id,则自动创建新的采购单

image-20211009144346072

2、新建MergerVo.java

  1. @Data
  2. public class MergerVo {
  3. private Long purchaseId; //整单id
  4. private List<Long> items; //合并项集合
  5. }

3、分配,就是修改【采购需求】里对应的【采购单id、采购需求状态】,即purchase_detail表

并且不能重复分配采购需求给不同的采购单,如果还没去采购,或者采购失败,就可以修改

PurchaseController.java

  1. @PostMapping("/merge")
  2. //@RequiresPermissions("ware:purchase:list")
  3. public R merge(@RequestBody MergeVo mergeVo){
  4. purchaseService.mergePurchase(mergeVo);
  5. return R.ok();
  6. }

PurchaseServiceImpl.java

  1. @Autowired
  2. private PurchaseDetailService detailService;
  3. @Transactional
  4. @Override
  5. public void mergePurchase(MergeVo mergeVo) {
  6. Long purchaseId = mergeVo.getPurchaseId();
  7. // 如果采购id为null 说明没选采购单
  8. if (purchaseId == null){
  9. //新建采购单
  10. PurchaseEntity purchaseEntity = new PurchaseEntity();
  11. purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
  12. this.save(purchaseEntity);
  13. purchaseId = purchaseEntity.getId();
  14. }
  15. //合并采购需求
  16. List<Long> items = mergeVo.getItems();
  17. Long finalPurchaseId = purchaseId;
  18. List<PurchaseDetailEntity> list = detailService.getBaseMapper().selectBatchIds(items).stream().filter(entity -> {
  19. //如果还没去采购,或者采购失败,就可以修改
  20. return entity.getStatus() < WareConstant.PurchaseDetailStatusEnum.BUYING.getCode()
  21. || entity.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode();
  22. }).map(entity -> {
  23. //修改状态,以及采购单id
  24. entity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
  25. entity.setPurchaseId(finalPurchaseId);
  26. return entity;
  27. }).collect(Collectors.toList());
  28. detailService.updateBatchById(list);
  29. }

对采购单的创建时间、更新时间进行自动填充

PurchaseEntity中添加注解

  1. /**
  2. * 创建日期
  3. */
  4. @TableField(fill = FieldFill.INSERT)
  5. private Date createTime;
  6. /**
  7. * 更新日期
  8. */
  9. @TableField(fill = FieldFill.INSERT_UPDATE)
  10. private Date updateTime;

新建MyMetaObjectHandler对注解进行处理

  1. @Slf4j
  2. @Component // 一定不要忘记把处理器加到IOC容器中!
  3. public class MyMetaObjectHandler implements MetaObjectHandler {
  4. // 插入时的填充策略
  5. @Override
  6. public void insertFill(MetaObject metaObject) {
  7. log.info("start insert fill.....");
  8. // setFieldValByName(String fieldName, Object fieldVal, MetaObject
  9. this.setFieldValByName("createTime",new Date(),metaObject);
  10. this.setFieldValByName("updateTime",new Date(),metaObject);
  11. }
  12. // 更新时的填充策略
  13. @Override
  14. public void updateFill(MetaObject metaObject) {
  15. log.info("start update fill.....");
  16. this.setFieldValByName("updateTime",new Date(),metaObject);
  17. }
  18. }

在配置文件中对时间json进行格式化

  1. jackson:
  2. date-format: yyyy-MM-dd HH:mm:ss

测试成功

image-20211009153458666

image-20211009153506973

8、领取采购单

采购单分配给了采购人员,采购人员在手机端领取采购单,此时的采购单应该为新建已分配状态,在采购人员领取后采购单的状态变为已领取采购需求的状态变为正在采购

1、库存系统06、url:/ware/purchase/received

2、PurchaseController.java

  1. /**
  2. * 领取采购单/ware/purchase/received
  3. */
  4. @PostMapping("/received")
  5. //@RequiresPermissions("ware:purchase:list")
  6. public R received(@RequestBody List<Long> ids){
  7. purchaseService.received(ids);
  8. return R.ok();
  9. }

3、PurchaseServiceImpl.java

  1. @Transactional
  2. @Override
  3. public void received(List<Long> ids) {
  4. // 没有采购需求直接返回,否则会破坏采购单
  5. if (ids == null || ids.size() == 0) {
  6. return;
  7. }
  8. List<PurchaseEntity> list = this.getBaseMapper().selectBatchIds(ids).stream().filter(entity -> {
  9. //确保采购单的状态是新建或者已分配
  10. return entity.getStatus() <= WareConstant.PurchaseStatusEnum.ASSIGNED.getCode();
  11. }).map(entity -> {
  12. //修改采购单的状态为已领取
  13. entity.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
  14. return entity;
  15. }).collect(Collectors.toList());
  16. this.updateBatchById(list);
  17. //修改该采购单下的所有采购需求的状态为正在采购
  18. UpdateWrapper<PurchaseDetailEntity> updateWrapper = new UpdateWrapper<>();
  19. updateWrapper.in("purchase_id", ids);
  20. PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
  21. purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
  22. detailService.update(purchaseDetailEntity, updateWrapper);
  23. }

4、用idea自带的HTTP Client发送post请求,模拟采购人员领取采购单,进行测试

  1. POST http://localhost:88/api//ware/purchase/received
  2. Content-Type: application/json
  3. [3]

5、测试成功

image-20211009162502907

image-20211009162110249

image-20211009162117863

9、完成采购

完成采购的步骤:

  • 判断所有采购需求的状态,采购需求全部完成时,采购单状态才为完成
  • 采购项完成的时候,增加库存(调用远程获取skuName)
  • 加上分页插件

1、库存系统07,url:/ware/purchase/done

2、新建PurchaseItemDoneVo,PurchaseDoneVo

  1. @Data
  2. public class PurchaseItemDoneVo {
  3. private Long itemId;
  4. private Integer status;
  5. private String reason;
  6. }
  1. @Data
  2. public class PurchaseDoneVo {
  3. private Long id;
  4. private List<PurchaseItemDoneVo> items;
  5. }

3、PurchaseController.java

  1. /**
  2. * 完成采购
  3. */
  4. @PostMapping("/done")
  5. //@RequiresPermissions("ware:purchase:list")
  6. public R received(@RequestBody PurchaseDoneVo vo){
  7. purchaseService.done(vo);
  8. return R.ok();
  9. }

4、PurchaseServiceImpl.java

  1. @Autowired
  2. private WareSkuService wareSkuService;
  3. @Autowired
  4. private ProductFeignService productFeignService;
  5. @Override
  6. public void done(PurchaseDoneVo vo) {
  7. //1、根据前端发过来的信息,更新采购需求的状态
  8. List<PurchaseItemDoneVo> items = vo.getItems();
  9. List<PurchaseDetailEntity> updateList = new ArrayList<>();
  10. boolean flag = true;
  11. for (PurchaseItemDoneVo item : items){
  12. Long detailId = item.getItemId();
  13. PurchaseDetailEntity detailEntity = detailService.getById(detailId);
  14. detailEntity.setStatus(item.getStatus());
  15. //采购需求失败
  16. if (item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
  17. flag = false;
  18. }else {
  19. //3、根据采购需求的状态,更新库存
  20. // sku_id, sku_num, ware_id
  21. // sku_id, ware_id, stock sku_name(调用远程服务获取), stock_locked(先获取已经有的库存,再加上新购买的数量)
  22. String skuName = "";
  23. try {
  24. R info = productFeignService.info(detailEntity.getSkuId());
  25. if(info.getCode() == 0){
  26. Map<String,Object> data=(Map<String,Object>)info.get("skuInfo");
  27. skuName = (String) data.get("skuName");
  28. }
  29. } catch (Exception e) {
  30. }
  31. //更新库存
  32. wareSkuService.addStock(detailEntity.getSkuId(), detailEntity.getWareId(), skuName, detailEntity.getSkuNum());
  33. }
  34. updateList.add(detailEntity);
  35. }
  36. //保存采购需求
  37. detailService.updateBatchById(updateList);
  38. //2、根据采购需求的状态,更新采购单的状态
  39. PurchaseEntity purchaseEntity = new PurchaseEntity();
  40. purchaseEntity.setId(vo.getId());
  41. purchaseEntity.setStatus(flag ? WareConstant.PurchaseStatusEnum.FINISH.getCode() : WareConstant.PurchaseStatusEnum.HASERROR.getCode());
  42. this.updateById(purchaseEntity);
  43. }

5、新建feign.ProductFeignService接口,用来远程获取skuName

ProductFeignService.java

  1. @FeignClient("gulimall-product")
  2. public interface ProductFeignService {
  3. @RequestMapping("/product/skuinfo/info/{skuId}")
  4. R info(@PathVariable("skuId") Long skuId);
  5. }

6、主启动类加上注解@EnableFeignClients

7、WareSkuServiceImpl.java 实现入库操作

  1. @Override
  2. public void addStock(Long skuId, Long wareId, String skuName, Integer skuNum) {
  3. WareSkuEntity wareSkuEntity = this.baseMapper.selectOne(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
  4. if (wareSkuEntity == null){
  5. //新增
  6. wareSkuEntity = new WareSkuEntity();
  7. wareSkuEntity.setStock(skuNum);
  8. }else {
  9. wareSkuEntity.setStock(wareSkuEntity.getStock() + skuNum);
  10. }
  11. wareSkuEntity.setSkuName(skuName);
  12. wareSkuEntity.setWareId(wareId);
  13. wareSkuEntity.setSkuId(skuId);
  14. this.saveOrUpdate(wareSkuEntity);
  15. }

8、添加分页插件,复制product服务中的即可

9、测试

  1. POST http://localhost:88/api/ware/purchase/done
  2. Content-Type: application/json
  3. {
  4. "id": 7,
  5. "items": [
  6. {"itemId":6,"status":3,"reason":"完成"},
  7. {"itemId":7,"status":3,"reason":"完成"}
  8. ]
  9. }

测试成功

image-20211009211738771

image-20211009211746310

image-20211009211756285

10、获取spu规格

1、商品系统22、url:/product/attr/base/listforspu/{spuId}

2、AttrController.java

  1. @Autowired
  2. private ProductAttrValueService productAttrValueService;
  3. @GetMapping("/base/listforspu/{spuId}")
  4. public R baseListforspu(@PathVariable("spuId") Long spuId){
  5. List<ProductAttrValueEntity> entityList = productAttrValueService.baseAttrlistForSpu(spuId);
  6. return R.ok().put("data", entityList);
  7. }

3、ProductAttrValueServiceImpl.java

  1. @Override
  2. public List<ProductAttrValueEntity> baseAttrlistForSpu(Long spuId) {
  3. List<ProductAttrValueEntity> entities = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
  4. return entities;
  5. }

测试,点击规格

image-20211010021657735

如果此时出现400页面,向gulimall_admin中的sys_menu表中添加一条数据即可

image-20211010021800755

11、修改商品规格

1、商品系统23,url:/product/attr/update/{spuId}

2、AttrController.java

  1. @PostMapping("/update/{spuId}")
  2. public R updateSpuAttr(@PathVariable("spuId") Long spuId, @RequestBody List<ProductAttrValueEntity> entities){
  3. productAttrValueService.updateSpuAttr(spuId, entities);
  4. return R.ok();
  5. }

3、ProductAttrValueServiceImpl.java

因为修改的时候,有新增有修改有删除。 所以就先把spuId对应的所有属性都删了,再新增

  1. @Override
  2. public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> entities) {
  3. //1、删除这个spuId对应的所有属性
  4. this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
  5. //2、新增回去
  6. for (ProductAttrValueEntity entity : entities){
  7. entity.setSpuId(spuId);
  8. }
  9. this.saveBatch(entities);
  10. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/462424
推荐阅读
相关标签
  

闽ICP备14008679号