using dodohold.core; using Dataoke; using Tea; using AlibabaCloud.SDK.BssOpenApi20171214.Models; using AlibabaCloud.SDK.Ecs20140526.Models; using AlibabaCloud.SDK.Vpc20160428.Models; using TencentCloud.Dbbrain.V20210527.Models; using Org.BouncyCastle.Bcpg.OpenPgp; using TencentCloud.Tke.V20180525.Models; using static molilian.core.TkPoolCore; using ZstdSharp.Unsafe; namespace molilian.core { public partial class AliyunPoolCore { private static readonly object _lockObj = new(); private static IEnumerable _cached; public static AliyunPoolDTO? GetOne(int id) { var list = List(); if (!list.Any()) return null; return list.Where(e => e.id == id).FirstOrDefault(); } public static IEnumerable List(bool force = false) { if (!force && _cached != null) return _cached; string cache_key = $"cache:aliyun_pool"; var list = RedisHelper.Get>(cache_key); if (force || list == null) { lock (_lockObj) { list = new DBContext.Table("aliyun_pool") .Where("status=@status", new { status = 1 }) .Select(); list = new DBContext.Table("aliyun_pool") .Where("status=@status", new { status = 1 }) .Select(); if (list == null) return default; RedisHelper.Set(cache_key, list, 30 * 86400); } } _cached = list; return list; } public static void Refresh() { _ = List(true); } public static void UpdateDrawBalance(string name, decimal amout) { new DBContext.Table("aliyun_pool") .Add("balance", amout) .Add("last_time", DateTime.Now) .Where("name=@name", new { name }) .Update(); _ = List(true); } public void GetBlances() { var list = new DBContext.Table("aliyun_pool").Where(string.Empty, new { }).Select(); foreach (var item in list) { if (string.IsNullOrEmpty(item.accessKeyId)) continue; decimal amout = 0; try { AliyunCore core = new AliyunCore(item.id); var response = core.QueryAccountBalance(); var balance = response.Body.Data.AvailableAmount; if (decimal.TryParse(balance, out amout)) { UpdateDrawBalance(item.name, amout); if (item.balance != amout && amout <= item.balance_alert_threshold) { string message = $"【{item.company}】阿里云当前余额:{amout} 元"; NotifyCore.AnPushNotify("余额预警", message); NotifyCore.Notify(new NifyMessage { message = message, priority = NifyMessagePriority.high, tags = ["red_circle"] }); } } } catch (Exception ex) { new LoggerLibrary("aliyun", "balance_error") .Info($"Account:{item.id}\tbalance:{amout}") .Info(ex.Message, ex.StackTrace) .SaveAsync(); } } } } }