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

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.i18nmessage.vo.*;
import com.yolo.keyboard.dal.dataobject.i18nmessage.KeyboardI18nMessageDO;
import com.yolo.keyboard.service.i18nmessage.KeyboardI18nMessageService;

@Tag(name = "管理后台 - 国际化错误提醒配置")
@RestController
@RequestMapping("/keyboard/I18n-message")
@Validated
public class KeyboardI18nMessageController {

    @Resource
    private KeyboardI18nMessageService i18nMessageService;

    @PostMapping("/create")
    @Operation(summary = "创建国际化错误提醒配置")
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:create')")
    public CommonResult<Long> createI18nMessage(@Valid @RequestBody KeyboardI18nMessageSaveReqVO createReqVO) {
        return success(i18nMessageService.createI18nMessage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新国际化错误提醒配置")
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:update')")
    public CommonResult<Boolean> updateI18nMessage(@Valid @RequestBody KeyboardI18nMessageSaveReqVO updateReqVO) {
        i18nMessageService.updateI18nMessage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除国际化错误提醒配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:delete')")
    public CommonResult<Boolean> deleteI18nMessage(@RequestParam("id") Long id) {
        i18nMessageService.deleteI18nMessage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除国际化错误提醒配置")
                @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:delete')")
    public CommonResult<Boolean> deleteI18nMessageList(@RequestParam("ids") List<Long> ids) {
        i18nMessageService.deleteI18nMessageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得国际化错误提醒配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:query')")
    public CommonResult<KeyboardI18nMessageRespVO> getI18nMessage(@RequestParam("id") Long id) {
        KeyboardI18nMessageDO i18nMessage = i18nMessageService.getI18nMessage(id);
        return success(BeanUtils.toBean(i18nMessage, KeyboardI18nMessageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得国际化错误提醒配置分页")
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:query')")
    public CommonResult<PageResult<KeyboardI18nMessageRespVO>> getI18nMessagePage(@Valid KeyboardI18nMessagePageReqVO pageReqVO) {
        PageResult<KeyboardI18nMessageDO> pageResult = i18nMessageService.getI18nMessagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardI18nMessageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出国际化错误提醒配置 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:I18n-message:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportI18nMessageExcel(@Valid KeyboardI18nMessagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardI18nMessageDO> list = i18nMessageService.getI18nMessagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "国际化错误提醒配置.xls", "数据", KeyboardI18nMessageRespVO.class,
                        BeanUtils.toBean(list, KeyboardI18nMessageRespVO.class));
    }

}