pdd.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. namespace molilian.core
  5. {
  6. public partial class TkLogCore
  7. {
  8. static string queue_parse_pdd_key = "queue:parse_logs:pdd";
  9. public static int task_insert_parse_pdd_logs(int limit, CSRedisClient redis)
  10. {
  11. int total = 0;
  12. using var connection = DBContext.GetOpenConnection();
  13. connection.Open();
  14. using var transaction = connection.BeginTransaction();
  15. try
  16. {
  17. for (int i = 0; i < limit; i++)
  18. {
  19. var data = redis.LPop<PddDataDTO>(queue_parse_pdd_key);
  20. if (data == null) break;
  21. if (_test_oaid.Equals(data.oaid) ||
  22. data.ip.Contains("127.0.0"))
  23. {
  24. save_pdd_parse_logs(data, "pdd_parse_logs_test", connection, transaction);
  25. }
  26. else
  27. {
  28. save_pdd_parse_logs(data, "pdd_parse_logs", connection, transaction);
  29. if (data.success)
  30. {
  31. save_pdd_parse_logs(data, "pdd_parse_logs_success", connection, transaction);
  32. }
  33. }
  34. total++;
  35. }
  36. transaction.Commit();
  37. }
  38. catch (Exception ex)
  39. {
  40. transaction.Rollback();
  41. new LoggerLibrary("TkLogCore_error", "task_insert_parse_pdd_logs")
  42. .Info(ex.Message, ex.StackTrace)
  43. .SaveAsync();
  44. throw;
  45. }
  46. finally
  47. {
  48. connection.Close();
  49. }
  50. return total;
  51. }
  52. public static async Task ParseLogAsync(PddDataDTO response)
  53. {
  54. try
  55. {
  56. var ts = DateTime.Now - response.create_time;
  57. response.elapsedTime = (int)ts.TotalMilliseconds;
  58. _ = RedisHelper.RPushAsync(queue_parse_pdd_key, response);
  59. #if DEBUG
  60. for (int i = 0; i < 100; i++)
  61. {
  62. var data = RedisHelper.LPop<PddDataDTO>(queue_parse_pdd_key);
  63. if (data == null) break;
  64. if (data.ip.Contains("127.0.0"))
  65. {
  66. save_pdd_parse_logs(data, "pdd_parse_logs_test", null, null);
  67. }
  68. else
  69. {
  70. save_pdd_parse_logs(data, "pdd_parse_logs", null, null);
  71. if (data.success)
  72. {
  73. save_pdd_parse_logs(data, "pdd_parse_logs_success", null, null);
  74. }
  75. }
  76. }
  77. #endif
  78. if (!response.ip.Contains("127.0.0"))
  79. {
  80. saveParseCache(response.channel.ToString(), response.accountId,
  81. response.accountName, response.success, response.message, response.reason,
  82. response.deeplink_url);
  83. }
  84. if (response.success || response.message.Equals("转链失败"))
  85. {
  86. PddPoolCore.CallsIncrBy(response.accountId);
  87. }
  88. if (response.reason.Equals("您的调用次数过高"))
  89. {
  90. PddPoolCore.TempSuspend(response.accountId);
  91. }
  92. if (response.success) saveClientRequestTotal(response.channel, response.ip, response.oaid);
  93. if (!response.success && ("nologin".Equals(response.reason) ||
  94. "方法不存在".Equals(response.reason) ||
  95. "未登录".Equals(response.reason)))
  96. {
  97. await Task.Run(() =>
  98. {
  99. PddPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}\n{response.rawContent2}");
  100. });
  101. }
  102. if ("没有匹配账号".Equals(response.reason))
  103. {
  104. PddPoolCore.AccountExhausted();
  105. }
  106. }
  107. catch (Exception ex) { }
  108. }
  109. private static int save_pdd_parse_logs(PddDataDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  110. {
  111. return new DBContext.Table(connection, tablename)
  112. .Add("end_point", data.end_point)
  113. .Add("channel", (int)data.channel)
  114. .Add("accountId", data.accountId)
  115. .Add("accountName", data.accountName)
  116. .Add("rawContent", data.rawContent)
  117. .Add("rawContent2", data.rawContent2)
  118. .Add("success", data.success)
  119. .Add("message", data.message)
  120. .Add("reason", data.reason)
  121. .Add("content", data.content)
  122. .Add("itemId", data.itemId)
  123. .Add("itemName", data.itemName)
  124. .Add("pic", data.pic)
  125. .Add("couponAmount", data.couponAmount)
  126. .Add("promotionPrice", data.promotionPrice)
  127. .Add("taoToken", data.taoToken)
  128. .Add("shortLinkurl", data.shortLinkurl)
  129. .Add("deeplink_url", data.deeplink_url)
  130. .Add("elapsedTime", data.elapsedTime)
  131. .Add("elapsedTime2", data.elapsedTime2)
  132. .Add("elapsedTime3", data.elapsedTime3)
  133. .Add("subCode", data.subCode)
  134. .Add("ip", data.ip)
  135. .Add("oaid", data.oaid)
  136. .Add("create_time", data.create_time)
  137. .Create(DBContext.InsertType.NORMAL, transaction);
  138. }
  139. }
  140. }