• 谷粒商城 (二十八) --------- 仓储服务 API 仓库管理



    一、查询仓库列表改为模糊查询

    WareInfoServiceImpl 修改 queryPage 方法

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

    二、查询商品库存

    WareSkuServiceImpl 中 queryPage 方法修改:

     @Override
     public PageUtils queryPage(Map<String, Object> params) {
         /**
          * skuId: 1
          * wareId: 2
          */
         QueryWrapper<WareSkuEntity> queryWrapper = new QueryWrapper<>();
         String skuId = (String) params.get("skuId");
         if(!StringUtils.isEmpty(skuId)){
             queryWrapper.eq("sku_id",skuId);
         }
    
         String wareId = (String) params.get("wareId");
         if(!StringUtils.isEmpty(wareId)){
             queryWrapper.eq("ware_id",wareId);
         }
    
    
         IPage<WareSkuEntity> page = this.page(
                 new Query<WareSkuEntity>().getPage(params),
                 queryWrapper
         );
    
         return new PageUtils(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

    三、采购需求检索

    PurchaseDetailServiceImpl 中 queryPage 修改

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /**
         * status: 0,//状态
         *    wareId: 1,//仓库id
         */
    
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>();
    
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            //purchase_id  sku_id
            queryWrapper.and(w->{
                w.eq("purchase_id",key).or().eq("sku_id",key);
            });
        }
    
        String status = (String) params.get("status");
        if(!StringUtils.isEmpty(status)){
            //purchase_id  sku_id
            queryWrapper.eq("status",status);
        }
    
        String wareId = (String) params.get("wareId");
        if(!StringUtils.isEmpty(wareId)){
            //purchase_id  sku_id
            queryWrapper.eq("ware_id",wareId);
        }
    
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );
    
        return new PageUtils(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

    四、合并采购需求

    采购流程如图:
    在这里插入图片描述
    A、查询未领取的采购单

    PurchaseController 中:

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

    PurchaseServiceImpl 中:

    @Override
    public PageUtils queryPageUnreceivePurchase(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );
    
        return new PageUtils(page);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    B、合并采购需求

    MergeVo:

    package com.fancy.gulimall.ware.vo;
    
    import lombok.Data;
    
    import java.util.List;
    
    @Data
    public class MergeVo {
    
       private Long purchaseId; //整单id
       private List<Long> items;//[1,2,3,4] //合并项集合
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    PurchaseController 中:

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

    PurchaseServiceImpl 中:

     @Transactional
     @Override
     public void mergePurchase(MergeVo mergeVo) {
         Long purchaseId = mergeVo.getPurchaseId();
         if(purchaseId == null){
             //1、新建一个
             PurchaseEntity purchaseEntity = new PurchaseEntity();
    
             purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
             purchaseEntity.setCreateTime(new Date());
             purchaseEntity.setUpdateTime(new Date());
             this.save(purchaseEntity);
             purchaseId = purchaseEntity.getId();
         }
    
         //TODO 确认采购单状态是0,1才可以合并
    
         List<Long> items = mergeVo.getItems();
         Long finalPurchaseId = purchaseId;
         List<PurchaseDetailEntity> collect = items.stream().map(i -> {
             PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
    
             detailEntity.setId(i);
             detailEntity.setPurchaseId(finalPurchaseId);
             detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());
             return detailEntity;
         }).collect(Collectors.toList());
    
    
         detailService.updateBatchById(collect);
    
         PurchaseEntity purchaseEntity = new PurchaseEntity();
         purchaseEntity.setId(purchaseId);
         purchaseEntity.setUpdateTime(new Date());
         this.updateById(purchaseEntity);
     }
    
    • 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

    C、领取采购单

    PurchaseController 中:

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

    PurchaseServiceImpl 中实现 received 方法

    @Override
    public void received(List<Long> ids) {
        //1、确认当前采购单是新建或者已分配状态
        List<PurchaseEntity> collect = ids.stream().map(id -> {
            PurchaseEntity byId = this.getById(id);
            return byId;
        }).filter(item -> {
            if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
                    item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIGNED.getCode()) {
                return true;
            }
            return false;
        }).map(item->{
            item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());
    
        //2、改变采购单的状态
        this.updateBatchById(collect);
    
        //3、改变采购项的状态
        collect.forEach((item)->{
            List<PurchaseDetailEntity> entities = detailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> detailEntities = entities.stream().map(entity -> {
                PurchaseDetailEntity entity1 = new PurchaseDetailEntity();
                entity1.setId(entity.getId());
                entity1.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
                return entity1;
            }).collect(Collectors.toList());
            detailService.updateBatchById(detailEntities);
        });
    }
    
    • 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

    PurchaseDetail 中实现:

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        List<PurchaseDetailEntity> purchaseId = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return purchaseId;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    ware 用常量类 WareConstant,在 common vo 中创建

    package com.fancy.common.constant;
    
    public class WareConstant {
    
        public enum  PurchaseStatusEnum{
            CREATED(0,"新建"),ASSIGNED(1,"已分配"),
            RECEIVE(2,"已领取"),FINISH(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,"新建"),ASSIGNED(1,"已分配"),
            BUYING(2,"正在采购"),FINISH(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;
            }
        }
    }
    
    • 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

    D、完成采购

    PurchaseController 中:

    ///ware/purchase/done
    @PostMapping("/done")
    public R finish(@RequestBody PurchaseDoneVo doneVo){
    
        purchaseService.done(doneVo);
    
        return R.ok();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    PurchaseDoneVo、PurchaseItemDoneVo 封装返回数据:

    package com.fancy.gulimall.ware.vo;
    
    import lombok.Data;
    
    import javax.validation.constraints.NotNull;
    import java.util.List;
    
    @Data
    public class PurchaseDoneVo {
    
        @NotNull
        private Long id;//采购单id
    
        private List<PurchaseItemDoneVo> items;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package com.fancy.gulimall.ware.vo;
    
    import lombok.Data;
    
    @Data
    public class PurchaseItemDoneVo {
        //{itemId:1,status:4,reason:""}
        private Long itemId;
        private Integer status;
        private String reason;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    @Transactional
    @Override
    public void done(PurchaseDoneVo doneVo) {
    
        Long id = doneVo.getId();
    
    
        //2、改变采购项的状态
        Boolean flag = true;
        List<PurchaseItemDoneVo> items = doneVo.getItems();
    
        List<PurchaseDetailEntity> updates = new ArrayList<>();
        for (PurchaseItemDoneVo item : items) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            if(item.getStatus() == WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                flag = false;
                detailEntity.setStatus(item.getStatus());
            }else{
                detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
                3、将成功采购的进行入库
                PurchaseDetailEntity entity = detailService.getById(item.getItemId());
                wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
    
            }
            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }
    
        detailService.updateBatchById(updates);
    
        //1、改变采购单状态
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setStatus(flag?WareConstant.PurchaseStatusEnum.FINISH.getCode():WareConstant.PurchaseStatusEnum.HASERROR.getCode());
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);
    }
    
    • 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

    WareSkuServiceImpl 中 addStock 方法

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1、判断如果还没有这个库存记录新增
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(entities == null || entities.size() == 0){
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setStock(skuNum);
            skuEntity.setWareId(wareId);
            skuEntity.setStockLocked(0);
            //TODO 远程查询sku的名字,如果失败,整个事务无需回滚
            //1、自己catch异常
            //TODO 还可以用什么办法让异常出现以后不回滚?高级
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
    
                if(info.getCode() == 0){
                    skuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){
    
            }
            wareSkuDao.insert(skuEntity);
        }else{
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }
    }
    
    • 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

    WareSkuDao 中方法实现
    在这里插入图片描述

    Mapper 映射文件

    在这里插入图片描述
    远程调用接口 ProductFeignService

    package com.fancy.gulimall.ware.feign;
    
    import com.fancy.common.utils.R;
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @FeignClient("gulimall-product")
    public interface ProductFeignService {
    
        /**
         * 1) 让所有请求过网关
         *   1、@FeignClient("gulimall-gateway"): 给gulimall-gateway 所在机器发请求
         *   2、/api/product/skuinfo/info/{skuId}
         *
         * 2)、直接让后台指定服务处理
         *   1、@FeignClient("gulimall-product")
         *   2、/product/skuinfo/info/{skuId}
         * @param skuId
         * @return
         */
        @RequestMapping("/product/skuinfo/info/{skuId}")
        public R info(@PathVariable("skuId") Long skuId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    mongodb 安装
    springboot整合actuator、admin对应用程序进行监控
    ssm springboot关于java mysql关于查询或者添加中文乱码问题
    服务器Debian 12.x中安装Jupyer并配置远程访问
    pandas读取文件
    xray长亭是自动化Web漏洞扫描神器
    深入理解JavaScript ES8的新特性
    Vue前后端交互、生命周期、组件化开发
    【接口幂等性】使用token,Redis保证接口幂等性
    ubuntu20.04+vtd环境搭建
  • 原文地址:https://blog.csdn.net/m0_51111980/article/details/126797579