ElePoolCore.cs 3.7 KB

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