package com.ruoyi.demo.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.demo.entity.Goods;
import com.ruoyi.demo.entity.bo.GoodsBo;
import com.ruoyi.demo.entity.bo.GoodsDeleteBo;
import com.ruoyi.demo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @PostMapping()
    public R addGoods(@RequestBody Goods goods){
        int i = goodsService.addGoods(goods);
        if (i == 2)
            return R.ok("已存在该商品");
        if (i == 1)
            return R.ok("添加成功");
        return R.fail("添加失败");
    }

    @DeleteMapping()
    public R deleteGoods(GoodsDeleteBo bo){
        int total = bo.getDeleteItems().size();
        int i = goodsService.deleteGoods(bo);
        return R.ok("选择删除" + total + "个，成功删除" + i + "个");
    }

    @GetMapping("/list")
    public R goodsList(GoodsBo bo){
        return R.ok(goodsService.getGoodsList(bo));
    }

    @GetMapping("/detail")
    public R goodsDetail(GoodsBo bo){
        return R.ok(goodsService.getGoodsDetail(bo));
    }
}
