promotion.cs 6.9 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 promotion_img_key = "queue:promotion:img";
  9. public static int task_insert_promotion_img_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<PromotionQueryDTO>(promotion_img_key);
  20. if (data == null) break;
  21. if (data.success)
  22. {
  23. data.similarPromotion = string.Empty;
  24. data.promotionImg = string.Empty;
  25. }
  26. //按日保存数据
  27. string daily_table = $"tk_promotion_logs_{data.create_time:yyyyMMdd}";
  28. save_tk_promotion_img_logs(data, daily_table, connection, transaction);
  29. total++;
  30. }
  31. transaction.Commit();
  32. }
  33. catch (Exception ex)
  34. {
  35. transaction.Rollback();
  36. new LoggerLibrary("TkLogCore_error", "task_insert_parse_pdd_logs")
  37. .Info(ex.Message, ex.StackTrace)
  38. .SaveAsync();
  39. throw;
  40. }
  41. finally
  42. {
  43. connection.Close();
  44. }
  45. return total;
  46. }
  47. private static int save_tk_promotion_img_logs(PromotionQueryDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  48. {
  49. try
  50. {
  51. return new DBContext.Table(connection, tablename)
  52. .Add("accountId", data.accountId)
  53. .Add("accountName", data.accountName)
  54. .Add("proxy_node", data.proxy_node)
  55. .Add("img", data.img)
  56. .Add("ip", data.ip)
  57. .Add("oaid", data.oaid)
  58. .Add("scene", data.scene)
  59. .Add("success", data.success)
  60. .Add("message", data.message)
  61. .Add("reason", data.reason)
  62. .Add("url", data.url)
  63. .Add("similarPromotion", data.similarPromotion)
  64. .Add("promotionImg", data.promotionImg)
  65. .Add("elapsedTime", data.elapsedTime)
  66. .Add("elapsedTime2", data.elapsedTime2)
  67. .Add("elapsedTime3", data.elapsedTime3)
  68. .Add("elapsedTime4", data.elapsedTime4)
  69. .Add("create_time", data.create_time)
  70. .Add("end_point", data.end_point)
  71. .Create(DBContext.InsertType.NORMAL, transaction);
  72. }
  73. catch (Exception ex) { return 0; }
  74. }
  75. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  76. {
  77. try
  78. {
  79. var ts = DateTime.Now - response.create_time;
  80. response.elapsedTime = (int)ts.TotalMilliseconds;
  81. _ = RedisHelper.RPushAsync(promotion_img_key, response);
  82. //using var connection = DBContext.GetOpenConnection();
  83. //connection.Insert(response);
  84. savePromotionCache(response.accountId, response.accountName, response.scene, response.success, response.message, response.reason);
  85. }
  86. catch (Exception ex) { }
  87. }
  88. private static void savePromotionCache(int accountId, string accountName, string scene, bool success, string message, string reason)
  89. {
  90. savePromotionAccountCache("all", success, message, reason);
  91. savePromotionAccountCache($"{accountId}", success, message, reason);
  92. savePromotionAccountCache($"all_scene_{scene}", success, message, reason);
  93. savePromotionAccountCache($"{accountId}_{scene}", success, message, reason);
  94. }
  95. private static void savePromotionAccountCache(string accountName, bool success, string message, string reason)
  96. {
  97. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMM}");
  98. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  99. RedisHelper.IncrBy($":promotion_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  100. string result = success ? "success" : "fail";
  101. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  102. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  103. RedisHelper.IncrBy($":promotion_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  104. if (!string.IsNullOrEmpty(message))
  105. {
  106. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  107. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  108. RedisHelper.SAdd($":promotion_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  109. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  110. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  111. RedisHelper.IncrBy($":promotion_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  112. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  113. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  114. RedisHelper.IncrBy($":promotion_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  115. }
  116. if (!string.IsNullOrEmpty(reason))
  117. {
  118. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  119. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  120. RedisHelper.SAdd($":promotion_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  121. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  122. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  123. RedisHelper.IncrBy($":promotion_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  124. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  125. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  126. RedisHelper.IncrBy($":promotion_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  127. }
  128. }
  129. }
  130. }