package com.chinacreator.process.dao;

import org.springframework.stereotype.*;
import org.apache.log4j.*;
import java.util.*;
import com.frameworkset.common.poolman.*;
import com.chinacreator.process.bean.*;
import java.sql.*;

@Component
public class TencentBillDao
{
    private static Logger logger;
    
    static {
        TencentBillDao.logger = Logger.getLogger((Class)TencentBillDao.class);
    }
    
    public List<TencentBillOrder> pageQuery(final String cpid, final String spid, final String starttime, final String endtime, final int pageno, final int pagesize) {
        final StringBuffer sb = new StringBuffer("");
        sb.append(" SELECT");
        sb.append("    orderid,");
        sb.append("    userid,");
        sb.append("    DECODE(busitype,'0','0','1','1','2','2','3','1','4','3') busitype,");
        sb.append("    TO_CHAR(DECODE(BUSITYPE, '4',ORDERTIME,INSERTTIME),'yyyyMMddHH24miss') optime,");
        sb.append("    resultcode,");
        sb.append("    DECODE(busitype,'0','\u8ba2\u8d2d','1','\u9000\u8ba2','2','\u6536\u5230\u9884\u8ba2\u8d2d\u6d88\u606f','3','\u9000\u8ba2','4','\u9884\u8ba2\u8d2d')||DECODE(resultcode,'0','\u6210\u529f','\u5931\u8d25') resultinfo");
        sb.append(" FROM TD_PRE_ORDERID_REC ");
        sb.append(" WHERE cpid = '" + cpid + "' AND spid = '" + spid + "' AND busitype IN('0','1','2','3','4') ");
        if (starttime != null && !"".equals(starttime.trim())) {
            sb.append(" AND inserttime>=to_date('" + starttime + "','yyyyMMdd')");
        }
        sb.append(" AND inserttime <to_date('" + endtime + "','yyyyMMdd')");
        sb.append(" ORDER BY inserttime");
        final PreparedDBUtil pdb = new PreparedDBUtil();
        try {
            TencentBillDao.logger.info((Object)sb.toString());
            return (List<TencentBillOrder>)pdb.executeSelectForList("net3g", sb.toString(), (long)((pageno - 1) * pagesize), pagesize, (Class)TencentBillOrder.class);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public int counts(final String cpid, final String spid, final String starttime, final String endtime) {
        final StringBuffer sb = new StringBuffer("");
        sb.append(" SELECT count(1) counts FROM TD_PRE_ORDERID_REC ");
        sb.append(" WHERE cpid = '" + cpid + "' AND spid = '" + spid + "' AND busitype IN('0','1','2','3','4') ");
        if (starttime != null && !"".equals(starttime.trim())) {
            sb.append(" AND inserttime>=to_date('" + starttime + "','yyyyMMdd')");
        }
        sb.append(" AND inserttime <to_date('" + endtime + "','yyyyMMdd')");
        final PreparedDBUtil pdb = new PreparedDBUtil();
        try {
            TencentBillDao.logger.info((Object)sb.toString());
            final TencentBillOrder bean = (TencentBillOrder)pdb.executeSelectForObject("net3g", sb.toString(), (Class)TencentBillOrder.class);
            return bean.getCounts();
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return 0;
        }
    }
    
    public void saveBillLog(final TencentBillLog bean) {
        try {
            final String sql = "insert into TD_TENCENT_PRE_ORDER_BILL(id,billdate,billtype,cpid,spid,datacounts,filecounts,files,status,resultcode,errorinfo,inserttime,filepath,history,retry)values(#[id],#[billdate],#[billtype],#[cpid],#[spid],#[datacounts],#[filecounts],#[files],#[status],#[resultcode],#[errorinfo],sysdate,#[filepath],'1','1')";
            SQLExecutor.insertBean("net3g", sql, (Object)bean);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
        }
    }
    
    public TencentBillLog existBillLog(final String billdate, final String billtype, final String cpid, final String spid) {
        try {
            final String sql = "select * from TD_TENCENT_PRE_ORDER_BILL where cpid ='" + cpid + "' and spid = '" + spid + "' and billdate ='" + billdate + "' and billtype = '" + billtype + "' and  history='1'";
            return (TencentBillLog)SQLExecutor.queryObjectWithDBName((Class)TencentBillLog.class, "net3g", sql, new Object[0]);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public List<TencentBillChannel> existChannels() {
        try {
            final String sql = "select * from TB_TENCENT_PRE_ORDER_BILL  where status='0' ";
            return (List<TencentBillChannel>)SQLExecutor.queryListBeanWithDBName((Class)TencentBillChannel.class, "net3g", sql, (Object)null);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public TencentBillChannel existChannel(final String cpid, final String spid) {
        try {
            final String sql = "select * from TB_TENCENT_PRE_ORDER_BILL  where status='0'  and cpid ='" + cpid + "' and spid = '" + spid + "'";
            return (TencentBillChannel)SQLExecutor.queryObjectWithDBName((Class)TencentBillChannel.class, "net3g", sql, new Object[0]);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public List<TencentBillLog> existBillLogAgain3() {
        try {
            final String sql = "select * from TD_TENCENT_PRE_ORDER_BILL where retry='3' and history='1'";
            return (List<TencentBillLog>)SQLExecutor.queryListBeanWithDBName((Class)TencentBillLog.class, "net3g", sql, (Object)null);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public List<TencentBillLog> existBillLogAgain4() {
        try {
            final String sql = "select * from TD_TENCENT_PRE_ORDER_BILL where retry='4' and history='1' ";
            return (List<TencentBillLog>)SQLExecutor.queryListBeanWithDBName((Class)TencentBillLog.class, "net3g", sql, (Object)null);
        }
        catch (Exception e) {
            TencentBillDao.logger.error((Object)"\u67e5\u8be2\u6570\u636e\u5931\u8d25", (Throwable)e);
            return null;
        }
    }
    
    public void updatetBillLog(final String id) {
        final String sql = "update TD_TENCENT_PRE_ORDER_BILL set history='0',retry='0' where id='" + id + "' ";
        try {
            SQLExecutor.updateWithDBName("net3g", sql, new Object[0]);
        }
        catch (SQLException e) {
            TencentBillDao.logger.error((Object)"\u66f4\u6539\u6570\u636e\u5931\u8d25", (Throwable)e);
        }
    }
    
    public void updatetBillLogFtp(final String id, final String status, final String code, final String info) {
        final String sql = "update TD_TENCENT_PRE_ORDER_BILL set history='1',retry='0',status='" + status + "',code='" + code + "',info='" + info + "',ftptime=sysdate where id='" + id + "' ";
        try {
            SQLExecutor.updateWithDBName("net3g", sql, new Object[0]);
        }
        catch (SQLException e) {
            TencentBillDao.logger.error((Object)"\u66f4\u6539\u6570\u636e\u5931\u8d25", (Throwable)e);
        }
    }
}
