GetHaoDaFTPInfoService.cs 27 KB

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