/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.cart.controller.app.cart;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.cart.controller.app.cart.param.AppCartIdsParm;
import co.yixiang.yshop.module.cart.controller.app.cart.param.AppCartNumParam;
import co.yixiang.yshop.module.cart.controller.app.cart.param.AppCartParam;
import co.yixiang.yshop.module.cart.service.storecart.AppStoreCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.Map;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * <p>
 * 购物车控制器
 * </p>
 *
 * @author hupeng
 * @since 2023-6-16
 */
@Slf4j
@RestController
@Tag(name = "用户 APP - 购物车")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/cart")
public class AppCartController {

    private final AppStoreCartService appStoreCartService;

    /**
     * 购物车 获取数量
     */
    @PreAuthenticated
    @GetMapping("/count")
    @Operation(summary = "获取数量")
    public CommonResult<Map<String,Object>> count(){
        Map<String,Object> map = new LinkedHashMap<>();
        Long uid = getLoginUserId();
        map.put("count",appStoreCartService.getUserCartNum(uid));
        return success(map);
    }

    /**
     * 购物车 添加
     */
    @PreAuthenticated
    @PostMapping("/add")
    @Operation(summary = "添加购物车")

    public CommonResult<Map<String,Object>> add(@Validated @RequestBody AppCartParam cartParam){
        Map<String,Object> map = new LinkedHashMap<>();
        Long uid = getLoginUserId();
        map.put("cartId",appStoreCartService.addCart(uid,cartParam.getProductId(),cartParam.getCartNum(),
                cartParam.getUniqueId(),cartParam.getIsNew(),cartParam.getCombinationId(),
                cartParam.getSecKillId(),cartParam.getBargainId()));
        return success(map);
    }


    /**
     * 购物车列表
     */
    @PreAuthenticated
    @GetMapping("/cart/list")
    @Operation(summary = "购物车列表")
    public CommonResult<Map<String,Object>> getList(){
        Long uid = getLoginUserId();
        return success(appStoreCartService.getUserProductCartList(uid,"",null));
    }

    /**
     * 修改产品数量
     */
    @PreAuthenticated
    @PostMapping("/num")
    @Operation(summary = "修改产品数量")
    public CommonResult<Boolean> cartNum(@Validated @RequestBody AppCartNumParam param){
        Long uid = getLoginUserId();
        appStoreCartService.changeUserCartNum(param.getId(), param.getNumber(),uid);
        return success(true);
    }

    /**
     * 购物车删除产品
     */
    @PreAuthenticated
    @PostMapping("/cart/del")
    @Operation(summary = "购物车删除产品")
    public CommonResult<Boolean> cartDel(@Validated @RequestBody AppCartIdsParm parm){
        Long uid = getLoginUserId();
        appStoreCartService.removeUserCart(uid, parm.getIds());
        return success(true);
    }





}

