GetHaoDaFTPInfoService.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
  49. // Thread.Sleep(60000);
  50. GetTradeData(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
  51. Thread.Sleep(600000);
  52. }
  53. }
  54. if (DateTime.Now.Hour > 10)
  55. {
  56. GetDepositData(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
  57. }
  58. Thread.Sleep(1800000);
  59. }
  60. }
  61. public void StartListen()
  62. {
  63. //每天凌晨执行获取好哒FTP昨日交易数据
  64. Thread th2 = new Thread(ListenDepositDataReady);
  65. th2.IsBackground = true;
  66. th2.Start();
  67. }
  68. /// <summary>
  69. /// 获取好哒FTP昨日交易数据
  70. /// </summary>
  71. public void ListenDepositDataReady()
  72. {
  73. while (true)
  74. {
  75. string content = RedisDbconn.Instance.RPop<string>("ListenDepositDataQueue2");
  76. if (!string.IsNullOrEmpty(content))
  77. {
  78. GetDepositData2(content);
  79. Thread.Sleep(2000);
  80. }
  81. else
  82. {
  83. Thread.Sleep(60000);
  84. }
  85. }
  86. }
  87. public void GetDepositData(string Date)
  88. {
  89. // 要下载的文件路径
  90. string filePath = "/haoda-deposit/deposit_" + Date + ".csv";
  91. try
  92. {
  93. // 创建FtpWebRequest对象
  94. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  95. request.Method = WebRequestMethods.Ftp.DownloadFile;
  96. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  97. // 使用WebResponse获取响应
  98. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  99. // 打开数据流
  100. Stream responseStream = response.GetResponseStream();
  101. StreamReader reader = new StreamReader(responseStream);
  102. // 读取数据
  103. string fileContents = reader.ReadToEnd();
  104. if (!string.IsNullOrEmpty(fileContents))
  105. {
  106. WebCMSEntities db = new WebCMSEntities();
  107. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  108. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  109. if(DataInfo.Length > 1)
  110. {
  111. var DataList = DataInfo[1].Split('\n');
  112. foreach (var DataListItem in DataList)
  113. {
  114. var DataListInfo = DataListItem.Split(',');
  115. string SnNo = DataListInfo[0]; //sn
  116. string MerNo = DataListInfo[1]; //商户编号
  117. string PosKind = DataListInfo[2]; //机具型号
  118. string ActDate = DataListInfo[3]; //激活时间
  119. string Deposit = DataListInfo[4]; //押金金额
  120. string PrizeAmt = DataListInfo[5]; //奖励金额
  121. string PrizeDate = DataListInfo[6]; //奖励发放时间
  122. string Name = DataListInfo[7]; //发放人姓名
  123. string MerName = DataListInfo[8]; //商户名称
  124. string PassDate = DataListInfo[9]; //审核通过时期
  125. string ActDateString = ActDate.Substring(0, 4) + "-" + ActDate.Substring(4, 2) + "-" + ActDate.Substring(6, 2);
  126. if(DateTime.Parse(ActDateString) >= DateTime.Parse(DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd")))
  127. {
  128. string PrizeDateString = "";
  129. if(PrizeDate == "None")
  130. {
  131. PrizeDate = Date;
  132. }
  133. else
  134. {
  135. PrizeDateString = PrizeDate.Substring(0, 4) + "-" + PrizeDate.Substring(4, 2) + "-" + PrizeDate.Substring(6, 2);
  136. }
  137. if(!db.BindRecord.Any(m => m.MerSnNo == SnNo))
  138. {
  139. string ProductType = "0";
  140. if(PosKind == "好哒语音王Y512") ProductType = "18";
  141. if(PosKind == "4G收款王M820") ProductType = "19";
  142. if(PosKind == "4G收款王M826") ProductType = "20";
  143. if(PosKind == "好哒S312") ProductType = "21";
  144. if(PosKind == "好哒扫码通M837") ProductType = "29";
  145. db.BindRecord.Add(new BindRecord()
  146. {
  147. CreateDate = DateTime.Now,
  148. UpdateTime = DateTime.Now, //机具绑定、解绑时间
  149. CreateTime = DateTime.Now, //商户操作时间
  150. MerSnNo = SnNo, //序列号
  151. MerNo = MerNo, //商户编号
  152. MerName = MerName,
  153. SeoTitle = PrizeAmt,
  154. SeoKeyword = ActDateString,
  155. ProductType = ProductType,
  156. Field1 = Deposit,
  157. Field2 = PrizeDate,
  158. Field3 = Name,
  159. Field4 = PassDate,
  160. Status = 1,
  161. });
  162. db.Merchants.Add(new Merchants()
  163. {
  164. SnNo = SnNo,
  165. CreateTime = DateTime.Now,
  166. UpdateTime = DateTime.Now,
  167. AgentName = Name,
  168. MerRealName = Name,
  169. MerNo = MerNo,
  170. MerName = Name,
  171. ProductType = ProductType,
  172. Status = 1,
  173. });
  174. db.SaveChanges();
  175. Thread.Sleep(1000);
  176. db.ActivateRecord.Add(new ActivateRecord()
  177. {
  178. SnNo = SnNo,
  179. CreateDate = DateTime.Now,
  180. SeoTitle = Deposit,
  181. ActivateDate = DateTime.Now,
  182. AgentNo = MerNo,
  183. MerRealName = Name,
  184. MerNo = MerNo,
  185. MerName = Name,
  186. ProductType = ProductType,
  187. ChannelSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  188. Status = 1,
  189. Field1 = PassDate,
  190. });
  191. db.SaveChanges();
  192. //推送MQ给创业帮
  193. var merchantAddInfo = mpdb2.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerNo) ?? new MpMainModels2.MerchantAddInfo();
  194. if (merchantAddInfo.BrandId == 1 && !string.IsNullOrEmpty(merchantAddInfo.CybMakerCode))
  195. {
  196. SortedList<string, string> obj = new SortedList<string, string>();
  197. obj.Add("create_time", ActDateString);
  198. obj.Add("sn", SnNo);
  199. obj.Add("deposit", Deposit);
  200. obj.Add("subject_type", merchantAddInfo.SubjectType);
  201. obj.Add("merch_no", merchantAddInfo.MchtNo);
  202. obj.Add("maker_code", merchantAddInfo.CybMakerCode);
  203. PushHelper.Instance.Do(obj, "cashNotify");
  204. }
  205. }
  206. }
  207. }
  208. }
  209. db.Dispose();
  210. mpdb2.Dispose();
  211. }
  212. // 关闭响应
  213. reader.Dispose();
  214. responseStream.Dispose();
  215. response.Close();
  216. }
  217. catch (WebException ex)
  218. {
  219. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  220. }
  221. }
  222. public void GetDepositData2(string Date)
  223. {
  224. // 要下载的文件路径
  225. string filePath = "/haoda-deposit/deposit_" + Date + ".csv";
  226. try
  227. {
  228. // 创建FtpWebRequest对象
  229. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  230. request.Method = WebRequestMethods.Ftp.DownloadFile;
  231. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  232. // 使用WebResponse获取响应
  233. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  234. // 打开数据流
  235. Stream responseStream = response.GetResponseStream();
  236. StreamReader reader = new StreamReader(responseStream);
  237. // 读取数据
  238. string fileContents = reader.ReadToEnd();
  239. if (!string.IsNullOrEmpty(fileContents))
  240. {
  241. WebCMSEntities db = new WebCMSEntities();
  242. MpMainModels2.WebCMSEntities mpdb2 = new MpMainModels2.WebCMSEntities();
  243. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  244. if(DataInfo.Length > 1)
  245. {
  246. var DataList = DataInfo[1].Split('\n');
  247. foreach (var DataListItem in DataList)
  248. {
  249. var DataListInfo = DataListItem.Split(',');
  250. string SnNo = DataListInfo[0]; //sn
  251. string MerNo = DataListInfo[1]; //商户编号
  252. string PosKind = DataListInfo[2]; //机具型号
  253. string ActDate = DataListInfo[3]; //激活时间
  254. string Deposit = DataListInfo[4]; //押金金额
  255. string PrizeAmt = DataListInfo[5]; //奖励金额
  256. string PrizeDate = DataListInfo[6]; //奖励发放时间
  257. string Name = DataListInfo[7]; //发放人姓名
  258. string ActDateString = ActDate.Substring(0, 4) + "-" + ActDate.Substring(4, 2) + "-" + ActDate.Substring(6, 2);
  259. string PrizeDateString = "";
  260. if(PrizeDate == "None")
  261. {
  262. PrizeDate = Date;
  263. }
  264. else
  265. {
  266. PrizeDateString = PrizeDate.Substring(0, 4) + "-" + PrizeDate.Substring(4, 2) + "-" + PrizeDate.Substring(6, 2);
  267. }
  268. string ProductType = "0";
  269. if(PosKind == "好哒语音王Y512") ProductType = "18";
  270. if(PosKind == "4G收款王M820") ProductType = "19";
  271. if(PosKind == "4G收款王M826") ProductType = "20";
  272. if(PosKind == "好哒S312") ProductType = "21";
  273. db.BindRecord.Add(new BindRecord()
  274. {
  275. CreateDate = DateTime.Now,
  276. UpdateTime = DateTime.Now, //机具绑定、解绑时间
  277. CreateTime = DateTime.Now, //商户操作时间
  278. MerSnNo = SnNo, //序列号
  279. MerNo = MerNo, //商户编号
  280. MerName = Name,
  281. SeoTitle = PrizeAmt,
  282. SeoKeyword = ActDateString,
  283. ProductType = ProductType,
  284. Field1 = Deposit,
  285. Field2 = PrizeDate,
  286. Status = 1,
  287. });
  288. db.Merchants.Add(new Merchants()
  289. {
  290. SnNo = SnNo,
  291. CreateTime = DateTime.Now,
  292. UpdateTime = DateTime.Now,
  293. AgentName = Name,
  294. MerRealName = Name,
  295. MerNo = MerNo,
  296. MerName = Name,
  297. ProductType = ProductType,
  298. Status = 1,
  299. });
  300. db.SaveChanges();
  301. Thread.Sleep(1000);
  302. db.ActivateRecord.Add(new ActivateRecord()
  303. {
  304. SnNo = SnNo,
  305. CreateDate = DateTime.Now,
  306. SeoTitle = Deposit,
  307. ActivateDate = DateTime.Now,
  308. AgentNo = MerNo,
  309. MerRealName = Name,
  310. MerNo = MerNo,
  311. MerName = Name,
  312. ProductType = ProductType,
  313. ChannelSerial = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8),
  314. Status = 1,
  315. });
  316. db.SaveChanges();
  317. //推送MQ给创业帮
  318. var merchantAddInfo = mpdb2.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerNo) ?? new MpMainModels2.MerchantAddInfo();
  319. if (merchantAddInfo.BrandId == 1 && !string.IsNullOrEmpty(merchantAddInfo.CybMakerCode))
  320. {
  321. SortedList<string, string> obj = new SortedList<string, string>();
  322. obj.Add("create_time", ActDateString);
  323. obj.Add("sn", SnNo);
  324. obj.Add("deposit", Deposit);
  325. obj.Add("subject_type", merchantAddInfo.SubjectType);
  326. obj.Add("merch_no", merchantAddInfo.MchtNo);
  327. obj.Add("maker_code", merchantAddInfo.CybMakerCode);
  328. PushHelper.Instance.Do(obj, "cashNotify");
  329. }
  330. }
  331. }
  332. db.Dispose();
  333. mpdb2.Dispose();
  334. }
  335. // 关闭响应
  336. reader.Dispose();
  337. responseStream.Dispose();
  338. response.Close();
  339. }
  340. catch (WebException ex)
  341. {
  342. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件数据异常");
  343. }
  344. }
  345. public void StartTrade()
  346. {
  347. //每天凌晨执行获取好哒FTP昨日交易数据
  348. Thread th2 = new Thread(GetTradeDataReady);
  349. th2.IsBackground = true;
  350. th2.Start();
  351. }
  352. /// <summary>
  353. /// 获取好哒FTP昨日交易数据
  354. /// </summary>
  355. public void GetTradeDataReady()
  356. {
  357. while (true)
  358. {
  359. if (DateTime.Now.Hour > 10 && DateTime.Now.Hour < 22)
  360. {
  361. string check = function.ReadInstance("/GetFTPTradeYesterday/check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt");
  362. if (string.IsNullOrEmpty(check))
  363. {
  364. function.WritePage("/GetFTPTradeYesterday/", "check" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + ".txt", DateTime.Now.ToString());
  365. GetTradeData(DateTime.Now.AddDays(-1).ToString("yyyyMMdd"));
  366. Thread.Sleep(600000);
  367. }
  368. }
  369. else
  370. {
  371. Thread.Sleep(1800000);
  372. }
  373. }
  374. }
  375. public void StartTradeListen()
  376. {
  377. //每天凌晨执行获取好哒FTP昨日交易数据
  378. Thread th2 = new Thread(ListenTradeDataReady);
  379. th2.IsBackground = true;
  380. th2.Start();
  381. }
  382. /// <summary>
  383. /// 获取好哒FTP昨日交易数据
  384. /// </summary>
  385. public void ListenTradeDataReady()
  386. {
  387. while (true)
  388. {
  389. string content = RedisDbconn.Instance.RPop<string>("ListenTradeDataQueue");
  390. if (!string.IsNullOrEmpty(content))
  391. {
  392. GetTradeData(content);
  393. Thread.Sleep(2000);
  394. }
  395. else
  396. {
  397. Thread.Sleep(60000);
  398. }
  399. }
  400. }
  401. public void GetTradeData(string Date)
  402. {
  403. // 要下载的文件路径
  404. string filePath = "/haoda-trade/" + Date + ".csv";
  405. function.WriteLog(DateTime.Now.ToString() + ":" + filePath, "获取好哒FTP文件交易数据");
  406. try
  407. {
  408. // 创建FtpWebRequest对象
  409. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServerAddress + filePath);
  410. request.Method = WebRequestMethods.Ftp.DownloadFile;
  411. request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
  412. // 使用WebResponse获取响应
  413. FtpWebResponse response = (FtpWebResponse)request.GetResponse();
  414. // 打开数据流
  415. Stream responseStream = response.GetResponseStream();
  416. StreamReader reader = new StreamReader(responseStream);
  417. // 读取数据
  418. string fileContents = reader.ReadToEnd();
  419. function.WriteLog("fileContents:" + fileContents, "获取好哒FTP文件交易数据");
  420. if (!string.IsNullOrEmpty(fileContents))
  421. {
  422. MpMainModels2.WebCMSEntities db = new MpMainModels2.WebCMSEntities();
  423. var DataInfo = fileContents.TrimEnd('\n').Split('\n', 2);
  424. if(DataInfo.Length > 1)
  425. {
  426. var DataList = DataInfo[1].Split('\n');
  427. foreach (var DataListItem in DataList)
  428. {
  429. var DataListInfo = DataListItem.Split(',');
  430. var MerchantNo = ""; // 商户号
  431. var BaseNo = ""; // 设备号
  432. var OrderNo = ""; // 订单号
  433. var PayWay = ""; // 支付方式(微信 支付宝)
  434. var TradeType = ""; // 交易类型
  435. var TradeAmount = ""; // 交易金额
  436. var TradeFee = ""; // 交易手续费
  437. var TradeDate = ""; // 交易时间
  438. var TradeCycle = ""; // 结算周期
  439. MerchantNo = DataListInfo[0];
  440. BaseNo = DataListInfo[1];
  441. OrderNo = DataListInfo[3];
  442. PayWay = DataListInfo[4];
  443. TradeType = DataListInfo[5];
  444. TradeAmount = DataListInfo[6];
  445. TradeFee = DataListInfo[7];
  446. TradeDate = DataListInfo[8];
  447. TradeCycle = DataListInfo[9];
  448. 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);
  449. function.WriteLog("OrderNo:" + OrderNo, "获取好哒FTP文件交易数据");
  450. function.WriteLog("MerchantNo:" + MerchantNo, "获取好哒FTP文件交易数据");
  451. var PayMode = 0;
  452. if (PayWay.Contains("支付宝")) PayMode = 1;
  453. if (PayWay.Contains("微信")) PayMode = 2;
  454. var merchantAddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.MchtNo == MerchantNo) ?? new MpMainModels2.MerchantAddInfo();
  455. if (merchantAddInfo.Id > 0)
  456. {
  457. function.WriteLog("找到商户", "获取好哒FTP文件交易数据");
  458. var merchantInfo = db.MerchantInfo.FirstOrDefault(m => m.Id == merchantAddInfo.Id) ?? new MpMainModels2.MerchantInfo();
  459. if(merchantInfo.IsAct == 0)
  460. {
  461. merchantInfo.IsAct = 1;
  462. }
  463. var orders = db.ConsumerOrders.FirstOrDefault(m => m.SeoTitle == OrderNo && m.OrderNo == OrderNo) ?? new MpMainModels2.ConsumerOrders();
  464. if (orders.Id == 0)
  465. {
  466. function.WriteLog("订单号不存在,开始入库", "获取好哒FTP文件交易数据");
  467. var query = db.ConsumerOrders.Add(new MpMainModels2.ConsumerOrders()
  468. {
  469. Status = 1,
  470. CreateDate = DateTime.Parse(TradeDate),
  471. UpdateDate = DateTime.Parse(TradeDate),
  472. SnNo = BaseNo,
  473. PayMoney = decimal.Parse(TradeAmount),
  474. PayMode = PayMode,
  475. SeoTitle = OrderNo,
  476. OrderNo = OrderNo,
  477. MerchantId = merchantAddInfo.Id,
  478. UserId = merchantInfo.UserId,
  479. SeoKeyword = merchantAddInfo.CybMakerCode,
  480. }).Entity;
  481. db.SaveChanges();
  482. function.WriteLog("入库完毕", "获取好哒FTP文件交易数据");
  483. //推送MQ给创业帮
  484. if (merchantAddInfo.BrandId == 1)
  485. {
  486. if(!string.IsNullOrEmpty(merchantAddInfo.CybMakerCode))
  487. {
  488. SortedList<string, string> obj = new SortedList<string, string>();
  489. obj.Add("create_time", TradeDate);
  490. obj.Add("sn", BaseNo);
  491. obj.Add("pay_money", TradeAmount);
  492. obj.Add("pay_mode", PayMode.ToString());
  493. obj.Add("order_no", OrderNo);
  494. obj.Add("merch_no", merchantAddInfo.MchtNo);
  495. obj.Add("maker_code", merchantAddInfo.CybMakerCode);
  496. PushHelper.Instance.Do(obj);
  497. }
  498. OrderMessageHelper.SendOrderMsg(query);
  499. }
  500. }
  501. }
  502. }
  503. }
  504. db.Dispose();
  505. }
  506. // 关闭响应
  507. reader.Dispose();
  508. responseStream.Dispose();
  509. response.Close();
  510. function.WriteLog("结束\n\n\n", "获取好哒FTP文件数据异常");
  511. }
  512. catch (WebException ex)
  513. {
  514. function.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "获取好哒FTP文件交易数据异常");
  515. }
  516. }
  517. }
  518. }