package com.sunyard.chsm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sunyard.chsm.auth.AuthCode;
import com.sunyard.chsm.constant.AuthCodeConst;
import com.sunyard.chsm.model.R;
import com.sunyard.chsm.param.KeyCreateReq;
import com.sunyard.chsm.param.KeyInfoQuery;
import com.sunyard.chsm.param.KeyInfoResp;
import com.sunyard.chsm.param.KeyManageReq;
import com.sunyard.chsm.param.KeyUpdateReq;
import com.sunyard.chsm.service.KeyManageService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 密钥管理类接口
 *
 * @author liulu
 * @since 2024/12/6
 */
@RestController
@RequestMapping("/key")
public class KeyManageController {

    @Resource
    private KeyManageService keyManageService;

    /**
     * 查询密钥列表
     *
     * @param query 查询条件
     * @return 分页列表
     */
    @PostMapping("/pageList")
    public R<Page<KeyInfoResp>> queryPageList(@RequestBody KeyInfoQuery query) {
        Page<KeyInfoResp> page = keyManageService.queryPageList(query);
        return R.data(page);
    }

    /**
     * 查询密钥详情
     *
     * @param id 查询条件
     * @return 分页列表
     */
    @PostMapping("/info")
    public R<KeyInfoResp> queryInfo(@RequestBody Long id) {
        Assert.notNull(id, "密钥id不能为空");
        KeyInfoResp resp = keyManageService.queryInfo(id);
        return R.data(resp);
    }

    /**
     * 创建密钥
     *
     * @return id
     */
    @PostMapping("/gen")
    public R<Long> save(@Valid @RequestBody KeyCreateReq req) {
        Long id = keyManageService.create(req);
        return R.data(id);
    }

    /**
     * 更新密钥
     *
     * @param req 参数
     * @return id
     */
    @PostMapping("/update")
    public R<Void> update(@Valid @RequestBody KeyUpdateReq req) {
        keyManageService.update(req);
        return R.ok();
    }

    /**
     * 启用密钥
     *
     * @param req 密钥ids
     * @return id
     */
    @PostMapping("/enable")
    @AuthCode(AuthCodeConst.key_enable)
    public R<Void> enableKey(@Valid @RequestBody KeyManageReq req) {
        keyManageService.enableKey(req.getIds());
        return R.ok();
    }

    /**
     * 停用密钥
     *
     * @param req 密钥id
     * @return id
     */
    @PostMapping("/disable")
    public R<Void> disableKey(@Valid @RequestBody KeyManageReq req) {
        keyManageService.disableKey(req.getIds());
        return R.ok();
    }

    /**
     * 归档密钥
     *
     * @param req 密钥id
     * @return id
     */
    @PostMapping("/archive")
    public R<Void> archiveKey(@Valid @RequestBody KeyManageReq req) {
        keyManageService.archiveKey(req.getIds());
        return R.ok();
    }

    /**
     * 销毁密钥
     *
     * @param req 密钥id
     * @return id
     */
    @PostMapping("/destroy")
    public R<Void> destroyKey(@Valid @RequestBody KeyManageReq req) {
        keyManageService.destroyKey(req.getIds());
        return R.ok();
    }

}
