package com.saga.thread.common;

import java.util.Date;

import org.zillion.util.log.LogUtil;

import com.saga.util.Constant;

/**
 * 统计
 *
 * @author 李井强
 */
public class StatsThread extends Thread {
	private Date from = new Date();

	public StatsThread() {
	}

	public void run() {
		LogUtil.warn("StatsThread" + " Starting ...");
		try {
			Thread.sleep(Constant.Millisecond.MINUTE_3);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while (true) {
			try {
				LogUtil.warn("StatsThread" + " begin ...");
				Date to = new Date();
				this.statsCollect(to);
				this.statsTransfer(to);
				this.statsSend(to);
				this.statsWTDB(to);
				this.statsPointSet(to);
				this.from = to;
				LogUtil.warn("StatsThread" + " end ...");
				Thread.sleep(Constant.Millisecond.MINUTE_5);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 指令统计
	 */
	private void statsPointSet(Date to) {
		// TODO Auto-generated method stub
		LogUtil.warn("StatsThread: " + "PointSet Remain: " + Constant.pointSetList.size());
	}

	/**
	 * 入库统计
	 */
	private void statsWTDB(Date to) {
		// TODO Auto-generated method stub
		LogUtil.warn("StatsThread: " + this.from + " ~ " + to + " Report received :" + Constant.collectCount
				+ " , saved :" + Constant.collectReportCount + " , to DB :" + Constant.report2DBCount + " Remain: "
				+ Constant.CollectReportList.size() + " , DB writen :" + Constant.writeDB + " Remain: "
				+ Constant.RecordListToDB.size());
		Constant.collectCount = 0;
		Constant.collectReportCount = 0;
		Constant.report2DBCount = 0;
		Constant.writeDB = 0;
	}

	/**
	 * 报文发送统计
	 */
	private void statsSend(Date to) {
		// TODO Auto-generated method stub
		LogUtil.warn("StatsThread: " + this.from + " ~ " + to + " SEND received :" + Constant.sendRec + " sent :"
				+ Constant.sendSend);
		Constant.sendRec = 0;
		Constant.sendSend = 0;
	}

	/**
	 * 报文转发统计
	 */
	private void statsTransfer(Date to) {
		// TODO Auto-generated method stub
		LogUtil.warn("StatsThread: " + this.from + " ~ " + to + " Transfer sent :" + Constant.transferSendCount
				+ " received :" + Constant.transferRecCount + " Remain: " + Constant.TransferReportList.size());
		Constant.transferSendCount = 0;
		Constant.transferRecCount = 0;
	}

	/**
	 * 报文接收统计
	 */
	private void statsCollect(Date to) {
		// TODO Auto-generated method stub
		LogUtil.warn("StatsThread: " + this.from + " ~ " + to + " Collect received :" + Constant.collectRec + " sent :"
				+ Constant.collectSend);
		Constant.collectRec = 0;
		Constant.collectSend = 0;
	}

}
