188 lines
5.8 KiB
Java
188 lines
5.8 KiB
Java
package com.gxwebsoft.shop.controller;
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
import com.gxwebsoft.common.core.web.ApiResult;
|
|
import com.gxwebsoft.common.core.web.BaseController;
|
|
import com.gxwebsoft.common.core.web.BatchParam;
|
|
import com.gxwebsoft.common.core.web.PageResult;
|
|
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.CartVo;
|
|
import io.swagger.annotations.Api;
|
|
import io.swagger.annotations.ApiOperation;
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* 购物车控制器
|
|
*
|
|
* @author 科技小王子
|
|
* @since 2024-07-29 21:02:39
|
|
*/
|
|
@Api(tags = "购物车管理")
|
|
@RestController
|
|
@RequestMapping("/api/shop/cart")
|
|
public class CartController extends BaseController {
|
|
@Resource
|
|
private CartService cartService;
|
|
@Resource
|
|
private MerchantService merchantService;
|
|
|
|
@ApiOperation("分页查询购物车")
|
|
@GetMapping("/page")
|
|
public ApiResult<PageResult<Cart>> page(CartParam param) {
|
|
// 使用关联查询
|
|
return success(cartService.pageRel(param));
|
|
}
|
|
|
|
@ApiOperation("查询全部购物车")
|
|
@GetMapping()
|
|
public ApiResult<CartVo> list(CartParam param) {
|
|
if (ObjectUtil.isEmpty(getLoginUser())) {
|
|
return success("请先登录",null);
|
|
}
|
|
// 附加用户ID
|
|
param.setUserId(getLoginUserId());
|
|
return success(cartService.listRel(param));
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('shop:cart:list')")
|
|
@ApiOperation("根据id查询购物车")
|
|
@GetMapping("/{id}")
|
|
public ApiResult<Cart> get(@PathVariable("id") Integer id) {
|
|
// 使用关联查询
|
|
return success(cartService.getByIdRel(id));
|
|
}
|
|
|
|
@ApiOperation("添加购物车")
|
|
@PostMapping()
|
|
public ApiResult<?> save(@RequestBody Cart cart) {
|
|
if (ObjectUtil.isEmpty(getLoginUser())) {
|
|
return success("请先登录",null);
|
|
}
|
|
// 修正用户ID
|
|
cart.setUserId(getLoginUserId());
|
|
// 修正购买数量
|
|
if (ObjectUtil.isEmpty(cart.getCartNum())) {
|
|
cart.setCartNum(1);
|
|
}
|
|
// 超出最大限制
|
|
if (cartService.count(new LambdaUpdateWrapper<Cart>().eq(Cart::getUserId,getLoginUserId())) > 50) {
|
|
return fail("购物车宝贝数量已满,建议您先去结算或清理~");
|
|
}
|
|
// 重复商品叠加数量
|
|
final Cart one = cartService.getOne(new LambdaQueryWrapper<Cart>()
|
|
.eq(Cart::getUserId, getLoginUserId())
|
|
.eq(Cart::getGoodsId,cart.getGoodsId())
|
|
.eq(Cart::getSpec,cart.getSpec())
|
|
.last("limit 1"));
|
|
if(ObjectUtil.isNotEmpty(one)){
|
|
one.setCartNum(cart.getCartNum()+1);
|
|
cartService.updateById(one);
|
|
return success("更新成功",one.getId());
|
|
}
|
|
if (cartService.save(cart)) {
|
|
return success("添加成功",cart.getId());
|
|
}
|
|
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@ApiOperation("修改购物车")
|
|
@PutMapping()
|
|
public ApiResult<?> update(@RequestBody Cart cart) {
|
|
if (cartService.updateById(cart)) {
|
|
final CartParam param = new CartParam();
|
|
param.setType(cart.getType());
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('shop:cart:remove')")
|
|
@ApiOperation("删除购物车")
|
|
@DeleteMapping("/{id}")
|
|
public ApiResult<?> remove(@PathVariable("id") Integer id) {
|
|
if (cartService.removeById(id)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('shop:cart:save')")
|
|
@ApiOperation("批量添加购物车")
|
|
@PostMapping("/batch")
|
|
public ApiResult<?> saveBatch(@RequestBody List<Cart> list) {
|
|
if (cartService.saveBatch(list)) {
|
|
return success("添加成功");
|
|
}
|
|
return fail("添加失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('shop:cart:update')")
|
|
@ApiOperation("批量修改购物车")
|
|
@PutMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody BatchParam<Cart> batchParam) {
|
|
if (batchParam.update(cartService, "id")) {
|
|
return success("修改成功");
|
|
}
|
|
return fail("修改失败");
|
|
}
|
|
|
|
@PreAuthorize("hasAuthority('shop:cart:remove')")
|
|
@ApiOperation("批量删除购物车")
|
|
@DeleteMapping("/batch")
|
|
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
|
|
if (cartService.removeByIds(ids)) {
|
|
return success("删除成功");
|
|
}
|
|
return fail("删除失败");
|
|
}
|
|
|
|
@ApiOperation("全选操作")
|
|
@PutMapping("/onSelectAll/{yes}")
|
|
public ApiResult<?> onSelectAll(@PathVariable("yes") Boolean yes) {
|
|
if (ObjectUtil.isEmpty(getLoginUser())) {
|
|
return success("请先登录",null);
|
|
}
|
|
final List<Cart> carts = cartService.list(new LambdaUpdateWrapper<Cart>()
|
|
.eq(Cart::getSelected,!yes)
|
|
.eq(Cart::getUserId, getLoginUserId()));
|
|
carts.forEach(d -> {
|
|
d.setSelected(yes);
|
|
});
|
|
return success("修改成功", cartService.updateBatchById(carts));
|
|
}
|
|
|
|
@ApiOperation("增加数量")
|
|
@PutMapping("/addCartNum")
|
|
public ApiResult<?> addCartNum(@RequestBody Cart cart) {
|
|
if (ObjectUtil.isEmpty(getLoginUser())) {
|
|
return success("请先登录",null);
|
|
}
|
|
cart.setCartNum(cart.getCartNum()+1);
|
|
cartService.updateById(cart);
|
|
return success("操作成功");
|
|
}
|
|
|
|
@ApiOperation("减少数量")
|
|
@PutMapping("/subCartNum")
|
|
public ApiResult<?> subCartNum(@RequestBody Cart cart) {
|
|
if (ObjectUtil.isEmpty(getLoginUser())) {
|
|
return success("请先登录",null);
|
|
}
|
|
cart.setCartNum(cart.getCartNum()-1);
|
|
cartService.updateById(cart);
|
|
return success("操作成功");
|
|
}
|
|
|
|
}
|