package com.waimai.web.admin.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.waimai.common.annotation.Log;
import com.waimai.common.core.controller.BaseController;
import com.waimai.common.core.domain.AjaxResult;
import com.waimai.common.enums.BusinessType;
import com.waimai.web.admin.domain.FoodCategory;
import com.waimai.web.admin.service.IFoodCategoryService;
import com.waimai.common.utils.poi.ExcelUtil;
import com.waimai.common.core.page.TableDataInfo;

/**
 * 食品分类Controller
 * 
 * @author callmeyan
 * @date 2023-03-24
 */
@RestController
@RequestMapping("/admin/category")
public class FoodCategoryController extends BaseController
{
    @Autowired
    private IFoodCategoryService foodCategoryService;

    /**
     * 查询食品分类列表
     */
    @PreAuthorize("@ss.hasPermi('admin:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(FoodCategory foodCategory)
    {
        startPage();
        List<FoodCategory> list = foodCategoryService.selectFoodCategoryList(foodCategory);
        return getDataTable(list);
    }

    /**
     * 导出食品分类列表
     */
    @PreAuthorize("@ss.hasPermi('admin:category:export')")
    @Log(title = "食品分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FoodCategory foodCategory)
    {
        List<FoodCategory> list = foodCategoryService.selectFoodCategoryList(foodCategory);
        ExcelUtil<FoodCategory> util = new ExcelUtil<FoodCategory>(FoodCategory.class);
        util.exportExcel(response, list, "食品分类数据");
    }

    /**
     * 获取食品分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(foodCategoryService.selectFoodCategoryById(id));
    }

    /**
     * 新增食品分类
     */
    @PreAuthorize("@ss.hasPermi('admin:category:add')")
    @Log(title = "食品分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FoodCategory foodCategory)
    {
        return toAjax(foodCategoryService.insertFoodCategory(foodCategory));
    }

    /**
     * 修改食品分类
     */
    @PreAuthorize("@ss.hasPermi('admin:category:edit')")
    @Log(title = "食品分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FoodCategory foodCategory)
    {
        return toAjax(foodCategoryService.updateFoodCategory(foodCategory));
    }

    /**
     * 删除食品分类
     */
    @PreAuthorize("@ss.hasPermi('admin:category:remove')")
    @Log(title = "食品分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(foodCategoryService.deleteFoodCategoryByIds(ids));
    }
}
