package com.chenyang.nse.bussiness.dao.table.core.dataproject;

import com.chenyang.nse.bussiness.config.HibernateConfig;
import com.chenyang.nse.bussiness.dao.BaseDao;
import com.chenyang.nse.bussiness.entity.orm.table.core.dataproject.TCoreSecretkey;
import java.util.List;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository
public class TCoreSecretkeyDao extends BaseDao<TCoreSecretkey, String> {
   public String queryIdByKeyAndSKey(String keyName, String sKeyName) {
      String sql = "select id from T_CORE_SECRETKEY where encryption_id  = (select id from T_CORE_ENCRYPTION where encryption_name='" + keyName + "') and secret_key_name='" + sKeyName + "'";
      return this.getSession().createNativeQuery(sql).getSingleResult().toString();
   }

   public List<TCoreSecretkey> getProjectSecretKeys(String projectid) {
      String sql = "select distinct s.id as id, s.createtime as createtime, s.createuser as createuser, s.flag as flag, s.note as note, s.updatetime as updatetime,s.updateuser as updateuser, s.privatekey as privatekey, s.publickey as publickey,s.encryption_id as encryption_id, s.secret_key_name as secret_key_name, s.sectet_key_value as sectet_key_value from t_core_secretkey s join t_core_encryption_field f on s.id = f.encryption_secretKey_id where f.projectid = '" + projectid + "'";
      if (HibernateConfig.PEIZHIKUDBTYPE.equals("2")) {
         sql = "select distinct s.id as id, s.createtime as createtime, s.createuser as createuser, s.flag as flag, cast(s.note as varchar) as note, s.updatetime as updatetime,s.updateuser as updateuser, s.privatekey as privatekey, s.publickey as publickey,s.encryption_id as encryption_id, s.secret_key_name as secret_key_name, s.sectet_key_value as sectet_key_value from t_core_secretkey s join t_core_encryption_field f on s.id = f.encryption_secretKey_id where f.projectid = '" + projectid + "'";
      }

      if (HibernateConfig.PEIZHIKUDBTYPE.equals("3")) {
         sql = "select distinct s.id as \"id\", s.createtime as \"createtime\", s.createuser as \"createuser\", s.flag as \"flag\", s.note as \"note\", s.updatetime as \"updatetime\",s.updateuser as \"updateuser\", s.privatekey as \"privatekey\", s.publickey as \"publickey\",s.encryption_id as \"encryption_id\", s.secret_key_name as \"secret_key_name\", s.sectet_key_value as \"sectet_key_value\"from t_core_secretkey s join t_core_encryption_field f on s.id = f.encryption_secretKey_id where f.projectid = '" + projectid + "'";
      }

      List<TCoreSecretkey> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.aliasToBean(TCoreSecretkey.class)).getResultList();
      return vo;
   }

   public List<TCoreSecretkey> getExportSecretKeys() {
      String sql = "select distinct s.id as id, s.createtime as createtime, s.createuser as createuser, s.flag as flag, s.note as note, s.updatetime as updatetime,s.updateuser as updateuser, s.privatekey as privatekey, s.publickey as publickey,s.encryption_id as encryption_id, s.secret_key_name as secret_key_name, s.sectet_key_value as sectet_key_value from t_core_secretkey s ";
      if (HibernateConfig.PEIZHIKUDBTYPE.equals("2")) {
         sql = "select distinct s.id as id, s.createtime as createtime, s.createuser as createuser, s.flag as flag, cast(s.note as varchar) as note, s.updatetime as updatetime,s.updateuser as updateuser, s.privatekey as privatekey, s.publickey as publickey, s.encryption_id as encryption_id, s.secret_key_name as secret_key_name, s.sectet_key_value as sectet_key_value from t_core_secretkey s ";
      }

      if (HibernateConfig.PEIZHIKUDBTYPE.equals("3")) {
         sql = "select distinct s.id as \"id\", s.createtime as \"createtime\", s.createuser as \"createuser\", s.flag as \"flag\", s.note as \"note\", s.updatetime as \"updatetime\",s.updateuser as \"updateuser\", s.privatekey as \"privatekey\", s.publickey as \"publickey\",s.encryption_id as \"encryption_id\", s.secret_key_name as \"secret_key_name\", s.sectet_key_value as \"sectet_key_value\"from t_core_secretkey s ";
      }

      List<TCoreSecretkey> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.aliasToBean(TCoreSecretkey.class)).getResultList();
      return vo;
   }
}
