拆分购物车类型:商城订单和外卖订单

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