DABusinessDataJob.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using log4net;
  2. using Quartz;
  3. using RabbitMQ.Client;
  4. using RDIFramework.Utilities;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Configuration;
  8. using System.Data;
  9. using System.Text;
  10. using TimedUpload.utils;
  11. namespace TimedUpload.QuartzJobs
  12. {
  13. [DisallowConcurrentExecution]
  14. public class DABusinessDataJob : IJob
  15. {
  16. private readonly ILog log = LogManager.GetLogger(typeof(DABusinessDataJob));
  17. private string manufacturerCode = Constants.ManufacturerCode;
  18. private string bmId = Constants.BmId;
  19. private string readDate = Constants.UserMeterDate;
  20. private string userMeterReadDate = Constants.UserMeterReadDate;
  21. public void Execute(IJobExecutionContext context)
  22. {
  23. string[] uploadUrls = Constants.UploadUrl.Split('|');
  24. Dictionary<string, IConnection> connections = new Dictionary<string, IConnection>();
  25. Dictionary<string, IModel> channels = new Dictionary<string, IModel>();
  26. Dictionary<string, IBasicProperties> properties = new Dictionary<string, IBasicProperties>();
  27. foreach (string uploadUrl in uploadUrls)
  28. {
  29. ConnectionFactory factory = new ConnectionFactory();
  30. factory.HostName = uploadUrl;//主机名,Rabbit会拿这个IP生成一个endpoint,这个很熟悉吧,就是socket绑定的那个终结点。
  31. factory.UserName = Constants.UploadUserName;//默认用户名,用户可以在服务端自定义创建,有相关命令行
  32. factory.Password = Constants.UploadPassword;//默认密码
  33. factory.AutomaticRecoveryEnabled = true; // 链接断开会自动重连
  34. IConnection connection = factory.CreateConnection();
  35. IModel channel = connection.CreateModel();
  36. channel.QueueDeclare("zone.userMeter", true, false, false, null);//创建一个名称为kibaqueue的消息队列
  37. channel.QueueDeclare("zone.userMeterHis", true, false, false, null);//创建一个名称为kibaqueue的消息队列
  38. IBasicProperties property = channel.CreateBasicProperties();
  39. property.ContentType = "text/plain";
  40. property.DeliveryMode = 2; //持久化
  41. connections.Add(uploadUrl, connection);
  42. properties.Add(uploadUrl, property);
  43. channels.Add(uploadUrl, channel);
  44. }
  45. if (channels.Count > 0)
  46. {
  47. SendZoneMeterUser(channels, properties);
  48. SendZoneMeterUserHis(channels, properties);
  49. }
  50. foreach (KeyValuePair<string, IConnection> item in connections)
  51. {
  52. IConnection connection = item.Value;
  53. connection.Close();
  54. }
  55. }
  56. /// <summary>
  57. /// 户表添加
  58. /// </summary>
  59. /// <param name="channel"></param>
  60. private void SendZoneMeterUser(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  61. {
  62. string meterId = Constants.UserMeterId;
  63. log.Info("营收户表基础数据同步任务开始执行.................\r\n");
  64. while (true)
  65. {
  66. string sql = "SELECT top 100 b.CM_ID,c.RouteCode,RouteName,a.CustomerCode,a.CustomerName,b.ElecAddress,b.DetailedAddress MeterAddress,a.DetailedAddress CustomerAddress";
  67. sql += " FROM BCS_Customer a,BCS_CustomerMeter b, BCS_MeterReadingRoute c WHERE a.Cus_ID = b.Cus_ID and b.Mrr_ID = c.MRR_ID ";
  68. sql += " AND b.CM_ID > " + meterId + " ORDER BY CM_ID";
  69. // log.Info(sql);
  70. DataTable dt = dbHelper.Fill(sql);
  71. // log.Info("营业收费户表 | " + dt.Rows.Count);
  72. if (dt == null || dt.Rows.Count == 0)
  73. {
  74. break;
  75. }
  76. StringBuilder message = new StringBuilder();
  77. for (int i = 0; i < dt.Rows.Count; i++)
  78. {
  79. message.Clear();
  80. try
  81. {
  82. DataRow dr = dt.Rows[i];
  83. if (i == dt.Rows.Count - 1)
  84. {
  85. meterId = Convert.ToString(dr["CM_ID"]);
  86. }
  87. string meterLineNo = Convert.ToString(dr["RouteCode"]);
  88. string meterLineName = Convert.ToString(dr["RouteName"]);
  89. string clientNo = Convert.ToString(dr["CustomerCode"]);
  90. string clientName = Convert.ToString(dr["CustomerName"]);
  91. string meterCode = Convert.ToString(dr["CM_ID"]);
  92. string meterAddress = string.IsNullOrEmpty(Convert.ToString(dr["CustomerAddress"])) ? "未知" : Convert.ToString(dr["CustomerAddress"]);
  93. message.Append("{");
  94. message.Append("\"meterLineNo\": \"").Append(meterLineNo).Append("\",");
  95. message.Append("\"meterLineName\": \"").Append(meterLineName).Append("\",");
  96. message.Append("\"clientNo\": \"").Append(clientNo).Append("\",");
  97. message.Append("\"clientName\": \"").Append(clientName).Append("\",");
  98. message.Append("\"meterCode\": \"").Append(meterCode).Append("\",");
  99. message.Append("\"meterAddress\": \"").Append(meterAddress).Append("\",");
  100. message.Append("\"fromWhere\": \"").Append(manufacturerCode).Append("\"");
  101. message.Append("}");
  102. foreach (KeyValuePair<string, IModel> item in channels)
  103. {
  104. string key = item.Key;
  105. IModel channel = item.Value;
  106. IBasicProperties property = properties[key];
  107. channel.BasicPublish("zone.userMeter", "", property, Encoding.UTF8.GetBytes(message.ToString())); //生产消息
  108. }
  109. }
  110. catch (Exception ex)
  111. {
  112. log.Info("营收户表基础数据推送失败:" + message.ToString() + "\r\n");
  113. log.Error(ex.Message + "===========" + ex.StackTrace + "\r\n");
  114. }
  115. }
  116. }
  117. UpdateAppConfig("UserMeterId", meterId);
  118. log.Info("营收户表基础数据同步任务结束执行.................\r\n");
  119. }
  120. /// <summary>
  121. /// 户表历史数据
  122. /// </summary>
  123. /// <param name="channel"></param>
  124. private void SendZoneMeterUserHis(Dictionary<string, IModel> channels, Dictionary<string, IBasicProperties> properties)
  125. {
  126. string userMeterReadId = Constants.UserMeterReadId;
  127. //log.Info("营收户表抄表数据同步任务开始执行.................\r\n");
  128. while (true)
  129. {
  130. string sql = "SELECT top 100 UsedWater_ID,CM_ID,ThisMeterNumber,ThisMeterDt,BetweenMeteNumber,b.CreateDT FROM BCS_UsedWater b";
  131. sql += " WHERE b.BM_ID >= " + bmId + " AND b.UsedWater_ID > " + userMeterReadId + " ORDER BY b.BM_ID,b.UsedWater_ID";
  132. // log.Info(sql);
  133. DataTable dt = dbHelper.Fill(sql);
  134. if (dt == null || dt.Rows.Count == 0)
  135. {
  136. break;
  137. }
  138. StringBuilder message = new StringBuilder();
  139. for (int i = 0; i < dt.Rows.Count; i++)
  140. {
  141. message.Clear();
  142. DataRow dr = dt.Rows[i];
  143. if (i == dt.Rows.Count - 1)
  144. {
  145. userMeterReadId = Convert.ToString(dr["UsedWater_ID"]);
  146. }
  147. string meterCode = Convert.ToString(dr["CM_ID"]);
  148. string getDateTime = Convert.ToDateTime(dr["ThisMeterDt"]).ToString("yyyy-MM-dd HH:mm:ss");
  149. string currReadingValue = Convert.ToString(dr["ThisMeterNumber"]);
  150. string waterUsed = Convert.ToString(dr["BetweenMeteNumber"]);
  151. try
  152. {
  153. message.Append("{");
  154. message.Append("\"meterCode\": \"").Append(meterCode).Append("\",");
  155. message.Append("\"fromWhere\": \"").Append(manufacturerCode).Append("\",");
  156. message.Append("\"saleWater\": \"").Append(waterUsed).Append("\",");
  157. message.Append("\"getDateTime\": \"").Append(getDateTime).Append("\",");
  158. message.Append("\"currReadingValue\": ").Append(currReadingValue);
  159. message.Append("}");
  160. foreach (KeyValuePair<string, IModel> item in channels)
  161. {
  162. string key = item.Key;
  163. IModel channel = item.Value;
  164. IBasicProperties property = properties[key];
  165. channel.BasicPublish("zone.userMeterHis", "", property, Encoding.UTF8.GetBytes(message.ToString())); //生产消息
  166. }
  167. }
  168. catch (Exception ex)
  169. {
  170. log.Info("营收户表抄表数据推送失败:" + message.ToString() + "\r\n");
  171. log.Error(ex.Message + "===========" + ex.StackTrace + "\r\n");
  172. }
  173. }
  174. }
  175. UpdateAppConfig("UserMeterReadId", userMeterReadId);
  176. log.Info("营收户表抄表数据同步任务结束执行.................\r\n");
  177. }
  178. /// <summary>
  179. /// 更新配置文件中的值
  180. /// </summary>
  181. /// <param name="key">键</param>
  182. /// <param name="value">值</param>
  183. private void UpdateAppConfig(String key, String value)
  184. {
  185. var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  186. cfg.AppSettings.Settings[key].Value = value;
  187. cfg.Save();
  188. ConfigurationManager.RefreshSection("appSettings");
  189. }
  190. static IDbProvider dbHelper
  191. {
  192. get
  193. {
  194. var DbDefine = DbFactoryProvider.GetProvider(CurrentDbType.SqlServer, Constants.ChargeDB);
  195. return DbDefine;
  196. }
  197. }
  198. }
  199. }