
package com.tmerclub.cloud.platform.controller.platform;

import com.tmerclub.cloud.api.platform.vo.SysAccessKeyVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.platform.dto.SysAccessKeyDTO;
import com.tmerclub.cloud.platform.model.SysAccessKey;
import com.tmerclub.cloud.platform.service.SysAccessKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 系统密钥
 * @author TRACK
 */
@RestController("platformSysAccessKeyController")
@RequestMapping("/p/sys_access_key")
@Tag(name = "系统密钥")
public class SysAccessKeyController {

    @Autowired
    private SysAccessKeyService sysAccessKeyService;


    @GetMapping("/page")
    @Operation(summary = "获取系统密钥列表", description = "分页获取系统密钥列表")
    public ServerResponseEntity<PageVO<SysAccessKey>> page(@Valid PageDTO pageDTO, SysAccessKeyDTO sysAccessKeyDTO) {
        PageVO<SysAccessKey> sysAccessKeyPage = sysAccessKeyService.page(pageDTO, sysAccessKeyDTO);
        return ServerResponseEntity.success(sysAccessKeyPage);
    }

    @GetMapping
    @Operation(summary = "获取系统密钥", description = "根据sysAccessKeyId获取系统密钥")
    public ServerResponseEntity<SysAccessKeyVO> getBySysAccessKeyId(@RequestParam Long sysAccessKeyId) {
        return ServerResponseEntity.success(sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId));
    }

    @PostMapping
    @Operation(summary = "保存系统密钥", description = "保存系统密钥")
    public ServerResponseEntity<Void> save(@Valid @RequestBody SysAccessKeyDTO sysAccessKeyDTO) {
        sysAccessKeyDTO.setSysAccessKeyId(null);
        sysAccessKeyService.save(sysAccessKeyDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新系统密钥", description = "更新系统密钥")
    public ServerResponseEntity<Void> update(@Valid @RequestBody SysAccessKeyDTO sysAccessKeyDTO) {
        sysAccessKeyService.update(sysAccessKeyDTO);
        sysAccessKeyService.removeCacheById(sysAccessKeyDTO.getAccessId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/reset_access_key")
    @Operation(summary = "重置系统访问密钥", description = "重置系统访问密钥")
    @Parameter(name = "sysAccessKeyId", description = "系统访问密钥id")
    public ServerResponseEntity<Boolean> resetAccessKey(@RequestParam("sysAccessKeyId") Long sysAccessKeyId) {
        if (Objects.isNull(sysAccessKeyId)) {
            throw new LuckException("sysAccessKeyId 不能为空");
        }
        SysAccessKeyVO sysAccessKeyVO = sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId);
        if (Objects.isNull(sysAccessKeyVO)) {
            return ServerResponseEntity.success();
        }
        sysAccessKeyService.resetAccessKey(sysAccessKeyId);
        sysAccessKeyService.removeCacheById(sysAccessKeyVO.getAccessId());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除系统密钥", description = "根据系统密钥id删除系统密钥")
    public ServerResponseEntity<Void> delete(@RequestParam Long sysAccessKeyId) {
        SysAccessKeyVO sysAccessKeyVO = sysAccessKeyService.getBySysAccessKeyId(sysAccessKeyId);
        if (Objects.isNull(sysAccessKeyVO)) {
            return ServerResponseEntity.success();
        }
        sysAccessKeyService.deleteById(sysAccessKeyId);
        sysAccessKeyService.removeCacheById(sysAccessKeyVO.getAccessId());
        return ServerResponseEntity.success();
    }
}
