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.entity.bo.PlanBo;
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(@RequestBody GoodsDeleteBo bo){
        int i = goodsService.deleteGoods(bo);
        if (i == 1)
            return R.ok("删除成功");
        return R.fail("删除失败");
    }

    @PostMapping("/list")
    public R goodsList(@RequestBody PlanBo bo){
        if (bo.getStartTime() != null && bo.getEndTime() != null)
            if (bo.getStartTime().compareTo(bo.getEndTime()) > 0)
                return R.fail("起始时间不能大于结束时间");
        return R.ok(goodsService.getGoodsList(bo));
    }

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

    @PutMapping()
    public R editGoods(@RequestBody Goods goods){
        int i = goodsService.editGoods(goods);
        if (i == 1)
            return R.ok("修改成功");
        return R.fail("修改失败");
    }
}
