package com.fdkankan.ucenter.controller;


import com.fdkankan.common.util.JwtUtil;
import com.fdkankan.ucenter.common.BaseController;
import com.fdkankan.ucenter.common.Result;
import com.fdkankan.ucenter.service.ICartService;
import com.fdkankan.ucenter.vo.request.CartParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 购物车
 */
@RestController
@RequestMapping("/ucenter/user/cart")
public class CartController extends BaseController {

    @Autowired
    ICartService cartService;

    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public Result add(@RequestBody CartParam param){
        param.setUserName(getUserName());
        cartService.add(param);
        return Result.success();
    }


    /**
     * 购物车商品减或移除
     */
    @PostMapping(value = "/remove")
    public Result remove(@RequestBody CartParam param){
        param.setUserName(getUserName());
        cartService.delete(param);
        return Result.success();
    }

    /**
     * 查找会员的购物车记录
     *
     */
    @PostMapping(value = "/list")
    public Result list() throws Exception {
        return Result.success(cartService.listByUser(getUserName()));
    }

    /**
     * 删除购物车
     *
     */
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody CartParam param) throws Exception {
        if (StringUtils.isEmpty(param.getIds())){
            return Result.failure("ids为空");
        }
        String[] split = param.getIds().split(",");
        List<String> ids = Arrays.asList(split);
        cartService.deleteIds(ids);
        return Result.success();
    }

}

