GetHaoDaFTPInfoService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 LitJson;
  8. using System.Net;
  9. using System.IO;
  10. using MySystem.SpModels;
  11. namespace MySystem
  12. {
  13. /// <summary>
  14. /// 获取好哒ftp数据
  15. /// </summary>
  16. public class GetHaoDaFTPInfoService
  17. {
  18. public readonly static GetHaoDaFTPInfoService Instance = new GetHaoDaFTPInfoService();
  19. private GetHaoDaFTPInfoService()
  20. { }
  21. public void Start()
  22. {
  23. //每天凌晨执行获取好哒FTP昨日交易数据
  24. Thread th2 = new Thread(GetFTPDataInfoYesterdayReady);
  25. th2.IsBackground = true;
  26. th2.Start();
  27. }
  28. /// <summary>
  29. /// 获取好哒FTP昨日交易数据
  30. /// </summary>
  31. public void GetFTPDataInfoYesterdayReady()
  32. {
  33. while (true)
  34. {
  35. if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 22)
  36. {
  37. string check = function.ReadInstance("/GetFTPDataInfoYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  38. if (string.IsNullOrEmpty(check))
  39. {
  40. function.WritePage("/GetFTPDataInfoYesterday/", "check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString());
  41. GetFTPDataInfoYesterday();
  42. Thread.Sleep(600000);
  43. }
  44. }
  45. else
  46. {
  47. Thread.Sleep(1800000);
  48. }
  49. }
  50. }
  51. public void GetFTPDataInfoYesterday()
  52. {
  53. // 47.108.253.46
  54. // 用户名:hdftp
  55. // 密:haodatradeftp2024
  56. // 目录:/haoda-trade
  57. // FTP服务器的地址
  58. string ftpServerAddress = "47.108.253.46";
  59. // FTP登录凭证
  60. string ftpUser = "hdftp";
  61. string ftpPassword = "haodatradeftp2024";
  62. // 要下载的文件路径
  63. string filePath = "/haoda-trade/deposit_" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".csv";
  64. try
  65. {
  66. // 创建FtpWebRequest对象
  67. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  68. request.Method = WebRequestMethods.Ftp.DownloadFile;
  69. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  70. // 使用WebResponse获取响应
  71. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  72. // 打开数据流
  73. Stream responseStream = response.GetResponseStream();
  74. StreamReader reader = new StreamReader(responseStream);
  75. // 读取数据
  76. string fileContents = reader.ReadToEnd();
  77. if (!string.IsNullOrEmpty(fileContents))
  78. {
  79. WebCMSEntities db = new WebCMSEntities();
  80. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  81. var DataList = DataInfo[1].Split('\n');
  82. foreach (var DataListItem in DataList)
  83. {
  84. var DataListInfo = DataListItem.Split(',');
  85. string SnNo = ""; //sn
  86. string MerNo = ""; //商户编号
  87. string ActDate = ""; //激活时间
  88. string Deposit = ""; //押金金额
  89. string PrizeAmt = ""; //奖励金额
  90. string PrizeDate = ""; //奖励发放时间
  91. string Name = ""; //发放人姓名
  92. string PosKind = ""; //机具型号
  93. SnNo = DataListInfo[0];
  94. MerNo = DataListInfo[1];
  95. ActDate = DataListInfo[2];
  96. Deposit = DataListInfo[3];
  97. PrizeAmt = DataListInfo[4];
  98. PrizeDate = DataListInfo[5];
  99. Name = DataListInfo[6];
  100. string ProductType = "0";
  101. if(PosKind == "1") ProductType = "18";
  102. if(PosKind == "2") ProductType = "19";
  103. if(PosKind == "3") ProductType = "20";
  104. if(PosKind == "4") ProductType = "21";
  105. db.BindRecord.Add(new BindRecord()
  106. {
  107. CreateDate = DateTime.Now,
  108. UpdateTime = DateTime.Parse(ActDate), //机具绑定、解绑时间
  109. CreateTime = DateTime.Parse(ActDate), //商户操作时间
  110. MerSnNo = SnNo, //序列号
  111. MerNo = MerNo, //商户编号
  112. MerName = Name,
  113. SeoTitle = PrizeAmt,
  114. SeoKeyword = ActDate,
  115. ProductType = ProductType,
  116. Field1 = Deposit,
  117. Field2 = PrizeDate,
  118. Status = 1,
  119. });
  120. db.Merchants.Add(new Merchants()
  121. {
  122. SnNo = SnNo,
  123. CreateTime = DateTime.Now,
  124. UpdateTime = DateTime.Parse(ActDate),
  125. AgentName = Name,
  126. MerRealName = Name,
  127. MerNo = MerNo,
  128. MerName = Name,
  129. ProductType = ProductType,
  130. Status = 1,
  131. });
  132. db.ActivateRecord.Add(new ActivateRecord()
  133. {
  134. SnNo = SnNo,
  135. CreateDate = DateTime.Now,
  136. SeoTitle = Deposit,
  137. ActivateDate = DateTime.Parse(ActDate),
  138. AgentNo = MerNo,
  139. MerRealName = Name,
  140. MerNo = MerNo,
  141. MerName = Name,
  142. ProductType = ProductType,
  143. ChannelSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  144. Status = 1,
  145. });
  146. }
  147. db.SaveChanges();
  148. db.Dispose();
  149. }
  150. // 关闭响应
  151. reader.Dispose();
  152. responseStream.Dispose();
  153. response.Close();
  154. }
  155. catch (WebException ex)
  156. {
  157. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  158. }
  159. }
  160. }
  161. }