package com.waimai.api;

import com.waimai.common.annotation.Anonymous;
import com.waimai.common.core.domain.R;
import com.waimai.web.admin.domain.Carts;
import com.waimai.web.admin.service.ICartsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/carts")
@Anonymous
public class CartsController {
    private static long uid = 1;
    @Autowired
    private ICartsService cartsService;

    @GetMapping("/list")
    public R<List<Carts>> selectList() {
        Carts c = new Carts();
        c.setUid(uid);
        List<Carts> carts = cartsService.selectCartsList(c);
        return R.ok(carts);
    }

    @PostMapping
    public R addToCart(@RequestBody Carts carts) {
        Carts c = new Carts();
        c.setUid(uid);
        c.setFid(carts.getFid());
        Carts cs = cartsService.selectCartsById(carts.getFid(), uid);
        if (cs == null) {
            carts.setUid(uid);
            cartsService.insertCarts(carts);
        } else {
            // 数量加1
            carts.setCount(
                    carts.getCount() + cs.getCount()
            );
            cartsService.updateCarts(carts);
        }
        return R.ok();
    }
}
