GetHaoDaFTPInfoService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. // 47.108.253.46
  22. // 用户名:hdftp
  23. // 密:haodatradeftp2024
  24. // 目录:/haoda-trade
  25. public string ftpServerAddress = "ftp://47.108.253.46";
  26. public string ftpUser = "hdftp";
  27. public string ftpPassword = "haodatradeftp2024";
  28. public void Start()
  29. {
  30. //每天凌晨执行获取好哒FTP昨日交易数据
  31. Thread th2 = new Thread(GetDepositDataReady);
  32. th2.IsBackground = true;
  33. th2.Start();
  34. }
  35. /// <summary>
  36. /// 获取好哒FTP昨日交易数据
  37. /// </summary>
  38. public void GetDepositDataReady()
  39. {
  40. while (true)
  41. {
  42. // if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 22)
  43. // {
  44. // string check = function.ReadInstance("/GetFTPDepositYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  45. // if (string.IsNullOrEmpty(check))
  46. // {
  47. // function.WritePage("/GetFTPDepositYesterday/", "check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString());
  48. GetDepositData();
  49. Thread.Sleep(600000);
  50. // }
  51. // }
  52. // else
  53. // {
  54. // Thread.Sleep(1800000);
  55. // }
  56. }
  57. }
  58. public void GetDepositData()
  59. {
  60. // 要下载的文件路径
  61. string filePath = "/haoda-deposit/deposit_" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".csv";
  62. try
  63. {
  64. // 创建FtpWebRequest对象
  65. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  66. request.Method = WebRequestMethods.Ftp.DownloadFile;
  67. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  68. // 使用WebResponse获取响应
  69. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  70. // 打开数据流
  71. Stream responseStream = response.GetResponseStream();
  72. StreamReader reader = new StreamReader(responseStream);
  73. // 读取数据
  74. string fileContents = reader.ReadToEnd();
  75. if (!string.IsNullOrEmpty(fileContents))
  76. {
  77. WebCMSEntities db = new WebCMSEntities();
  78. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  79. var DataList = DataInfo[1].Split('\n');
  80. foreach (var DataListItem in DataList)
  81. {
  82. var DataListInfo = DataListItem.Split(',');
  83. string SnNo = DataListInfo[0]; //sn
  84. string MerNo = DataListInfo[1]; //商户编号
  85. string ActDate = DataListInfo[2]; //激活时间
  86. string Deposit = DataListInfo[3]; //押金金额
  87. string PrizeAmt = DataListInfo[4]; //奖励金额
  88. string PrizeDate = DataListInfo[5]; //奖励发放时间
  89. string Name = DataListInfo[6]; //发放人姓名
  90. string PosKind = DataListInfo[7]; //机具型号
  91. string ActDateString = ActDate.Substring(0, 4) + "-" + ActDate.Substring(4, 2) + "-" + ActDate.Substring(6, 2);
  92. string PrizeDateString = PrizeDate.Substring(0, 4) + "-" + PrizeDate.Substring(4, 2) + "-" + PrizeDate.Substring(6, 2);
  93. string ProductType = "0";
  94. if(PosKind == "1") ProductType = "18";
  95. if(PosKind == "4G收款王M820") ProductType = "19";
  96. if(PosKind == "4G收款王M826") ProductType = "20";
  97. if(PosKind == "4") ProductType = "21";
  98. db.BindRecord.Add(new BindRecord()
  99. {
  100. CreateDate = DateTime.Now,
  101. UpdateTime = DateTime.Parse(ActDateString), //机具绑定、解绑时间
  102. CreateTime = DateTime.Parse(ActDateString), //商户操作时间
  103. MerSnNo = SnNo, //序列号
  104. MerNo = MerNo, //商户编号
  105. MerName = Name,
  106. SeoTitle = PrizeAmt,
  107. SeoKeyword = ActDateString,
  108. ProductType = ProductType,
  109. Field1 = Deposit,
  110. Field2 = PrizeDate,
  111. Status = 1,
  112. });
  113. db.Merchants.Add(new Merchants()
  114. {
  115. SnNo = SnNo,
  116. CreateTime = DateTime.Now,
  117. UpdateTime = DateTime.Parse(ActDateString),
  118. AgentName = Name,
  119. MerRealName = Name,
  120. MerNo = MerNo,
  121. MerName = Name,
  122. ProductType = ProductType,
  123. Status = 1,
  124. });
  125. db.SaveChanges();
  126. db.ActivateRecord.Add(new ActivateRecord()
  127. {
  128. SnNo = SnNo,
  129. CreateDate = DateTime.Now,
  130. SeoTitle = Deposit,
  131. ActivateDate = DateTime.Parse(ActDateString),
  132. AgentNo = MerNo,
  133. MerRealName = Name,
  134. MerNo = MerNo,
  135. MerName = Name,
  136. ProductType = ProductType,
  137. ChannelSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  138. Status = 1,
  139. });
  140. db.SaveChanges();
  141. }
  142. db.Dispose();
  143. }
  144. // 关闭响应
  145. reader.Dispose();
  146. responseStream.Dispose();
  147. response.Close();
  148. }
  149. catch (WebException ex)
  150. {
  151. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  152. }
  153. }
  154. public void StartTrade()
  155. {
  156. //每天凌晨执行获取好哒FTP昨日交易数据
  157. Thread th2 = new Thread(GetTradeDataReady);
  158. th2.IsBackground = true;
  159. th2.Start();
  160. }
  161. /// <summary>
  162. /// 获取好哒FTP昨日交易数据
  163. /// </summary>
  164. public void GetTradeDataReady()
  165. {
  166. while (true)
  167. {
  168. // if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 22)
  169. // {
  170. // string check = function.ReadInstance("/GetFTPTradeYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  171. // if (string.IsNullOrEmpty(check))
  172. // {
  173. // function.WritePage("/GetFTPTradeYesterday/", "check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString());
  174. GetTradeData();
  175. Thread.Sleep(600000);
  176. // }
  177. // }
  178. // else
  179. // {
  180. // Thread.Sleep(1800000);
  181. // }
  182. }
  183. }
  184. public void GetTradeData()
  185. {
  186. // 要下载的文件路径
  187. string filePath = "/haoda-trade/" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".csv";
  188. try
  189. {
  190. // 创建FtpWebRequest对象
  191. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  192. request.Method = WebRequestMethods.Ftp.DownloadFile;
  193. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  194. // 使用WebResponse获取响应
  195. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  196. // 打开数据流
  197. Stream responseStream = response.GetResponseStream();
  198. StreamReader reader = new StreamReader(responseStream);
  199. // 读取数据
  200. string fileContents = reader.ReadToEnd();
  201. if (!string.IsNullOrEmpty(fileContents))
  202. {
  203. WebCMSEntities db = new WebCMSEntities();
  204. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  205. var DataList = DataInfo[1].Split('\n');
  206. foreach (var DataListItem in DataList)
  207. {
  208. var DataListInfo = DataListItem.Split(',');
  209. string MerNo = DataListInfo[0]; // 商户号
  210. string SnNo = DataListInfo[1]; // 设备号
  211. string OrderNo = DataListInfo[2]; // 订单号
  212. string PayMode = DataListInfo[3]; // 支付方式(微信 支付宝)
  213. string TradeType = DataListInfo[4]; // 交易类型
  214. string TradeAmount = DataListInfo[5]; // 交易金额
  215. string TradeFee = DataListInfo[6]; // 交易手续费
  216. string TradeDate = DataListInfo[7]; // 交易时间
  217. string TradeCycle = DataListInfo[8]; // 结算周期
  218. string PosKind = DataListInfo[9];
  219. string TradeDateString = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " " + TradeDate.Substring(8, 2) + ":" + TradeDate.Substring(10, 2) + ":" + TradeDate.Substring(12, 2);
  220. string ProductType = "0";
  221. if(PosKind == "1") ProductType = "18";
  222. if(PosKind == "4G收款王M820") ProductType = "19";
  223. if(PosKind == "4G收款王M826") ProductType = "20";
  224. if(PosKind == "4") ProductType = "21";
  225. db.TradeRecord.Add(new TradeRecord()
  226. {
  227. CreateDate = DateTime.Now,
  228. UpdateDate = DateTime.Parse(TradeDateString),
  229. TradeSnNo = SnNo, //序列号
  230. MerNo = MerNo, //商户编号
  231. Remark = "交易成功",
  232. SettleFee = decimal.Parse(function.CheckNum(TradeFee)),
  233. BankCardType = "02",
  234. TradeType = TradeType,
  235. TradeStatus = "00",
  236. TradeAmount = decimal.Parse(function.CheckNum(TradeAmount)),
  237. TradeSerialNo = OrderNo,
  238. ProductType = ProductType,
  239. Field1 = PayMode,
  240. Field2 = TradeCycle,
  241. Status = 1,
  242. });
  243. db.SaveChanges();
  244. }
  245. db.Dispose();
  246. }
  247. // 关闭响应
  248. reader.Dispose();
  249. responseStream.Dispose();
  250. response.Close();
  251. }
  252. catch (WebException ex)
  253. {
  254. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  255. }
  256. }
  257. }
  258. }