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

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

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.cook.dto.AllocRecipeStallDTO;
import com.bonus.canteen.core.cook.dto.CookRecipeBindDTO;
import com.bonus.canteen.core.cook.dto.CookRecipeDTO;
import com.bonus.canteen.core.cook.dto.CookRecipeDetailDTO;
import com.bonus.canteen.core.cook.vo.AllocRecipeStallVO;
import com.bonus.canteen.core.cook.vo.CookRecipeDetailVO;
import com.bonus.canteen.core.cook.vo.CookRecipeVO;
import com.bonus.common.log.enums.OperaType;
//import com.bonus.canteen.core.cook.common.annotation.PreventRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.canteen.core.cook.domain.CookRecipe;
import com.bonus.canteen.core.cook.service.ICookRecipeService;
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;
import org.springframework.web.multipart.MultipartFile;

/**
 * 菜品计划信息Controller
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Api(tags = "菜品计划信息接口")
@RestController
@RequestMapping("/cook_recipe")
public class CookRecipeController extends BaseController {
    @Autowired
    private ICookRecipeService cookRecipeService;

    /**
     * 查询菜品计划信息列表
     */
    @ApiOperation(value = "查询菜品计划信息列表")
    //@RequiresPermissions("cook:recipe:list")
    @GetMapping("/list")
    public TableDataInfo list(CookRecipe cookRecipe) {
        startPage();
        List<CookRecipe> list = cookRecipeService.selectCookRecipeList(cookRecipe);
        return getDataTable(list);
    }

    @ApiOperation(value = "当前菜谱")
    //@RequiresPermissions("menu:recipe:list")
    @PostMapping("/currentList")
    public TableDataInfo currentList(@RequestBody AllocRecipeStallDTO content) {
        startPage();
        List<AllocRecipeStallVO> list = cookRecipeService.selectCookRecipeList4Current(content);
        return getDataTable(list);
    }

    /**
     * 导出菜品计划信息列表
     */
    @ApiOperation(value = "导出菜品计划信息列表")
    //@PreventRepeatSubmit
    //@RequiresPermissions("cook:recipe:export")
    @SysLog(title = "菜品计划信息", businessType = OperaType.EXPORT, logType = 1,module = "菜谱管理->导出菜品计划信息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CookRecipe cookRecipe) {
        List<CookRecipe> list = cookRecipeService.selectCookRecipeList(cookRecipe);
        ExcelUtil<CookRecipe> util = new ExcelUtil<CookRecipe>(CookRecipe.class);
        util.exportExcel(response, list, "菜品计划信息数据");
    }

    /**
     * 获取菜品计划信息详细信息
     */
    @ApiOperation(value = "获取菜品计划信息详细信息")
    //@RequiresPermissions("cook:recipe:query")
    @GetMapping(value = "/{recipeId}")
    public AjaxResult getInfo(@PathVariable("recipeId") Long recipeId) {
        return success(cookRecipeService.selectCookRecipeByRecipeId(recipeId));
    }

    @ApiOperation("获取指定日期菜谱计划菜品详情")
    //@RequiresPermissions("cook:recipe:query")
    @PostMapping({"/getRecipeDetailByDate"})
    public AjaxResult getRecipeDetailByDate(@RequestBody CookRecipeDetailDTO dto) {
        List<CookRecipeDetailVO> list = cookRecipeService.getRecipeDetailByDate(dto);
        return success(list);
    }

    @PostMapping({"/getRecipeDetail"})
    @ApiOperation("获取菜谱计划-菜谱详情")
    public AjaxResult getRecipeDetail(@RequestBody CookRecipeDetailDTO dto) {
        List<CookRecipeDetailVO> list = cookRecipeService.getRecipeDetail(dto);
        return success(list);
    }

    @PostMapping({"/template/detail"})
    @ApiOperation("获取循环菜谱计划-菜谱详情")
    public AjaxResult getTemplateRecipeDetail(@RequestBody CookRecipeDetailDTO dto) {
        List<CookRecipeDetailVO> list = cookRecipeService.selectTemplateDetailList(dto);
        return success(list);
    }

    /**
     * 新增菜品计划信息
     */
    @ApiOperation(value = "新增菜品计划信息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("cook:recipe:add")
    @SysLog(title = "菜品计划信息", businessType = OperaType.INSERT, logType = 1,module = "菜谱管理->新增菜品计划信息")
    @PostMapping
    public AjaxResult add(@RequestBody CookRecipeDTO cookRecipeDTO) {
        try {
            return toAjax(cookRecipeService.insertCookRecipe(cookRecipeDTO));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改菜品计划信息
     */
    @ApiOperation(value = "修改菜品计划信息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("cook:recipe:edit")
    @SysLog(title = "菜品计划信息", businessType = OperaType.UPDATE, logType = 1,module = "菜谱管理->修改菜品计划信息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CookRecipeDTO cookRecipeDTO) {
        try {
            return toAjax(cookRecipeService.updateCookRecipe(cookRecipeDTO));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @ApiOperation(value = "菜谱绑定")
    @SysLog(title = "菜谱绑定", businessType = OperaType.DELETE, logType = 1,module = "菜谱管理->菜谱绑定")
    @PostMapping("/app/bind")
    public AjaxResult bindAppRecipe(@RequestBody @Valid CookRecipeBindDTO dto) {
        this.cookRecipeService.bindCookRecipe(dto);
        return AjaxResult.success();
    }

    @PostMapping({"/recipe/import/dishes"})
    @ApiOperation("批量导入菜谱中的菜品信息")
    @Transactional
    public AjaxResult recipeImportDishes(@RequestParam("file") MultipartFile excel, @RequestParam("recipeId") Long recipeId) {
        try{
            return ObjectUtil.isNull(excel) ? AjaxResult.error("请选择导入文件 ^_^") :
                    AjaxResult.success(this.cookRecipeService.recipeImportDishes(excel, recipeId));
        }catch (Exception e){
            String message = e.toString();
            //取倒数第二个：之后的信息
            String finalMessage = message.substring(message.lastIndexOf(":") - 7);
            String errorMes = finalMessage.replace("Error:", "");
            System.err.println("errorMes="+errorMes);
            return error("导入失败："+errorMes);
        }
    }

    /**
     * 删除菜品计划信息
     */
    @ApiOperation(value = "删除菜品计划信息")
    //@PreventRepeatSubmit
    //@RequiresPermissions("cook:recipe:remove")
    @SysLog(title = "菜品计划信息", businessType = OperaType.DELETE, logType = 1,module = "菜谱管理->删除菜品计划信息")
	@PostMapping("/del/{recipeIds}")
    public AjaxResult remove(@PathVariable Long[] recipeIds) {
        return toAjax(cookRecipeService.deleteCookRecipeByRecipeIds(recipeIds));
    }
}
