package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.biz.dal.entity.GoodsCategory;
import com.ym.mec.biz.dal.page.GoodsCategoryQueryInfo;
import com.ym.mec.biz.service.GoodsCategoryService;
import com.ym.mec.biz.service.GoodsService;
import com.ym.mec.common.controller.BaseController;

@RequestMapping("category")
@Api(tags = "商品分类服务")
@RestController
public class GoodsCategoryController extends BaseController {

    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "删除商品分类")
    @PostMapping("/del/{id}")
    public Object del(@ApiParam(value = "商品分类编号", required = true) @PathVariable("id") Integer id) {
        int num = goodsService.findGoodsNumByCategoryId(id);
        if (num > 0) {
            return failed("商品分类下有商品,不能删除");
        }
        goodsCategoryService.delete(id);
        return succeed();
    }

    @ApiOperation(value = "新增、修改商品类型")
    @PostMapping("/upset")
    public Object upset(@RequestBody GoodsCategory goodsCategory){
        goodsCategoryService.upsetGoodsCategory(goodsCategory);
        return succeed();
    }

    @ApiOperation(value = "分页查询商品分类列表")
    @GetMapping("/queryPage")
    public Object queryPage(GoodsCategoryQueryInfo queryInfo) {
        return succeed(goodsCategoryService.queryPage(queryInfo));
    }

    @ApiOperation(value = "根据商品分类编号查询商品分类")
    @GetMapping("/get/{id}")
    public Object get(@ApiParam(value = "商品分类编号", required = true) @PathVariable("id") Integer id) {
        return succeed(goodsCategoryService.get(id));
    }
}
