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

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.usercharacter.vo.*;
import com.yolo.keyboard.dal.dataobject.usercharacter.KeyboardUserCharacterDO;
import com.yolo.keyboard.service.usercharacter.KeyboardUserCharacterService;

@Tag(name = "管理后台 - 用户人设管理")
@RestController
@RequestMapping("/keyboard/user-character")
@Validated
public class KeyboardUserCharacterController {

    @Resource
    private KeyboardUserCharacterService userCharacterService;

    @PostMapping("/create")
    @Operation(summary = "创建用户人设管理")
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:create')")
    public CommonResult<Long> createUserCharacter(@Valid @RequestBody KeyboardUserCharacterSaveReqVO createReqVO) {
        return success(userCharacterService.createUserCharacter(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户人设管理")
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:update')")
    public CommonResult<Boolean> updateUserCharacter(@Valid @RequestBody KeyboardUserCharacterSaveReqVO updateReqVO) {
        userCharacterService.updateUserCharacter(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户人设管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:delete')")
    public CommonResult<Boolean> deleteUserCharacter(@RequestParam("id") Long id) {
        userCharacterService.deleteUserCharacter(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户人设管理")
                @PreAuthorize("@ss.hasPermission('keyboard:user-character:delete')")
    public CommonResult<Boolean> deleteUserCharacterList(@RequestParam("ids") List<Long> ids) {
        userCharacterService.deleteUserCharacterListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户人设管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:query')")
    public CommonResult<KeyboardUserCharacterRespVO> getUserCharacter(@RequestParam("id") Long id) {
        KeyboardUserCharacterDO userCharacter = userCharacterService.getUserCharacter(id);
        return success(BeanUtils.toBean(userCharacter, KeyboardUserCharacterRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户人设管理分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:query')")
    public CommonResult<PageResult<KeyboardUserCharacterRespVO>> getUserCharacterPage(@Valid KeyboardUserCharacterPageReqVO pageReqVO) {
        PageResult<KeyboardUserCharacterDO> pageResult = userCharacterService.getUserCharacterPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserCharacterRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户人设管理 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-character:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCharacterExcel(@Valid KeyboardUserCharacterPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserCharacterDO> list = userCharacterService.getUserCharacterPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户人设管理.xls", "数据", KeyboardUserCharacterRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserCharacterRespVO.class));
    }

}