GetHaoDaFTPInfoService.cs 26 KB

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