package com.chinacreator.process.dao;

import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

@Component
public class BoyuanSyncOrderDao {

    public List<HashMap> queryList() throws SQLException {
        String sql = " select id,userid,spid,to_char(ordertime,'yyyy-mm-dd hh24:mi:ss') ordertime,to_char(canceltime,'yyyy-mm-dd hh24:mi:ss') canceltime,to_char(endtime,'yyyy-mm-dd hh24:mi:ss') endtime,status,to_char(SYNCOUNT) SYNCOUNT,channel from TD_boyuan_syncOrder_WAIT where inserttime <sysdate-1/24/60/2 and  (resultcode = '1' or (resultcode <> '0000' and SYNCOUNT < 6)) order by inserttime";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public void updataQueryList(String id,String resultcode) throws SQLException {
        String sql = "update TD_boyuan_syncOrder_WAIT set resultcode = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,resultcode,id);
    }

    public int updExecTimeout(){
        int res = 0;
        String sql = "update TD_boyuan_syncOrder_WAIT set resultcode = '1' where resultcode = '2' and updatetime < sysdate-1/24/6";
        try {
            Object obj = SQLExecutor.updateWithDBName(DataSource.NET3G, sql, null);
            res = (Integer)obj;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    public List<HashMap> getInvokeUrl(String projname,String invokename) throws SQLException {
        String sql = "SELECT INVOKEURL, PARAM1, PARAM2 from TB_PROJINVOKEURL_CONF where projname = ? and invokename = ? and status = '0'";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql,projname,invokename);
        return  hashMaps;
    }

    public String getProductid(String spid) throws SQLException {
        String sql = "select productid from tb_intensive_productid_conf where spid = ?";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{spid});
    }


    public void updata(String id,String resultcode,String resultinfo,String resultdata,int syncount) throws SQLException {
        String sql = "update TD_boyuan_syncOrder_WAIT set resultcode = ?,resultinfo = ?,resultdata = ?,syncount = ?,updatetime = sysdate where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,resultcode,resultinfo,resultdata,syncount,id);
    }

    public String getChannel(String spid)throws SQLException {
        String sql = "SELECT CHANNEL FROM tb_jiyuechanl_sp_config WHERE SPID = ? AND STATUS = '0'";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{spid});
    }
}
