SIMPosMerchantTradeService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using MySystem.Models;
  7. using System.Collections.Generic;
  8. /// <summary>
  9. /// 广电卡交易数据导入
  10. /// </summary>
  11. namespace MySystem
  12. {
  13. public class SIMPosMerchantTradeService
  14. {
  15. public readonly static SIMPosMerchantTradeService Instance = new SIMPosMerchantTradeService();
  16. private SIMPosMerchantTradeService()
  17. {
  18. }
  19. public void Start()//启动
  20. {
  21. Thread thread = new Thread(ImportPostDo);
  22. thread.IsBackground = true;
  23. thread.Start();
  24. }
  25. public void ImportPostDo()
  26. {
  27. while (true)
  28. {
  29. try
  30. {
  31. string data = RedisDbconn.Instance.RPop<string>("SIMPosMerchantTradeQueue");
  32. if (!string.IsNullOrEmpty(data))
  33. {
  34. string[] dataList = data.Split("#cut#");
  35. string _ExcelPath = dataList[0];
  36. string _Kind = dataList[1];
  37. string checkKey = dataList[2];
  38. string Operator = dataList[3]; // 操作人
  39. int SuccessCount = 0;
  40. int DoCount = 0;
  41. string FullExcelPath = function.getPath(_ExcelPath);
  42. FullExcelPath = FullExcelPath.Replace("//", "/");
  43. DataTable list = new PublicFunction().ExcelToDataTable(FullExcelPath);
  44. int TotalCount = list.Rows.Count;
  45. while (DoCount < list.Rows.Count)
  46. {
  47. WebCMSEntities db = new WebCMSEntities();
  48. //导入人工已退
  49. if (_Kind == "1")
  50. {
  51. var tran = db.Database.BeginTransaction();
  52. try
  53. {
  54. int Size = 100;
  55. if (list.Rows.Count - DoCount < 100)
  56. {
  57. Size = list.Rows.Count - DoCount;
  58. }
  59. Dictionary<string, int> storeData = new Dictionary<string, int>();
  60. for (int i = DoCount; i < DoCount + Size; i++)
  61. {
  62. DataRow dr = list.Rows[i];
  63. string PosSn = dr[0].ToString().Replace("机具SN:", "");// 退押机具Sn
  64. string Statuss = dr[1].ToString(); // 结果
  65. string SeoDescription = dr[2].ToString();// 备注
  66. int Status = 0;
  67. if (Statuss == "SUCCESS")//成功
  68. {
  69. Status = 1;
  70. }
  71. if (Statuss == "FAIL")//失败
  72. {
  73. Status = 2;
  74. }
  75. var pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == PosSn) ?? new PosMachinesTwo();
  76. var merchantDepositReturns = db.MerchantDepositReturns.FirstOrDefault(m => m.MerchantId == pos.BindMerchantId && m.Status == 0) ?? new MerchantDepositReturns();
  77. if (merchantDepositReturns.Id == 0)
  78. {
  79. RedisDbconn.Instance.AddList("ErrList" + checkKey, "以下操作失败,机具" + PosSn + ',' + "对应的退押订单" + merchantDepositReturns.SeoKeyword + "该订单未找到或不符合规则");
  80. }
  81. else
  82. {
  83. //失败
  84. if (Status == 2)
  85. {
  86. var posm = db.PosMerchantInfo.FirstOrDefault(m => m.Id == merchantDepositReturns.MerchantId) ?? new PosMerchantInfo();
  87. posm.StandardStatus = 101;
  88. }
  89. merchantDepositReturns.Status = Status;
  90. merchantDepositReturns.UpdateDate = DateTime.Now;
  91. merchantDepositReturns.SeoDescription = SeoDescription;
  92. merchantDepositReturns.SeoTitle = Operator;
  93. SuccessCount += 1;
  94. }
  95. }
  96. DoCount += Size;
  97. db.SaveChanges();
  98. tran.Commit();
  99. if (DoCount >= list.Rows.Count)
  100. {
  101. RedisDbconn.Instance.Set("SIMPosMerchantTradeCheckImport:" + checkKey, "success|" + SuccessCount);
  102. RedisDbconn.Instance.SetExpire("SIMPosMerchantTradeCheckImport:" + checkKey, 60000);
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. DoCount = list.Rows.Count;
  108. function.WriteLog(ex.ToString(), "导入首台服务费退还结果");
  109. tran.Rollback();
  110. ErrorMsg msg = new ErrorMsg()
  111. {
  112. Time = DateTime.Now,
  113. ErrorContent = ex.ToString(),
  114. };
  115. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入首台服务费退还结果Excel文件异常");
  116. }
  117. tran.Dispose();
  118. }
  119. }
  120. }
  121. else
  122. {
  123. Thread.Sleep(5000);
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
  129. }
  130. }
  131. }
  132. }
  133. }