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.TCoreProjectDataarea;
import java.util.List;
import java.util.Map;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

@Repository
public class TCoreProjectDataareaDao extends BaseDao<TCoreProjectDataarea, String> {
   public List<Map<String, Object>> selectCheckDataArea(String id) {
      String sql = "SELECT * FROM(SELECT a.id id,a.rulestyle rulestyle,c.id areatid,c.dataarea dataarea,a.rule_id ruleid,b.name rulename,b.ruleexample ruleexample,a.defaultrule defaultrule FROM t_core_project_findrule a,t_console_searchregex b,t_core_dataarea c WHERE a.rule_id = b.id AND a.dataarea_id = c.id AND a.project_id = '" + id + "' order by rulename) aa union SELECT * FROM( SELECT a1.id id,a1.rulestyle rulestyle,c1.id areatid,c1.dataarea dataarea,a1.rule_id ruleid,b1.name rulename,b1.ruledemo ruleexample,a1.defaultrule defaultrule FROM t_core_project_findrule a1,t_core_masking_ruleinfo b1,t_core_dataarea c1 WHERE a1.rule_id = b1.id AND a1.dataarea_id = c1.id AND a1.project_id = '" + id + "' order by rulename) bb";
      List<Map<String, Object>> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
      return vo;
   }

   public List<Map<String, Object>> selectDataAreaHas(String id) {
      String sql = "SELECT a.dataarea_id dataarea_id FROM t_core_project_findrule a WHERE  a.project_id = '" + id + "' group by a.dataarea_id";
      List<Map<String, Object>> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
      return vo;
   }

   public List<Map<String, Object>> selectDataAreaHasEM() {
      String sql = "SELECT a.dataarea_id dataarea_id FROM t_core_project_findrule a group by a.dataarea_id";
      List<Map<String, Object>> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
      return vo;
   }

   public List<Map<String, Object>> selectArea() {
      String sql = "SELECT count(1) as s FROM t_core_project_findrule ";
      List<Map<String, Object>> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
      return vo;
   }

   public List<Map<String, Object>> selectAreaCount(String id) {
      String sql = "SELECT count(distinct dataarea_id)  as s FROM t_core_project_findrule where project_id ='" + id + "' union all SELECT count(1) as s FROM t_core_project_findrule where rulestyle ='0' and project_id ='" + id + "' union all SELECT count(1) as s FROM t_core_project_findrule where rulestyle ='1' and project_id ='" + id + "' ";
      if (HibernateConfig.PEIZHIKUDBTYPE.equals("3")) {
         sql = "SELECT count(distinct dataarea_id)  as \"s\" FROM t_core_project_findrule where project_id ='" + id + "' union all SELECT count(1) as s FROM t_core_project_findrule where rulestyle ='0' and project_id ='" + id + "' union all SELECT count(1) as s FROM t_core_project_findrule where rulestyle ='1' and project_id ='" + id + "' ";
      }

      List<Map<String, Object>> vo = this.getSession().createNativeQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).getResultList();
      return vo;
   }
}
