AliyunPoolCore.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using dodohold.core;
  2. using Dataoke;
  3. using Tea;
  4. using AlibabaCloud.SDK.BssOpenApi20171214.Models;
  5. using AlibabaCloud.SDK.Ecs20140526.Models;
  6. using AlibabaCloud.SDK.Vpc20160428.Models;
  7. using TencentCloud.Dbbrain.V20210527.Models;
  8. using Org.BouncyCastle.Bcpg.OpenPgp;
  9. using TencentCloud.Tke.V20180525.Models;
  10. using static molilian.core.TkPoolCore;
  11. using ZstdSharp.Unsafe;
  12. namespace molilian.core
  13. {
  14. public partial class AliyunPoolCore
  15. {
  16. private static readonly object _lockObj = new();
  17. private static IEnumerable<AliyunPoolDTO> _cached;
  18. public static AliyunPoolDTO? GetOne(int id)
  19. {
  20. var list = List();
  21. if (!list.Any()) return null;
  22. return list.Where(e => e.id == id).FirstOrDefault();
  23. }
  24. public static IEnumerable<AliyunPoolDTO> List(bool force = false)
  25. {
  26. if (!force && _cached != null) return _cached;
  27. string cache_key = $"cache:aliyun_pool";
  28. var list = RedisHelper.Get<IEnumerable<AliyunPoolDTO>>(cache_key);
  29. if (force || list == null)
  30. {
  31. lock (_lockObj)
  32. {
  33. list = new DBContext.Table("aliyun_pool")
  34. .Where("status=@status", new { status = 1 })
  35. .Select<AliyunPoolDTO>();
  36. list = new DBContext.Table("aliyun_pool")
  37. .Where("status=@status", new { status = 1 })
  38. .Select<AliyunPoolDTO>();
  39. if (list == null) return default;
  40. RedisHelper.Set(cache_key, list, 30 * 86400);
  41. }
  42. }
  43. _cached = list;
  44. return list;
  45. }
  46. public static void Refresh()
  47. {
  48. _ = List(true);
  49. }
  50. public static void UpdateDrawBalance(string name, decimal amout)
  51. {
  52. new DBContext.Table("aliyun_pool")
  53. .Add("balance", amout)
  54. .Add("last_time", DateTime.Now)
  55. .Where("name=@name", new { name })
  56. .Update();
  57. _ = List(true);
  58. }
  59. public void GetBlances()
  60. {
  61. var list = new DBContext.Table("aliyun_pool").Where(string.Empty, new { }).Select<AliyunPoolDTO>();
  62. foreach (var item in list)
  63. {
  64. if (string.IsNullOrEmpty(item.accessKeyId)) continue;
  65. decimal amout = 0;
  66. try
  67. {
  68. AliyunCore core = new AliyunCore(item.id);
  69. var response = core.QueryAccountBalance();
  70. var balance = response.Body.Data.AvailableAmount;
  71. if (decimal.TryParse(balance, out amout))
  72. {
  73. UpdateDrawBalance(item.name, amout);
  74. if (item.balance != amout && amout <= item.balance_alert_threshold)
  75. {
  76. string message = $"【{item.company}】阿里云当前余额:{amout} 元";
  77. NotifyCore.AnPushNotify("余额预警", message);
  78. NotifyCore.Notify(new NifyMessage
  79. {
  80. message = message,
  81. priority = NifyMessagePriority.high,
  82. tags = ["red_circle"]
  83. });
  84. }
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. new LoggerLibrary("aliyun", "balance_error")
  90. .Info($"Account:{item.id}\tbalance:{amout}")
  91. .Info(ex.Message, ex.StackTrace)
  92. .SaveAsync();
  93. }
  94. }
  95. }
  96. }
  97. }