整合Ware服务
将服务注册到nacos中
spring:
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: gulimall-ware
@MapperScan("com.atguigu.gulimall.ware.dao")//mybatis包扫描
@SpringBootApplication
@EnableDiscoveryClient//开启服务发现
@EnableTransactionManagement //开启事务管理
修改网关路由
- id: ware
uri: lb://gulimall-ware
predicates:
- Path=/api/ware/**
filters:
- RewritePath=/api/(?<segment>/?.*), /$\{segment}
仓库维护的模糊查询
在WareInfoController中
@RequestMapping("/list")
// @RequiresPermissions("ware:wareinfo:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = wareInfoService.queryPageByConfitions(params);
return R.ok().put("page", page);
}
编写queryPageByConfitions(params)方法
@Override
public PageUtils queryPageByConfitions(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);
}
商品库存的模糊查询
实现效果:
在WareSkuController中
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("ware:waresku:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = wareSkuService.queryPageByConditions(params);
return R.ok().put("page", page);
}
编写queryPageByConditions(params)方法
@Override
public PageUtils queryPageByConditions(Map<String, Object> params) {
QueryWrapper<WareSkuEntity> wareSkuEntityQueryWrapper = new QueryWrapper<>();
// wareId: 123,//仓库id
// skuId: 123//商品id
String wareId = (String) params.get("wareId");
if (!StringUtils.isEmpty(wareId)){
wareSkuEntityQueryWrapper.eq("ware_id",wareId);
}
String skuId = (String) params.get("skuId");
if (!StringUtils.isEmpty(skuId)){
wareSkuEntityQueryWrapper.eq("sku_id",skuId);
}
IPage<WareSkuEntity> page = this.page(
new Query<WareSkuEntity>().getPage(params),
wareSkuEntityQueryWrapper
);
return new PageUtils(page);
}
采购需求的模糊查询
使用新增创建一个采购需求
并进行模糊查询
在PurchaseDetailController中
@RequestMapping("/list")
// @RequiresPermissions("ware:purchasedetail:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = purchaseDetailService.queryPageByConditions(params);
return R.ok().put("page", page);
}
创建方法queryPageByConditions(params);
@Override
public PageUtils queryPageByConditions(Map<String, Object> params) {
// key:'华为',//检索关键字
// status:0,//状态
// wareId:1,//仓库id
QueryWrapper<PurchaseDetailEntity> purchaseDetailEntityQueryWrapper = new QueryWrapper<>();
String key = (String) params.get("key");
if (!StringUtils.isEmpty(key)){
purchaseDetailEntityQueryWrapper.and((queryWrapper)->{
queryWrapper.eq("id",key).or().eq("sku_id",key);
});
}
String status = (String) params.get("status");
if (!StringUtils.isEmpty(status)){
purchaseDetailEntityQueryWrapper.eq("status",status);
}
String wareId = (String) params.get("wareId");
if (!StringUtils.isEmpty(wareId)){
purchaseDetailEntityQueryWrapper.eq("ware_id",wareId);
}
IPage<PurchaseDetailEntity> page = this.page(
new Query<PurchaseDetailEntity>().getPage(params),
purchaseDetailEntityQueryWrapper
);
return new PageUtils(page);
}
合并采购需求
点击合并页面会发送一个请求
一、查询采购单
在PurchaseController中
@RequestMapping("/unreceive/list")
// @RequiresPermissions("ware:purchase:list")
public R Unreceivelist(@RequestParam Map<String, Object> params){
PageUtils page = purchaseService.queryPageUnreceive(params);
return R.ok().put("page", page);
}
编写queryPageUnreceive(params)方法
@Override
public PageUtils queryPageUnreceive(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);
}
效果展示
二、合并采购单
点击合并,发送请求
创建一个vo对象接受参数
@Data
public class MergeVo {
private Long purchaseId; //整单id
private List<Long> items; //合并项集合
}
在PurchseController中
@RequestMapping("/merge")
// @RequiresPermissions("ware:purchase:delete")
public R merge(@RequestBody MergeVo mergeVo){
purchaseService.mergePurchase(mergeVo);
return R.ok();
}
编写mergePurchase(mergeVo)方法
@Transactional
@Override
public void mergePurchase(MergeVo mergeVo) {
Long purchaseId = mergeVo.getPurchaseId();
//判断要合并到的采购单是否存在
if (purchaseId==null){
//需要创建一个采购单
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setCreateTime(new Date());
purchaseEntity.setUpdateTime(new Date());
purchaseEntity.setStatus(WareConstant.PurchaseStatusEnum.CREATED.getCode());
this.save(purchaseEntity);
//新建完采购单后,就有采购单id
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());
//进行更新就可以
purchaseDetailService.updateBatchById(collect);
//修改更新时间,采购单id
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setId(purchaseId);
purchaseEntity.setUpdateTime(new Date());
this.updateById(purchaseEntity);
}
}
并将原来的创建采购单的方法也设置创建事件和更新时间
@RequestMapping("/save")
//@RequiresPermissions("ware:purchase:save")
public R save(@RequestBody PurchaseEntity purchase){
purchase.setCreateTime(new Date());
purchase.setUpdateTime(new Date());
purchaseService.save(purchase);
return R.ok();
}
创建一个枚举类用来存放订单的状态
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;
}
}
}
领取采购单
使用postman模拟人工领取采购单http://localhost:88/api/ware/purchase/received
在PurchaseController中
@PostMapping("received")
public R received(@RequestBody List<Long> ids){
purchaseService.receiverByIds(ids);
return R.ok();
}
@Override
public void receiverByIds(List<Long> ids) {
//修改采购单的状态
List<PurchaseEntity> purchaseEntity= ids.stream().map((id) -> {
PurchaseEntity byId = this.getById(id);
return byId;
//过滤出status为0或1的
}).filter((item)->{
if (item.getStatus()==WareConstant.PurchaseStatusEnum.CREATED.getCode()||
item.getStatus()==WareConstant.PurchaseDetailStatusEnum.ASSIGNED.getCode()){
return true;
}
return false;
}).collect(Collectors.toList());
//修改他的状态
purchaseEntity.forEach(item->{
item.setUpdateTime(new Date());
item.setStatus(WareConstant.PurchaseStatusEnum.RECEIVE.getCode());
this.updateById(item);
//改变采购需求的状态
List<PurchaseDetailEntity> entities=purchaseDetailService.listDetailByPuchersId(item.getId());
List<PurchaseDetailEntity> collect = entities.stream().map(entity -> {
PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.BUYING.getCode());
purchaseDetailEntity.setId(entity.getId());
return purchaseDetailEntity;
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(collect);
});
}
}
创建方法List listDetailByPuchersId(Long id);
@Override
public List<PurchaseDetailEntity> listDetailByPuchersId(Long id) {
//查询采购需求的状态
List<PurchaseDetailEntity> purchase_id = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
return purchase_id;
}
效果展示:
完成采购
使用postman发送请求http://localhost:88/api/ware/purchase/done
{
"id": 1,
"items": [
{"itemId":1,"status":3,"reason":""},
{"itemId":2,"status":4,"reason":"无货"}
]
}
在PurchaseController中
///ware/purchase/done
@PostMapping("/done")
public R done(@RequestBody PurchaseDoneVo purchaseDoneVo){
purchaseService.done(purchaseDoneVo);
return R.ok();
}
编写done(purchaseDoneVo)方法
@Override
public void done(PurchaseDoneVo purchaseDoneVo) {
Long id = purchaseDoneVo.getId();
//1、改变采购需求的状态
Boolean flag=true;
List<PurchaseDetailEntity> purchaseDetailEntityList=new ArrayList<>();
List<PurchaseItemDoneVo> items = purchaseDoneVo.getItems();
for (PurchaseItemDoneVo item : items) {
PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
if (item.getStatus()==WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode()){
//失败的
flag=false;
purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.HASERROR.getCode());
}
else{
//已完成
purchaseDetailEntity.setStatus(WareConstant.PurchaseDetailStatusEnum.FINISH.getCode());
//3、将成功采购的入库
//传参:采购单id,采购数量,仓库id
PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());
Long skuId = byId.getSkuId();
Integer skuNum = byId.getSkuNum();
Long wareId = byId.getWareId();
wareSkuService.addStock(skuId,skuNum,wareId);
}
purchaseDetailEntity.setId(item.getItemId());
purchaseDetailEntityList.add(purchaseDetailEntity);
}
purchaseDetailService.updateBatchById(purchaseDetailEntityList);
//2、改变采购单的状态--通过上面的flag决定这个采购单的状态
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);
}
编写addStock(Long skuId, Integer skuNum, Long wareId)方法
@Transactional
@Override
public void addStock(Long skuId, Integer skuNum, Long wareId) {
List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
if (list==null||list.size()==0){
//如果没有则需要新增
WareSkuEntity wareSkuEntity=new WareSkuEntity();
wareSkuEntity.setSkuId(skuId);
wareSkuEntity.setWareId(wareId);
wareSkuEntity.setStock(skuNum);
wareSkuEntity.setStockLocked(0);
//skuname没有设置
try {
R info = productFeignService.info(skuId);
if (info.getCode() == 0) {
//查询成功
Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
wareSkuEntity.setSkuName((String) skuInfo.get("skuName"));
}
}catch (Exception e){
}
this.baseMapper.insert(wareSkuEntity);
}
else{
//如果库存中有skuId和wareId,则直接update
this.baseMapper.addStock(skuId,skuNum,wareId);
}
}
由于设置spuName需要调用查询name
远程调用
@FeignClient("gulimall-product")
public interface ProductFeignService {
//查询sku信息
@RequestMapping("/product/skuinfo/info/{skuId}")
R info(@PathVariable("skuId") Long skuId);
}
获取spu规格
当点击规格页面报错则需要在src/router/index.js添加
{ path: '/product-attrupdate', component: _import('modules/product/attrupdate'), name: 'attr-update', meta: { title: '规格维护', isTab: true } }
在AttrController中
@GetMapping("/base/listforspu/{spuId}")
public R getSpu(@PathVariable("spuId") Long spuId){
List<ProductAttrValueEntity> list=productAttrValueService.baseAttrListForSpu(spuId);
return R.ok().put("data",list);
}
编写baseAttrListForSpu(spuId);方法
@Override
public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
List<ProductAttrValueEntity> list = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
return list;
}
规格维护
在AttrController中
///product/attr/update/{spuId}
@PostMapping("/update/{spuId}")
public R updateSpu(@PathVariable("spuId") Long spuId,
@RequestBody List<ProductAttrValueEntity> entityList){
productAttrValueService.updateBySpuId(spuId,entityList);
return R.ok();
}
编写方法updateBySpuId(spuId,entityList)
@Override
public void updateBySpuId(Long spuId, List<ProductAttrValueEntity> entityList) {
//先将存在的删除,然后在新增
this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
List<ProductAttrValueEntity> collect = entityList.stream().map((item) -> {
item.setSpuId(spuId);
return item;
}).collect(Collectors.toList());
this.saveBatch(collect);
}
}