package org.springblade.modules.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springblade.common.constant.CommonConstant;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.modules.api.request.AddCartRequest;
import org.springblade.modules.api.request.RemoveCartRequest;
import org.springblade.modules.api.response.CartListResponse;
import org.springblade.modules.mall.entity.GoodsSpec;
import org.springblade.modules.mall.entity.ShoppingCart;
import org.springblade.modules.mall.service.IShoppingCartService;
import org.springblade.modules.mall.vo.ShoppingCartVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Author pangqijun
 * Date 2022/8/1
 * Description
 */
@RestController
@Api(value = "购物车", tags = "10.购物车")
@RequestMapping(CommonConstant.API_URL + "/cart")
public class ShoppingCartController {

	@Resource
	private IShoppingCartService shoppingCartService;

	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "添加购物车")
	@PostMapping("/add")
	public R addShoppingCart(@RequestBody AddCartRequest request) {
		ShoppingCart shoppingCart = BeanUtil.copyProperties(request, ShoppingCart.class);
		shoppingCart.setUserId(AuthUtil.getUserId());
		shoppingCartService.add(shoppingCart);
		return R.success("添加成功");
	}

	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "删除购物车商品")
	@PostMapping("/remove")
	public R removeShoppingCart(@RequestBody RemoveCartRequest request) {
		shoppingCartService.remove(request.getShoppingCartIds());
		return R.success("操作成功");
	}

	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "购物车列表")
	@PostMapping("/list")
	public R<List<ShoppingCartVO>> list(Query query) {
		return R.data(shoppingCartService.list(query));
	}

	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "增加数量")
	@PostMapping("/increase")
	public R<List<CartListResponse>> increase(@RequestParam("shoppingCartId") Long shoppingCartId) {
		shoppingCartService.increase(shoppingCartId);
		return R.success("操作成功");
	}

	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "减少数量")
	@PostMapping("/reduce")
	public R<List<CartListResponse>> reduce(@RequestParam("shoppingCartId") Long shoppingCartId) {
		shoppingCartService.reduce(shoppingCartId);
		return R.success("操作成功");
	}

	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取购物车数量")
	@PostMapping("/quantity")
	public R quantity() {
		return R.data(shoppingCartService.quantity(AuthUtil.getUserId()));
	}

	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "清空购物车")
	@PostMapping("/clear")
	public R clear() {
		shoppingCartService.clear(AuthUtil.getUserId());
		return R.success("操作成功");
	}

	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "获取商品规格")
	@GetMapping("/getGoodsSpec")
	public R<List<GoodsSpec>> getGoodsSpec(@ApiParam(value = "商品id") @RequestParam Long goodsInfoId) {
		return R.data(shoppingCartService.getGoodsSpec(goodsInfoId));
	}

	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "绑定商品规格")
	@PostMapping("/bindGoodsSpec")
	public R<List<GoodsSpec>> bindGoodsSpec(@ApiParam(value = "规格id") Long goodsSpecId, @ApiParam(value = "购物车id") Long shoppingCartId) {
		shoppingCartService.bindGoodsSpec(shoppingCartId, goodsSpecId);
		return R.success("操作成功");
	}

}
