package com.ruoyi.web.controller.mall;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.MemberCart;
import com.ruoyi.mall.domain.query.MemberCartQuery;
import com.ruoyi.mall.domain.vo.MemberCartVO;
import com.ruoyi.mall.service.impl.MemberCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/cart")
public class MemberCartController {
    @Autowired
    private MemberCartService memberCartService;

    /**
     * 添加购物车
     *
     * @return 购物车商品
     */
    @PostMapping("add")
    public ResponseEntity<Integer> add(@RequestBody MemberCart memberCart) {
        return ResponseEntity.ok(memberCartService.insert(memberCart));
    }

    /**
     * 购物车列表
     *
     * @return 购物车列表
     */
    @GetMapping("list")
    public ResponseEntity<List<Map>> list() {
        MemberCartQuery query = new MemberCartQuery();
        query.setMemberId(SecurityUtils.getAppLoginUser().getAppUserId());
        return ResponseEntity.ok(memberCartService.selectList(query, null));
    }

    /**
     * 当前用户的购物车商品数量
     *
     * @return
     */
    @GetMapping("goodscount")
    public ResponseEntity<Integer> goodscount() {
        return ResponseEntity.ok(memberCartService.mineCartNum());
    }
    /**
     * 当前用户的购物车商品id列表
     *
     * @return
     */
    @GetMapping("cart-ids")
    public ResponseEntity<List<Long>> cartIds() {
        return ResponseEntity.ok(memberCartService.mineCartIds());
    }



    /**
     * 修改购物车
     *
     * @return 是否修改
     */
    @PostMapping("modify")
    public ResponseEntity<Integer> modify(@Valid @RequestBody MemberCart memberCart) {
        return ResponseEntity.ok(memberCartService.update(memberCart));
    }

    /**
     * 删除购物车商品
     *
     * @return
     */
    @DeleteMapping("remove")
    public ResponseEntity<Integer> remove(@RequestBody Map<String,List<Long>> map) {
        List<Long> ids=map.get("ids");
        return ResponseEntity.ok(memberCartService.deleteByIds(ids));
    }


}
