PreWithdrawalResultsService.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 PreWithdrawalResultsService
  14. {
  15. public readonly static PreWithdrawalResultsService Instance = new PreWithdrawalResultsService();
  16. private PreWithdrawalResultsService()
  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>("PreWithdrawalResultsQueue");
  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 Id = dr[0].ToString();
  64. string IsOk = dr[11].ToString();
  65. var id = int.Parse(Id);
  66. var Info = db.PreAmountRecord.FirstOrDefault(m => m.Id == id) ?? new PreAmountRecord();
  67. if (Info.Id > 0)
  68. {
  69. if (IsOk == "是") Info.Status = 1;
  70. if (IsOk == "否")
  71. {
  72. Info.Status = -1;
  73. var userAccount = db.UserAccount.FirstOrDefault(m => m.Id == Info.UserId) ?? new UserAccount();
  74. if (userAccount.Id > 0)
  75. {
  76. userAccount.PreTempAmount += Info.UseAmount;//退还卡充值临额
  77. userAccount.ValidPreAmount += Info.UseAmount;//退还可用额度
  78. }
  79. }
  80. SuccessCount += 1;
  81. }
  82. else
  83. {
  84. RedisDbconn.Instance.AddList("ErrList" + checkKey, "未找到记录为Id" + Id + "相关信息");
  85. }
  86. }
  87. DoCount += Size;
  88. db.SaveChanges();
  89. tran.Commit();
  90. if (DoCount >= list.Rows.Count)
  91. {
  92. RedisDbconn.Instance.Set("PreWithdrawalResultsCheckImport:" + checkKey, "success|" + SuccessCount);
  93. RedisDbconn.Instance.SetExpire("PreWithdrawalResultsCheckImport:" + checkKey, 60000);
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. DoCount = list.Rows.Count;
  99. function.WriteLog(ex.ToString(), "导入结算金额");
  100. tran.Rollback();
  101. ErrorMsg msg = new ErrorMsg()
  102. {
  103. Time = DateTime.Now,
  104. ErrorContent = ex.ToString(),
  105. };
  106. function.WriteLog(Newtonsoft.Json.JsonConvert.SerializeObject(msg), "导入结算金额Excel文件异常");
  107. }
  108. tran.Dispose();
  109. }
  110. }
  111. }
  112. else
  113. {
  114. Thread.Sleep(50000);
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "后台导入Excel文件队列异常");
  120. }
  121. }
  122. }
  123. }
  124. }