ChargeRecord.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Text;
  6. namespace LYFZ.CloudServerData.DAL
  7. {
  8. public class ChargeRecord : BaseDataOperate<LYFZ.CloudServerData.Model.ChargeRecord>
  9. {
  10. public ChargeRecord(LYFZ.CloudServerData.Model.DBConnection dbConn)
  11. {
  12. base.DBConn = dbConn;
  13. }
  14. /// <summary>
  15. /// 结算流量
  16. /// </summary>
  17. /// <param name="_DateYear"></param>
  18. /// <param name="_DateMonth"></param>
  19. /// <param name="msg"></param>
  20. /// <returns></returns>
  21. public bool ChargeTraffic(int _DateYear, int _DateMonth, ref string msg)
  22. {
  23. try
  24. {
  25. if (_DateYear > DateTime.Now.Year || (_DateMonth >= DateTime.Now.Month && _DateMonth!=12))
  26. {
  27. msg = "只能执行当月以前的流量结算,不能结当月或未来月份的流量";
  28. return false;
  29. }
  30. else
  31. {
  32. LYFZ.CloudServerData.DAL.CloudServerCollection serverDal = new CloudServerCollection(this.DBConn);
  33. List<LYFZ.CloudServerData.Model.CloudServerCollection> serverModelList = serverDal.GetAllModelList();
  34. StringBuilder sqlString = new StringBuilder();
  35. foreach (LYFZ.CloudServerData.Model.CloudServerCollection serverModel in serverModelList)
  36. {
  37. LYFZ.CloudServerData.Model.DBConnection myDbconn = new Model.DBConnection();
  38. myDbconn.DataBaseServer = serverModel.DataBaseServerIP;
  39. myDbconn.DataBaseName = "NetworkTrafficStatistics";
  40. myDbconn.DataBasePort = serverModel.DataBasePort;
  41. myDbconn.DataBaseUser =serverModel.DataBaseUser;
  42. myDbconn.DataBasePass = serverModel.DataBasePass;
  43. StringBuilder trafficSql = new StringBuilder();
  44. trafficSql.Append(String.Format(" select EnterpriseID,sum(FlowDataLength) as FlowDataLength,sum(UserFlowDataLength) as UserFlowDataLength,DateYear,DateMonth "));
  45. trafficSql.Append(String.Format(" from dbo.[tb_NetworkTrafficRecord] "));
  46. trafficSql.Append(String.Format(" where DateYear={0} and DateMonth={1} ", _DateYear, _DateMonth));
  47. trafficSql.Append(String.Format(" group by EnterpriseID,DateYear,DateMonth "));
  48. System.Data.DataTable tb = LYFZ.Helper.SQLHelper.Query(trafficSql.ToString(), myDbconn.DBConnectionString).Tables[0];
  49. sqlString.Clear();
  50. foreach (System.Data.DataRow row in tb.Rows)
  51. {
  52. string EnterpriseID = row["EnterpriseID"].ToString();
  53. string Identifier = String.Format("{0}_{1}_{2}_{3}", serverModel.ID, EnterpriseID, row["DateYear"].ToString(), row["DateMonth"].ToString());
  54. sqlString.AppendFormat("if not EXISTS (select [Identifier] from [dbo].[tb_ChargeRecord] where [Identifier]='{0}')\r\n", Identifier);
  55. sqlString.AppendFormat("begin \r\n");
  56. sqlString.AppendFormat("if EXISTS (select [EnterpriseID] from [dbo].[tb_Enterprise] where [EnterpriseID]='{0}')\r\n", EnterpriseID);
  57. sqlString.AppendFormat("begin \r\n");
  58. sqlString.AppendFormat("INSERT INTO [dbo].[tb_ChargeRecord] ([ID],[Identifier],[ServerID],[EID],[FlowDataLength],[UserFlowDataLength],[status],[ChargeDateTime],[StatisticsTime]) VALUES({0},'{1}',{2},'{3}',{4},{5},{6},'{7}-{8}',{9}) \r\n"
  59. , LYFZ.WinAPI.CustomPublicMethod.GenerateId(), Identifier, serverModel.ID, EnterpriseID, row["FlowDataLength"].ToString(), row["UserFlowDataLength"].ToString(), 1, row["DateYear"].ToString(), row["DateMonth"].ToString(), DateTime.Now.ToJavaScriptTimeStamp());
  60. sqlString.AppendFormat(" update tb_Enterprise set [Balance]=[Balance]-({0}/1024.0) where [EnterpriseID]='{1}' \r\n", row["UserFlowDataLength"].ToString(), EnterpriseID);
  61. sqlString.AppendFormat("end \r\n");
  62. sqlString.AppendFormat("end \r\n");
  63. // sqlList.AppendFormat("GO \r\n");
  64. }
  65. if (sqlString.ToString().Trim().Length > 0)
  66. {
  67. LYFZ.Helper.SQLHelper.ExecuteSql(sqlString.ToString(), this.DBConn.DBConnectionString);
  68. }
  69. sqlString.Clear();
  70. }
  71. return true;
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. msg = "流量结算出错:"+ex.Message;
  77. return false;
  78. }
  79. }
  80. public DataTable SearchRechargeRecordData(string searchText, string searService, string mouthText, int pageIndx, int pageSize)
  81. {
  82. string sql = @" SELECT top " + (pageIndx * pageSize) + @" [tb_ChargeRecord].*,EnterpriseName,CloudServerName
  83. FROM [tb_ChargeRecord] inner join tb_Enterprise on [tb_ChargeRecord].EID = tb_Enterprise.EnterpriseID
  84. inner join tb_CloudServerCollection on tb_CloudServerCollection.ID = ServerID
  85. ";
  86. string where = "";
  87. if (!string.IsNullOrEmpty(searchText))
  88. {
  89. where += "( EID like '%" + searchText + "%' or EnterpriseName like '%" + searchText + "%' )";
  90. }
  91. if (!string.IsNullOrEmpty(searService))
  92. {
  93. if (!string.IsNullOrEmpty(where))
  94. {
  95. where += " and ";
  96. }
  97. where += " and [ServerID] = '" + searService + "' ";
  98. }
  99. if (!string.IsNullOrEmpty(mouthText))
  100. {
  101. if (!string.IsNullOrEmpty(where))
  102. {
  103. where += " and ";
  104. }
  105. where += " and [ChargeDateTime] = '" + mouthText + "' ";
  106. }
  107. if (!string.IsNullOrEmpty(where))
  108. {
  109. sql = sql + " where " + where;
  110. }
  111. DataTable dt = LYFZ.Helper.SQLHelper.Query(sql, base.DBConn.DBConnectionString).Tables[0];
  112. DataTable jsonDt = dt.AsEnumerable().Skip((pageIndx - 1) * pageSize).Take(pageSize).CopyToDataTable();
  113. return jsonDt;
  114. }
  115. }
  116. }