package cn.iocoder.yudao.module.system.controller.admin.dishimage;

import cn.iocoder.yudao.module.system.controller.admin.dishimage.vo.DishImagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dishimage.vo.DishImageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.dishimage.vo.DishImageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dishimage.DishImageDO;
import cn.iocoder.yudao.module.system.service.dishimage.DishImageService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;



@Tag(name = "管理后台 - 本周菜单")
@RestController
@RequestMapping("/t/dish-image")
@Validated
public class DishImageController {

    @Resource
    private DishImageService dishImageService;

    @PostMapping("/create")
    @Operation(summary = "创建本周菜单")
    @PreAuthorize("@ss.hasPermission('t:dish-image:create')")
    public CommonResult<Integer> createDishImage(@Valid @RequestBody DishImageSaveReqVO createReqVO) {
        return success(dishImageService.createDishImage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新本周菜单")
    @PreAuthorize("@ss.hasPermission('t:dish-image:update')")
    public CommonResult<Boolean> updateDishImage(@Valid @RequestBody DishImageSaveReqVO updateReqVO) {
        dishImageService.updateDishImage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除本周菜单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:dish-image:delete')")
    public CommonResult<Boolean> deleteDishImage(@RequestParam("id") Integer id) {
        dishImageService.deleteDishImage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得本周菜单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:dish-image:query')")
    public CommonResult<DishImageRespVO> getDishImage(@RequestParam("id") Integer id) {
        DishImageDO dishImage = dishImageService.getDishImage(id);
        return success(BeanUtils.toBean(dishImage, DishImageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得本周菜单分页")
    @PreAuthorize("@ss.hasPermission('t:dish-image:query')")
    public CommonResult<PageResult<DishImageRespVO>> getDishImagePage(@Valid DishImagePageReqVO pageReqVO) {
        PageResult<DishImageDO> pageResult = dishImageService.getDishImagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishImageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出本周菜单 Excel")
    @PreAuthorize("@ss.hasPermission('t:dish-image:export')")
    @OperateLog(type = EXPORT)
    public void exportDishImageExcel(@Valid DishImagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishImageDO> list = dishImageService.getDishImagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "本周菜单.xls", "数据", DishImageRespVO.class,
                        BeanUtils.toBean(list, DishImageRespVO.class));
    }

}