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

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.userthemes.vo.*;
import com.yolo.keyboard.dal.dataobject.userthemes.KeyboardUserThemesDO;
import com.yolo.keyboard.service.userthemes.KeyboardUserThemesService;

@Tag(name = "管理后台 - 用户主题")
@RestController
@RequestMapping("/keyboard/user-themes")
@Validated
public class KeyboardUserThemesController {

    @Resource
    private KeyboardUserThemesService userThemesService;

    @PostMapping("/create")
    @Operation(summary = "创建用户主题")
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:create')")
    public CommonResult<Long> createUserThemes(@Valid @RequestBody KeyboardUserThemesSaveReqVO createReqVO) {
        return success(userThemesService.createUserThemes(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户主题")
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:update')")
    public CommonResult<Boolean> updateUserThemes(@Valid @RequestBody KeyboardUserThemesSaveReqVO updateReqVO) {
        userThemesService.updateUserThemes(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户主题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:delete')")
    public CommonResult<Boolean> deleteUserThemes(@RequestParam("id") Long id) {
        userThemesService.deleteUserThemes(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户主题")
                @PreAuthorize("@ss.hasPermission('keyboard:user-themes:delete')")
    public CommonResult<Boolean> deleteUserThemesList(@RequestParam("ids") List<Long> ids) {
        userThemesService.deleteUserThemesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户主题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:query')")
    public CommonResult<KeyboardUserThemesRespVO> getUserThemes(@RequestParam("id") Long id) {
        KeyboardUserThemesDO userThemes = userThemesService.getUserThemes(id);
        return success(BeanUtils.toBean(userThemes, KeyboardUserThemesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户主题分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:query')")
    public CommonResult<PageResult<KeyboardUserThemesRespVO>> getUserThemesPage(@Valid KeyboardUserThemesPageReqVO pageReqVO) {
        PageResult<KeyboardUserThemesDO> pageResult = userThemesService.getUserThemesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserThemesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户主题 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-themes:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserThemesExcel(@Valid KeyboardUserThemesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserThemesDO> list = userThemesService.getUserThemesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户主题.xls", "数据", KeyboardUserThemesRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserThemesRespVO.class));
    }

}