⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cdrreportthread.java

📁 WAP PUSH后台源码,WAP PUSH后台源码
💻 JAVA
字号:
package com.sxit.wap.threads;

/**
 * <p>Title: wap II</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: sxit</p>
 * @author not attributable
 * @version 1.0
 */
//package com.sxit.wap;
import java.sql.*;
import java.io.*;
import java.text.DateFormat;
import java.util.Date;

public class CDRReportThread
    extends Thread {
  private String path = "//export//home//tssx//cdr//request//";
  private String fileName = "";
  private CDRReportDetail detail = null;
  public CDRReportThread(String path) {
    this.path = path;
    detail = new CDRReportDetail();
    start();
  }

  public Connection getConnection() {
    Connection conn = null;
    try {
      Class.forName("oracle.jdbc.driver.OracleDriver");
      conn = DriverManager.getConnection(
          "jdbc:oracle:thin:@10.0.0.67:1521:ora8", "sxitwap", "sxitwap");
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return conn;
  }

  public static void main(String[] args) {
    System.out.println("end update ............................");
    CDRReportThread thread = new CDRReportThread("//export//home//tssx//cdr//request//");
  }

  public String dateToString(Date date) {
    return new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(date);
  }

  public Date getNextTime(Date date) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(java.util.Calendar.MINUTE, 15);
    return calendar.getTime();
  }

  public Date getLasrTime(Date date) {
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(java.util.Calendar.MINUTE, -15);
    return calendar.getTime();
  }

  public void run() {
    System.out.println("============");
    Date nextTime = null; //下次要记录的时刻
    try {
      nextTime = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(
          "2004-07-02 00:00:00");
    }
    catch (Exception e) {
      e.printStackTrace();
    }

    while (true) {
      try {
        nextTime = getNextTime(nextTime); //得到下一次要记录的时刻,当前为一天
        //System.out.print("Time is:  " +dateToString(nextTime));
        System.out.println("Now:  " + dateToString(new Date()));
        if (nextTime.getTime() > System.currentTimeMillis())
          this.sleep(nextTime.getTime() - System.currentTimeMillis() +
                     1000 * 30);
      }
      catch (Exception e1) {
        //e1.printStackTrace();
      }

      makeCdrReport(path, getLasrTime(nextTime));
    }
  }

  public void makeCdrReport(String path, Date date) {
    String sqlHead = "select c.cp_name , h.sub_date , c.cp_tel , h.fee_code ,h.fee_type ,u.user_mdn ,decode(ch.channel_code,null,'',ch.channel_code) channel_code, ch.channel_name , ch.channel_url ,c.id, h.rowid ";
    String sqlHead1 =
        "select min(h.sub_date) ,max(h.sub_date),count(*),sum(h.fee_code)";
    String sql =
        "  from wap_user_sub_his h,wap_cp c,wap_user u ,wap_channel ch"
        + " where h.user_mdn = u.user_mdn"
        + " and h.channel_id = ch.id"
        + " and ch.cp_id = c.id"
        + " and u.user_mdn <>'test' "
        + " and h.is_done = 0 ";

    Connection conn = getConnection();
    Statement statement = null;
    java.io.PrintWriter out = null;

    //开始写文件,读取记录集
    try {
      statement = conn.createStatement();
      System.out.println(sqlHead+sql);
      ResultSet result1 = statement.executeQuery(sqlHead1 + sql);
      int rows = 0;
      if (result1.next()) {

        rows = result1.getInt(3);
        int count = result1.getInt(4);
        if (rows == 0)
          detail.beforGetFileHead(new java.sql.Timestamp(System.
              currentTimeMillis()),
                                  new java.sql.Timestamp(System.currentTimeMillis()),
                                  0, 0);
        else {
          java.sql.Timestamp firstDate = result1.getTimestamp(1);
          java.sql.Timestamp lastDate = result1.getTimestamp(2);
          detail.beforGetFileHead(firstDate, lastDate, rows, count);
        }
      }
      Date currTime = new Date();
      try {
        fileName = detail.getFileName(date, currTime);
        java.io.FileOutputStream file = new java.io.FileOutputStream(path +
            fileName); // new Date()
        out = new java.io.PrintWriter(new java.io.BufferedOutputStream(new java.
            io.DataOutputStream(file)));

      }
      catch (Exception e1) {
        e1.printStackTrace();
      }

      ResultSet result = statement.executeQuery(sqlHead + sql);

      out.print(detail.getFileHead(currTime));
      if (rows > 0)
        detail.doFileBody(result, out);
        //关闭文件
      try {
        out.flush();
        out.close();
      }
      catch (Exception e1) {
        e1.printStackTrace();
      }

      result1.close();
      result.close();
      if(backup(date,path+fileName,fileName)){
        System.out.println("拷贝正确");
      }else{
        System.out.println("拷贝出错");
      }
      // Ftp ftp = new Ftp("10.0.0.34",21,"sxit","sxitwap2",path+fileName,"//export//home0//sxit//cdr//request//"+fileName);
      // System.out.println(ftp.UpFile());
    }
    catch (Exception e1) {
      System.out.println(e1.getMessage());
    }
    finally {
      try {
        statement.close();
      }
      catch (Exception e2) {
        e2.printStackTrace();
      }
      try {
        conn.close();
      }
      catch (Exception e3) {
        e3.printStackTrace();
      }
    }
  }

  public static boolean backup(Date date,String sourcefile, String filename){
    String backup = "//export//home//tssx//cdr//request_bak//" + getMonth(date) + "//" + filename;
    if(copy(sourcefile,backup)){
        return true;
    }else{
        return false;
    }
  }
  public static String getMonth(Date date) {
    Timestamp time = new Timestamp(date.getTime());
    return DateUtil.getMM(time);
  }

  public static boolean copy(String filepath, String backpath) {
    try {
      File file = new File(filepath);
      File back = new File(backpath);
      FileInputStream in = new FileInputStream(file);
      FileOutputStream out = new FileOutputStream(back);
      byte[] bytes = new byte[1024];
      int c;
      while ( (c = in.read(bytes)) != -1) {
        out.write(bytes, 0, c);
      }
      in.close();
      out.close();
      return true;
    }
    catch (Exception ex) {
      ex.printStackTrace();
      return false;
    }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -