/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.stereotype.Repository
 */
package com.hst.taxService.dao.imp;

import com.hst.common.util.DBUtil;
import com.hst.framework.common.HstSqlMapClientTemplate;
import com.hst.framework.util.HstEncodeUtil;
import com.hst.framework.util.HstToolsUtil;
import com.hst.taxService.dao.TaxPublicQueryDao;
import com.hst.taxService.domain.PublicExeSqlRequestDomain;
import com.hst.taxService.domain.PublicProcQueryRequestDomain;
import com.hst.taxService.domain.PublicProcQueryResponseDomain;
import com.hst.taxService.domain.PublicSQLQueryRequestDomain;
import com.hst.taxService.domain.PublicSQLQueryResponseDomain;
import com.hst.taxService.domain.PublicSelectSqlDomain;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class TaxPublicQueryDaoImp
implements TaxPublicQueryDao {
    @Autowired
    private HstSqlMapClientTemplate sqlMapClientTemplate;

    @Override
    public void publicSqlQuery(Connection conn, PublicSQLQueryRequestDomain request, PublicSQLQueryResponseDomain response) throws Exception {
        LinkedHashMap<String, String> map = null;
        PreparedStatement pstmt = null;
        ResultSetMetaData rsmd = null;
        ResultSet rs = null;
        try {
            boolean isIso = false;
            if ("2".equals(request.getCXDB()) || "3".equals(request.getCXDB())) {
                isIso = true;
            }
            pstmt = isIso ? conn.prepareStatement(HstEncodeUtil.GBK2ISO(request.getSQL())) : conn.prepareStatement(request.getSQL());
            rs = pstmt.executeQuery();
            rsmd = rs.getMetaData();
            int colSize = 0;
            colSize = rsmd.getColumnCount();
            ArrayList<String> columns = new ArrayList<String>();
            int i = 0;
            while (i < colSize) {
                columns.add(rsmd.getColumnName(i + 1));
                ++i;
            }
            while (rs.next()) {
                map = new LinkedHashMap<String, String>();
                for (String col : columns) {
                    if (HstToolsUtil.isNullOrEmpty(rs.getString(col))) {
                        map.put(col, "");
                        continue;
                    }
                    if (isIso) {
                        map.put(col, HstEncodeUtil.ISO2GBK(rs.getString(col)));
                        continue;
                    }
                    map.put(col, rs.getString(col));
                }
                response.getDataList().add(map);
            }
        }
        catch (Throwable throwable) {
            DBUtil.close(rs, pstmt);
            throw throwable;
        }
        DBUtil.close(rs, pstmt);
    }

    @Override
    public void publicProcQuery(Connection conn, PublicProcQueryRequestDomain request, PublicProcQueryResponseDomain response) throws Exception {
        LinkedHashMap<String, String> map = null;
        CallableStatement cstmt = null;
        ResultSetMetaData rsmd = null;
        ResultSet rs = null;
        StringBuffer function = new StringBuffer("");
        try {
            try {
                boolean isIso = false;
                if ("2".equals(request.getCXDB()) || "3".equals(request.getCXDB())) {
                    isIso = true;
                }
                function.append("{call ").append(request.getPROC()).append("(?,?)}");
                conn.setAutoCommit(false);
                cstmt = conn.prepareCall(function.toString());
                if (isIso) {
                    cstmt.setString(1, HstEncodeUtil.GBK2ISO(request.getWHERE()));
                } else {
                    cstmt.setString(1, request.getWHERE());
                }
                cstmt.registerOutParameter(2, -10);
                cstmt.execute();
                rs = (ResultSet)cstmt.getObject(2);
                rsmd = rs.getMetaData();
                int colSize = 0;
                colSize = rsmd.getColumnCount();
                ArrayList<String> columns = new ArrayList<String>();
                int i = 0;
                while (i < colSize) {
                    columns.add(rsmd.getColumnName(i + 1));
                    ++i;
                }
                while (rs.next()) {
                    map = new LinkedHashMap<String, String>();
                    for (String col : columns) {
                        if (HstToolsUtil.isNullOrEmpty(rs.getString(col))) {
                            map.put(col, "");
                            continue;
                        }
                        if (isIso) {
                            map.put(col, HstEncodeUtil.ISO2GBK(rs.getString(col)));
                            continue;
                        }
                        map.put(col, rs.getString(col));
                    }
                    response.getDataList().add(map);
                }
                conn.commit();
            }
            catch (Exception e) {
                if (conn != null) {
                    conn.rollback();
                }
                throw e;
            }
        }
        catch (Throwable throwable) {
            DBUtil.close(rs, cstmt);
            throw throwable;
        }
        DBUtil.close(rs, cstmt);
    }

    @Override
    public Connection getGlptConn() throws Exception {
        return this.sqlMapClientTemplate.getDataSource().getConnection();
    }

    @Override
    public PublicSelectSqlDomain querySql(PublicExeSqlRequestDomain request) throws Exception {
        CallableStatement cstmt = null;
        Connection conn = null;
        PublicSelectSqlDomain domain = null;
        try {
            try {
                String proc = "{call P_PUB_GT3_GET_SELECT_SQL(?,?,?,?,?,?)}";
                conn = this.getGlptConn();
                conn.setAutoCommit(false);
                cstmt = conn.prepareCall(proc);
                cstmt.setString(1, request.getSQLID());
                cstmt.setString(2, request.getWHERE());
                cstmt.registerOutParameter(3, 12);
                cstmt.registerOutParameter(4, 12);
                cstmt.registerOutParameter(5, 12);
                cstmt.registerOutParameter(6, 12);
                cstmt.execute();
                domain = new PublicSelectSqlDomain();
                domain.setCode((String)cstmt.getObject(3));
                domain.setMess(HstEncodeUtil.ISO2GBK((String)cstmt.getObject(4)));
                domain.setSqlDb((String)cstmt.getObject(5));
                domain.setSql(HstEncodeUtil.ISO2GBK((String)cstmt.getObject(6)));
                conn.commit();
            }
            catch (Exception e) {
                if (conn != null) {
                    conn.rollback();
                }
                throw e;
            }
        }
        catch (Throwable throwable) {
            DBUtil.close(null, cstmt);
            DBUtil.close(conn);
            throw throwable;
        }
        DBUtil.close(null, cstmt);
        DBUtil.close(conn);
        return domain;
    }
}
