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

import com.chenyang.nse.bussiness.dao.BaseDao;
import com.chenyang.nse.bussiness.dao.PageInfo;
import com.chenyang.nse.bussiness.entity.orm.table.core.SensitiveCategoryLog;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository
public class SensitiveCategoryLogDao extends BaseDao<SensitiveCategoryLog, String> {
   public void save(StringBuilder sql) {
      this.getSession().createNativeQuery(sql.toString()).executeUpdate();
   }

   public List<Map<String, Object>> queryAllCategorySensitiveLog(PageInfo pageInfo, String ruletype, String createuser, String projectid) {
      StringBuilder sqlcount = new StringBuilder();
      StringBuilder sql = new StringBuilder();
      sqlcount.append("SELECT COUNT(1) from t_sensitive_category_log t where 1 = 1 ");
      sql.append("SELECT  t.id,t.user,(select scope_name from t_core_data_scope where id = t.scope_category ) as scope_category,(select sensitive_name from t_core_sensitive_level where id = t.scope_level ) as scope_level,t.createtime,t.tablename,t.columnname,t.schemaname,t.projectid, ").append(" (select sysname from t_core_datasystem where id = t.datasystemid ) as datasystemid").append(" from t_sensitive_category_log t where 1 = 1 ");
      if (StringUtils.isNotBlank(projectid)) {
         sql.append(" AND t.projectid = '").append(projectid).append("'");
         sqlcount.append(" AND t.projectid = '").append(projectid).append("'");
      }

      sql.append(" order by t.operationtime desc");
      long count = (long)((BigInteger)this.getSession().createNativeQuery(sqlcount.toString()).getSingleResult()).intValue();
      pageInfo.setCount(count);
      return this.getSession().createNativeQuery(sql.toString()).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setFirstResult(pageInfo.getOffset()).setMaxResults(pageInfo.getPagesize()).getResultList();
   }
}
