package cn.iocoder.yudao.module.member.controller.admin.diningplates;

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.*;

import cn.iocoder.yudao.module.member.controller.admin.diningplates.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.diningplates.DiningPlatesDO;
import cn.iocoder.yudao.module.member.service.diningplates.DiningPlatesService;

@Tag(name = "管理后台 - 餐盘")
@RestController
@RequestMapping("/member/dining-plates")
@Validated
public class DiningPlatesController {

    @Resource
    private DiningPlatesService diningPlatesService;

    @PostMapping("/create")
    @Operation(summary = "创建餐盘")
    @PreAuthorize("@ss.hasPermission('member:dining-plates:create')")
    public CommonResult<Long> createDiningPlates(@Valid @RequestBody DiningPlatesSaveReqVO createReqVO) {
        return success(diningPlatesService.createDiningPlates(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐盘")
    @PreAuthorize("@ss.hasPermission('member:dining-plates:update')")
    public CommonResult<Boolean> updateDiningPlates(@Valid @RequestBody DiningPlatesSaveReqVO updateReqVO) {
        diningPlatesService.updateDiningPlates(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐盘")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:dining-plates:delete')")
    public CommonResult<Boolean> deleteDiningPlates(@RequestParam("id") Long id) {
        diningPlatesService.deleteDiningPlates(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐盘")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:dining-plates:query')")
    public CommonResult<DiningPlatesRespVO> getDiningPlates(@RequestParam("id") Long id) {
        DiningPlatesDO diningPlates = diningPlatesService.getDiningPlates(id);
        return success(BeanUtils.toBean(diningPlates, DiningPlatesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐盘分页")
    @PreAuthorize("@ss.hasPermission('member:dining-plates:query')")
    public CommonResult<PageResult<DiningPlatesRespVO>> getDiningPlatesPage(@Valid DiningPlatesPageReqVO pageReqVO) {
        PageResult<DiningPlatesDO> pageResult = diningPlatesService.getDiningPlatesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DiningPlatesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出餐盘 Excel")
    @PreAuthorize("@ss.hasPermission('member:dining-plates:export')")
    @OperateLog(type = EXPORT)
    public void exportDiningPlatesExcel(@Valid DiningPlatesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DiningPlatesDO> list = diningPlatesService.getDiningPlatesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "餐盘.xls", "数据", DiningPlatesRespVO.class,
                        BeanUtils.toBean(list, DiningPlatesRespVO.class));
    }

}