package com.hzya.frame.sysnew.application.service.impl;

import com.hzya.frame.sysnew.application.api.dao.ISysApplicationApiDao;
import com.hzya.frame.sysnew.application.api.entity.SysApplicationApiEntity;
import com.hzya.frame.sysnew.application.apiAuth.dao.ISysApplicationApiAuthDao;
import com.hzya.frame.sysnew.application.apiAuth.entity.SysApplicationApiAuthEntity;
import com.hzya.frame.sysnew.application.apiAythDetail.dao.ISysApplicationApiAuthDetailDao;
import com.hzya.frame.sysnew.application.apiAythDetail.entity.SysApplicationApiAuthDetailEntity;
import com.hzya.frame.sysnew.application.dao.ISysApplicationDao;
import com.hzya.frame.sysnew.application.entity.SysApplicationEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Content 应用缓存
 * @Author 👻👻👻👻👻👻👻👻👻👻 llg
 * @Version 1.0
 */
@Service
public class ApplicationCache {
    Logger logger = LoggerFactory.getLogger(ApplicationCache.class);
    private final ConcurrentHashMap<String, Object> applicationMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Object> applicationApiMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Object> applicationApiAuthMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, Object> applicationApiAuthDetailMap = new ConcurrentHashMap<>();

    @Resource
    private ISysApplicationApiAuthDetailDao sysApplicationApiAuthDetailDao;
    @Resource
    private ISysApplicationApiAuthDao sysApplicationApiAuthDao;
    @Resource
    private ISysApplicationApiDao sysApplicationApiDao;
    @Resource
    private ISysApplicationDao sysApplicationDao;
    public void saveData(String type,String key, Object value) {
        switch (type) {
            case "1": //
                applicationMap.put(key, value);
                break;
            case "2": //
                applicationApiMap.put(key, value);
                break;
            case "3": //
                applicationApiAuthMap.put(key, value);
                break;
            case "4": //
                applicationApiAuthDetailMap.put(key, value);
                break;
            default:
        }
    }

    public Object get(String type,String key) {
        Object o = null;
        switch (type) {
            case "1": //
                o = applicationMap.get(key);
                break;
            case "2": //
                o = applicationApiMap.get(key);
                break;
            case "3": //
                o = applicationApiAuthMap.get(key);
                break;
            case "4": //
                o = applicationApiAuthDetailMap.get(key);
                break;
            default:
        }
        return o;
    }
    public void reloadData(String type) {
        switch (type) {
            case "1": //
                applicationMap.clear();
                SysApplicationEntity sendApp = new SysApplicationEntity();
                sendApp.setSts("Y");
                List<SysApplicationEntity> sysApplicationEntities = sysApplicationDao.queryBase(sendApp);
                if(sysApplicationEntities != null && sysApplicationEntities.size() > 0){
                    for (int i = 0; i < sysApplicationEntities.size(); i++) {
                        applicationMap.put("publicKey"+sysApplicationEntities.get(i).getPublicKey()+"secretKey"+sysApplicationEntities.get(i).getSecretKey(), sysApplicationEntities.get(i));
                        applicationMap.put("appId"+sysApplicationEntities.get(i).getAppId(), sysApplicationEntities.get(i));
                    }
                }
                break;
            case "2": //
                applicationApiMap.clear();
                SysApplicationApiEntity receiveApi = new SysApplicationApiEntity();
                receiveApi.setSts("Y");
                List<SysApplicationApiEntity> receiveApiList = sysApplicationApiDao.queryBase(receiveApi);
                if(receiveApiList != null && receiveApiList.size() > 0){
                    for (int i = 0; i < receiveApiList.size(); i++) {
                        applicationApiMap.put("appId"+receiveApiList.get(i).getAppId()+"apiCode"+receiveApiList.get(i).getApiCode(), receiveApiList.get(i));
                    }
                }
                break;
            case "3": //
                applicationApiAuthMap.clear();
                SysApplicationApiAuthEntity sysApplicationApiAuthEntity = new SysApplicationApiAuthEntity();
                sysApplicationApiAuthEntity.setSts("Y");
                sysApplicationApiAuthEntity.setSystemStatus("1");
                List<SysApplicationApiAuthEntity> sysApplicationApiAuthEntities = sysApplicationApiAuthDao.queryBase(sysApplicationApiAuthEntity);
                if(sysApplicationApiAuthEntities != null && sysApplicationApiAuthEntities.size() > 0){
                    for (int i = 0; i < sysApplicationApiAuthEntities.size(); i++) {
                        applicationApiAuthMap.put("name"+sysApplicationApiAuthEntities.get(i).getName()+"appId"+sysApplicationApiAuthEntities.get(i).getAppId(), sysApplicationApiAuthEntities.get(i));
                    }
                }
                break;
            case "4": //
                applicationApiAuthDetailMap.clear();
                SysApplicationApiAuthDetailEntity sysApplicationApiAuthDetailEntity = new SysApplicationApiAuthDetailEntity();
                sysApplicationApiAuthDetailEntity.setSts("Y");
                sysApplicationApiAuthDetailEntity.setStatusType("2");
                List<SysApplicationApiAuthDetailEntity> sysApplicationApiAuthDetailEntities = sysApplicationApiAuthDetailDao.queryBase(sysApplicationApiAuthDetailEntity);
                if(sysApplicationApiAuthDetailEntities != null && sysApplicationApiAuthDetailEntities.size() > 0){
                    for (int i = 0; i < sysApplicationApiAuthDetailEntities.size(); i++) {
                        applicationApiAuthDetailMap.put("appId"+sysApplicationApiAuthDetailEntities.get(i).getAppId()+"apiId"+sysApplicationApiAuthDetailEntities.get(i).getApiId()+"tripartiteSystemId"+sysApplicationApiAuthDetailEntities.get(i).getTripartiteSystemId(), sysApplicationApiAuthDetailEntities.get(i));
                    }
                }
                break;
            default:
        }
    }

}
