package hw.tagApi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import hw.tagApi.common.annotation.Log;
import hw.tagApi.common.core.controller.BaseController;
import hw.tagApi.common.core.domain.AjaxResult;
import hw.tagApi.common.enums.BusinessType;
import hw.tagApi.system.domain.SysApiToken;
import hw.tagApi.system.service.ISysApiTokenService;
import hw.tagApi.common.utils.poi.ExcelUtil;
import hw.tagApi.common.core.page.TableDataInfo;

/**
 * 用户令牌Controller
 *
 * @author Yinq
 * @date 2025-04-23
 */
@RestController
@RequestMapping("/system/apiToken")
public class SysApiTokenController extends BaseController {
    @Autowired
    private ISysApiTokenService sysApiTokenService;

    /**
     * 查询用户令牌列表
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysApiToken sysApiToken) {
        startPage();
        List<SysApiToken> list = sysApiTokenService.selectSysApiTokenList(sysApiToken);
        return getDataTable(list);
    }

    /**
     * 导出用户令牌列表
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:export')")
    @Log(title = "用户令牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysApiToken sysApiToken) {
        List<SysApiToken> list = sysApiTokenService.selectSysApiTokenList(sysApiToken);
        ExcelUtil<SysApiToken> util = new ExcelUtil<SysApiToken>(SysApiToken.class);
        util.exportExcel(response, list, "用户令牌数据");
    }

    /**
     * 获取用户令牌详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:query')")
    @GetMapping(value = "/{tokenId}")
    public AjaxResult getInfo(@PathVariable("tokenId") Long tokenId) {
        return success(sysApiTokenService.selectSysApiTokenByTokenId(tokenId));
    }

    /**
     * 新增用户令牌
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:add')")
    @Log(title = "用户令牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysApiToken sysApiToken) {
        return toAjax(sysApiTokenService.insertSysApiToken(sysApiToken));
    }

    /**
     * 修改用户令牌
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:edit')")
    @Log(title = "用户令牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysApiToken sysApiToken) {
        return toAjax(sysApiTokenService.updateSysApiToken(sysApiToken));
    }

    /**
     * 删除用户令牌
     */
    @PreAuthorize("@ss.hasPermi('system:apiToken:remove')")
    @Log(title = "用户令牌", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tokenIds}")
    public AjaxResult remove(@PathVariable Long[] tokenIds) {
        return toAjax(sysApiTokenService.deleteSysApiTokenByTokenIds(tokenIds));
    }
}
