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

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

import com.bonus.canteen.core.cook.dto.MenuDishesImportDTO;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.canteen.core.cook.domain.CookDishes;
import com.bonus.canteen.core.cook.service.ICookDishesService;
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_dishes")
public class CookDishesController extends BaseController {
    @Autowired
    private ICookDishesService cookDishesService;

    /**
     * 查询菜品信息列表
     */
    @ApiOperation(value = "查询菜品信息列表")
    @GetMapping("/list")
    public TableDataInfo list(CookDishes cookDishes) {
        startPage();
        List<CookDishes> list = cookDishesService.selectCookDishesList(cookDishes);
        return getDataTable(list);
    }

    /**
     * 导出菜品信息列表
     */
    @ApiOperation(value = "导出菜品信息列表")
    @SysLog(title = "菜品信息", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出菜品信息")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CookDishes cookDishes) {
        List<CookDishes> list = cookDishesService.selectCookDishesList(cookDishes);
        ExcelUtil<CookDishes> util = new ExcelUtil<CookDishes>(CookDishes.class);
        util.exportExcel(response, list, "菜品信息数据");
    }

    /**
     * 获取菜品信息详细信息
     */
    @ApiOperation(value = "获取菜品信息详细信息")
    @GetMapping(value = "/getInfo/{dishesId}")
    public AjaxResult getInfo(@PathVariable("dishesId") Long dishesId) {
        return success(cookDishesService.selectCookDishesByDishesId(dishesId));
    }

    /**
     * 新增菜品信息
     */
    @ApiOperation(value = "新增菜品信息")
    @SysLog(title = "菜品信息", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增菜品信息")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody CookDishes cookDishes) {
        try {
            return toAjax(cookDishesService.insertCookDishes(cookDishes));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 修改菜品信息
     */
    @ApiOperation(value = "修改菜品信息")
    @SysLog(title = "菜品信息", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改菜品信息")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody CookDishes cookDishes) {
        try {
            return toAjax(cookDishesService.updateCookDishes(cookDishes));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 删除菜品信息
     */
    @ApiOperation(value = "删除菜品信息")
    @SysLog(title = "菜品信息", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除菜品信息")
	@PostMapping("/del/{dishesIds}")
    public AjaxResult remove(@PathVariable Long[] dishesIds) {
        return toAjax(cookDishesService.deleteCookDishesByDishesIds(dishesIds));
    }

    @PostMapping({"/import/check"})
    @ApiOperation("导入之前检查数据是否可以导入")
    public AjaxResult importDishesCheck(@RequestParam("file") MultipartFile excel) {
        return AjaxResult.success(this.cookDishesService.importDishesCheck(excel));
    }


    @PostMapping({"/upload/importDishes"})
    @ApiOperation("导入成功的菜品信息")
    public AjaxResult importDishes(@RequestBody List<MenuDishesImportDTO> request) {
        try {
            this.cookDishesService.newImportDishes(request);
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
