coupon.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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_coupon_key = "queue:coupon_logs";
  9. public static async Task<int> InsertCouponLogAsync(int limit, YunhuiKit.RedisClient 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 = await redis.LPopAsync<UnionCouponDTO>(queue_coupon_key);
  20. if (data == null) break;
  21. if (_test_oaid.Equals(data.oaid) ||
  22. data.ip.StartsWith("127.0.0"))
  23. {
  24. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  25. connection.Insert(test_data);
  26. }
  27. else
  28. {
  29. connection.Insert(data);
  30. if (data.success)
  31. {
  32. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  33. connection.Insert(success_data);
  34. }
  35. }
  36. total++;
  37. }
  38. transaction.Commit();
  39. }
  40. catch (Exception ex)
  41. {
  42. transaction.Rollback();
  43. new LoggerLibrary("TkLogCore_error", "task_insert_parse_coupon_logs")
  44. .Info(ex.Message, ex.StackTrace)
  45. .SaveAsync();
  46. throw;
  47. }
  48. finally
  49. {
  50. connection.Close();
  51. }
  52. return total;
  53. }
  54. public static int task_insert_parse_coupon_logs(int limit, CSRedisClient redis)
  55. {
  56. int total = 0;
  57. using var connection = DBContext.GetOpenConnection();
  58. connection.Open();
  59. using var transaction = connection.BeginTransaction();
  60. try
  61. {
  62. for (int i = 0; i < limit; i++)
  63. {
  64. var data = redis.LPop<UnionCouponDTO>(queue_coupon_key);
  65. if (data == null) break;
  66. if (_test_oaid.Equals(data.oaid) ||
  67. data.ip.StartsWith("127.0.0"))
  68. {
  69. var test_data = data.Convert2Json().Convert2Object<TestUnionCouponDTO>();
  70. connection.Insert(test_data);
  71. }
  72. else
  73. {
  74. connection.Insert(data);
  75. if (data.success)
  76. {
  77. var success_data = data.Convert2Json().Convert2Object<SuccessUnionCouponDTO>();
  78. connection.Insert(success_data);
  79. }
  80. }
  81. total++;
  82. }
  83. transaction.Commit();
  84. }
  85. catch (Exception ex)
  86. {
  87. transaction.Rollback();
  88. new LoggerLibrary("TkLogCore_error", "task_insert_parse_coupon_logs")
  89. .Info(ex.Message, ex.StackTrace)
  90. .SaveAsync();
  91. throw;
  92. }
  93. finally
  94. {
  95. connection.Close();
  96. }
  97. return total;
  98. }
  99. public static async Task CouponLogAsync(UnionCouponDTO response, AlimamaPlus? alimamaPlus = null)
  100. {
  101. try
  102. {
  103. var ts = DateTime.Now - response.create_time;
  104. response.elapsedTime = (int)ts.TotalMilliseconds;
  105. _ = RedisHelper.RPushAsync(queue_coupon_key, response);
  106. if (!response.ip.StartsWith("127.0.0"))
  107. {
  108. saveCouponCache(response.channel.ToString(), response.accountId, response.accountName, response.success, response.message, response.reason);
  109. }
  110. if (response.success) await saveClientRequestTotalAsync(response.channel, response.ip, response.oaid);
  111. if (!response.success && "nologin".Equals(response.message))
  112. {
  113. switch (response.channel)
  114. {
  115. case TkChannelEnum.tb:
  116. if (alimamaPlus != null)
  117. {
  118. (bool success, string message) = alimamaPlus.RenewCookie();
  119. if (success) return;
  120. }
  121. TkPoolCore.Disabled(response.accountId, response.accountName, $"{response.rawContent}");
  122. break;
  123. }
  124. }
  125. }
  126. catch (Exception ex)
  127. {
  128. _ = new LoggerLibrary("unionCoupon", "database_error")
  129. .Info(response.rawContent)
  130. .Info(response.Convert2Json())
  131. .Info(ex.Message, ex.StackTrace)
  132. .SaveAsync();
  133. }
  134. }
  135. private static void saveCouponCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  136. {
  137. saveAccountCouponCache("all", success, message, reason);
  138. saveAccountCouponCache($"{channel}", success, message, reason);
  139. if (accountId != 0)
  140. {
  141. saveAccountCouponCache($"{channel}_{accountId}", success, message, reason);
  142. }
  143. }
  144. private static void saveAccountCouponCache(string accountName, bool success, string message, string reason)
  145. {
  146. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMM}");
  147. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  148. RedisHelper.IncrBy($":coupon_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  149. string result = success ? "success" : "fail";
  150. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  151. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  152. RedisHelper.IncrBy($":coupon_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  153. if (!string.IsNullOrEmpty(message))
  154. {
  155. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  156. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  157. RedisHelper.SAdd($":coupon_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  158. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  159. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  160. RedisHelper.IncrBy($":coupon_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  161. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  162. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  163. RedisHelper.IncrBy($":coupon_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  164. }
  165. if (!string.IsNullOrEmpty(reason))
  166. {
  167. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  168. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  169. RedisHelper.SAdd($":coupon_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  170. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  171. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  172. RedisHelper.IncrBy($":coupon_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  173. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  174. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  175. RedisHelper.IncrBy($":coupon_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  176. }
  177. }
  178. }
  179. }