谷粒商城7——【基础篇完结】商品服务-新增商品 、仓库管理 商品属性的上传 feign远程调用方法 复杂业务逻辑的实现 基础篇完结-小总结

九、商品服务-新增商品

1.调试会员等级相关接口

将会员模块加入到网关路由

image-20220707210824203

2.获取分类相关的品牌

image-20220707211255543

接口文档:

image-20220707211947634

新建一个BrandRespVo来返回数据

package com.henu.soft.merist.gulimall.product.vo;

import lombok.Data;

@Data
public class BrandRespVo {
    private Long brandId;

    private String brandName;
}

===================CategoryBrandRelationController.java==============
    /**
     * 获取当前分类关联的所有品牌列表
     */
    @GetMapping("/brands/list")
    public R brandList (@RequestParam("catId") Long catelogId){
        List<BrandRespVo> brandRespVos= categoryBrandRelationService.getBrandsByCatelogId(catelogId);

        return R.ok().put("data",brandRespVos);
    }
==================CategoryBrandRelationServiceImpl.java======================
    @Override
    public List<BrandRespVo> getBrandsByCatelogId(Long catelogId) {
        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catelogId));
        List<BrandRespVo> brandRespVos = new ArrayList<BrandRespVo>();
        if (categoryBrandRelationEntities.size() != 0 && categoryBrandRelationEntities != null){
            brandRespVos = categoryBrandRelationEntities.stream().map(categoryBrandRelationEntity -> {
                BrandRespVo brandRespVo = new BrandRespVo();
                brandRespVo.setBrandId(categoryBrandRelationEntity.getBrandId());
                BrandEntity brandEntity = brandDao.selectById(categoryBrandRelationEntity.getBrandId());
                if (brandEntity != null) {
                    brandRespVo.setBrandName(brandEntity.getName());
                }
                return brandRespVo;
            }).collect(Collectors.toList());
        }
        return brandRespVos;
    }

image-20220708171925492

3.获取分类下所有分组及属性

image-20220708203713377

==================AttrGroupController.java======================
    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public R withattr(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getCategoryBrandGroupByCatelogId(catelogId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }
=================AttrGroupServiceImpl.java==========================
    @Override
    public List<AttrGroupWithAttrsVo> getCategoryBrandGroupByCatelogId(Long catelogId) {
        //查询基本数据
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));

        //查询attrs
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map( group->{
            AttrGroupWithAttrsVo attrsVo = new AttrGroupWithAttrsVo();
            //存入基本数据
            BeanUtils.copyProperties(group,attrsVo);
            //存入attrs
            List<AttrEntity> relationAttr = attrService.getRelationAttr(group.getAttrGroupId());
            attrsVo.setAttrs(relationAttr);
            return attrsVo;
        }).collect(Collectors.toList());

        return collect;
    }

image-20220708203826454

4.提交商品数据

提交数据,在控制台获取到表单的json数据

image-20220708210057026

image-20220708210049723

在线json转java实体类网站:www.bejson.com

image-20220708210147008

将生成的vo类中的所有价格字段、小数字段都改为BigDecimal数据结构 清空get、set方法增加@Data注解

保存业务流程分析

image-20220708211628956

商品服务调用优惠券积分服务

image-20220708215927991

image-20220708220012851

image-20220708220034994

image-20220708220129218

image-20220708220517102

image-20220708220936120

image-20220708221149083

多条件查询

 @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);
    }

日期格式处理

image-20220708225012818

十、仓库管理

1.仓库维护-查询

仓库查询

image-20220709145541709

在生成的代码上加上检索条件即可

image-20220709145621873

库存查询

image-20220709150542142

采购需求查询

image-20220709150833849

image-20220709152623662

2.合并采购需求

采购简要流程

image-20220709152724779

在【合并到采购单】里获取到所有未被领取的采购单

image-20220709152837101

image-20220709154657005

合并采购需求

image-20220709155534373

在common模块新增一个WareConstant

package com.henu.soft.merist.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;
        }
    }
}

业务逻辑

@Transactional
    @Override
    public void mergePurchase(MergeVo mergeVo) {
        Long purchaseId = mergeVo.getPurchaseId();

        if (purchaseId == null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();

            purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId = purchaseEntity.getId();
        }

        List<Long> items = mergeVo.getItems();
        Long finalPurchaseId = purchaseId;

        List<PurchaseDetailEntity> collect = items.stream().map(i -> {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();

            purchaseDetailEntity.setId(i);
            purchaseDetailEntity.setPurchaseId(finalPurchaseId);
            purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode());

            return purchaseDetailEntity;
        }).collect(Collectors.toList());

        detailService.updateBatchById(collect);

        //时间
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);

    }

3.模拟移动端员工领取采购单

image-20220709185831641

@Override
    public void getReveived(List<Long> ids) {
        //确认采购单是新建或者已分配状态
        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());

        //更新采购单的状态
        this.updateBatchById(collect);

        //改变采购项的状态
        collect.forEach(item -> {
            List<PurchaseDetailEntity> entities = detailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect1 = 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(collect1);
        });
    }

Postman模拟app访问接口

image-20220709185950970

image-20220709190007828

image-20220709190126873

4.完成采购

image-20220709190155551

新建两个vo接收数据

image-20220709202443020

远程调用了product的info

image-20220709202542178

  =======================PurchaseServiceImpl.java======================
	@Transactional
    @Override
    public void postDone(PurchaseDoneVo purchaseDoneVo) {

        Long id = purchaseDoneVo.getId();

        //改变采购项的状态
        Boolean flag = true;//用来判断是否进库
        List<PurchaseItemsDoneVo> items = purchaseDoneVo.getItems();
        List<PurchaseDetailEntity> updates = new ArrayList<>();

        //默认传来的status 只有 3、4 两种状态即 已完成、有错误
        for (PurchaseItemsDoneVo 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());
                //成功入库
                PurchaseDetailEntity entity = purchaseDetailService.getById(item.getItemId());
                wareSkuService.addstock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
            }

            detailEntity.setId(item.getItemId());
            updates.add(detailEntity);
        }

        detailService.updateBatchById(updates);

        //改变采购单的状态

        //将成功采购的进行入库
    }
==============================WareSkuServcieImpl.java============================
	@Override
    public void addstock(Long skuId, Long wareId, Integer skuNum) {
        //库不存在就新增
        List<WareSkuEntity> entities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id",skuId));
        if (entities == null || entities.size() == 0){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
            //TODO 远程查询sku的名字 失败则整个事务无需回滚
            //自己catch异常
            //TODO 还有什么方法让异常出现后不回滚?高级
            try {
                R info = productFeignService.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");

                if (info.getCode() == 0){
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception e){

            }
            wareSkuDao.insert(wareSkuEntity);
        }else {
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }
    }

更新成功

image-20220709202554393

5.获取SPU规格

image-20220709224819340

@Override
public List<ProductAttrValueEntity> getSpuList(Long spuId) {
    return productAttrValueDao.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
}

6.修改SPU规格

image-20220710073731961

@Override
public void updateSpu(Long spuId,List<ProductAttrValueEntity> productAttrValueEntityList) {
    //删除之前的spu
    this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));

    //保存更新的spu
    List<ProductAttrValueEntity> collect = productAttrValueEntityList.stream().map(entity -> {
        entity.setSpuId(spuId);
        return entity;
    }).collect(Collectors.toList());
    this.saveBatch(collect);

}

十一、基础篇完结——总结

image-20220710100727209

理论篇章包括:

代码CRUD包括:

在理论篇章,一些环境的搭建、测试都会随着后续版本的提升 或者在入职公司后开发版本的不同 而产生不同的错误信息。所以更要掌握的是在官网学习的能力,独自阅读文档的能力。在部署、测试时产生的经验往往都是面试时的谈资。同时为了准备面试也要开始背八股文了。

5、6、7篇都是代码的CRUD,其中还包含一些配置性的代码,比如网关的配置、OSS对象存储等等。CRUD写到后面已经可以自己根据接口开发了,最近想利用空闲时间根据renren-fast开发一个比较新颖的项目,有意的同学可以联系我一起想点子,做一个自己的项目。

作为一名准大三生,这个暑假才开始考驾驶证,不过等车的时候就是个背八股文、看面经、夯实基础的好机会,不学车的时候就做项目、敲代码。加油吧~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HotRabbit.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值