GetHaoDaBlueCircleBindService.cs 10 KB

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