package com.ruoyi.system.service.impl;

import java.security.KeyPair;
import java.util.List;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.sign.RSAUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysApplyConfigMapper;
import com.ruoyi.system.domain.SysApplyConfig;
import com.ruoyi.system.service.ISysApplyConfigService;
import com.ruoyi.common.utils.SecurityUtils;

import javax.annotation.PostConstruct;

/**
 * 系统应用注册Service业务层处理
 * 
 * @author JiangYuQi
 * @date 2024-01-13
 */
@Service
public class SysApplyConfigServiceImpl implements ISysApplyConfigService 
{
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private SysApplyConfigMapper sysApplyConfigMapper;

    /**
     * 项目启动时，初始化注册应用到缓存
     */
    @PostConstruct
    public void init()
    {
        loadingSysApplyConfigCache();
    }

    /**
     * 加载注册应用
     */
    @Override
    public void loadingSysApplyConfigCache()
    {
        List<SysApplyConfig> configList = sysApplyConfigMapper.selectSysApplyConfigList(new SysApplyConfig());
        if(CollectionUtils.isNotEmpty(configList)){
            for (SysApplyConfig sysApplyConfig : configList) {
                redisCache.setCacheObject(CacheConstants.YANZHU_SYSTEM_CONFIG + sysApplyConfig.getAppId(), sysApplyConfig);
            }
        }
    }

    /**
     * 查询系统应用注册
     * 
     * @param id 系统应用注册主键
     * @return 系统应用注册
     */
    @Override
    public SysApplyConfig selectSysApplyConfigById(Long id)
    {
        return sysApplyConfigMapper.selectSysApplyConfigById(id);
    }

    /**
     * 查询系统应用注册列表
     * 
     * @param sysApplyConfig 系统应用注册
     * @return 系统应用注册
     */
    @Override
    public List<SysApplyConfig> selectSysApplyConfigList(SysApplyConfig sysApplyConfig)
    {
        return sysApplyConfigMapper.selectSysApplyConfigList(sysApplyConfig);
    }

    /**
     * 新增系统应用注册
     * 
     * @param sysApplyConfig 系统应用注册
     * @return 结果
     */
    @Override
    public int insertSysApplyConfig(SysApplyConfig sysApplyConfig)
    {
        sysApplyConfig.setCreateBy(SecurityUtils.getUsername());
        sysApplyConfig.setCreateTime(DateUtils.getNowDate());
        try {
            // 生成密钥对
            KeyPair keyPair = RSAUtil.getKeyPair();
            // 公匙
            String publicKey = RSAUtil.getPublicKeyBase64(keyPair);
            sysApplyConfig.setPublicKey(publicKey);
            // 私匙
            String privateKey = RSAUtil.getPrivateKeyBase64(keyPair);
            sysApplyConfig.setPrivateKey(privateKey);
        }catch (Exception e){
            throw new ServiceException();
        }
        int res = sysApplyConfigMapper.insertSysApplyConfig(sysApplyConfig);
        if(res>0){
            this.loadingSysApplyConfigCache();
        }
        return res;
    }

    /**
     * 修改系统应用注册
     * 
     * @param sysApplyConfig 系统应用注册
     * @return 结果
     */
    @Override
    public int updateSysApplyConfig(SysApplyConfig sysApplyConfig)
    {
        sysApplyConfig.setUpdateBy(SecurityUtils.getUsername());
        sysApplyConfig.setUpdateTime(DateUtils.getNowDate());
        int res = sysApplyConfigMapper.updateSysApplyConfig(sysApplyConfig);
        if(res>0){
            this.loadingSysApplyConfigCache();
        }
        return res;
    }

    /**
     * 批量删除系统应用注册
     * 
     * @param ids 需要删除的系统应用注册主键
     * @return 结果
     */
    @Override
    public int deleteSysApplyConfigByIds(Long[] ids)
    {
        int res = sysApplyConfigMapper.deleteSysApplyConfigByIds(ids);
        if(res>0){
            this.loadingSysApplyConfigCache();
        }
        return res;
    }

    /**
     * 删除系统应用注册信息
     * 
     * @param id 系统应用注册主键
     * @return 结果
     */
    @Override
    public int deleteSysApplyConfigById(Long id)
    {
        int res = sysApplyConfigMapper.deleteSysApplyConfigById(id);
        if(res>0){
            this.loadingSysApplyConfigCache();
        }
        return res;
    }
}
