package com.chenyang.nse.bussiness.jar.db;

import com.chenyang.nse.bussiness.dao.BaseDao;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

@Repository
public class TCoreClientRuleChangeLogDao extends BaseDao<TCoreClientRuleChangeLog, String> {
   public List<TCoreClientRuleChangeLog> queryLogs(String clientId, int currentpage, int pageSize) {
      this.getSession().clear();
      List<TCoreClientRuleChangeLog> resultList = new ArrayList();

      try {
         StringBuilder sql = new StringBuilder();
         sql.append("SELECT *FROM t_core_client_rule_change_log  where t_client_id='" + clientId + "' ");
         sql.append("order by create_time desc ");
         sql.append("limit " + (currentpage - 1) * pageSize + "," + pageSize);
         resultList = this.getSession().createNativeQuery(sql.toString(), TCoreClientRuleChangeLog.class).getResultList();

         for(TCoreClientRuleChangeLog tCoreClientRuleChangeLog : resultList) {
            String ruleStr = tCoreClientRuleChangeLog.getRule_str();
            tCoreClientRuleChangeLog.setRule_str(StringUtils.isEmpty(ruleStr) ? "" : this.deleteSecretKey(ruleStr));
         }
      } catch (Exception e) {
         e.printStackTrace();
      }

      return resultList;
   }

   public String deleteSecretKey(String str) {
      if (StringUtils.isEmpty(str)) {
         return "";
      } else {
         StringBuilder sb;
         int i;
         for(i = 0; (i = str.indexOf(",\"secretkey\":\"")) > 0; str = sb.toString()) {
            int n = str.indexOf("\"", i + 15) + 1;
            sb = new StringBuilder(str);
            sb.delete(i, n);
         }

         return str;
      }
   }

   public Integer queryLogsTotal(String clientId) {
      this.getSession().clear();
      List<TCoreClientRuleChangeLog> resultList = new ArrayList();

      try {
         StringBuilder sql = new StringBuilder();
         sql.append("SELECT *FROM t_core_client_rule_change_log  where t_client_id='" + clientId + "' ");
         resultList = this.getSession().createNativeQuery(sql.toString(), TCoreClientRuleChangeLog.class).getResultList();
      } catch (Exception e) {
         e.printStackTrace();
      }

      return resultList.size();
   }

   public static void main(String[] args) {
      String str = "[{\"datasystemid\":\"4028826384bd32240184bd437e4f0006\",\"dbip\":\"192.168.2.191\",\n\"dbport\":\"3306\",\"dbservername\":\"ry\",\"schema\":\"ry\",\"tableInfo\":[\n{\"columninfo\":[{\"columnname\":\"user_name\",\"encryptiontype\":\n\"SM4\",\"isLike\":\"0\",\"secretkey\":\"E20D0F3FBF8DCAB88C6A335CA944CD65\"},\"{\"columninfo\":[{\"columnname\":\"user_name\",\"encryptiontype\":\\n\" +\n                \"SM4\",\"isLike\":\"0\",\"secretkey\":\"E20D0F3FBF8DCAB88C6A335CA944CD65\"}],\n\"tablename\":\"sys_user\"}]}]";

      int i;
      StringBuilder sb;
      for(str = null; (i = str.indexOf(",\"secretkey\":\"")) > 0; str = sb.toString()) {
         int n = str.indexOf("\"", i + 15) + 1;
         sb = new StringBuilder(str);
         sb.delete(i, n);
      }

      System.out.println(str);
   }
}
