/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  org.apache.commons.logging.Log
 *  org.apache.commons.logging.LogFactory
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.orm.ibatis.SqlMapClientTemplate
 *  org.springframework.stereotype.Service
 */
package com.hst.task.runable;

import com.hst.common.util.DBUtil;
import com.hst.framework.common.HstSqlMapClientTemplate;
import com.hst.framework.constants.WebConstants;
import com.hst.framework.dao.BaseDao;
import com.hst.framework.util.HstDateUtil;
import com.hst.framework.util.HstEncodeUtil;
import com.hst.task.domain.TaskData;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

@Service
public class JhsjTsTaskRunable {
    public static Log _logger = LogFactory.getLog(JhsjTsTaskRunable.class);
    @Autowired
    private SqlMapClientTemplate smzbsSqlMapClientTemplate;
    @Autowired
    private HstSqlMapClientTemplate sqlMapClientTemplate;
    private BaseDao dao;

    public BaseDao getDao(String daoName) {
        this.dao = (BaseDao)WebConstants.WEB_APP_CONTEXT.getBean(daoName);
        return this.dao;
    }

    public void doUploadJhsj(TaskData task) throws Exception {
        block11: {
            Connection targetConn = null;
            Connection localConn = null;
            try {
                try {
                    targetConn = this.sqlMapClientTemplate.getDataSource().getConnection();
                    localConn = this.smzbsSqlMapClientTemplate.getDataSource().getConnection();
                    targetConn.setAutoCommit(false);
                    if ("U".equals(task.getCsbz().toUpperCase())) {
                        Map<String, Object> resultMap = this.queryUploadDataList(task, localConn);
                        if ("0".equals(resultMap.get("code"))) {
                            List uploadDataList = (List)resultMap.get("dataList");
                            String ksyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            if (StringUtils.isNotBlank((String)task.getInsert_sql())) {
                                String dbcode = WebConstants.JHXT_XTCS.get("2003");
                                if (StringUtils.isBlank((String)dbcode)) {
                                    dbcode = "ISO";
                                }
                                this.doInsertSql(task, uploadDataList, targetConn, dbcode);
                            }
                            String jsyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            this.hxclProc(task, ksyxsj, jsyxsj, String.valueOf(uploadDataList.size()), targetConn, "Y", "");
                        } else {
                            String ksyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            String jsyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            this.hxclProc(task, ksyxsj, jsyxsj, "0", targetConn, "N", (String)resultMap.get("mess"));
                        }
                    }
                    targetConn.commit();
                }
                catch (Exception e) {
                    _logger.error((Object)e);
                    if (targetConn != null) {
                        targetConn.rollback();
                    }
                    DBUtil.close(localConn);
                    DBUtil.close(targetConn);
                    break block11;
                }
            }
            catch (Throwable throwable) {
                DBUtil.close(localConn);
                DBUtil.close(targetConn);
                throw throwable;
            }
            DBUtil.close(localConn);
            DBUtil.close(targetConn);
        }
    }

    public void doDownLoad(TaskData task) throws Exception {
        block15: {
            Connection remoteConn = null;
            Connection localConn = null;
            String ksyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
            try {
                try {
                    remoteConn = this.sqlMapClientTemplate.getDataSource().getConnection();
                    localConn = this.smzbsSqlMapClientTemplate.getDataSource().getConnection();
                    localConn.setAutoCommit(false);
                    if ("D".equals(task.getCsbz().toUpperCase())) {
                        Map<String, Object> resultMap;
                        String dbcode = WebConstants.JHXT_XTCS.get("2003");
                        if (StringUtils.isBlank((String)dbcode)) {
                            dbcode = "ISO";
                        }
                        if ("0".equals((resultMap = this.queryUploadDataByProcForOracle(task, remoteConn, dbcode)).get("code"))) {
                            List downloadList = (List)resultMap.get("dataList");
                            String delSql = "";
                            if ("3001".equals(task.getSjtbrw_bm())) {
                                for (Object[] data : downloadList) {
                                    WebConstants.JHXT_XTCS.put((String)data[0], (String)data[1]);
                                }
                                delSql = task.getDelete_sql();
                            } else {
                                delSql = task.getDelete_sql();
                            }
                            this.doExecteUpdateSql(task, delSql, localConn);
                            this.doInsertSql(task, downloadList, localConn, "GBK");
                            if ("3001".equals(task.getSjtbrw_bm())) {
                                String updateJhxtCsSql = "UPDATE SYSTEM   SET VALUE = B.XTCS_VALUE  FROM SYSTEM A , NSFW_CS_XTCS B WHERE A.[KEY] = B.JHXT_BM ";
                                String insertJhxtCsSql = "INSERT INTO SYSTEM ([KEY],VALUE,MEMO) SELECT JHXT_BM ,XTCS_VALUE,XTCS_SM  FROM NSFW_CS_XTCS  A WHERE NOT EXISTS (SELECT  1 FROM SYSTEM  B WHERE B.[KEY] = A.JHXT_BM) AND JHXT_BM IS NOT NULL ";
                                this.doExecteUpdateSql(task, updateJhxtCsSql, localConn);
                                this.doExecteUpdateSql(task, insertJhxtCsSql, localConn);
                            }
                            String jsyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            if (StringUtils.isNotBlank((String)task.getHxcl_proc())) {
                                this.hxclProc(task, ksyxsj, jsyxsj, String.valueOf(downloadList.size()), remoteConn, "Y", "");
                            }
                        } else {
                            String jsyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                            this.hxclProc(task, ksyxsj, jsyxsj, "0", remoteConn, "N", (String)resultMap.get("mess"));
                        }
                    }
                    localConn.commit();
                }
                catch (Exception e) {
                    _logger.error((Object)e);
                    if (localConn != null) {
                        localConn.rollback();
                    }
                    String jsyxsj = HstDateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss");
                    String mess = "";
                    mess = e.getMessage().length() > 1000 ? e.getMessage().substring(0, 1000) : e.getMessage();
                    this.hxclProc(task, ksyxsj, jsyxsj, "0", remoteConn, "N", mess);
                    DBUtil.close(localConn);
                    DBUtil.close(remoteConn);
                    break block15;
                }
            }
            catch (Throwable throwable) {
                DBUtil.close(localConn);
                DBUtil.close(remoteConn);
                throw throwable;
            }
            DBUtil.close(localConn);
            DBUtil.close(remoteConn);
        }
    }

    private Map<String, Object> queryUploadDataList(TaskData task, Connection conn) throws Exception {
        if ("1".equals(task.getZxyjbz())) {
            return this.queryUploadDataBySelect(task, conn);
        }
        return this.queryUploadDataByProcForSqlServer(task, conn);
    }

    public Map<String, Object> queryUploadDataBySelect(TaskData task, Connection conn) throws Exception {
        Statement pstmt = null;
        ResultSet rs = null;
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", "0");
        resultMap.put("mess", "");
        ArrayList<Object[]> list = new ArrayList<Object[]>();
        try {
            _logger.debug((Object)("\u6267\u884cselect_sql\uff1a" + task.getSelect_sql()));
            pstmt = conn.prepareStatement(task.getSelect_sql());
            rs = pstmt.executeQuery();
            int size = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                Object[] obj = new Object[size];
                int i = 0;
                while (i < size) {
                    obj[i] = rs.getObject(i + 1);
                    ++i;
                }
                list.add(obj);
            }
            resultMap.put("dataList", list);
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        catch (Exception e) {
            _logger.error((Object)("\u6267\u884cSELECT\u8bed\u53e5\u51fa\u9519\uff0csjtbrw_bm\uff1a" + task.getSjtbrw_bm() + "sql:" + task.getSelect_sql()), (Throwable)e);
            if (e.getMessage().length() > 1000) {
                resultMap.put("mess", e.getMessage().substring(0, 1000));
            } else {
                resultMap.put("mess", e.getMessage());
            }
            resultMap.put("code", "01");
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        finally {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }

    public Map<String, Object> queryUploadDataByProcForSqlServer(TaskData task, Connection conn) throws Exception {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", "0");
        resultMap.put("mess", "");
        Statement cstmt = null;
        ResultSet rs = null;
        ArrayList<Object[]> list = new ArrayList<Object[]>();
        try {
            String proc = task.getSelect_sql();
            proc = "{ call " + proc + " }";
            cstmt = conn.prepareCall(proc);
            int parIndex = 1;
            if ("Y".equals(task.getSjcsbz())) {
                cstmt.setString(parIndex++, task.getYwzhsj());
            }
            cstmt.registerOutParameter(parIndex++, 4);
            cstmt.registerOutParameter(parIndex++, 12);
            _logger.debug((Object)("\u6267\u884cproc\uff1a" + proc + cstmt.getParameterMetaData()));
            rs = cstmt.executeQuery();
            int size = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                Object[] obj = new Object[size];
                int i = 0;
                while (i < size) {
                    obj[i] = rs.getObject(i + 1);
                    ++i;
                }
                list.add(obj);
            }
            resultMap.put("dataList", list);
            String code = String.valueOf(cstmt.getInt(parIndex - 2));
            String mess = cstmt.getString(parIndex - 1);
            resultMap.put("code", code);
            resultMap.put("mess", mess);
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        catch (Exception e) {
            _logger.error((Object)("\u6267\u884cPROC\u8bed\u53e5\u51fa\u9519\uff0c\u4efb\u52a1\u7f16\u7801\uff1a" + task.getSjtbrw_bm() + "proc" + task.getSelect_sql()), (Throwable)e);
            if (e.getMessage().length() > 1000) {
                resultMap.put("mess", e.getMessage().substring(0, 1000));
            } else {
                resultMap.put("mess", e.getMessage());
            }
            resultMap.put("code", "01");
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        finally {
            if (rs != null) {
                rs.close();
            }
            if (cstmt != null) {
                cstmt.close();
            }
        }
    }

    public Map<String, Object> queryUploadDataByProcForOracle(TaskData task, Connection conn, String dbcode) throws Exception {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("code", "0");
        resultMap.put("mess", "");
        Statement cstmt = null;
        ResultSet rs = null;
        ArrayList<Object[]> list = new ArrayList<Object[]>();
        try {
            String proc = task.getSelect_sql();
            proc = "{ call " + proc + " }";
            cstmt = conn.prepareCall(proc);
            int parIndex = 1;
            cstmt.setString(parIndex++, WebConstants.SMZ_RXBD_BSDT_DJXH);
            cstmt.registerOutParameter(parIndex++, -10);
            _logger.debug((Object)("\u6267\u884cproc\uff1a" + proc + cstmt.getParameterMetaData()));
            cstmt.execute();
            rs = (ResultSet)cstmt.getObject(parIndex - 1);
            int size = rs.getMetaData().getColumnCount();
            while (rs.next()) {
                Object[] obj = new Object[size];
                int i = 0;
                while (i < size) {
                    obj[i] = "ISO".equals(dbcode) ? HstEncodeUtil.ISO2GBK(rs.getString(i + 1)) : rs.getString(i + 1);
                    ++i;
                }
                list.add(obj);
            }
            resultMap.put("dataList", list);
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        catch (Exception e) {
            _logger.error((Object)("\u6267\u884cPROC\u8bed\u53e5\u51fa\u9519\uff0c\u4efb\u52a1\u7f16\u7801\uff1a" + task.getSjtbrw_bm() + "proc" + task.getSelect_sql()), (Throwable)e);
            if (e.getMessage().length() > 1000) {
                resultMap.put("mess", e.getMessage().substring(0, 1000));
            } else {
                resultMap.put("mess", e.getMessage());
            }
            resultMap.put("code", "01");
            HashMap<String, Object> hashMap = resultMap;
            return hashMap;
        }
        finally {
            if (rs != null) {
                rs.close();
            }
            if (cstmt != null) {
                cstmt.close();
            }
        }
    }

    public void doInsertSql(TaskData task, List<Object[]> dataList, Connection conn, String dbcode) throws Exception {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            try {
                _logger.info((Object)("\u6267\u884c\u4efb\u52a1\u7f16\u53f7:" + task.getSjtbrw_bm() + ",INSERTSQL:" + task.getInsert_sql()));
                pstmt = conn.prepareStatement(task.getInsert_sql());
                int i = 0;
                while (i < dataList.size()) {
                    Object[] obj = dataList.get(i);
                    int j = 0;
                    while (j < obj.length) {
                        Object temp = obj[j];
                        if (obj[j] != null && "ISO".equalsIgnoreCase(dbcode)) {
                            try {
                                temp = HstEncodeUtil.GBK2ISO(obj[j].toString());
                            }
                            catch (Exception e) {
                                throw new RuntimeException(e);
                            }
                        }
                        pstmt.setObject(j + 1, temp);
                        ++j;
                    }
                    pstmt.addBatch();
                    if ((i + 1) % 500 == 0) {
                        pstmt.executeBatch();
                    }
                    ++i;
                }
                pstmt.executeBatch();
            }
            catch (Exception e) {
                _logger.error((Object)("\u63d2\u5165\u6570\u636e\u65f6\u51fa\u9519\uff0c\u4efb\u52a1\u7f16\u7801\uff1a" + task.getSjtbrw_bm()), (Throwable)e);
                throw e;
            }
        }
        catch (Throwable throwable) {
            DBUtil.close(rs, pstmt);
            throw throwable;
        }
        DBUtil.close(rs, pstmt);
    }

    public void doExecteUpdateSql(TaskData task, String sql, Connection conn) throws Exception {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String uploadDbEncode = WebConstants.JHXT_XTCS.get("2003");
        if (StringUtils.isBlank((String)uploadDbEncode)) {
            uploadDbEncode = "ISO";
        }
        try {
            try {
                _logger.info((Object)("\u6267\u884c\u4e0b\u884c\u5220\u9664\u6570\u636e,\u4efb\u52a1\u7f16\u7801:" + task.getSjtbrw_bm() + ",sql:" + sql));
                pstmt = conn.prepareStatement(sql);
                pstmt.executeUpdate();
            }
            catch (Exception e) {
                _logger.error((Object)("\u6267\u884c\u4e0b\u884c\u5220\u9664\u6570\u636e\uff0c\u4efb\u52a1\u7f16\u7801\uff1a" + task.getSjtbrw_bm()), (Throwable)e);
                throw e;
            }
        }
        catch (Throwable throwable) {
            DBUtil.close(rs, pstmt);
            throw throwable;
        }
        DBUtil.close(rs, pstmt);
    }

    public void hxclProc(TaskData task, String ksyxsj, String jsyxsj, String yxjls, Connection conn, String cgbz, String mess) throws Exception {
        block8: {
            CallableStatement cstmt = null;
            String uploadDbEncode = WebConstants.JHXT_XTCS.get("2003");
            if (StringUtils.isBlank((String)uploadDbEncode)) {
                uploadDbEncode = "ISO";
            }
            try {
                try {
                    _logger.info((Object)("\u6267\u884c\u4efb\u52a1\u7f16\u53f7:" + task.getSjtbrw_bm() + ",HXCLPROC:" + task.getHxcl_proc() + ",\u53c2\u6570[" + task.getSjtbrw_bm() + "," + WebConstants.SMZ_RXBD_BSDT_DJXH + "," + ksyxsj + "," + jsyxsj + "," + yxjls + "," + cgbz + "," + mess + "]"));
                    String function = "";
                    function = "{call " + task.getHxcl_proc() + " }";
                    cstmt = conn.prepareCall(function);
                    int parIndex = 1;
                    cstmt.setString(parIndex++, task.getSjtbrw_bm());
                    cstmt.setString(parIndex++, WebConstants.SMZ_RXBD_BSDT_DJXH);
                    cstmt.setString(parIndex++, ksyxsj);
                    cstmt.setString(parIndex++, jsyxsj);
                    cstmt.setString(parIndex++, yxjls);
                    cstmt.setString(parIndex++, cgbz);
                    if ("ISO".equals(uploadDbEncode)) {
                        cstmt.setString(parIndex++, HstEncodeUtil.GBK2ISO(mess));
                    } else {
                        cstmt.setString(parIndex++, mess);
                    }
                    cstmt.execute();
                }
                catch (Exception e) {
                    _logger.error((Object)("\u6267\u884cHXCLPROC\u51fa\u9519\uff0csjtbrw_bm\uff1a" + task.getSjtbrw_bm() + ",proc" + task.getHxcl_proc()), (Throwable)e);
                    DBUtil.close(null, cstmt);
                    break block8;
                }
            }
            catch (Throwable throwable) {
                DBUtil.close(null, cstmt);
                throw throwable;
            }
            DBUtil.close(null, cstmt);
        }
    }
}
