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

import java.util.List;
import javax.servlet.http.HttpServletResponse;
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.canteen.core.cook.domain.CookMaterialType;
import com.bonus.canteen.core.cook.service.ICookMaterialTypeService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;

/**
 * 原料类别Controller
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Api(tags = "原料类别接口")
@RestController
@RequestMapping("/cook_material_type")
public class CookMaterialTypeController extends BaseController {
    @Autowired
    private ICookMaterialTypeService cookMaterialTypeService;

    /**
     * 查询原料类别列表
     */
    @ApiOperation(value = "查询原料类别列表")
    @GetMapping("/list")
    public AjaxResult list(CookMaterialType cookMaterialType) {
        List<CookMaterialType> list = cookMaterialTypeService.selectCookMaterialTypeList(cookMaterialType);
        return AjaxResult.success(list);
    }

    /**
     * 导出原料类别列表
     */
    @ApiOperation(value = "导出原料类别列表")
    @SysLog(title = "原料类别", businessType = OperaType.EXPORT, logType = 1,module = "原料->导出原料类别")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CookMaterialType cookMaterialType) {
        List<CookMaterialType> list = cookMaterialTypeService.selectCookMaterialTypeList(cookMaterialType);
        ExcelUtil<CookMaterialType> util = new ExcelUtil<CookMaterialType>(CookMaterialType.class);
        util.exportExcel(response, list, "原料类别数据");
    }

    /**
     * 获取原料类别详细信息
     */
    @ApiOperation(value = "获取原料类别详细信息")
    @GetMapping(value = "/{materialTypeId}")
    public AjaxResult getInfo(@PathVariable("materialTypeId") Long materialTypeId) {
        return success(cookMaterialTypeService.selectCookMaterialTypeByMaterialTypeId(materialTypeId));
    }

    /**
     * 新增原料类别
     */
    @ApiOperation(value = "新增原料类别")
    @SysLog(title = "原料类别", businessType = OperaType.INSERT, logType = 1,module = "原料->新增原料类别")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CookMaterialType cookMaterialType) {
        try {
            return toAjax(cookMaterialTypeService.insertCookMaterialType(cookMaterialType));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改原料类别
     */
    @ApiOperation(value = "修改原料类别")
    @SysLog(title = "原料类别", businessType = OperaType.UPDATE, logType = 1,module = "原料->修改原料类别")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CookMaterialType cookMaterialType) {
        try {
            return toAjax(cookMaterialTypeService.updateCookMaterialType(cookMaterialType));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除原料类别
     */
    @ApiOperation(value = "删除原料类别")
    @SysLog(title = "原料类别", businessType = OperaType.DELETE, logType = 1,module = "原料->删除原料类别")
	@PostMapping("/del/{materialTypeIds}")
    public AjaxResult remove(@PathVariable Long[] materialTypeIds) {
        return toAjax(cookMaterialTypeService.deleteCookMaterialTypeByMaterialTypeIds(materialTypeIds));
    }

    @ApiOperation(value = "原料树形结构")
    @SysLog(title = "原料树形结构", businessType = OperaType.QUERY, logType = 1,module = "经研院食堂->原料管理->原料树形结构")
    @PostMapping("/getTree")
    public AjaxResult getTree() {
        return success(cookMaterialTypeService.getTree());
    }
}
