GetHaoDaFTPInfoService.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using System.Threading;
  6. using MySystem.Models.Main;
  7. using Library;
  8. using LitJson;
  9. using System.Net;
  10. using System.IO;
  11. using AppConfig;
  12. namespace MySystem
  13. {
  14. /// <summary>
  15. /// 获取好哒ftp数据
  16. /// </summary>
  17. public class GetHaoDaFTPInfoService
  18. {
  19. public readonly static GetHaoDaFTPInfoService Instance = new GetHaoDaFTPInfoService();
  20. private GetHaoDaFTPInfoService()
  21. { }
  22. public void Start()
  23. {
  24. // //半小时执行获取好哒FTP交易数据
  25. // Thread th = new Thread(GetFTPDataInfoHalfHour);
  26. // th.IsBackground = true;
  27. // th.Start();
  28. // //每天凌晨执行获取好哒FTP今日交易数据
  29. // Thread th1 = new Thread(GetFTPDataInfoToday);
  30. // th1.IsBackground = true;
  31. // th1.Start();
  32. //每天凌晨执行获取好哒FTP昨日交易数据
  33. Thread th2 = new Thread(GetFTPDataInfoYesterdayReady);
  34. th2.IsBackground = true;
  35. th2.Start();
  36. }
  37. /// <summary>
  38. /// 没半小时获取好哒FTP今日交易数据
  39. /// </summary>
  40. public void GetFTPDataInfoHalfHour()
  41. {
  42. // 47.108.253.46
  43. // 用户名:hdftp
  44. // 密:haodatradeftp2024
  45. // 目录:/haoda-trade
  46. // FTP服务器的地址
  47. string ftpServerAddress = Base.HaoDaFtpHost;
  48. // FTP登录凭证
  49. string ftpUser = Base.HaoDaFtpLoginName;
  50. string ftpPassword = Base.HaoDaFtpLoginPwd;
  51. // 要下载的文件路径
  52. string filePath = "/haoda-trade/" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
  53. while (true)
  54. {
  55. try
  56. {
  57. // 创建FtpWebRequest对象
  58. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  59. request.Method = WebRequestMethods.Ftp.DownloadFile;
  60. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  61. // 使用WebResponse获取响应
  62. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  63. // 打开数据流
  64. Stream responseStream = response.GetResponseStream();
  65. using (StreamReader reader = new StreamReader(responseStream))
  66. {
  67. WebCMSEntities db = new WebCMSEntities();
  68. // 读取数据
  69. string fileContents = reader.ReadToEnd();
  70. if (!string.IsNullOrEmpty(fileContents))
  71. {
  72. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  73. var DataList = DataInfo[1].Split('\n');
  74. foreach (var DataListItem in DataList)
  75. {
  76. var DataListInfo = DataListItem.Split(',');
  77. var MerchantNo = ""; // 商户号
  78. var BaseNo = ""; // 设备号
  79. var OrderNo = ""; // 订单号
  80. var PayWay = ""; // 支付方式(微信 支付宝)
  81. var TradeType = ""; // 交易类型
  82. var TradeAmount = ""; // 交易金额
  83. var TradeFee = ""; // 交易手续费
  84. var TradeDate = ""; // 交易时间
  85. var TradeCycle = ""; // 结算周期
  86. MerchantNo = DataListInfo[0];
  87. BaseNo = DataListInfo[1];
  88. OrderNo = DataListInfo[2];
  89. PayWay = DataListInfo[3];
  90. TradeType = DataListInfo[4];
  91. TradeAmount = DataListInfo[5];
  92. TradeFee = DataListInfo[6];
  93. TradeDate = DataListInfo[7];
  94. TradeCycle = DataListInfo[8];
  95. TradeDate = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " " + TradeDate.Substring(8, 2) + ":" + TradeDate.Substring(10, 2) + ":" + TradeDate.Substring(12, 2);
  96. var PayMode = 0;
  97. if (PayWay.Contains("支付宝")) PayMode = 1;
  98. if (PayWay.Contains("微信")) PayMode = 2;
  99. var merchantAddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerchantNo) ?? new MerchantAddInfo();
  100. if (merchantAddInfo.Id > 0)
  101. {
  102. var merchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == merchantAddInfo.Id) ?? new MerchantInfo();
  103. var orders = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == OrderNo && OrderNo == OrderNo) ?? new ConsumerOrders();
  104. if (orders.Id == 0)
  105. {
  106. var query = db.ConsumerOrders.Add(new ConsumerOrders()
  107. {
  108. Status = 1,
  109. CreateDate = DateTime.Parse(TradeDate),
  110. UpdateDate = DateTime.Parse(TradeDate),
  111. SnNo = BaseNo,
  112. PayMoney = decimal.Parse(TradeAmount),
  113. PayMode = PayMode,
  114. SeoTitle = OrderNo,
  115. OrderNo = OrderNo,
  116. MerchantId = merchantAddInfo.Id,
  117. UserId = merchantInfo.UserId
  118. }).Entity;
  119. }
  120. }
  121. }
  122. db.SaveChanges();
  123. db.Dispose();
  124. }
  125. // 关闭响应
  126. response.Close();
  127. Thread.Sleep(1800000);
  128. }
  129. }
  130. catch (WebException ex)
  131. {
  132. // 如果抛出WebException,则可能是文件不存在
  133. // 根据响应状态码判断是否文件不存在
  134. if (((FtpWebResponse)ex.Response).StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
  135. {
  136. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString() + "文件" + filePath + "不存在", "获取好哒FTP文件数据异常"); // 文件不存在,返回false
  137. }
  138. else
  139. {
  140. // 其他错误
  141. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  142. }
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// 获取好哒FTP今日交易数据
  148. /// </summary>
  149. public void GetFTPDataInfoToday()
  150. {
  151. // 47.108.253.46
  152. // 用户名:hdftp
  153. // 密:haodatradeftp2024
  154. // 目录:/haoda-trade
  155. // FTP服务器的地址
  156. string ftpServerAddress = Base.HaoDaFtpHost;
  157. // FTP登录凭证
  158. string ftpUser = Base.HaoDaFtpLoginName;
  159. string ftpPassword = Base.HaoDaFtpLoginPwd;
  160. // 要下载的文件路径
  161. string filePath = "/haoda-trade/" + DateTime.Now.ToString("yyyyMMdd") + ".csv";
  162. while (true)
  163. {
  164. if (DateTime.Now.Hour > 0 && DateTime.Now.Hour < 9)
  165. {
  166. try
  167. {
  168. // 创建FtpWebRequest对象
  169. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  170. request.Method = WebRequestMethods.Ftp.DownloadFile;
  171. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  172. // 使用WebResponse获取响应
  173. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  174. // 打开数据流
  175. Stream responseStream = response.GetResponseStream();
  176. using (StreamReader reader = new StreamReader(responseStream))
  177. {
  178. WebCMSEntities db = new WebCMSEntities();
  179. // 读取数据
  180. string fileContents = reader.ReadToEnd();
  181. if (!string.IsNullOrEmpty(fileContents))
  182. {
  183. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  184. var DataList = DataInfo[1].Split('\n');
  185. foreach (var DataListItem in DataList)
  186. {
  187. var DataListInfo = DataListItem.Split(',');
  188. var MerchantNo = ""; // 商户号
  189. var BaseNo = ""; // 设备号
  190. var OrderNo = ""; // 订单号
  191. var PayWay = ""; // 支付方式(微信 支付宝)
  192. var TradeType = ""; // 交易类型
  193. var TradeAmount = ""; // 交易金额
  194. var TradeFee = ""; // 交易手续费
  195. var TradeDate = ""; // 交易时间
  196. var TradeCycle = ""; // 结算周期
  197. MerchantNo = DataListInfo[0];
  198. BaseNo = DataListInfo[1];
  199. OrderNo = DataListInfo[2];
  200. PayWay = DataListInfo[3];
  201. TradeType = DataListInfo[4];
  202. TradeAmount = DataListInfo[5];
  203. TradeFee = DataListInfo[6];
  204. TradeDate = DataListInfo[7];
  205. TradeCycle = DataListInfo[8];
  206. TradeDate = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " " + TradeDate.Substring(8, 2) + ":" + TradeDate.Substring(10, 2) + ":" + TradeDate.Substring(12, 2);
  207. var PayMode = 0;
  208. if (PayWay.Contains("支付宝")) PayMode = 1;
  209. if (PayWay.Contains("微信")) PayMode = 2;
  210. var merchantAddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerchantNo) ?? new MerchantAddInfo();
  211. if (merchantAddInfo.Id > 0)
  212. {
  213. var merchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == merchantAddInfo.Id) ?? new MerchantInfo();
  214. var orders = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == OrderNo && OrderNo == OrderNo) ?? new ConsumerOrders();
  215. if (orders.Id == 0)
  216. {
  217. var query = db.ConsumerOrders.Add(new ConsumerOrders()
  218. {
  219. Status = 1,
  220. CreateDate = DateTime.Parse(TradeDate),
  221. UpdateDate = DateTime.Parse(TradeDate),
  222. SnNo = BaseNo,
  223. PayMoney = decimal.Parse(TradeAmount),
  224. PayMode = PayMode,
  225. SeoTitle = OrderNo,
  226. OrderNo = OrderNo,
  227. MerchantId = merchantAddInfo.Id,
  228. UserId = merchantInfo.UserId
  229. }).Entity;
  230. }
  231. }
  232. }
  233. db.SaveChanges();
  234. db.Dispose();
  235. }
  236. // 关闭响应
  237. response.Close();
  238. Thread.Sleep(1800000);
  239. }
  240. }
  241. catch (WebException ex)
  242. {
  243. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  244. }
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// 获取好哒FTP昨日交易数据
  250. /// </summary>
  251. public void GetFTPDataInfoYesterdayReady()
  252. {
  253. while (true)
  254. {
  255. if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 22)
  256. {
  257. string check = function.ReadInstance("/GetFTPDataInfoYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  258. if (string.IsNullOrEmpty(check))
  259. {
  260. function.WritePage("/GetFTPDataInfoYesterday/", "check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString());
  261. GetFTPDataInfoYesterday();
  262. Thread.Sleep(600000);
  263. }
  264. }
  265. else
  266. {
  267. Thread.Sleep(1800000);
  268. }
  269. }
  270. }
  271. public void GetFTPDataInfoYesterday()
  272. {
  273. // 47.108.253.46
  274. // 用户名:hdftp
  275. // 密:haodatradeftp2024
  276. // 目录:/haoda-trade
  277. // FTP服务器的地址
  278. string ftpServerAddress = Base.HaoDaFtpHost;
  279. // FTP登录凭证
  280. string ftpUser = Base.HaoDaFtpLoginName;
  281. string ftpPassword = Base.HaoDaFtpLoginPwd;
  282. // 要下载的文件路径
  283. string filePath = "/haoda-trade/" + DateTime.Now.AddDays(-1).ToString("yyyyMMdd") + ".csv";
  284. try
  285. {
  286. // 创建FtpWebRequest对象
  287. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  288. request.Method = WebRequestMethods.Ftp.DownloadFile;
  289. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  290. // 使用WebResponse获取响应
  291. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  292. // 打开数据流
  293. Stream responseStream = response.GetResponseStream();
  294. StreamReader reader = new StreamReader(responseStream);
  295. // 读取数据
  296. string fileContents = reader.ReadToEnd();
  297. if (!string.IsNullOrEmpty(fileContents))
  298. {
  299. WebCMSEntities db = new WebCMSEntities();
  300. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  301. var DataList = DataInfo[1].Split('\n');
  302. foreach (var DataListItem in DataList)
  303. {
  304. var DataListInfo = DataListItem.Split(',');
  305. var MerchantNo = ""; // 商户号
  306. var BaseNo = ""; // 设备号
  307. var OrderNo = ""; // 订单号
  308. var PayWay = ""; // 支付方式(微信 支付宝)
  309. var TradeType = ""; // 交易类型
  310. var TradeAmount = ""; // 交易金额
  311. var TradeFee = ""; // 交易手续费
  312. var TradeDate = ""; // 交易时间
  313. var TradeCycle = ""; // 结算周期
  314. MerchantNo = DataListInfo[0];
  315. BaseNo = DataListInfo[1];
  316. OrderNo = DataListInfo[2];
  317. PayWay = DataListInfo[3];
  318. TradeType = DataListInfo[4];
  319. TradeAmount = DataListInfo[5];
  320. TradeFee = DataListInfo[6];
  321. TradeDate = DataListInfo[7];
  322. TradeCycle = DataListInfo[8];
  323. TradeDate = TradeDate.Substring(0, 4) + "-" + TradeDate.Substring(4, 2) + "-" + TradeDate.Substring(6, 2) + " " + TradeDate.Substring(8, 2) + ":" + TradeDate.Substring(10, 2) + ":" + TradeDate.Substring(12, 2);
  324. var PayMode = 0;
  325. if (PayWay.Contains("支付宝")) PayMode = 1;
  326. if (PayWay.Contains("微信")) PayMode = 2;
  327. var merchantAddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerchantNo) ?? new MerchantAddInfo();
  328. if (merchantAddInfo.Id > 0)
  329. {
  330. var merchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == merchantAddInfo.Id) ?? new MerchantInfo();
  331. var orders = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == OrderNo && m.OrderNo == OrderNo) ?? new ConsumerOrders();
  332. if (orders.Id == 0)
  333. {
  334. var query = db.ConsumerOrders.Add(new ConsumerOrders()
  335. {
  336. Status = 1,
  337. CreateDate = DateTime.Parse(TradeDate),
  338. UpdateDate = DateTime.Parse(TradeDate),
  339. SnNo = BaseNo,
  340. PayMoney = decimal.Parse(TradeAmount),
  341. PayMode = PayMode,
  342. SeoTitle = OrderNo,
  343. OrderNo = OrderNo,
  344. MerchantId = merchantAddInfo.Id,
  345. UserId = merchantInfo.UserId
  346. }).Entity;
  347. }
  348. }
  349. }
  350. db.SaveChanges();
  351. db.Dispose();
  352. }
  353. // 关闭响应
  354. reader.Dispose();
  355. responseStream.Dispose();
  356. response.Close();
  357. }
  358. catch (WebException ex)
  359. {
  360. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  361. }
  362. }
  363. }
  364. }