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.Food;
import com.waimai.web.admin.service.IFoodService;
import com.waimai.common.utils.poi.ExcelUtil;
import com.waimai.common.core.page.TableDataInfo;

/**
 * 食品Controller
 * 
 * @author callmeyan
 * @date 2023-03-24
 */
@RestController
@RequestMapping("/admin/food")
public class FoodController extends BaseController
{
    @Autowired
    private IFoodService foodService;

    /**
     * 查询食品列表
     */
    @PreAuthorize("@ss.hasPermi('admin:food:list')")
    @GetMapping("/list")
    public TableDataInfo list(Food food)
    {
        startPage();
        List<Food> list = foodService.selectFoodList(food);
        return getDataTable(list);
    }

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

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

    /**
     * 新增食品
     */
    @PreAuthorize("@ss.hasPermi('admin:food:add')")
    @Log(title = "食品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Food food)
    {
        return toAjax(foodService.insertFood(food));
    }

    /**
     * 修改食品
     */
    @PreAuthorize("@ss.hasPermi('admin:food:edit')")
    @Log(title = "食品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Food food)
    {
        return toAjax(foodService.updateFood(food));
    }

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