MeituanPoolCore.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using dodohold.core;
  2. namespace molilian.core
  3. {
  4. public partial class MeituanPoolCore
  5. {
  6. private static string _end_point;
  7. static MeituanPoolCore()
  8. {
  9. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  10. }
  11. private static readonly object _lockObj = new();
  12. private static IEnumerable<MeituanPoolDTO> _cached;
  13. public static MeituanPoolDTO? GetOne()
  14. {
  15. var list = List();
  16. if (!list.Any()) return null;
  17. return list.Where(IsNotExceedDailyIncomeLimit).OrderBy(l => Guid.NewGuid()).FirstOrDefault();
  18. }
  19. public static MeituanPoolDTO? GetOne(int id)
  20. {
  21. var list = List();
  22. if (!list.Any()) return null;
  23. return list.Where(e => e.id == id && IsNotExceedDailyIncomeLimit(e)).FirstOrDefault();
  24. }
  25. private static bool IsNotExceedDailyIncomeLimit(MeituanPoolDTO item)
  26. {
  27. if (!string.IsNullOrEmpty(_end_point) && !string.IsNullOrEmpty(item.end_point))
  28. {
  29. if (item.end_point != _end_point) return false;
  30. }
  31. // 使用初始化参数创建工作时间表
  32. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  33. if (item.daily_calls_limit > 0)
  34. {
  35. int daily_num = RiskControlCore.GetCalls(TkChannelEnum.meituan, item.id, DateTime.Now.ToString("yyyyMMdd"));
  36. if (daily_num >= item.daily_calls_limit) return false;
  37. }
  38. if (item.hourly_calls_limit > 0)
  39. {
  40. int hourly_num = RiskControlCore.GetCalls(TkChannelEnum.meituan, item.id, DateTime.Now.ToString("yyyyMMddHH"));
  41. if (hourly_num >= item.hourly_calls_limit) return false;
  42. }
  43. return true;
  44. }
  45. public static IEnumerable<MeituanPoolDTO> List(bool force = false)
  46. {
  47. if (!force && _cached != null) return _cached;
  48. string cache_key = $"cache:meituan_pool";
  49. var list = RedisHelper.Get<IEnumerable<MeituanPoolDTO>>(cache_key);
  50. if (force || list == null)
  51. {
  52. lock (_lockObj)
  53. {
  54. list = new DBContext.Table("meituan_pool")
  55. .Where("status=@status", new { status = 1 })
  56. .Select<MeituanPoolDTO>();
  57. if (list == null) return default;
  58. foreach (var item in list)
  59. {
  60. RiskControlCore.SetCallsAsync(TkChannelEnum.meituan, item.id, DateTime.Now.ToString("yyyyMMddHH"), item.current_hourly_calls);
  61. RiskControlCore.SetCallsAsync(TkChannelEnum.meituan, item.id, DateTime.Now.ToString("yyyyMMdd"), item.current_daily_calls);
  62. }
  63. RedisHelper.Set(cache_key, list, 30 * 86400);
  64. }
  65. }
  66. _cached = list;
  67. return list;
  68. }
  69. public static void Refresh()
  70. {
  71. _ = List(true);
  72. }
  73. internal static void AccountExhausted()
  74. {
  75. string cache_key = $"cache:tk_pool:account:exhausted";
  76. long count = RedisHelper.IncrBy(cache_key);
  77. if (count > 1) return;
  78. RedisHelper.Expire(cache_key, 3600);
  79. NotifyCore.Notify(new NifyMessage
  80. {
  81. message = $"【美团联盟】没有匹配账号",
  82. priority = NifyMessagePriority.high,
  83. tags = ["red_circle"]
  84. });
  85. _ = NotifyCore.QYWeixinPushNotifyAsync("没账号", $"【美团联盟】没有匹配账号");
  86. }
  87. }
  88. }