feat(移动端): 订单流程优化

master
wayn 4 years ago
parent b87aa75190
commit 13ad33710d

@ -14,4 +14,10 @@ public class ErrorCode {
*/
public static final int PARAMETER_TYPE_ERROR = 4002;
/**
*
*/
public static final int ORDER_NOT_EXISTS_ERROR = 5001;
}

@ -8,7 +8,7 @@
<version>1.1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>waynboot-message</artifactId>
<artifactId>waynboot-message-consumer</artifactId>
<version>1.1.0</version>
<dependencies>

@ -54,16 +54,6 @@ public class CartController extends BaseController {
return iCartService.changeNum(cartId, number);
}
@PostMapping("addNum/{cartId}/{number}")
public R addNum(@PathVariable Long cartId, @PathVariable Integer number) {
return iCartService.addNum(cartId, number);
}
@PostMapping("minusNum/{cartId}/{number}")
public R minusNum(@PathVariable Long cartId, @PathVariable Integer number) {
return iCartService.minusNum(cartId, number);
}
@DeleteMapping("{cartId}")
public R delete(@PathVariable Long cartId) {
return R.result(iCartService.removeById(cartId));

@ -59,24 +59,6 @@ public interface ICartService extends IService<Cart> {
*/
R changeNum(Long cartId, Integer number);
/**
*
*
* @param cartId ID
* @param number
* @return r
*/
R addNum(Long cartId, Integer number);
/**
*
*
* @param cartId ID
* @param number
* @return r
*/
R minusNum(Long cartId, Integer number);
/**
*
* @param cart

@ -8,6 +8,7 @@ import com.wayn.common.core.domain.shop.Goods;
import com.wayn.common.core.domain.shop.GoodsProduct;
import com.wayn.common.core.service.shop.IGoodsProductService;
import com.wayn.common.core.service.shop.IGoodsService;
import com.wayn.common.exception.BusinessException;
import com.wayn.common.util.R;
import com.wayn.common.util.bean.MyBeanUtil;
import com.wayn.mobile.api.domain.Cart;
@ -39,7 +40,7 @@ import java.util.stream.Collectors;
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements ICartService {
@Autowired
private CartMapper cartMapper;
private ICartService iCartService;
@Autowired
private IGoodsService iGoodsService;
@ -49,7 +50,7 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
@Override
public Cart checkExistsGoods(Long userId, Long goodsId, Long productId) {
return cartMapper.selectOne(new QueryWrapper<Cart>()
return iCartService.getOne(new QueryWrapper<Cart>()
.eq("user_id", userId)
.eq("goods_id", goodsId)
.eq("product_id", productId));
@ -64,7 +65,7 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
return R.error("参数错误");
}
Goods goods = iGoodsService.getById(goodsId);
if (Objects.isNull(iGoodsProductService) || !goods.getIsOnSale()) {
if (!goods.getIsOnSale()) {
return R.error("商品已经下架");
}
Long userId = SecurityUtils.getLoginUser().getMember().getId();
@ -137,24 +138,21 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements IC
@Override
public R changeNum(Long cartId, Integer number) {
Cart cart = iCartService.getById(cartId);
Long productId = cart.getProductId();
GoodsProduct goodsProduct = iGoodsProductService.getById(productId);
Integer productNumber = goodsProduct.getNumber();
if (number > productNumber) {
throw new BusinessException(String.format("库存不足,该商品只剩%d件了", productNumber));
}
return R.result(update().setSql("number = " + number).eq("id", cartId).update(), "修改失败");
}
@Override
public R addNum(Long cartId, Integer number) {
return R.result(update().setSql("number = number + 1").eq("id", cartId).update(), "添加失败");
}
@Override
public R minusNum(Long cartId, Integer number) {
return R.result(update().setSql("number = number - 1").eq("id", cartId).last("and number > 1").update(), "最少购买一件");
}
@Override
public R addDefaultGoodsProduct(Cart cart) {
Long goodsId = cart.getGoodsId();
List<GoodsProduct> products = iGoodsProductService.list(new QueryWrapper<GoodsProduct>().eq("goods_id", goodsId));
List<GoodsProduct> goodsProducts = products.stream().filter(goodsProduct -> goodsProduct.getDefaultSelected()).collect(Collectors.toList());
List<GoodsProduct> goodsProducts = products.stream().filter(GoodsProduct::getDefaultSelected).collect(Collectors.toList());
GoodsProduct defaultProduct;
// 如果默认选中货品不为空则取默认选中货品,否则取第一个货品
if (CollectionUtils.isNotEmpty(goodsProducts)) {

@ -14,6 +14,7 @@ import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.wayn.common.config.WaynConfig;
import com.wayn.common.constant.ErrorCode;
import com.wayn.common.constant.SysConstants;
import com.wayn.common.core.domain.shop.*;
import com.wayn.common.core.domain.vo.OrderVO;
@ -74,6 +75,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
@Autowired
private IGoodsProductService iGoodsProductService;
@Autowired
private IGoodsService iGoodsService;
@Autowired
private WxPayService wxPayService;
@Autowired
private IMemberService iMemberService;
@ -166,16 +169,20 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
checkedGoodsList = iCartService.listByIds(cartIdArr);
}
List<Long> goodsIds = checkedGoodsList.stream().map(Cart::getGoodsId).collect(Collectors.toList());
List<GoodsProduct> goodsProducts = iGoodsProductService.listByIds(goodsIds);
List<GoodsProduct> goodsProducts = iGoodsProductService.list(new QueryWrapper<GoodsProduct>().in("goods_id", goodsIds));
Map<Long, GoodsProduct> goodsIdMap = goodsProducts.stream().collect(
Collectors.toMap(GoodsProduct::getId, goodsProduct -> goodsProduct));
// 商品货品数量减少
for (Cart checkGoods : checkedGoodsList) {
Long productId = checkGoods.getProductId();
Long goodsId = checkGoods.getGoodsId();
GoodsProduct product = goodsIdMap.get(productId);
int remainNumber = product.getNumber() - checkGoods.getNumber();
if (remainNumber < 0) {
throw new RuntimeException("下单的商品货品数量大于库存量");
Goods goods = iGoodsService.getById(goodsId);
String goodsName = goods.getName();
String[] specifications = product.getSpecifications();
throw new BusinessException(String.format("%s,%s 库存不足", goodsName, StringUtils.join(specifications, " ")));
}
if (!iGoodsProductService.reduceStock(productId, checkGoods.getNumber())) {
throw new BusinessException("商品货品库存减少失败");
@ -469,7 +476,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
public R testPayNotify(String orderSn) {
Order order = getOne(new QueryWrapper<Order>().eq("order_sn", orderSn));
if (order == null) {
return R.error("订单不存在,编号:" + orderSn);
return R.error(ErrorCode.ORDER_NOT_EXISTS_ERROR, "订单不存在,编号:" + orderSn);
}
// 检查这个订单是否已经处理过

@ -77,6 +77,6 @@ spring:
password: guest
es:
host: 182.254.214.165
host: wayn.ltd
port: 9200
scheme: http

Loading…
Cancel
Save