feat 完善小程序订单管理
This commit is contained in:
parent
4c9bc37771
commit
e68e7ef115
@ -1,13 +1,21 @@
|
|||||||
package me.xiaoyan.point.api.controller;
|
package me.xiaoyan.point.api.controller;
|
||||||
|
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.xiaoyan.point.api.error.BizException;
|
import me.xiaoyan.point.api.error.BizException;
|
||||||
import me.xiaoyan.point.api.pojo.OrderInfo;
|
import me.xiaoyan.point.api.pojo.OrderInfo;
|
||||||
|
import me.xiaoyan.point.api.pojo.UserInfo;
|
||||||
import me.xiaoyan.point.api.pojo.vo.CreateOrderData;
|
import me.xiaoyan.point.api.pojo.vo.CreateOrderData;
|
||||||
|
import me.xiaoyan.point.api.pojo.vo.OrderQueryParam;
|
||||||
|
import me.xiaoyan.point.api.pojo.vo.PageDataResult;
|
||||||
import me.xiaoyan.point.api.service.GoodsService;
|
import me.xiaoyan.point.api.service.GoodsService;
|
||||||
import me.xiaoyan.point.api.service.OrderInfoService;
|
import me.xiaoyan.point.api.service.OrderInfoService;
|
||||||
|
import me.xiaoyan.point.api.util.OrderStatus;
|
||||||
|
import me.xiaoyan.point.api.util.QueryWrapperUtil;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -47,9 +55,9 @@ public class ShopOrderInfoController {
|
|||||||
//TODO 应该定时更新缓存数据
|
//TODO 应该定时更新缓存数据
|
||||||
goodsService.queryAllGoodsIdAndStock().forEach(g -> {
|
goodsService.queryAllGoodsIdAndStock().forEach(g -> {
|
||||||
log.info("缓存 id:{} stock:{} ", g.getId(), g.getStock());
|
log.info("缓存 id:{} stock:{} ", g.getId(), g.getStock());
|
||||||
if(g.getStock() == 0){
|
if (g.getStock() == 0) {
|
||||||
// 原本就没有库存
|
// 原本就没有库存
|
||||||
stockOutMap.put(g.getId(),true);
|
stockOutMap.put(g.getId(), true);
|
||||||
}
|
}
|
||||||
// 缓存库存
|
// 缓存库存
|
||||||
stringRedisTemplate.opsForValue().set(cacheKey(g.getId()), g.getStock().toString());
|
stringRedisTemplate.opsForValue().set(cacheKey(g.getId()), g.getStock().toString());
|
||||||
@ -64,7 +72,7 @@ public class ShopOrderInfoController {
|
|||||||
int uid = StpUtil.getLoginIdAsInt();
|
int uid = StpUtil.getLoginIdAsInt();
|
||||||
// 如果限制用户只能购买的数量,可以添加一个map记录用户的请求数
|
// 如果限制用户只能购买的数量,可以添加一个map记录用户的请求数
|
||||||
int size = stockOutMap.size();
|
int size = stockOutMap.size();
|
||||||
Long buyId = (long)data.getGoodsId();
|
Long buyId = (long) data.getGoodsId();
|
||||||
//1.内存判断
|
//1.内存判断
|
||||||
if (size > 0 && stockOutMap.containsKey(buyId) && stockOutMap.get(buyId)) {
|
if (size > 0 && stockOutMap.containsKey(buyId) && stockOutMap.get(buyId)) {
|
||||||
throw BizException.create("库存不足");
|
throw BizException.create("库存不足");
|
||||||
@ -74,22 +82,59 @@ public class ShopOrderInfoController {
|
|||||||
if (count < 0) {
|
if (count < 0) {
|
||||||
log.info("stock count ===>" + count);
|
log.info("stock count ===>" + count);
|
||||||
// 对缓存进行库存 还原
|
// 对缓存进行库存 还原
|
||||||
stringRedisTemplate.opsForValue().increment(cacheKey(buyId),data.getBuyCount()); //
|
stringRedisTemplate.opsForValue().increment(cacheKey(buyId), data.getBuyCount()); //
|
||||||
throw BizException.create("库存不足");
|
throw BizException.create("库存不足");
|
||||||
}
|
}
|
||||||
if(count == 0){
|
if (count == 0) {
|
||||||
// 此时库存没有了 , 保存到已买完的对象
|
// 此时库存没有了 , 保存到已买完的对象
|
||||||
stockOutMap.put(buyId,true);
|
stockOutMap.put(buyId, true);
|
||||||
}
|
}
|
||||||
//3.数据库
|
//3.数据库
|
||||||
try{
|
try {
|
||||||
return orderInfoService.create(uid, data);
|
return orderInfoService.create(uid, data);
|
||||||
}catch (BizException e){
|
} catch (BizException e) {
|
||||||
//下单失败 对缓存进行库存 还原
|
//下单失败 对缓存进行库存 还原
|
||||||
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId()),data.getBuyCount()); //
|
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId()), data.getBuyCount()); //
|
||||||
stockOutMap.put((long)data.getGoodsId(),false);
|
stockOutMap.put((long) data.getGoodsId(), false);
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 只能查询自己关联的订单信息
|
||||||
|
@PostMapping("/query")
|
||||||
|
public Page<OrderInfo> query(@RequestBody OrderQueryParam param) {
|
||||||
|
param.setUid(StpUtil.getLoginIdAsInt());
|
||||||
|
return orderInfoService.queryByPage(param, true, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public OrderInfo get(@PathVariable("id") String id) {
|
||||||
|
return orderInfoService.getOneByIdAndUid(id, StpUtil.getLoginIdAsInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean updateStatus(String id, int status) {
|
||||||
|
if (!StringUtils.hasText(id)) throw BizException.paramError();
|
||||||
|
QueryWrapper<OrderInfo> q = QueryWrapperUtil.builder()
|
||||||
|
.eq("id", id)
|
||||||
|
.eq("uid", StpUtil.getLoginIdAsInt())
|
||||||
|
.build();
|
||||||
|
return orderInfoService.update(
|
||||||
|
OrderInfo.builder()
|
||||||
|
.status(status)
|
||||||
|
.build(),
|
||||||
|
q
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}/cancel")
|
||||||
|
public boolean cancelOrder(@PathVariable("id") String id) {
|
||||||
|
return updateStatus(id, OrderStatus.CANCELED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}/delete")
|
||||||
|
public boolean deleteOrder(@PathVariable("id") String id) {
|
||||||
|
return updateStatus(id, OrderStatus.DELETE);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ public class OrderAdminController {
|
|||||||
|
|
||||||
@PostMapping("/list")
|
@PostMapping("/list")
|
||||||
public PageDataResult list(@RequestBody OrderQueryParam param) {
|
public PageDataResult list(@RequestBody OrderQueryParam param) {
|
||||||
return PageDataResult.convert(orderInfoService.queryByPage(param));
|
return PageDataResult.convert(orderInfoService.queryByPage(param,false,true));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 修改
|
// 修改
|
||||||
|
@ -52,5 +52,7 @@ public class OrderInfo {
|
|||||||
private Integer status;
|
private Integer status;
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private UserInfo owner;
|
private UserInfo owner;
|
||||||
|
@TableField(exist = false)
|
||||||
|
private Goods goods;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,8 @@ package me.xiaoyan.point.api.pojo.dto;
|
|||||||
|
|
||||||
public class OrderStatus {
|
public class OrderStatus {
|
||||||
public static final int DELETE = 0;
|
public static final int DELETE = 0;
|
||||||
public static final int CANCEL = 1;
|
public static final int NOT_CONFIRM = 1;
|
||||||
public static final int CONFIRM = 2;
|
public static final int CONFIRM = 2;
|
||||||
public static final int DONE = 3;
|
public static final int CANCEL = 3;
|
||||||
|
public static final int FINISH = 4;
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,6 @@ public class OrderQueryParam extends PageParam {
|
|||||||
private String title;
|
private String title;
|
||||||
private String createTimeStart;
|
private String createTimeStart;
|
||||||
private String createTimeEnd;
|
private String createTimeEnd;
|
||||||
|
private int uid;
|
||||||
|
private Integer status;
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,8 @@ public interface OrderInfoService extends IService<OrderInfo> {
|
|||||||
|
|
||||||
OrderInfo create(int uid, CreateOrderData data);
|
OrderInfo create(int uid, CreateOrderData data);
|
||||||
|
|
||||||
Page queryByPage(OrderQueryParam param);
|
Page queryByPage(OrderQueryParam param,boolean queryGoods,boolean queryUser);
|
||||||
|
|
||||||
|
OrderInfo getOneByIdAndUid(String id, int uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,9 +72,11 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||||||
.id(OrderIdGenerator.next())
|
.id(OrderIdGenerator.next())
|
||||||
.gid((long) data.getGoodsId())
|
.gid((long) data.getGoodsId())
|
||||||
.uid(uid)
|
.uid(uid)
|
||||||
|
.orderTitle(goods.getTitle())
|
||||||
.count(data.getBuyCount())
|
.count(data.getBuyCount())
|
||||||
.price(goods.getPrice())
|
.price(goods.getPrice())
|
||||||
.status(OrderStatus.CONFIRM)
|
// 默认为待确认
|
||||||
|
.status(OrderStatus.NOT_CONFIRM)
|
||||||
.build();
|
.build();
|
||||||
if (save(orderInfo)) {
|
if (save(orderInfo)) {
|
||||||
return orderInfo;
|
return orderInfo;
|
||||||
@ -83,7 +85,7 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Page queryByPage(OrderQueryParam param) {
|
public Page queryByPage(OrderQueryParam param, boolean queryGoods, boolean queryUser) {
|
||||||
|
|
||||||
final QueryWrapper q = QueryWrapperUtil.builder()
|
final QueryWrapper q = QueryWrapperUtil.builder()
|
||||||
.eq("id", param.getId())
|
.eq("id", param.getId())
|
||||||
@ -92,14 +94,33 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
|
|||||||
.le("create_time", param.getCreateTimeEnd())
|
.le("create_time", param.getCreateTimeEnd())
|
||||||
.ne("status", DataStatus.DELETE)
|
.ne("status", DataStatus.DELETE)
|
||||||
.build();
|
.build();
|
||||||
|
// 根据状态筛选
|
||||||
|
if (param.getStatus() != null && param.getStatus() > 0) {
|
||||||
|
q.eq("status", param.getStatus());
|
||||||
|
}
|
||||||
|
q.orderByDesc("create_time");
|
||||||
final Page<OrderInfo> page = getBaseMapper().selectPage(param.getPage(), q);
|
final Page<OrderInfo> page = getBaseMapper().selectPage(param.getPage(), q);
|
||||||
page.getRecords().forEach(o -> {
|
page.getRecords().forEach(o -> {
|
||||||
|
if (queryUser) {
|
||||||
// 查询订单的归属用户
|
// 查询订单的归属用户
|
||||||
o.setOwner(userInfoService.getById(o.getUid()));
|
o.setOwner(userInfoService.getById(o.getUid()));
|
||||||
|
}
|
||||||
|
if (queryGoods) {
|
||||||
|
// 查询订单商品信息
|
||||||
|
o.setGoods(goodsService.getById(o.getGid()));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public OrderInfo getOneByIdAndUid(String id, int uid) {
|
||||||
|
QueryWrapper q = new QueryWrapper();
|
||||||
|
q.eq("uid", uid);
|
||||||
|
q.eq("id", id);
|
||||||
|
return getOne(q);
|
||||||
|
}
|
||||||
|
|
||||||
public long buyHistoryCount(int uid, int gid) {
|
public long buyHistoryCount(int uid, int gid) {
|
||||||
QueryWrapper q = new QueryWrapper();
|
QueryWrapper q = new QueryWrapper();
|
||||||
q.eq("uid", uid);
|
q.eq("uid", uid);
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package me.xiaoyan.point.api.util;
|
||||||
|
|
||||||
|
public class OrderStatus {
|
||||||
|
public static final int NOT_CONFIRM = 1;
|
||||||
|
public static final int CONFIRMED = 2;
|
||||||
|
public static final int CANCELED = 3;
|
||||||
|
public static final int FINISH = 4;
|
||||||
|
public static final int DELETE = 0;
|
||||||
|
}
|
@ -24,6 +24,10 @@ public class QueryWrapperUtil {
|
|||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
public QueryWrapperUtil eq(String column, int value) {
|
||||||
|
q.eq(column, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public QueryWrapperUtil ne(String column, Object value) {
|
public QueryWrapperUtil ne(String column, Object value) {
|
||||||
q.ne(column, value);
|
q.ne(column, value);
|
||||||
|
@ -45,6 +45,7 @@
|
|||||||
and stock > 0
|
and stock > 0
|
||||||
and category = #{category}
|
and category = #{category}
|
||||||
and status != 0
|
and status != 0
|
||||||
|
order by online_time desc,id desc
|
||||||
</select>
|
</select>
|
||||||
<select id="queryAllGoodsIdAndStock" resultType="me.xiaoyan.point.api.pojo.Goods">
|
<select id="queryAllGoodsIdAndStock" resultType="me.xiaoyan.point.api.pojo.Goods">
|
||||||
select id,stock
|
select id,stock
|
||||||
|
Loading…
x
Reference in New Issue
Block a user