using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LYFZ.CloudServerData.DAL
{
public class NetworkTrafficRecord
{
public NetworkTrafficRecord()
{
}
///
/// 流量数据传输类型
///
public enum FlowDataType
{
///
/// 任何类型
///
Any,
///
/// 云服务器管理流量
///
CloudManagement,
///
/// 云服务器登录流量
///
CloudLogIn,
///
/// 普通消息传输
///
Message,
///
/// SQL数据传输
///
SQLData,
///
/// 文件传输
///
File,
///
/// WEBAPP接口数据传输
///
WEBAPP,
}
///
/// 插入流量统计记录
///
/// 数据库连接串对象
/// 企业ID
/// 流量类型 0 为输出流量(客户端下载) 1 为输入流量(客户端上传)
/// 数据类型
///
/// 流量倍数
///
public static bool AddNetworkTrafficRecord(string dbConnectionString, string _EnterpriseID, int _FlowType, FlowDataType _DataType, long _DataLength, int multiple)
{
try
{
if (multiple <= 0)
{
multiple = 1;
}
double myDataLength = _DataLength / 1024.00 / 1024.00;
double UserFlowDataLength=myDataLength*multiple;
string sql = "INSERT INTO [dbo].[tb_NetworkTrafficRecord]([ID],[EnterpriseID],[FlowType],[DataType],[FlowDataLength],[DateTime],[UserFlowDataLength])"
+ "VALUES(" + LYFZ.WinAPI.CustomPublicMethod.GenerateId() + ",'" + _EnterpriseID + "'," + _FlowType + ",'" + _DataType.ToString() + "'," + myDataLength + ",getdate()," + UserFlowDataLength + ")";
LYFZ.Helper.SQLHelper.ExecuteSql(sql, dbConnectionString);
return true;
}
catch
{
return false;
}
}
///
/// 插入流量统计记录
///
/// 数据库连接串对象
/// 企业ID
/// 流量类型 0 为输出流量(客户端下载) 1 为输入流量(客户端上传)
/// 数据类型
///
/// 流量倍数
///
public static bool AddNetworkTrafficRecordThreadPool(LYFZ.CloudServerData.Model.DBConnection dbConn, string _EnterpriseID, int _FlowType, FlowDataType _DataType, long _DataLength, int multiple)
{
try
{
if (String.IsNullOrEmpty(_EnterpriseID))
{
_EnterpriseID = "LYFZ_Null";
}
var parameter = new {
EnterpriseID = _EnterpriseID,
FlowType=_FlowType,
DataType=(int)_DataType,
DataLength=_DataLength,
dbconn = dbConn.GetDBConnectionString(),
multiple = multiple
};
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(WaitThreadFunc), Newtonsoft.Json.JsonConvert.SerializeObject(parameter));
return true;
}
catch {
return false;
}
}
public static void WaitThreadFunc(object obj)
{
dynamic parameter = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString());
AddNetworkTrafficRecord(parameter.dbconn.ToString(), parameter.EnterpriseID.ToString(), Convert.ToInt32(parameter.FlowType), (FlowDataType)Convert.ToInt32(parameter.DataType), Convert.ToInt64(parameter.DataLength), Convert.ToInt32(parameter.multiple));
}
///
/// 合并统计流量记录
///
///
///
public static int SumNetworkTrafficRecord(LYFZ.CloudServerData.Model.DBConnection dbConn)
{
System.Data.SqlClient.SqlParameter[] parameters = {
new System.Data.SqlClient.SqlParameter("@ReturnI", System.Data.SqlDbType.Int)
};
parameters[0].Direction = System.Data.ParameterDirection.Output;
LYFZ.Helper.SQLHelper.RunProcedure("PROCE_SumNetworkTrafficRecord", parameters, "SumNetworkTrafficRecord_ds", 240, dbConn.GetDBConnectionString());
return Convert.ToInt32(parameters[0].Value);
}
///
/// 获取统计数据
///
/// 数据库连接
/// 查询年 开始
/// 查询月 开始
/// 查询年 结束
/// 查询月 结束
/// 企业ID 可以为空
///
public static System.Data.DataTable GetSumNetworkTrafficRecord(LYFZ.CloudServerData.Model.DBConnection dbConn,int yaerStart, int monthStart, int yaerEnd, int monthEnd, string EnterpriseID)
{
string sql = String.Format("select flowtype,datatype,sum(FlowDataLength) as FlowDataLength,sum(UserFlowDataLength) as UserFlowDataLength,DateYear,DateMonth from [dbo].[tb_NetworkTrafficRecord] where DateYear>={0} and DateYear<={1} and DateMonth>={2} and DateMonth<={3}", yaerStart, yaerEnd, monthStart, monthEnd);
if (!String.IsNullOrEmpty(EnterpriseID))
{
sql += String.Format(" and [EnterpriseID]='{0}' group by EnterpriseID,flowtype,datatype,DateYear,DateMonth",EnterpriseID);
}
else {
sql += String.Format(" group by flowtype,datatype,DateYear,DateMonth");
}
return LYFZ.Helper.SQLHelper.Query(sql,dbConn.GetDBConnectionString()).Tables[0];
}
}
}