package com.chinacreator.process.dao;

import com.chinacreator.process.bean.WyjLogBean;
import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

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

@Component
public class WyjAsyncOrderDao {

    private static Logger logger = Logger.getLogger("wyjAsyncOrder");

    public List<HashMap> queryList()throws SQLException {
        String sql = " select id,userid,province,area,cpid,spid,to_char(inserttime,'YYYY/MM/DD HH24:MI:SS') inserttime,resultcode,resultinfo,channel,extend1 from td_wyj_order_job where inserttime <SYSDATE -1/24/6 AND resultcode = '1' order by inserttime";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public String getValue(String name) throws SQLException {
        String sql = "select value from TB_DICTIONRY_INFO where name = ?";
        return SQLExecutor.queryFieldWithDBName("net3g", sql, new Object[]{name});
    }

    public void addWYJLog(WyjLogBean wyjLogBean) throws SQLException {
        String sql = "insert into TD_WYJ_ORDER_REC (id,userid,province,area,cpid,spid,resultcode,resultinfo,channel,extend1) values " +
                "(TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS')||SEQ_COMMON6.NEXTVAL,#[userid],#[province],#[area],'wyj',#[spid],#[resultcode],#[resultinfo],#[channel],#[extend1])";
        SQLExecutor.insertBean(DataSource.NET3G, sql, wyjLogBean);
    }

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

    public List<HashMap> queryRecList()throws SQLException {
        String sql = "select id,userid,province,area,cpid,spid,to_char(ordertime,'YYYY/MM/DD HH24:MI:SS') ordertime,resultcode,resultinfo,channel,extend1 from td_wyj_order_rec where ordertime >SYSDATE -1/24/6 and ordertime < sysdate";
        List<HashMap> hashMaps = SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
        return  hashMaps;
    }

    public void updataInserttime(String id,String inserttime) throws SQLException {
        String sql = "update td_wyj_order_job set inserttime = to_date(?,'YYYY/MM/DD HH24:MI:SS') where id = ?";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql,inserttime,id);
    }

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

        return res;
    }

    public boolean queryOrderLog(String userid, String spid,String inserttime) throws SQLException {
        String sql = "select count(1) from net3g.tl_order_log where userid = ? and spid  = ? and status = '1' and channel = 'kf' \n" +
                "and inserttime between to_date(?,'YYYY/MM/DD HH24:MI:SS')-1/24/60 \n" +
                "and  to_date(?,'YYYY/MM/DD HH24:MI:SS')+1/24/60 and errorinfo like '%不订购违约金产品%'";
        return NumberUtils.toInt(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql, userid, spid,inserttime,inserttime), 0) > 0;
    }
}
