GetHaoDaFTPInfoService.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 > 12 && DateTime.Now.Hour < 22)
  36. {
  37. // string check = function.ReadInstance("/GetFTPDepositYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  38. // if (string.IsNullOrEmpty(check))
  39. // {
  40. // function.WritePage("/GetFTPDepositYesterday/", "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 = "ftp://47.108.253.46";
  59. // FTP登录凭证
  60. string ftpUser = "hdftp";
  61. string ftpPassword = "haodatradeftp2024";
  62. // 要下载的文件路径
  63. string filePath = "/haoda-trade/test_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. PosKind = DataListInfo[7];
  101. string ProductType = "0";
  102. if(PosKind == "1") ProductType = "18";
  103. if(PosKind == "2") ProductType = "19";
  104. if(PosKind == "3") ProductType = "20";
  105. if(PosKind == "4") ProductType = "21";
  106. db.BindRecord.Add(new BindRecord()
  107. {
  108. CreateDate = DateTime.Now,
  109. UpdateTime = DateTime.Parse(ActDate), //机具绑定、解绑时间
  110. CreateTime = DateTime.Parse(ActDate), //商户操作时间
  111. MerSnNo = SnNo, //序列号
  112. MerNo = MerNo, //商户编号
  113. MerName = Name,
  114. SeoTitle = PrizeAmt,
  115. SeoKeyword = ActDate,
  116. ProductType = ProductType,
  117. Field1 = Deposit,
  118. Field2 = PrizeDate,
  119. Status = 1,
  120. });
  121. db.Merchants.Add(new Merchants()
  122. {
  123. SnNo = SnNo,
  124. CreateTime = DateTime.Now,
  125. UpdateTime = DateTime.Parse(ActDate),
  126. AgentName = Name,
  127. MerRealName = Name,
  128. MerNo = MerNo,
  129. MerName = Name,
  130. ProductType = ProductType,
  131. Status = 1,
  132. });
  133. db.SaveChanges();
  134. db.ActivateRecord.Add(new ActivateRecord()
  135. {
  136. SnNo = SnNo,
  137. CreateDate = DateTime.Now,
  138. SeoTitle = Deposit,
  139. ActivateDate = DateTime.Parse(ActDate),
  140. AgentNo = MerNo,
  141. MerRealName = Name,
  142. MerNo = MerNo,
  143. MerName = Name,
  144. ProductType = ProductType,
  145. ChannelSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  146. Status = 1,
  147. });
  148. db.SaveChanges();
  149. }
  150. db.Dispose();
  151. }
  152. // 关闭响应
  153. reader.Dispose();
  154. responseStream.Dispose();
  155. response.Close();
  156. }
  157. catch (WebException ex)
  158. {
  159. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  160. }
  161. }
  162. }
  163. }