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

import com.yolo.keyboard.controller.admin.themestyles.vo.KeyboardThemeStylesPageReqVO;
import com.yolo.keyboard.controller.admin.themestyles.vo.KeyboardThemeStylesRespVO;
import com.yolo.keyboard.controller.admin.themestyles.vo.KeyboardThemeStylesSaveReqVO;
import com.yolo.keyboard.dal.dataobject.themestyles.KeyboardThemeStylesDO;
import com.yolo.keyboard.service.themestyles.KeyboardThemeStylesService;
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.*;


@Tag(name = "管理后台 - 主题风格")
@RestController
@RequestMapping("/keyboard-server/keyboard-theme-styles")
@Validated
public class KeyboardThemeStylesController {

    @Resource
    private KeyboardThemeStylesService keyboardThemeStylesService;

    @PostMapping("/create")
    @Operation(summary = "创建主题风格")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:create')")
    public CommonResult<Long> createKeyboardThemeStyles(@Valid @RequestBody KeyboardThemeStylesSaveReqVO createReqVO) {
        return success(keyboardThemeStylesService.createKeyboardThemeStyles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新主题风格")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:update')")
    public CommonResult<Boolean> updateKeyboardThemeStyles(@Valid @RequestBody KeyboardThemeStylesSaveReqVO updateReqVO) {
        keyboardThemeStylesService.updateKeyboardThemeStyles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除主题风格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:delete')")
    public CommonResult<Boolean> deleteKeyboardThemeStyles(@RequestParam("id") Long id) {
        keyboardThemeStylesService.deleteKeyboardThemeStyles(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除主题风格")
                @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:delete')")
    public CommonResult<Boolean> deleteKeyboardThemeStylesList(@RequestParam("ids") List<Long> ids) {
        keyboardThemeStylesService.deleteKeyboardThemeStylesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得主题风格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:query')")
    public CommonResult<KeyboardThemeStylesRespVO> getKeyboardThemeStyles(@RequestParam("id") Long id) {
        KeyboardThemeStylesDO keyboardThemeStyles = keyboardThemeStylesService.getKeyboardThemeStyles(id);
        return success(BeanUtils.toBean(keyboardThemeStyles, KeyboardThemeStylesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得主题风格分页")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:query')")
    public CommonResult<PageResult<KeyboardThemeStylesRespVO>> getKeyboardThemeStylesPage(@Valid KeyboardThemeStylesPageReqVO pageReqVO) {
        PageResult<KeyboardThemeStylesDO> pageResult = keyboardThemeStylesService.getKeyboardThemeStylesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardThemeStylesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出主题风格 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-theme-styles:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKeyboardThemeStylesExcel(@Valid KeyboardThemeStylesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardThemeStylesDO> list = keyboardThemeStylesService.getKeyboardThemeStylesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "主题风格.xls", "数据", KeyboardThemeStylesRespVO.class,
                        BeanUtils.toBean(list, KeyboardThemeStylesRespVO.class));
    }

}