拆分购物车类型:商城订单和外卖订单
This commit is contained in:
@@ -135,6 +135,7 @@ public class UserCard implements Serializable {
|
||||
@ApiModelProperty(value = "创建时间")
|
||||
private Integer createTime;
|
||||
|
||||
@ApiModelProperty(value = "更新时间")
|
||||
private Integer updateTime;
|
||||
|
||||
@ApiModelProperty(value = "身份证号码")
|
||||
|
||||
@@ -2,21 +2,16 @@ package com.gxwebsoft.shop.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.shop.entity.GoodsCategory;
|
||||
import com.gxwebsoft.shop.entity.Merchant;
|
||||
import com.gxwebsoft.shop.service.CartService;
|
||||
import com.gxwebsoft.shop.entity.Cart;
|
||||
import com.gxwebsoft.shop.param.CartParam;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.core.web.PageParam;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.core.web.BatchParam;
|
||||
import com.gxwebsoft.common.core.annotation.OperationLog;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.shop.entity.Cart;
|
||||
import com.gxwebsoft.shop.entity.Merchant;
|
||||
import com.gxwebsoft.shop.param.CartParam;
|
||||
import com.gxwebsoft.shop.service.CartService;
|
||||
import com.gxwebsoft.shop.service.MerchantService;
|
||||
import com.gxwebsoft.shop.vo.CartShopVo;
|
||||
import com.gxwebsoft.shop.vo.CartVo;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@@ -25,11 +20,12 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.gxwebsoft.common.core.utils.CommonUtil.toTreeData;
|
||||
|
||||
/**
|
||||
* 购物车控制器
|
||||
*
|
||||
@@ -48,14 +44,18 @@ public class CartController extends BaseController {
|
||||
@ApiOperation("我的购物车")
|
||||
@GetMapping("/myCart")
|
||||
public ApiResult<?> myCart(CartParam param){
|
||||
HashMap<String, Object> map = computeCart(param);
|
||||
|
||||
// 记录当前登录用户id
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser == null) {
|
||||
return fail("请先登录",null);
|
||||
}
|
||||
|
||||
// 查询购物车
|
||||
param.setUserId(getLoginUserId().longValue());
|
||||
param.setUserId(loginUser.getUserId());
|
||||
final List<Cart> carts = cartService.listRelMpj(param);
|
||||
|
||||
// final List<Cart> carts = cartService.list(new LambdaQueryWrapper<Cart>()
|
||||
// .eq(Cart::getUserId, loginUser.getUserId())
|
||||
// .eq(Cart::getIsPay, 0)
|
||||
@@ -65,10 +65,14 @@ public class CartController extends BaseController {
|
||||
|
||||
// 收集店铺ID
|
||||
final Set<Integer> merchantIds = carts.stream().map(Cart::getMerchantId).collect(Collectors.toSet());
|
||||
if (CollectionUtils.isEmpty(merchantIds)) {
|
||||
map.put("selectAll",false);
|
||||
return success(map);
|
||||
}
|
||||
|
||||
// 查询店铺列
|
||||
final List<Merchant> merchants = merchantService.list(new LambdaQueryWrapper<Merchant>()
|
||||
.select(Merchant::getMerchantId,Merchant::getMerchantName,Merchant::getImage,Merchant::getShopType)
|
||||
.select(Merchant::getMerchantId,Merchant::getMerchantName,Merchant::getImage,Merchant::getShopType, Merchant::getAddress)
|
||||
.in(Merchant::getMerchantId, merchantIds));
|
||||
|
||||
// 返回空列表
|
||||
@@ -84,7 +88,6 @@ public class CartController extends BaseController {
|
||||
});
|
||||
|
||||
// 计算购物车金额
|
||||
HashMap<String, Object> map = computeCart();
|
||||
map.put("merchants",merchants);
|
||||
|
||||
return success(map);
|
||||
@@ -93,26 +96,43 @@ public class CartController extends BaseController {
|
||||
@ApiOperation("添加购物车")
|
||||
@PostMapping()
|
||||
public ApiResult<?> save(@RequestBody Cart cart) {
|
||||
|
||||
// 记录当前登录用户id
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser == null) {
|
||||
return fail("请先登录");
|
||||
}
|
||||
|
||||
// 查询参数
|
||||
final CartParam param = new CartParam();
|
||||
param.setType(cart.getType());
|
||||
|
||||
// 购物车是否已存在
|
||||
final Cart item = cartService.getOne(new LambdaQueryWrapper<Cart>().eq(Cart::getUserId, loginUser.getUserId()).eq(Cart::getGoodsId, cart.getGoodsId()).last("limit 1"));
|
||||
cart.setUserId(loginUser.getUserId().longValue());
|
||||
final Cart item = cartService.getOne(new LambdaQueryWrapper<Cart>()
|
||||
.eq(Cart::getUserId, loginUser.getUserId())
|
||||
.eq(Cart::getGoodsId, cart.getGoodsId())
|
||||
.eq(Cart::getType,cart.getType())
|
||||
.last("limit 1"));
|
||||
|
||||
cart.setUserId(loginUser.getUserId());
|
||||
// 字段验证
|
||||
if(cart.getCartNum() == null){
|
||||
return fail("购买数量不能为空");
|
||||
}
|
||||
|
||||
System.out.println("cart = " + cart);
|
||||
|
||||
// 修改购物车
|
||||
if (item != null) {
|
||||
item.setCartNum(item.getCartNum() + cart.getCartNum());
|
||||
cartService.updateById(item);
|
||||
return success("添加成功",computeCart());
|
||||
|
||||
return success("添加成功",computeCart(param));
|
||||
}
|
||||
|
||||
// 新添加商品
|
||||
if (cartService.save(cart)) {
|
||||
return success("添加成功",computeCart());
|
||||
return success("添加成功",computeCart(param));
|
||||
}
|
||||
|
||||
return fail("添加失败");
|
||||
@@ -122,21 +142,26 @@ public class CartController extends BaseController {
|
||||
@PutMapping()
|
||||
public ApiResult<?> update(@RequestBody Cart cart) {
|
||||
if (cartService.updateById(cart)) {
|
||||
return success("修改成功",computeCart());
|
||||
final CartParam param = new CartParam();
|
||||
param.setType(cart.getType());
|
||||
return success("修改成功",computeCart(param));
|
||||
}
|
||||
return fail("修改失败");
|
||||
}
|
||||
|
||||
@ApiOperation("计算购物车金额")
|
||||
@GetMapping("/count")
|
||||
public HashMap<String, Object> computeCart() {
|
||||
// 查询购物车数据
|
||||
final List<Cart> carts = cartService.list(new LambdaQueryWrapper<Cart>()
|
||||
.select(Cart::getId,Cart::getPrice,Cart::getCartNum)
|
||||
public HashMap<String, Object> computeCart(CartParam param) {
|
||||
final LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
queryWrapper.select(Cart::getId,Cart::getPrice,Cart::getCartNum)
|
||||
.eq(Cart::getUserId, getLoginUserId())
|
||||
.eq(Cart::getType,param.getType())
|
||||
.eq(Cart::getSelected,1)
|
||||
.eq(Cart::getIsPay, 0)
|
||||
);
|
||||
.eq(Cart::getIsPay, 0);
|
||||
|
||||
// 查询购物车数据
|
||||
final List<Cart> carts = cartService.list(queryWrapper);
|
||||
|
||||
// 计算单品合计价格
|
||||
carts.forEach(d -> {
|
||||
@@ -170,6 +195,9 @@ public class CartController extends BaseController {
|
||||
@ApiOperation("全选操作")
|
||||
@PutMapping("/onSelectAll/{yes}")
|
||||
public ApiResult<?> onSelectAll(@PathVariable("yes") Boolean yes) {
|
||||
final CartParam param = new CartParam();
|
||||
param.setType(0);
|
||||
|
||||
// 是否全选
|
||||
if (yes.equals(true)) {
|
||||
// 全勾选
|
||||
@@ -179,7 +207,7 @@ public class CartController extends BaseController {
|
||||
.eq(Cart::getSelected,0)
|
||||
.set(Cart::getSelected,1)
|
||||
);
|
||||
return success("修改成功",computeCart());
|
||||
return success("修改成功",computeCart(param));
|
||||
}
|
||||
// 全不勾选
|
||||
cartService.update(new LambdaUpdateWrapper<Cart>()
|
||||
@@ -188,7 +216,7 @@ public class CartController extends BaseController {
|
||||
.eq(Cart::getSelected,1)
|
||||
.set(Cart::getSelected,0)
|
||||
);
|
||||
return success("修改成功",computeCart());
|
||||
return success("修改成功",computeCart(param));
|
||||
}
|
||||
|
||||
|
||||
@@ -222,7 +250,7 @@ public class CartController extends BaseController {
|
||||
@PreAuthorize("hasAuthority('shop:cart:list')")
|
||||
@ApiOperation("根据id查询购物车")
|
||||
@GetMapping("/{id}")
|
||||
public ApiResult<Cart> get(@PathVariable("id") Long id) {
|
||||
public ApiResult<Cart> get(@PathVariable("id") Integer id) {
|
||||
// 使用关联查询
|
||||
return success(cartService.getByIdRel(id));
|
||||
}
|
||||
|
||||
@@ -233,7 +233,7 @@ public class OrderController extends BaseController {
|
||||
|
||||
// 从购物车获取订单数据
|
||||
final CartParam cartParam = new CartParam();
|
||||
cartParam.setUserId(getLoginUserId().longValue());
|
||||
cartParam.setUserId(loginUser.getUserId());
|
||||
cartParam.setSelected(true);
|
||||
final List<Cart> carts = cartService.listRelMpj(cartParam);
|
||||
final ArrayList<OrderGoods> orderGoods = new ArrayList<>();
|
||||
|
||||
@@ -28,16 +28,19 @@ public class Cart implements Serializable {
|
||||
|
||||
@ApiModelProperty(value = "购物车表ID")
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "类型 0商城 1预定 ")
|
||||
private Integer type;
|
||||
|
||||
@ApiModelProperty(value = "商品ID")
|
||||
private Long goodsId;
|
||||
@ApiModelProperty(value = "唯一标识")
|
||||
private String code;
|
||||
|
||||
@ApiModelProperty(value = "商品属性")
|
||||
private String goodsAttrUnique;
|
||||
@ApiModelProperty(value = "商品ID")
|
||||
private Integer goodsId;
|
||||
|
||||
@ApiModelProperty(value = "商品规格")
|
||||
private String spec;
|
||||
|
||||
@ApiModelProperty(value = "商品价格")
|
||||
private BigDecimal price;
|
||||
@@ -70,7 +73,7 @@ public class Cart implements Serializable {
|
||||
private Integer merchantId;
|
||||
|
||||
@ApiModelProperty(value = "用户ID")
|
||||
private Long userId;
|
||||
private Integer userId;
|
||||
|
||||
@ApiModelProperty(value = "租户id")
|
||||
private Integer tenantId;
|
||||
@@ -85,6 +88,10 @@ public class Cart implements Serializable {
|
||||
@TableField(exist = false)
|
||||
private String merchantName;
|
||||
|
||||
@ApiModelProperty(value = "店铺地址")
|
||||
@TableField(exist = false)
|
||||
private String address;
|
||||
|
||||
@ApiModelProperty(value = "商品名称")
|
||||
@TableField(exist = false)
|
||||
private String goodsName;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
<!-- 关联查询sql -->
|
||||
<sql id="selectSql">
|
||||
SELECT a.*,b.goods_name,b.image,c.merchant_name
|
||||
SELECT a.*,b.goods_name,b.image,c.merchant_name,c.address
|
||||
FROM shop_cart a
|
||||
LEFT JOIN shop_goods b ON a.goods_id = b.goods_id
|
||||
LEFT JOIN shop_merchant c ON a.merchant_id = b.merchant_id
|
||||
@@ -18,8 +18,8 @@
|
||||
<if test="param.goodsId != null">
|
||||
AND a.goods_id LIKE CONCAT('%', #{param.goodsId}, '%')
|
||||
</if>
|
||||
<if test="param.goodsAttrUnique != null">
|
||||
AND a.goods_attr_unique LIKE CONCAT('%', #{param.goodsAttrUnique}, '%')
|
||||
<if test="param.code != null">
|
||||
AND a.code LIKE CONCAT('%', #{param.code}, '%')
|
||||
</if>
|
||||
<if test="param.cartNum != null">
|
||||
AND a.cart_num = #{param.cartNum}
|
||||
|
||||
@@ -26,17 +26,22 @@ public class CartParam extends BaseParam {
|
||||
|
||||
@ApiModelProperty(value = "购物车表ID")
|
||||
@QueryField(type = QueryType.EQ)
|
||||
private Long id;
|
||||
private Integer id;
|
||||
|
||||
@ApiModelProperty(value = "类型 0商城 1预定 ")
|
||||
@QueryField(type = QueryType.EQ)
|
||||
private Integer type;
|
||||
|
||||
@ApiModelProperty(value = "商品ID")
|
||||
private Long goodsId;
|
||||
@QueryField(type = QueryType.EQ)
|
||||
private Integer goodsId;
|
||||
|
||||
@ApiModelProperty(value = "商品属性")
|
||||
private String goodsAttrUnique;
|
||||
@ApiModelProperty(value = "唯一标识")
|
||||
private String code;
|
||||
|
||||
@ApiModelProperty(value = "商品规格")
|
||||
@QueryField(type = QueryType.EQ)
|
||||
private String spec;
|
||||
|
||||
@ApiModelProperty(value = "商品数量")
|
||||
@QueryField(type = QueryType.EQ)
|
||||
@@ -63,7 +68,7 @@ public class CartParam extends BaseParam {
|
||||
private Integer bargainId;
|
||||
|
||||
@ApiModelProperty(value = "用户ID")
|
||||
private Long userId;
|
||||
private Integer userId;
|
||||
|
||||
@ApiModelProperty(value = "是否选中")
|
||||
private Boolean selected;
|
||||
|
||||
@@ -37,7 +37,7 @@ public interface CartService extends IService<Cart> {
|
||||
* @param id 购物车表ID
|
||||
* @return Cart
|
||||
*/
|
||||
Cart getByIdRel(Long id);
|
||||
Cart getByIdRel(Integer id);
|
||||
|
||||
List<Cart> listRelMpj(CartParam param);
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements Ca
|
||||
}
|
||||
|
||||
@Override
|
||||
public Cart getByIdRel(Long id) {
|
||||
public Cart getByIdRel(Integer id) {
|
||||
CartParam param = new CartParam();
|
||||
param.setId(id);
|
||||
return param.getOne(baseMapper.selectListRel(param));
|
||||
@@ -52,17 +52,20 @@ public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements Ca
|
||||
@Override
|
||||
public List<Cart> listRelMpj(CartParam param) {
|
||||
|
||||
System.out.println("param = " + param);
|
||||
System.out.println("userId = " + param.getUserId());
|
||||
// 多表查询
|
||||
MPJLambdaWrapper<Cart> mpjLambdaWrapper = new MPJLambdaWrapper<>();
|
||||
mpjLambdaWrapper.selectAll(Cart.class)
|
||||
.select(Goods::getGoodsName)
|
||||
.select(Goods::getImage)
|
||||
.leftJoin(Goods.class,Goods::getGoodsId,Cart::getGoodsId);
|
||||
.leftJoin(Goods.class,Goods::getGoodsId,Cart::getGoodsId).eq(Cart::getUserId,param.getUserId());
|
||||
|
||||
if(param.getSelected() != null){
|
||||
mpjLambdaWrapper.eq(Cart::getSelected,param.getSelected());
|
||||
}
|
||||
if(param.getType() != null){
|
||||
mpjLambdaWrapper.eq(Cart::getType,param.getType());
|
||||
}
|
||||
|
||||
return baseMapper.selectJoinList(Cart.class, mpjLambdaWrapper);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user