package com.chinacreator.process.dao;

import com.chinacreator.process.util.DataSource;
import com.frameworkset.common.poolman.SQLExecutor;
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 OrderKsPreDao {
    private Logger log = Logger.getLogger("ksPre");

    public void updateKsPre(String status, String resultCode, String resultMsg, String userid, String spid, String cpid) throws SQLException {
        String sql = "update td_order_ks_pre set status= ? , result_code= ? , result_info= ? ,CANCEL_SUBMIT=sysdate where status='1' and userid=? and spid=? and cpid=? ";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql, status, resultCode, resultMsg, userid, spid, cpid);
    }

    public void updateErrorKsPre(String status, String resultCode, String resultMsg, String userid, String spid, String cpid, Integer retryNub) throws SQLException {
        String sql = "update td_order_ks_pre set status= ? , result_code= ? , result_info= ? , retry_nub=retry_nub+? , RETRY_TIME=sysdate  where status='1' and userid=? and spid=? and cpid=? ";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql, status, resultCode, resultMsg, retryNub, userid, spid, cpid);
    }

    public boolean findRetryNub(String userid, String spid, String cpid) throws SQLException {
        String sql = "select retry_nub　from  td_order_ks_pre  where userid= ? and spid= ? and cpid=? and status='1'";
        return Integer.parseInt(SQLExecutor.queryFieldWithDBName(DataSource.NET3G, sql, userid, spid, cpid)) < 3;
    }

    public void updateStatus(String status, String userid, String spid, String cpid) throws SQLException {
        String sql = "update td_order_ks_pre set status= ? where status='0' and userid=? and spid=? and cpid=? ";
        SQLExecutor.updateWithDBName(DataSource.NET3G, sql, status, userid, spid, cpid);
    }

    public List<HashMap> queryKsPreList() throws SQLException {
        String sql = "select *　from td_order_ks_pre where status='0'  and sysdate>cancel_time";
        return SQLExecutor.queryListWithDBName(HashMap.class, DataSource.NET3G, sql);
    }

    public void addOrderLog(String userid, String cpid, String spid, String resultCode, String resultInfo, String channel, String status) {
        String sql = "insert into  NET3G.TL_ORDER_LOG(ID,USERID,CPID,SPID,ERRORCODE,ERRORINFO,INSERTTIME,CHANNEL,STATUS) \n" +
                "VALUES(to_char(sysdate, 'yyyymmddhh24miss') || SEQ_COMMON6.nextval,?,?,?,?,?,sysdate,?,?)";
        try {
            SQLExecutor.insertWithDBName(DataSource.NET3G, sql, userid, cpid, spid, resultCode, resultInfo, channel, status);
        } catch (SQLException e) {
            e.printStackTrace();
            log.info(userid+"===添加日志失败==="+e.getMessage());
        }
    }
}
