CpsPoolCore.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using dodohold.core;
  2. using System.Threading.Channels;
  3. namespace molilian.core
  4. {
  5. public partial class CpsPoolCore
  6. {
  7. private static string _end_point;
  8. private static Dictionary<string, decimal> _incomeAmt = new();
  9. private static Dictionary<string, int> _calls = new();
  10. static CpsPoolCore()
  11. {
  12. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  13. }
  14. private static readonly object _lockObj = new();
  15. private static IEnumerable<CpsLinksDTO> _links_cached;
  16. private static IEnumerable<CpsPoolDTO> _cached;
  17. public static CpsLinksDTO? GetOne(string channel)
  18. {
  19. var list = List();
  20. if (!list.Any()) return null;
  21. var pool = list.Where(e => e.channel_code == channel && e.status).FirstOrDefault();
  22. if (pool == null) return null;
  23. var links = LinksList();
  24. if (!links.Any()) return null;
  25. return links.Where(e => IsEligible(e, channel)).OrderByDescending(e => e.priority_weight).FirstOrDefault();
  26. }
  27. private static bool IsEligible(CpsLinksDTO item, string channel)
  28. {
  29. if (item.channel_code != channel) return false;
  30. // 使用初始化参数创建工作时间表
  31. if (!new WorkSchedule(item.time_range).IsWorkHour()) return false;
  32. var now = DateTime.Now;
  33. if (item.start_time > now || now > item.end_time) return false;
  34. return true;
  35. }
  36. public static IEnumerable<CpsPoolDTO> List(bool force = false)
  37. {
  38. #if DEBUG
  39. return new DBContext.Table("cps_pool")
  40. .Where("status=@status", new { status = 1 })
  41. .Select<CpsPoolDTO>();
  42. #else
  43. if (!force && _cached != null) return _cached;
  44. string cache_key = $"cache:cps_pool";
  45. var list = RedisHelper.Get<IEnumerable<CpsPoolDTO>>(cache_key);
  46. if (force || list == null)
  47. {
  48. lock (_lockObj)
  49. {
  50. list = new DBContext.Table("cps_pool")
  51. .Where("status=@status", new { status = 1 })
  52. .Select<CpsPoolDTO>();
  53. if (list == null) return default;
  54. RedisHelper.Set(cache_key, list, 30 * 86400);
  55. }
  56. }
  57. _cached = list;
  58. return list;
  59. #endif
  60. }
  61. public static IEnumerable<CpsLinksDTO> LinksList(bool force = false)
  62. {
  63. #if DEBUG
  64. return new DBContext.Table("cps_links")
  65. .Where("status=@status", new { status = 1 })
  66. .Select<CpsLinksDTO>();
  67. #else
  68. if (!force && _links_cached != null) return _links_cached;
  69. string cache_key = $"cache:cps_links";
  70. var list = RedisHelper.Get<IEnumerable<CpsLinksDTO>>(cache_key);
  71. if (force || list == null)
  72. {
  73. lock (_lockObj)
  74. {
  75. list = new DBContext.Table("cps_links")
  76. .Where("status=@status", new { status = 1 })
  77. .Select<CpsLinksDTO>();
  78. if (list == null) return default;
  79. RedisHelper.Set(cache_key, list, 30 * 86400);
  80. }
  81. }
  82. _links_cached = list;
  83. return list;
  84. #endif
  85. }
  86. public static void Refresh()
  87. {
  88. _ = List(true);
  89. _ = LinksList(true);
  90. }
  91. internal static void AccountExhausted()
  92. {
  93. string cache_key = $"cache:tk_pool:account:exhausted";
  94. long count = RedisHelper.IncrBy(cache_key);
  95. if (count > 1) return;
  96. RedisHelper.Expire(cache_key, 3600);
  97. NotifyCore.Notify(new NifyMessage
  98. {
  99. message = $"【CPS优惠券】没有匹配账号",
  100. priority = NifyMessagePriority.high,
  101. tags = ["red_circle"]
  102. });
  103. NotifyCore.AnPushNotify("没账号", $"【CPS优惠券】没有匹配账号");
  104. }
  105. }
  106. }