前言
- 学习视频:Java项目《谷粒商城》架构师级Java项目实战,对标阿里P6-P7,全网最强
- 学习文档:
-
接口文档:谷粒商城接口文档
- 本内容仅用于个人学习笔记,如有侵扰,联系删除
2、库存系统
2.1、仓库维护
页面
2.1.1、仓库列表
接口文档:库存系统 - 01、仓库列表
GET /ware/wareinfo/list
请求参数
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
key: '华为'//检索关键字
}
分页数据
响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"id": 2,
"name": "aa",
"address": "bbb",
"areacode": "124"
}]
}
}
功能效果
修改“com.atguigu.gulimall.ware.controller.WareSkuController”类,代码如下:
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = wareSkuService.queryPage(params);
return R.ok().put("page", page);
}
修改“com.atguigu.gulimall.ware.service.WareInfoService”类,代码如下:
PageUtils queryPage(Map<String, Object> params);
修改“com.atguigu.gulimall.ware.service.impl.WareInfoServiceImpl”类,代码如下:
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<WareInfoEntity> queryWrapper = new QueryWrapper<>();
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
queryWrapper.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),
queryWrapper
);
return new PageUtils(page);
}
2.2、商品库存
页面
2.2.1、查询商品库存
接口文档:库存系统 - 02、查询商品库存
GET /ware/waresku/list
请求参数
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
wareId: 123,//仓库id
skuId: 123//商品id
}
分页数据
响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"id": 1,
"skuId": 1,
"wareId": 1,
"stock": 1,
"skuName": "dd",
"stockLocked": 1
}]
}
}
功能效果
修改“com.atguigu.gulimall.ware.controller.WareSkuController”类,代码如下:
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = wareSkuService.queryPage(params);
return R.ok().put("page", page);
}
修改“com.atguigu.gulimall.ware.service.impl.WareSkuServiceImpl”类,代码如下:
PageUtils queryPage(Map<String, Object> params);
修改“com.atguigu.gulimall.ware.service.impl.WareSkuServiceImpl”类,代码如下:
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<WareSkuEntity> queryWapper = new QueryWrapper<>();
String skuId = (String) params.get("skuId");
if (!StringUtils.isEmpty(skuId)) {
queryWapper.eq("sku_id", skuId);
}
String wareId = (String) params.get("wareId");
if (!StringUtils.isEmpty(wareId)) {
queryWapper.eq("ware_id", wareId);
}
IPage<WareSkuEntity> page = this.page(new Query<WareSkuEntity>().getPage(params), queryWapper);
return new PageUtils(page);
}
2.3、采购单维护
采购简要流程
2.3.1、采购单需求
页面
1)、查询采购需求
接口文档:库存系统 - 03、查询采购需求
GET /ware/purchasedetail/list
请求参数
{
page: 1,//当前页码
limit: 10,//每页记录数
sidx: 'id',//排序字段
order: 'asc/desc',//排序方式
key: '华为',//检索关键字
status: 0,//状态
wareId: 1,//仓库id
}
分页数据
响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"id": 2,
"purchaseId": 1,
"skuId": 1,
"skuNum": 2,
"skuPrice": 22.0000,
"wareId": 1,
"status": 1
}]
}
}
功能效果
修改“com.atguigu.gulimall.ware.controller.PurchaseDetailController”类,代码如下:
/**
* 列表
*/
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = purchaseDetailService.queryPage(params);
return R.ok().put("page", page);
}
修改“com.atguigu.gulimall.ware.service.impl.PurchaseDetailServiceImpl”类,代码如下:
@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);
}
2)、查询未领取的采购单
接口文档:库存系统 - 05、查询未领取的采购单
GET /ware/purchase/unreceive/list
请求参数
响应数据
{
"msg": "success",
"code": 0,
"page": {
"totalCount": 0,
"pageSize": 10,
"totalPage": 0,
"currPage": 1,
"list": [{
"id": 1,
"assigneeId": 1,
"assigneeName": "aa",
"phone": "123",
"priority": 1,
"status": 1,
"wareId": 1,
"amount": 22.0000,
"createTime": "2019-12-12",
"updateTime": "2019-12-12"
}]
}
}
功能效果
修改“com.atguigu.gulimall.ware.controller.PurchaseController”类,代码如下:
// /ware/purchase/unreceive/list
@RequestMapping("/unreceive/list")
public R unreceiveList(@RequestParam Map<String, Object> params){
PageUtils page = purchaseService.queryPageUnreceive(params);
return R.ok().put("page", page);
}
修改“com.atguigu.gulimall.ware.service.PurchaseService”类,代码如下:
PageUtils queryPageUnreceive(Map<String, Object> params);
修改“com.atguigu.gulimall.ware.service.impl.PurchaseServiceImpl”类,代码如下:
@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);
}
3)、合并采购需求
接口文档:库存系统 - 04、合并采购需求
POST /ware/purchase/merge
请求参数
{
purchaseId: 1, //整单id
items:[1,2,3,4] //合并项集合
}
分页数据
响应数据
{
"msg": "success",
"code": 0
}
功能效果
修改“com.atguigu.gulimall.ware.controller.PurchaseController”类,代码如下:
@PostMapping("/merge")
public R merge(@RequestBody MergeVo mergeVo){
purchaseService.mergePurchase(mergeVo);
return R.ok();
}
修改“com.atguigu.gulimall.ware.service.PurchaseService”类,代码如下:
void mergePurchase(MergeVo mergeVo);
修改“com.atguigu.gulimall.ware.service.impl.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());
purchaseDetailService.updateBatchById(collect);
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setId(purchaseId);
purchaseEntity.setUpdateTime(new Date());
this.updateById(purchaseEntity);
}
4)、领取采购单
接口文档:库存系统 - 06、领取采购单
POST /ware/purchase/received
请求参数
[1,2,3,4]//采购单id
分页数据
响应数据
{
"msg": "success",
"code": 0
}
修改“com.atguigu.gulimall.ware.controller.PurchaseController”类,代码如下:
/**
* 领取采购单
*/
@PostMapping("/received")
public R received(@RequestBody List<Long> ids){
purchaseService.received(ids);
return R.ok();
}
修改“com.atguigu.gulimall.ware.service.PurchaseService”类,代码如下:
void received(List<Long> ids);
修改“com.atguigu.gulimall.ware.service.impl.PurchaseServiceImpl”类,代码如下:
@Override
public void received(List<Long> ids) {
// 1、确认当前采购单是新建或者已分配状态
List<PurchaseEntity> collect = ids.stream().map(id -> {
PurchaseEntity purchaseEntity = this.getById(id);
return purchaseEntity;
}).filter(item -> {
if (item.getStatus() == WareConstant.PurchaseStatusEnum.CREATED.getCode() ||
item.getStatus() == WareConstant.PurchaseStatusEnum.ASSIIGNED.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> detailEntities = purchaseDetailService.listDetailByPurchaseId(item.getId());
List<PurchaseDetailEntity> entities = detailEntities.stream().map(entity -> {
PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
detailEntity.setId(entity.getId());
detailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
return detailEntity;
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(entities);
});
}
修改“com.atguigu.gulimall.ware.service.PurchaseDetailService”类,代码如下:
List<PurchaseDetailEntity> listDetailByPurchaseId(Long id);
修改“com.atguigu.gulimall.ware.service.impl.PurchaseDetailServiceImpl”类,代码如下:
@Override
public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
List<PurchaseDetailEntity> purchaseDetails = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
return purchaseDetails;
}
2.3.2、采购单
页面
1)、完成采购
接口文档:库存系统 - 07、完成采购
POST /ware/purchase/done
请求参数
{
id: 123,//采购单id
items: [{itemId:1,status:4,reason:""}]//完成/失败的需求详情
}
响应数据
{
"msg": "success",
"code": 0
}
修改“com.atguigu.gulimall.ware.controller.PurchaseController”类,代码如下:
@PostMapping("/done")
public R finish(@RequestBody PurchaseDoneVo doneVo){
purchaseService.done(doneVo);
return R.ok();
}
修改“com.atguigu.gulimall.ware.controller.PurchaseController”类,代码如下:
void done(PurchaseDoneVo doneVo);
修改“com.atguigu.gulimall.ware.service.impl.PurchaseServiceImpl”类,代码如下:
@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 = purchaseDetailService.getById(item.getItemId());
wareSkuService.addStock(entity.getSkuId(),entity.getWareId(),entity.getSkuNum());
}
detailEntity.setId(item.getItemId());
updates.add(detailEntity);
}
purchaseDetailService.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);
}
3、将成功采购的进行入库
修改“com.atguigu.gulimall.ware.service.WareSkuService”类,代码如下:
void addStock(Long skuId, Long wareId, Integer skuNum);
修改“com.atguigu.gulimall.ware.service.impl.WareSkuServiceImpl”类,代码如下:
@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 wareSkuEntity = new WareSkuEntity();
wareSkuEntity.setId(skuId);
wareSkuEntity.setStock(skuNum);
wareSkuEntity.setWareId(wareId);
wareSkuEntity.setStockLocked(0);
//TODO 远程查询sku的名字 如果失败,整个事务不需要回滚
// 1、自己catch异常
// 2、TODO 还可以用什么办法让异常出现以后不回滚?高级
try{
R info = productFeignService.info(skuId);
if (info.getCode() == 0){
Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
wareSkuEntity.setSkuName((String) data.get("skuName"));
}
}catch (Exception e){
}
wareSkuDao.insert(wareSkuEntity);
}else {
wareSkuDao.addStock(skuId, wareId, skuNum);
}
}
TODO 远程查询sku的名字 如果失败,整个事务不需要回滚
修改“com.atguigu.gulimall.ware.feign.ProductFeignService”类,代码如下:
package com.atguigu.gulimall.ware.feign;
import com.atguigu.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author WangTianShun
* @date 2020/10/17 14:20
*/
@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);
}
修改“com.atguigu.gulimall.ware.dao.WareSkuDao”类,代码如下:
void addStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);
修改“com.atguigu.gulimall.ware.dao.WareSkuDao”类,代码如下:
<update id="addStock">
update wms_ware_sku set stock = stock + #{skuNum} where sku_id = #{skuId} and ware_id = #{wareId}
</update>