package com.bonus.canteen.core.menu.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.bonus.canteen.core.menu.domain.MenuMaterialCategory;
import com.bonus.canteen.core.menu.dto.CategoryTreeListDTO;
import com.bonus.canteen.core.menu.dto.LongIds;
import com.bonus.canteen.core.menu.dto.MenuMaterialCategoryAddDTO;
import com.bonus.canteen.core.menu.dto.StringIds;
import com.bonus.canteen.core.menu.service.IMenuMaterialCategoryService;
import com.bonus.common.log.enums.OperaType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 原料类别Controller
 *
 * @author xsheng
 * @date 2025-04-03
 */
@Api(tags = "原料类别接口")
@RestController
@RequestMapping("/menu_material_category")
public class MenuMaterialCategoryController extends BaseController {
    @Autowired
    private IMenuMaterialCategoryService menuMaterialCategoryService;

    /**
     * 查询原料类别列表
     */
    @ApiOperation(value = "查询原料类别列表")
    //@RequiresPermissions("menu:category:list")
    @GetMapping("/list")
    public TableDataInfo list(MenuMaterialCategory menuMaterialCategory) {
        startPage();
        List<MenuMaterialCategory> list = menuMaterialCategoryService.selectMenuMaterialCategoryList(menuMaterialCategory);
        return getDataTable(list);
    }

    /**
     * 导出原料类别列表
     */
    @ApiOperation(value = "导出原料类别列表")
    //@PreventRepeatSubmit
    //@RequiresPermissions("menu:category:export")
    @SysLog(title = "原料类别", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出原料类别")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuMaterialCategory menuMaterialCategory) {
        List<MenuMaterialCategory> list = menuMaterialCategoryService.selectMenuMaterialCategoryList(menuMaterialCategory);
        ExcelUtil<MenuMaterialCategory> util = new ExcelUtil<MenuMaterialCategory>(MenuMaterialCategory.class);
        util.exportExcel(response, list, "原料类别数据");
    }

    /**
     * 获取原料类别详细信息
     */
    @ApiOperation(value = "获取原料类别详细信息")
    //@RequiresPermissions("menu:category:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(menuMaterialCategoryService.selectMenuMaterialCategoryById(id));
    }

    /**
     * 新增原料类别
     */
    @ApiOperation(value = "新增原料类别")
    //@PreventRepeatSubmit
    //@RequiresPermissions("menu:category:add")
    @SysLog(title = "原料类别", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增原料类别")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody @Valid MenuMaterialCategoryAddDTO dto) {
        try {
            return toAjax(menuMaterialCategoryService.insertMenuMaterialCategory(dto));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改原料类别
     */
    @ApiOperation(value = "修改原料类别")
    //@PreventRepeatSubmit
    //@RequiresPermissions("menu:category:edit")
    @SysLog(title = "原料类别", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改原料类别")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody MenuMaterialCategory menuMaterialCategory) {
        try {
            return toAjax(menuMaterialCategoryService.updateMenuMaterialCategory(menuMaterialCategory));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除原料类别
     */
    @ApiOperation(value = "删除原料类别")
    //@PreventRepeatSubmit
    //@RequiresPermissions("menu:category:remove")
    @SysLog(title = "原料类别", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除原料类别")
	@PostMapping("/del")
    public AjaxResult remove(@RequestBody StringIds ids) {
        return toAjax(menuMaterialCategoryService.deleteMenuMaterialCategoryByIds(ids.getIds()));
    }

    @ApiOperation(value = "原料树形结构")
    //@PreventRepeatSubmit
    //@RequiresPermissions("menu:category:remove")
    @SysLog(title = "原料树形结构", businessType = OperaType.QUERY, logType = 1,module = "经研院食堂->原料管理")
    @PostMapping("/getTree")
    public AjaxResult getTree( @RequestBody CategoryTreeListDTO content) {
        return success(menuMaterialCategoryService.getTree(content));
    }
}
