package com.ag.until;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.wb.common.Code;
import com.wb.common.CodeBuffer;
import com.wb.common.KVBuffer;
import com.wb.util.DbUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Administrator on 2017/4/17.
 */
public class CodeUtil {
    public static ConcurrentHashMap<String, ConcurrentHashMap<Object, String>> buffer;

    /**
     * ????????
     */
    public static void setCodeBuffer(HttpServletRequest request, HttpServletResponse response) {
        String tableName = request.getParameter("tableName");
        String keyName = request.getParameter("keyName");
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            String kvSql = null, codSql = null, jndi = null;
            conn = DbUtil.getConnection("default");
            st = conn.createStatement();
            rs = st.executeQuery("select KV_SQL ,COD_SQL,JNDI from  CODE_SET  where  TABLE_NAME='" + tableName + "'");
            while (rs.next()) {
                kvSql = rs.getString("KV_SQL");
                codSql = rs.getString("COD_SQL");
                jndi = rs.getString("JNDI");
                if (tableName.equals("S_CODE")) {
                    kvSql += " where SYS_CATEGORY ='" + keyName + "' ";
                    codSql += " where SYS_CATEGORY='" + keyName + "' ";
                }
            }
            KVExecute(jndi, kvSql);
            CodExecute(jndi, codSql);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(rs);
            DbUtil.close(st);
            DbUtil.close(conn);
        }
    }

    /**
     * ??KVbuffer ?map
     *
     * @param jndi
     * @param sql
     */
    private static void KVExecute(String jndi, String sql) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {

            conn = DbUtil.getConnection(jndi);
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            String K, keyName = null, preKeyName = null;
            ConcurrentHashMap<Object, String> map = new ConcurrentHashMap<Object, String>();

            while (rs.next()) {
                keyName = rs.getString("KEY_NAME");
                if (preKeyName != null && !preKeyName.equals(keyName)) {
                    KVBuffer.buffer.remove(preKeyName);
                    KVBuffer.buffer.put(preKeyName, map);
                    map = new ConcurrentHashMap<Object, String>();
                }
                K = rs.getString(2);
                String V = rs.getString(3) == null ? "@" : rs.getString(3);
                map.put(K, V);
                preKeyName = keyName;
            }
            if (preKeyName != null) {
                KVBuffer.buffer.remove(preKeyName);
                KVBuffer.buffer.put(preKeyName, map);
            }
        } catch (Exception e) {

        } finally {

            DbUtil.close(rs);
            DbUtil.close(st);
            DbUtil.close(conn);
        }
    }

    /**
     * ??Codebuffer ?map
     *
     * @param jndi
     * @param sql
     */
    private static void CodExecute(String jndi, String sql) {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            conn = DbUtil.getConnection(jndi);
            st = conn.createStatement();
            rs = st.executeQuery(sql);
            String keyName = null, preKeyName = null;
            List<Code> list = new ArrayList<Code>();

            // 1 tablename 2 K 3 V4 COMV 5 COMV2
            while (rs.next()) {
                keyName = rs.getString(1);
                if (preKeyName != null && !preKeyName.equals(keyName)) {
                    CodeBuffer.buffer.remove(preKeyName);
                    CodeBuffer.buffer.put(preKeyName, list);
                    list = new ArrayList<Code>();
                }
                Code code = new Code();
                code.setFldId(keyName);// 1 tablename 2 K 3 V 4 COMV 5 COMV2
                code.setFldNam(rs.getString(3));
                code.setSysCod(rs.getString(2));// ????
                code.setcName(rs.getString(3));// ?????
                code.seteName(rs.getString(3));// ?????
                code.setComNam1(rs.getString(4));// comName
                code.setComNam2(rs.getString(5));// comName
                list.add(code);
                preKeyName = keyName;
            }
            if (preKeyName != null) {
                CodeBuffer.buffer.remove(preKeyName);
                CodeBuffer.buffer.put(preKeyName, list);
            }
        } catch (Exception e) {

        } finally {

            DbUtil.close(rs);
            DbUtil.close(st);
            DbUtil.close(conn);

        }
    }



}
