promotion.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using dodohold.core;
  2. using CSRedis;
  3. using System.Data;
  4. using YunhuiKit;
  5. namespace molilian.core
  6. {
  7. public partial class TkLogCore
  8. {
  9. static string promotion_img_key = "queue:promotion:img";
  10. public static async Task<int> InsertPromotionImgAsync(int limit, YunhuiKit.RedisClient redis)
  11. {
  12. int total = 0;
  13. using var connection = DBContext.GetOpenConnection();
  14. connection.Open();
  15. using var transaction = connection.BeginTransaction();
  16. try
  17. {
  18. for (int i = 0; i < limit; i++)
  19. {
  20. var data = await redis.LPopAsync<PromotionQueryDTO>(promotion_img_key);
  21. if (data == null) break;
  22. if (data.success)
  23. {
  24. data.similarPromotion = string.Empty;
  25. data.promotionImg = string.Empty;
  26. }
  27. //按日保存数据
  28. string daily_table = $"tk_promotion_logs_{data.create_time:yyyyMMdd}";
  29. save_tk_promotion_img_logs(data, daily_table, connection, transaction);
  30. total++;
  31. }
  32. transaction.Commit();
  33. }
  34. catch (Exception ex)
  35. {
  36. transaction.Rollback();
  37. new LoggerLibrary("TkLogCore_error", "task_insert_parse_pdd_logs")
  38. .Info(ex.Message, ex.StackTrace)
  39. .SaveAsync();
  40. throw;
  41. }
  42. finally
  43. {
  44. connection.Close();
  45. }
  46. return total;
  47. }
  48. public static int task_insert_promotion_img_logs(int limit, CSRedisClient redis)
  49. {
  50. int total = 0;
  51. using var connection = DBContext.GetOpenConnection();
  52. connection.Open();
  53. using var transaction = connection.BeginTransaction();
  54. try
  55. {
  56. for (int i = 0; i < limit; i++)
  57. {
  58. var data = redis.LPop<PromotionQueryDTO>(promotion_img_key);
  59. if (data == null) break;
  60. if (data.success)
  61. {
  62. data.similarPromotion = string.Empty;
  63. data.promotionImg = string.Empty;
  64. }
  65. //按日保存数据
  66. string daily_table = $"tk_promotion_logs_{data.create_time:yyyyMMdd}";
  67. save_tk_promotion_img_logs(data, daily_table, connection, transaction);
  68. total++;
  69. }
  70. transaction.Commit();
  71. }
  72. catch (Exception ex)
  73. {
  74. transaction.Rollback();
  75. new LoggerLibrary("TkLogCore_error", "task_insert_parse_pdd_logs")
  76. .Info(ex.Message, ex.StackTrace)
  77. .SaveAsync();
  78. throw;
  79. }
  80. finally
  81. {
  82. connection.Close();
  83. }
  84. return total;
  85. }
  86. private static int save_tk_promotion_img_logs(PromotionQueryDTO data, string tablename, IDbConnection connection, IDbTransaction transaction)
  87. {
  88. try
  89. {
  90. return new DBContext.Table(connection, tablename)
  91. .Add("accountId", data.accountId)
  92. .Add("accountName", data.accountName)
  93. .Add("proxy_node", data.proxy_node)
  94. .Add("proxy_nodename", data.proxy_nodename)
  95. //.Add("img", data.img)
  96. .Add("img", string.Empty)
  97. .Add("ip", data.ip)
  98. .Add("oaid", data.oaid)
  99. .Add("scene", data.scene)
  100. .Add("success", data.success)
  101. .Add("message", data.message)
  102. .Add("reason", data.reason)
  103. .Add("url", data.url)
  104. .Add("similarPromotion", data.similarPromotion)
  105. .Add("promotionImg", data.promotionImg)
  106. .Add("elapsedTime", data.elapsedTime)
  107. .Add("elapsedTime2", data.elapsedTime2)
  108. .Add("elapsedTime3", data.elapsedTime3)
  109. .Add("elapsedTime4", data.elapsedTime4)
  110. .Add("create_time", data.create_time)
  111. .Add("end_point", data.end_point)
  112. .Create(DBContext.InsertType.NORMAL, transaction);
  113. }
  114. catch (Exception ex) { return 0; }
  115. }
  116. public static async Task PromotionImgLogAsync(PromotionQueryDTO response)
  117. {
  118. try
  119. {
  120. var ts = DateTime.Now - response.create_time;
  121. response.elapsedTime = (int)ts.TotalMilliseconds;
  122. _ = RedisKit.RPushAsync(promotion_img_key, response);
  123. _ = savePromotionCacheAsync(response.accountId, response.accountName, response.scene, response.success, response.message, response.reason);
  124. }
  125. catch (Exception ex) { }
  126. }
  127. private static async Task savePromotionCacheAsync(int accountId, string accountName, string scene, bool success, string message, string reason)
  128. {
  129. await savePromotionAccountCacheAsync("all", success, message, reason);
  130. await savePromotionAccountCacheAsync($"{accountId}", success, message, reason);
  131. await savePromotionAccountCacheAsync($"all_scene_{scene}", success, message, reason);
  132. await savePromotionAccountCacheAsync($"{accountId}_{scene}", success, message, reason);
  133. }
  134. private static async Task savePromotionAccountCacheAsync(string accountName, bool success, string message, string reason)
  135. {
  136. await SaveStatsAccountCacheAsync("promotion_total", accountName, success, message, reason);
  137. }
  138. }
  139. }