package com.seer.teach.mall.app.order.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.DecryptionAnnotation;
import com.seer.teach.common.annotation.EncryptionAnnotation;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.mall.request.CartPageReq;
import com.seer.teach.mall.request.CartSaveReq;
import com.seer.teach.mall.request.CartUpdateReq;
import com.seer.teach.mall.response.CartResp;
import com.seer.teach.mall.app.order.service.IAppCartService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/app/user/mall/cart")
@RestController
@Tag(name = "APP - 购物车")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
@EncryptionAnnotation
@DecryptionAnnotation
@LogPrint
public class AppCartController {

    private final IAppCartService cartService;

    @PostMapping("/list")
    @SaCheckPermission("app:cart:list")
    @Operation(summary = "购物车信息分页查询")
    public ResultBean<PageListBean<CartResp>> getCartList(@RequestBody @Validated CartPageReq params){
       return ResultBean.success(cartService.getCartList(params));
    }

    @PutMapping("/number")
    @SaCheckPermission("app:cart:update")
    @Operation(summary = "购物车数量信息修改")
    public ResultBean<Boolean> updateCart(@RequestBody @Validated CartUpdateReq params){
        return ResultBean.success(cartService.updateCart(params));
    }

    @PostMapping("delete")
    @SaCheckPermission("app:cart:delete")
    @Operation(summary = "购物车信息删除")
    public ResultBean<Boolean> deleteCart(@Parameter(description = "购物车ID集合") @RequestBody List<Integer> ids){
        return ResultBean.success(cartService.deleteCart(ids));
    }

    @PostMapping
    @SaCheckPermission("app:cart:create")
    @Operation(summary = "购物车商品添加")
    public ResultBean<Boolean> saveCart(@RequestBody @Validated CartSaveReq params){
        return ResultBean.success(cartService.saveCart(params));
    }
}