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

import com.yolo.keyboard.dal.dataobject.user.KeyboardUserDO;
import com.yolo.keyboard.controller.admin.user.vo.KeyboardUserPageReqVO;
import com.yolo.keyboard.controller.admin.user.vo.KeyboardUserRespVO;
import com.yolo.keyboard.controller.admin.user.vo.KeyboardUserSaveReqVO;
import com.yolo.keyboard.service.user.KeyboardUserService;
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.*;
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-user")
@Validated
public class KeyboardUserController {

    @Resource
    private KeyboardUserService keyboardUserService;

    @PostMapping("/create")
    @Operation(summary = "创建键盘用户")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-user:create')")
    public CommonResult<Long> createKeyboardUser(@Valid @RequestBody KeyboardUserSaveReqVO createReqVO) {
        return success(keyboardUserService.createKeyboardUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新键盘用户")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-user:update')")
    public CommonResult<Boolean> updateKeyboardUser(@Valid @RequestBody KeyboardUserSaveReqVO updateReqVO) {
        keyboardUserService.updateKeyboardUser(updateReqVO);
        return success(true);
    }

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

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

    @GetMapping("/get")
    @Operation(summary = "获得键盘用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-user:query')")
    public CommonResult<KeyboardUserRespVO> getKeyboardUser(@RequestParam("id") Long id) {
        KeyboardUserDO keyboardUser = keyboardUserService.getKeyboardUser(id);
        return success(BeanUtils.toBean(keyboardUser, KeyboardUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得键盘用户分页")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-user:query')")
    public CommonResult<PageResult<KeyboardUserRespVO>> getKeyboardUserPage(@Valid KeyboardUserPageReqVO pageReqVO) {
        PageResult<KeyboardUserDO> pageResult = keyboardUserService.getKeyboardUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出键盘用户 Excel")
    @PreAuthorize("@ss.hasPermission('keyboard-server:keyboard-user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportKeyboardUserExcel(@Valid KeyboardUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserDO> list = keyboardUserService.getKeyboardUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "键盘用户.xls", "数据", KeyboardUserRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserRespVO.class));
    }

}