1.给我们的ware服务开启功能
2.路由网关配置
3.给我们的仓库维护带上模糊查询条件
@RequestMapping("/list")
//@RequiresPermissions("ware:wareinfo:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = wareInfoService.queryPageByCondition(params);
return R.ok().put("page", page);
}
@Override
public PageUtils queryPageByCondition(Map<String, Object> params) {
QueryWrapper<WareInfoEntity> wrapper = new QueryWrapper<>();
String key= (String) params.get("key");
if(!StringUtils.isEmpty(key)){
wrapper.and(w->{
w.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),wrapper
);
return new PageUtils(page);
}
4.修改日志等级
5.商品的库存功能
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
String skuId= (String) params.get("skuId");
if(!StringUtils.isEmpty(skuId)){
wrapper.eq("sku_id",skuId);
}
String wareId=(String) params.get("wareId");
if(!StringUtils.isEmpty(wareId)){
wrapper.eq("ware_id",wareId);
}
IPage<WareSkuEntity> page = this.page(
new Query<WareSkuEntity>().getPage(params),wrapper
);
return new PageUtils(page);
}
6.采购单维护
采购需求检索:
@Override
public PageUtils queryPage(Map<String, Object> params) {
QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
String key=(String) params.get("key");
if(!StringUtils.isEmpty(key)){
wrapper.and(w->{
w.eq("purchase_id",key).or().eq("sku_id",key);
});
}
String status=(String) params.get("status");
if(!StringUtils.isEmpty(status)){
wrapper.eq("status",status);
}
String wareId=(String) params.get("wareId");
if(!StringUtils.isEmpty(wareId)){
wrapper.eq("ware_id",wareId);
}
IPage<PurchaseDetailEntity> page = this.page(
new Query<PurchaseDetailEntity>().getPage(params),wrapper
);
return new PageUtils(page);
}
7.合并采购需求
@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);
}
@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);
}
在common模块的constant里添加枚举类
public class WareConstant {
public enum WareStatusEnum{
CREATED(0,"新建"),
ASSIGNED(1,"已分配"),
RECEIVE(2,"已领取"),
FINISH(3,"已完成"),
ERROR(4,"有异常");
private int code;
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
WareStatusEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
public enum WareDetailStatusEnum{
CREATED(0,"新建"),
ASSIGNED(1,"已分配"),
BUYING(2,"正在采购"),
FINISH(3,"已完成"),
ERROR(4,"采购失败");
private int code;
private String msg;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
WareDetailStatusEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
}
}
以下操作都在ware模块里
添加vo
@Data
public class MergeVo {
private Long purchaseId;
private List<Long> items;
}
purchaseController
@PostMapping("/merge")
public R merge(@RequestBody MergeVo mergeVo){
purchaseService.mergePurchase(mergeVo);
return R.ok();
}
@Override
@Transactional
public void mergePurchase(MergeVo mergeVo) {
Long purchaseId = mergeVo.getPurchaseId();
//如果没有就新建一个采购单
if (purchaseId == null) {
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setStatus(WareConstant.WareStatusEnum.CREATED.getCode());//新建状态
purchaseEntity.setCreateTime(new Date());
purchaseEntity.setUpdateTime(new Date());
this.save(purchaseEntity);
purchaseId = purchaseEntity.getId();//没有id的时候是通过保存了之后获取得到的
} else {
List<Long> items = mergeVo.getItems();
Long id = purchaseId;
List<PurchaseDetailEntity> collect = items.stream().map((i) -> {
PurchaseDetailEntity entity = new PurchaseDetailEntity();
entity.setId(i);
entity.setPurchaseId(id);
entity.setStatus(WareConstant.WareDetailStatusEnum.ASSIGNED.getCode());
return entity;
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(collect);
//更新一下时间
PurchaseEntity entity = new PurchaseEntity();
entity.setId(purchaseId);
entity.setUpdateTime(new Date());
this.updateById(entity);
}
}
对controller的save方法做了如下修改
@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();
}
效果展示
合并采购单后采购需求
合并后采购单改变状态与更新时间
8.领取采购单
使用postman模拟一个手机app发送request请求
/**
* 领取采购单
*/
@PostMapping("/received")
public R received(@RequestBody List<Long> ids){
purchaseService.received(ids);
return R.ok();
}
@Override
public void received(List<Long> ids) {//采购单的id
//1.确认当前的采购单是新建或者已分配状态
List<PurchaseEntity> collect = ids.stream().map(id -> {
PurchaseEntity byId = this.getById(id);
return byId;
}).filter(i -> {
if (i.getStatus() == WareConstant.WareStatusEnum.ASSIGNED.getCode() ||
i.getStatus() == WareConstant.WareStatusEnum.CREATED.getCode()) {
return true;
}
return false;
}).peek(item -> {
item.setStatus(WareConstant.WareStatusEnum.RECEIVE.getCode());
item.setUpdateTime(new Date());//更新时间也需要改
}).collect(Collectors.toList());
//2.改变采购单的状态
this.updateBatchById(collect);
//3.改变采购项的状态
List<Long> byId = collect.stream().map(item -> {
return item.getId();
}).collect(Collectors.toList());
List<PurchaseDetailEntity> purchaseDetailEntities = purchaseDetailService.list(new QueryWrapper<PurchaseDetailEntity>().in("purchase_id", byId))
.stream().peek(item -> {
item.setStatus(WareConstant.WareDetailStatusEnum.BUYING.getCode());
}).collect(Collectors.toList());
purchaseDetailService.updateBatchById(purchaseDetailEntities);
}
发送请求后,指定的已分配采购单变成领取状态
9.完成采购单
配置ware的mybatis内容
@EnableTransactionManagement//开启事务
@Configuration
public class WareMybatisConfig {
//引入分页插件
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor=new PaginationInterceptor();
paginationInterceptor.setOverflow(true);//请求页面大于最后页面 false为默认-请求到空数据 true--跳到第一页
paginationInterceptor.setLimit(1000);//每页最大受限1000条 -1不受限制
return paginationInterceptor;
}
}
ware模块中封装两个vo
public class PurchaseDoneItemVo {
private Long itemId;
private Integer status;
private String reason;
}
public class PurchaseDoneVo {
@NotNull
private Long id;//采购id
List<PurchaseDoneItemVo> items;
}
controller-purchaseController
@PostMapping("/done")
public R finish(@RequestBody PurchaseDoneVo vo){
purchaseService.Done(vo);
return R.ok();
}
@Transactional
@Override
public void Done(PurchaseDoneVo vo) {
Long id = vo.getId();
//1.改变采购项的状态
List<PurchaseDoneItemVo> items=vo.getItems();
ArrayList<PurchaseDetailEntity> detailEntities = new ArrayList<>();
Boolean flag=true;//标志位,如果有失败的话就改成false
for(PurchaseDoneItemVo item:items){
PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
if(item.getStatus()==WareConstant.WareDetailStatusEnum.ERROR.getCode()){
flag=false;
detailEntity.setStatus(item.getStatus());
}else{
detailEntity.setStatus(WareConstant.WareDetailStatusEnum.FINISH.getCode());
//2.将成功采购的商品进行入库
PurchaseDetailEntity byId = purchaseDetailService.getById(item.getItemId());//这里循环查库了,但是好像也没办法优化
wareSkuService.addStock(byId.getSkuId(),byId.getWareId(),byId.getSkuNum());
}
detailEntity.setId(item.getItemId());
detailEntities.add(detailEntity);
}
purchaseDetailService.updateBatchById(detailEntities);
//3.改变采购单的状态
PurchaseEntity purchaseEntity = new PurchaseEntity();
purchaseEntity.setId(vo.getId());
purchaseEntity.setStatus(flag?WareConstant.WareStatusEnum.FINISH.getCode():WareConstant.WareStatusEnum.ERROR.getCode());
purchaseEntity.setUpdateTime(new Date());
this.updateById(purchaseEntity);
}
来到addstock方法,接口上的方法我就省略没写
@Override
public void addStock(Long skuId, Long wareId, Integer skuNum) {
//判断如果没有这个库存记录的话就新增
List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
if(wareSkuEntities==null||wareSkuEntities.size()==0){
WareSkuEntity wareSkuEntity = new WareSkuEntity();
wareSkuEntity.setStock(skuNum);
wareSkuEntity.setSkuId(skuId);
wareSkuEntity.setWareId(wareId);
wareSkuDao.insert(wareSkuEntity);
}else{
wareSkuDao.addStock(skuId,wareId,skuNum);
}
}
@Mapper
public interface WareSkuDao extends BaseMapper<WareSkuEntity> {
void addStock(@Param("skuId") Long skuId, @Param("wareId") Long wareId, @Param("skuNum") Integer skuNum);
}
<update id="addStock">
Update wms_ware_sku set stock=stock+#{skuNum} where sku_id=#{skuId} and ware_id=#{wareId}
</update>
测试:
细化addStock方法
远程调用查询sku的名字
ware模块中主函数添加注解
feign中增加远程接口
feignservice其实有两种写法加/api前缀或者是不加
加的话就是给网关发请求,不加就是给模块发请求
@Override
public void addStock(Long skuId, Long wareId, Integer skuNum) {
//判断如果没有这个库存记录的话就新增
List<WareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
if(wareSkuEntities==null||wareSkuEntities.size()==0){
WareSkuEntity wareSkuEntity = new WareSkuEntity();
wareSkuEntity.setStock(skuNum);
wareSkuEntity.setSkuId(skuId);
wareSkuEntity.setWareId(wareId);
wareSkuEntity.setStockLocked(0);
//远程方法查询sku的名字
//因为冗余字段而引起的事务回滚是不划算的,所以我们拿住异常让他继续走下去
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);
}
}
10.spu规格维护
这是product中的功能
@GetMapping("/base/listforspu/{spuId}")
public R baseAttrListForSpu(@PathVariable("spuId") Long spuId) {
List<ProductAttrValueEntity> data= productAttrValueService.baseAttrListForSpu(spuId);
return R.ok().put("data", data);
}
@Override
public List<ProductAttrValueEntity> baseAttrListForSpu(Long spuId) {
List<ProductAttrValueEntity> list = this.baseMapper.selectList(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
return list;
}
数据库回显bug
点击确认修改:
@RequestMapping("/update/{spuId}")
//@RequiresPermissions("product:attr:update")
public R updateSpuAttr(@PathVariable("spuId") Long spuId
,@RequestBody List<ProductAttrValueEntity> list) {//使用RequestBody转换json数据
productAttrValueService.updateSpuAttr(spuId,list);
return R.ok();
}
@Transactional
@Override
public void updateSpuAttr(Long spuId, List<ProductAttrValueEntity> list) {
//由于有些属性可能有然后没了,有些属性可能没了但是又有了就不用update然后判断是插入还是删除
//先删除之前所有spuid对应的属性
this.baseMapper.delete(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
//插入操作
List<ProductAttrValueEntity> collect = list.stream().peek(item -> item.setSpuId(spuId)).collect(Collectors.toList());
this.saveBatch(collect);
}