package com.ruoyi.security.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.security.mapper.EncryptionKeyMapper;
import com.ruoyi.security.domain.EncryptionKey;
import com.ruoyi.security.service.IEncryptionKeyService;

/**
 * 加密密钥Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-20
 */
@Service
public class EncryptionKeyServiceImpl implements IEncryptionKeyService 
{
    @Autowired
    private EncryptionKeyMapper encryptionKeyMapper;

    /**
     * 查询加密密钥
     * 
     * @param keyId 加密密钥主键
     * @return 加密密钥
     */
    @Override
    public EncryptionKey selectEncryptionKeyByKeyId(Long keyId)
    {
        return encryptionKeyMapper.selectEncryptionKeyByKeyId(keyId);
    }

    /**
     * 根据密钥标识符查询加密密钥
     * 
     * @param keyIdentifier 密钥标识符
     * @return 加密密钥
     */
    @Override
    public EncryptionKey selectEncryptionKeyByIdentifier(String keyIdentifier)
    {
        return encryptionKeyMapper.selectEncryptionKeyByIdentifier(keyIdentifier);
    }

    /**
     * 查询主密钥
     * 
     * @param keyUsage 密钥用途
     * @return 主密钥
     */
    @Override
    public EncryptionKey selectPrimaryKey(String keyUsage)
    {
        return encryptionKeyMapper.selectPrimaryKey(keyUsage);
    }

    /**
     * 查询活跃密钥列表
     * 
     * @param keyUsage 密钥用途
     * @return 活跃密钥列表
     */
    @Override
    public List<EncryptionKey> selectActiveKeys(String keyUsage)
    {
        return encryptionKeyMapper.selectActiveKeys(keyUsage);
    }

    /**
     * 查询即将过期的密钥列表
     * 
     * @param days 天数
     * @return 即将过期的密钥列表
     */
    @Override
    public List<EncryptionKey> selectExpiringKeys(int days)
    {
        return encryptionKeyMapper.selectExpiringKeys(days);
    }

    /**
     * 查询加密密钥列表
     * 
     * @param encryptionKey 加密密钥
     * @return 加密密钥
     */
    @Override
    public List<EncryptionKey> selectEncryptionKeyList(EncryptionKey encryptionKey)
    {
        return encryptionKeyMapper.selectEncryptionKeyList(encryptionKey);
    }

    /**
     * 新增加密密钥
     * 
     * @param encryptionKey 加密密钥
     * @return 结果
     */
    @Override
    public int insertEncryptionKey(EncryptionKey encryptionKey)
    {
        return encryptionKeyMapper.insertEncryptionKey(encryptionKey);
    }

    /**
     * 修改加密密钥
     * 
     * @param encryptionKey 加密密钥
     * @return 结果
     */
    @Override
    public int updateEncryptionKey(EncryptionKey encryptionKey)
    {
        return encryptionKeyMapper.updateEncryptionKey(encryptionKey);
    }

    /**
     * 更新密钥使用统计
     * 
     * @param keyId 密钥ID
     * @return 结果
     */
    @Override
    public int updateKeyUsageStats(Long keyId)
    {
        return encryptionKeyMapper.updateKeyUsageStats(keyId);
    }

    /**
     * 设置主密钥
     * 
     * @param keyId 密钥ID
     * @param keyUsage 密钥用途
     * @return 结果
     */
    @Override
    public int setPrimaryKey(Long keyId, String keyUsage)
    {
        // 先清除现有主密钥标志
        encryptionKeyMapper.clearPrimaryKeyFlag(keyUsage);
        // 设置新的主密钥
        return encryptionKeyMapper.setPrimaryKey(keyId, keyUsage);
    }

    /**
     * 批量删除加密密钥
     * 
     * @param keyIds 需要删除的加密密钥主键集合
     * @return 结果
     */
    @Override
    public int deleteEncryptionKeyByKeyIds(Long[] keyIds)
    {
        return encryptionKeyMapper.deleteEncryptionKeyByKeyIds(keyIds);
    }

    /**
     * 删除加密密钥信息
     * 
     * @param keyId 加密密钥主键
     * @return 结果
     */
    @Override
    public int deleteEncryptionKeyByKeyId(Long keyId)
    {
        return encryptionKeyMapper.deleteEncryptionKeyByKeyId(keyId);
    }
}