package com.yolo.keyboard.controller.admin.warningmessage;

import org.springframework.web.bind.annotation.*;
import jakarta.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 jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.warningmessage.vo.*;
import com.yolo.keyboard.dal.dataobject.warningmessage.KeyboardWarningMessageDO;
import com.yolo.keyboard.service.warningmessage.KeyboardWarningMessageService;

@Tag(name = "管理后台 - 用户注销提示信息")
@RestController
@RequestMapping("/keyboard/warning-message")
@Validated
public class KeyboardWarningMessageController {

    @Resource
    private KeyboardWarningMessageService warningMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建用户注销提示信息")
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:create')")
    public CommonResult<Long> createWarningMessage(@Valid @RequestBody KeyboardWarningMessageSaveReqVO createReqVO) {
        return success(warningMessageService.createWarningMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户注销提示信息")
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:update')")
    public CommonResult<Boolean> updateWarningMessage(@Valid @RequestBody KeyboardWarningMessageSaveReqVO updateReqVO) {
        warningMessageService.updateWarningMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户注销提示信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:delete')")
    public CommonResult<Boolean> deleteWarningMessage(@RequestParam("id") Long id) {
        warningMessageService.deleteWarningMessage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户注销提示信息")
                @PreAuthorize("@ss.hasPermission('keyboard:warning-message:delete')")
    public CommonResult<Boolean> deleteWarningMessageList(@RequestParam("ids") List<Long> ids) {
        warningMessageService.deleteWarningMessageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户注销提示信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:query')")
    public CommonResult<KeyboardWarningMessageRespVO> getWarningMessage(@RequestParam("id") Long id) {
        KeyboardWarningMessageDO warningMessage = warningMessageService.getWarningMessage(id);
        return success(BeanUtils.toBean(warningMessage, KeyboardWarningMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户注销提示信息分页")
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:query')")
    public CommonResult<PageResult<KeyboardWarningMessageRespVO>> getWarningMessagePage(@Valid KeyboardWarningMessagePageReqVO pageReqVO) {
        PageResult<KeyboardWarningMessageDO> pageResult = warningMessageService.getWarningMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardWarningMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户注销提示信息 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:warning-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWarningMessageExcel(@Valid KeyboardWarningMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardWarningMessageDO> list = warningMessageService.getWarningMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户注销提示信息.xls", "数据", KeyboardWarningMessageRespVO.class,
                        BeanUtils.toBean(list, KeyboardWarningMessageRespVO.class));
    }

}