GetHaoDaOrderCodeBindService.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using Library;
  7. using System.Net;
  8. using System.IO;
  9. using MySystem.SpModels;
  10. namespace MySystem
  11. {
  12. /// <summary>
  13. /// 获取好哒ftp数据
  14. /// </summary>
  15. public class GetHaoDaOrderCodeBindService
  16. {
  17. public readonly static GetHaoDaOrderCodeBindService Instance = new GetHaoDaOrderCodeBindService();
  18. private GetHaoDaOrderCodeBindService()
  19. { }
  20. public void Start()
  21. {
  22. //每天凌晨执行获取好哒FTP昨日交易数据
  23. Thread th = new Thread(GetOrderCodeBindDataReady);
  24. th.IsBackground = true;
  25. th.Start();
  26. }
  27. /// <summary>
  28. /// 获取好哒FTP昨日交易数据
  29. /// </summary>
  30. public void GetOrderCodeBindDataReady()
  31. {
  32. while (true)
  33. {
  34. if (DateTime.Now > DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd") + " 10:05:00"))
  35. {
  36. SaveOrderCodeBindData(DateTime.Now.ToString("yyyyMMdd"));
  37. Thread.Sleep(2000);
  38. }
  39. Thread.Sleep(180000);
  40. }
  41. }
  42. public void StartListen()
  43. {
  44. //每天凌晨执行获取好哒FTP昨日交易数据
  45. Thread th2 = new Thread(ListenOrderCodeBindDataReady);
  46. th2.IsBackground = true;
  47. th2.Start();
  48. }
  49. /// <summary>
  50. /// 获取好哒FTP昨日交易数据
  51. /// </summary>
  52. public void ListenOrderCodeBindDataReady()
  53. {
  54. while (true)
  55. {
  56. string content = RedisDbconn.Instance.RPop<string>("ListenOrderCodeBindDataQueue");
  57. if (!string.IsNullOrEmpty(content))
  58. {
  59. SaveOrderCodeBindData(content);
  60. Thread.Sleep(2000);
  61. }
  62. else
  63. {
  64. Thread.Sleep(60000);
  65. }
  66. }
  67. }
  68. public void SaveOrderCodeBindData(string Date, bool IsRedis = false)
  69. {
  70. // 要下载的文件路径
  71. string filePath = "/haoda-deposit/xlh_bind_" + Date + ".csv";
  72. try
  73. {
  74. // 创建FtpWebRequest对象
  75. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(GetHaoDaFTPInfoService.Instance.ftpServerAddress + filePath);
  76. request.Method = WebRequestMethods.Ftp.DownloadFile;
  77. request.Credentials = new NetworkCredential(GetHaoDaFTPInfoService.Instance.ftpUser, GetHaoDaFTPInfoService.Instance.ftpPassword);
  78. // 使用WebResponse获取响应
  79. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  80. // 打开数据流
  81. Stream responseStream = response.GetResponseStream();
  82. StreamReader reader = new StreamReader(responseStream);
  83. // 读取数据
  84. string fileContents = reader.ReadToEnd();
  85. if (!string.IsNullOrEmpty(fileContents))
  86. {
  87. MpMainModels2.WebCMSEntities db = new MpMainModels2.WebCMSEntities();
  88. List<string> SnNos = db.HdOrderCodeTmp.Select(m => m.Sn).ToList();
  89. List<string> ChkSnNos = new List<string>();
  90. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  91. if (DataInfo.Length > 1)
  92. {
  93. int index = 0;
  94. var DataList = DataInfo[1].Split('\n');
  95. foreach (var DataListItem in DataList)
  96. {
  97. if(IsRedis)
  98. {
  99. RedisDbconn.Instance.AddList("ListenOrderCodeBindDataByOneQueue", DataListItem);
  100. }
  101. else
  102. {
  103. SaveOrderCodeBindDataOneReady(db, DataListItem);
  104. index += 1;
  105. if(index % 200 == 0 && index > 0)
  106. {
  107. index = 0;
  108. db.SaveChanges();
  109. }
  110. }
  111. }
  112. if(!IsRedis)
  113. {
  114. index = 0;
  115. db.SaveChanges();
  116. }
  117. }
  118. db.Dispose();
  119. }
  120. // 关闭响应
  121. reader.Dispose();
  122. responseStream.Dispose();
  123. response.Close();
  124. }
  125. catch (WebException ex)
  126. {
  127. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  128. }
  129. }
  130. public void StartOrderCodeBindListenBridge()
  131. {
  132. Thread th2 = new Thread(ListenOrderCodeBindDataBridgeReady);
  133. th2.IsBackground = true;
  134. th2.Start();
  135. }
  136. public void ListenOrderCodeBindDataBridgeReady()
  137. {
  138. while (true)
  139. {
  140. string content = RedisDbconn.Instance.RPop<string>("cache:ListenOrderCodeBindDataByOneQueue");
  141. if (!string.IsNullOrEmpty(content))
  142. {
  143. RedisDbconn.Instance.AddList("ListenOrderCodeBindDataByOneQueue", content);
  144. }
  145. else
  146. {
  147. Thread.Sleep(10000);
  148. }
  149. }
  150. }
  151. public void StartOrderCodeBindListenByOne()
  152. {
  153. //每天凌晨执行获取好哒FTP昨日交易数据
  154. Thread th2 = new Thread(ListenOrderCodeBindDataByOneReady);
  155. th2.IsBackground = true;
  156. th2.Start();
  157. }
  158. /// <summary>
  159. /// 获取好哒FTP昨日交易数据
  160. /// </summary>
  161. public void ListenOrderCodeBindDataByOneReady()
  162. {
  163. int index = 0;
  164. MpMainModels2.WebCMSEntities db = new MpMainModels2.WebCMSEntities();
  165. bool dbConn = true;
  166. while (true)
  167. {
  168. string content = RedisDbconn.Instance.RPop<string>("ListenOrderCodeBindDataByOneQueue");
  169. if (!string.IsNullOrEmpty(content))
  170. {
  171. if(!dbConn)
  172. {
  173. db = new MpMainModels2.WebCMSEntities();
  174. dbConn = true;
  175. }
  176. SaveOrderCodeBindDataOneReady(db, content);
  177. index += 1;
  178. if(index % 200 == 0 && index > 0)
  179. {
  180. index = 0;
  181. db.SaveChanges();
  182. }
  183. }
  184. else
  185. {
  186. index = 0;
  187. if(dbConn)
  188. {
  189. db.SaveChanges();
  190. db.Dispose();
  191. dbConn = false;
  192. }
  193. }
  194. }
  195. }
  196. public void SaveOrderCodeBindDataOneReady(MpMainModels2.WebCMSEntities db, string content)
  197. {
  198. try
  199. {
  200. var DataListInfo = content.Split(',');
  201. string sn = DataListInfo[0]; //SN号
  202. string create_time = DataListInfo[1]; //创建时间
  203. string mcht_no = DataListInfo[2]; //商户号
  204. string mcht_nm = DataListInfo[3]; //商户名称
  205. string through_date = DataListInfo[4]; //截止日期
  206. string channel_kind = DataListInfo[5]; //商户类型
  207. string opr_name = DataListInfo[6]; //操作员
  208. string opr_invite_code = DataListInfo[7]; //邀请码
  209. string sync_status = DataListInfo[8]; //同步状态
  210. MpMainModels2.HdOrderCodeTmp edit = db.HdOrderCodeTmp.FirstOrDefault(m => m.Sn == sn);
  211. if(edit == null)
  212. {
  213. function.WriteLog(DateTime.Now.ToString() + ":add:" + sn, "好哒点餐码绑定");
  214. db.HdOrderCodeTmp.Add(new MpMainModels2.HdOrderCodeTmp()
  215. {
  216. Sn = sn,
  217. CreateTime = DateTime.Parse(create_time),
  218. MchtNo = mcht_no,
  219. MchtNm = mcht_nm,
  220. ThroughDate = through_date,
  221. ChannelKind = channel_kind,
  222. OprName = opr_name,
  223. OprInviteCode = opr_invite_code,
  224. SyncStatus = sync_status,
  225. });
  226. }
  227. else
  228. {
  229. function.WriteLog(DateTime.Now.ToString() + ":update:" + sn, "好哒点餐码绑定");
  230. edit.Status = 0;
  231. edit.CreateTime = DateTime.Parse(create_time);
  232. edit.MchtNo = mcht_no;
  233. edit.MchtNm = mcht_nm;
  234. edit.ThroughDate = through_date;
  235. edit.ChannelKind = channel_kind;
  236. edit.OprName = opr_name;
  237. edit.OprInviteCode = opr_invite_code;
  238. edit.SyncStatus = sync_status;
  239. }
  240. db.SaveChanges();
  241. }
  242. catch(Exception ex)
  243. {
  244. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒点餐码绑定异常");
  245. }
  246. }
  247. }
  248. }