package com.cf.imes.module.system.controller.admin.tokenconfig;


import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigPageReqVO;
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigRespVO;
import com.cf.imes.module.system.controller.admin.tokenconfig.vo.TokenConfigSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.tokenconfig.TokenConfigDO;
import com.cf.imes.module.system.service.tokenconfig.TokenConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


/**
 * @author token配置管理
 */
@Tag(name = "管理后台 - Token管理")
@RestController
@RequestMapping("/system/token")
@Validated
public class TokenConfigController {

    @Resource
    private TokenConfigService tokenConfigService;


    @GetMapping()
    @Operation(summary = "获得token配置的分页列表")
    @PreAuthorize("@ss.hasAnyPermissions('system:token:query')")
    public CommonResult<PageResult<TokenConfigRespVO>> getTokenConfigPage(@Valid TokenConfigPageReqVO pageReqVO) {
        PageResult<TokenConfigRespVO> pageResult = tokenConfigService.getTokenConfigPage(pageReqVO);

        return success(pageResult);

    }



    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取对应的token配置信息")
    @PreAuthorize("@ss.hasAnyPermissions('system:token:query','system:token:update')")
    public CommonResult<TokenConfigRespVO> getTokenConConfig(@PathVariable("id") Long id) {
        TokenConfigDO tokenConConfig = tokenConfigService.getTokenConConfig(id);
        return success(BeanUtils.toBean(tokenConConfig, TokenConfigRespVO.class));
    }



    @PostMapping()
    @Operation(summary = "token配置新增")
    @PreAuthorize("@ss.hasAnyPermissions('system:token:create')")
    public CommonResult<Boolean> insertTokenConfig(@Valid @RequestBody TokenConfigSaveReqVO reqVO) {
        tokenConfigService.insertTokenConfig(reqVO);
        return success(true);
    }



    @PutMapping()
    @Operation(summary = "token配置修改")
    @PreAuthorize("@ss.hasAnyPermissions('system:token:update')")
    public CommonResult<Boolean> updateTokenConfig(@Valid @RequestBody TokenConfigSaveReqVO reqVO) {
        tokenConfigService.updateTokenConfig(reqVO);
        return success(true);
    }



    @DeleteMapping("/{id}")
    @Operation(summary = "token配置删除")
    @PreAuthorize("@ss.hasPermission('system:token:delete')")
    public CommonResult<Boolean> deleteTokenConfig(@PathVariable("id") Long id) {
        tokenConfigService.deleteTokenConfig(id);
        return success(true);
    }




    @GetMapping("/check")
    @PermitAll
    @Operation(summary = "token校验")
    public CommonResult<Long> checkTokenConfig(@RequestParam("token") String token) {

        return success(tokenConfigService.checkTokenConfig(token));
    }



    @GetMapping("/copy")
    @Operation(summary = "token复制")
    @PreAuthorize("@ss.hasPermission('system:token:copy')")
    public CommonResult<String> tokenConfigCopy(@RequestParam("id") Long id) {

        return success(tokenConfigService.tokenConfigCopy(id));
    }




    @GetMapping("/export")
    @Operation(summary = "导出配置信息")
    @PreAuthorize("@ss.hasAnyPermissions('system:token:export')")
    @OperateLog(type = EXPORT)
    public void exportTokenConfigExcel(@Valid TokenConfigPageReqVO pageReqVO,
                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TokenConfigRespVO> list = tokenConfigService.exportTokenConfigExcel(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "token配置信息表.xls", "数据", TokenConfigRespVO.class,
                BeanUtils.toBean(list, TokenConfigRespVO.class));
    }



}
