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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storediningplates.vo.StoreDiningPlatesPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storediningplates.vo.StoreDiningPlatesRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storediningplates.vo.StoreDiningPlatesSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storediningplates.StoreDiningPlatesDO;
import cn.iocoder.yudao.module.member.service.storediningplates.StoreDiningPlatesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;



@Tag(name = "管理后台 - 门店餐盘设置")
@RestController
@RequestMapping("/t/store-dining-plates")
@Validated
public class StoreDiningPlatesController {

    @Resource
    private StoreDiningPlatesService storeDiningPlatesService;

    @PostMapping("/create")
    @Operation(summary = "创建门店餐盘设置")
    @PreAuthorize("@ss.hasPermission('t:store-dining-plates:create')")
    public CommonResult<Long> createStoreDiningPlates(@Valid @RequestBody StoreDiningPlatesSaveReqVO createReqVO) {
        return success(storeDiningPlatesService.createStoreDiningPlates(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店餐盘设置")
    @PreAuthorize("@ss.hasPermission('t:store-dining-plates:update')")
    public CommonResult<Boolean> updateStoreDiningPlates(@Valid @RequestBody StoreDiningPlatesSaveReqVO updateReqVO) {
        storeDiningPlatesService.updateStoreDiningPlates(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得门店餐盘设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:store-dining-plates:query')")
    public CommonResult<StoreDiningPlatesRespVO> getStoreDiningPlates(@RequestParam("id") Long id) {
        StoreDiningPlatesDO storeDiningPlates = storeDiningPlatesService.getStoreDiningPlates(id);
        return success(BeanUtils.toBean(storeDiningPlates, StoreDiningPlatesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店餐盘设置分页")
    @PreAuthorize("@ss.hasPermission('t:store-dining-plates:query')")
    public CommonResult<PageResult<StoreDiningPlatesRespVO>> getStoreDiningPlatesPage(@Valid StoreDiningPlatesPageReqVO pageReqVO) {
        PageResult<StoreDiningPlatesDO> pageResult = storeDiningPlatesService.getStoreDiningPlatesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreDiningPlatesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店餐盘设置 Excel")
    @PreAuthorize("@ss.hasPermission('t:store-dining-plates:export')")
    @OperateLog(type = EXPORT)
    public void exportStoreDiningPlatesExcel(@Valid StoreDiningPlatesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreDiningPlatesDO> list = storeDiningPlatesService.getStoreDiningPlatesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "门店餐盘设置.xls", "数据", StoreDiningPlatesRespVO.class,
                        BeanUtils.toBean(list, StoreDiningPlatesRespVO.class));
    }

}