using CloudGaming.AppConfigModel; using CloudGaming.Core.AgileConfig; using CloudGaming.Core.AgileConfig.AgileConfigApi; using CloudGaming.Core.AgileConfig.AgileConfigClient; using CloudGaming.Repository.Game.Entities.Ext; using IdGen; using Newtonsoft.Json; using Refit; using AppConfig = CloudGaming.Code.DataBaseModel.AppConfig; namespace CloudGaming.Api.Admin.ApplicationServices.Apps.Ext; /// /// 服务 AppConfigService /// public class AppConfigService(IServiceProvider serviceProvider) : ApplicationService(serviceProvider)// ApplicationGameService(serviceProvider) { [Autowired] public virtual AppConfigTenant AppConfigTenant { get; set; } [AppSettings("AgileConfig")] public virtual AgileConfigVM AgileConfig { get; set; } [Autowired] public virtual IAccountService AccountService { get; set; } [Autowired] public virtual AppConfig appConfig { get; set; } /// /// 获取列表数据 /// /// /// public async Task FindListAsync(PagingSearchInput pagingSearchInput) { var query = AppConfigTenant.Tenants //名称 .WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search?.Name), w => w.Name.Contains(pagingSearchInput.Search.Name ?? "")) .OrderByDescending(w => w.Name) .AsQueryable() ; var result = await RepositoryBaseExtensions.AsPagingViewAsync(query, pagingSearchInput); result.Size = pagingSearchInput.Size; result.PageCount = 1; result.Page = pagingSearchInput.Page; result.Total = AppConfigTenant.Tenants.Count; return result; } /// /// 根据id数组删除 /// /// /// public async Task DeleteListAsync(List ids) { bool isDelete = false; if (ids != null && ids.Count > 0) { foreach (var id in ids) { if (Guid.Empty != id) { var form = AppConfigTenant.Tenants.FirstOrDefault(it => it.TenantId == id); if (form != null) { AppConfigTenant.Tenants.Remove(form); isDelete = true; } } } } if (isDelete) { await SynchronizeData(); } return; } /// /// 查询表单数据 /// /// /// public async Task> FindFormAsync(Guid? id) { var res = new Dictionary(); AppConfig? form = null; if (id == null || id == Guid.Empty) { form = appConfig; } else if (id != Guid.Empty && AppConfigTenant != null && AppConfigTenant.Tenants.Count > 0) { form = AppConfigTenant.Tenants.FirstOrDefault(it => it.TenantId == id); } form = form.NullSafe(); if (id is Guid newId) { res[nameof(id)] = newId == Guid.Empty ? "" : newId; } else { res[nameof(id)] = id; } res[nameof(form)] = form; return res; } /// /// 保存数据 /// /// /// public async Task SaveFormAsync(AppConfig form) { if (form.TenantId == Guid.Empty) { form.TenantId = Guid.NewGuid(); } if (form.Payment == null) { form.Payment = new PaymentModel() { AlipayConfig = new AliPayConfig(), WeChatConfig = new WeChatConfig() }; } if (AppConfigTenant == null) { AppConfigTenant = new AppConfigTenant(); } if (AppConfigTenant.Tenants == null) { AppConfigTenant.Tenants = new List(); } var t = AppConfigTenant.Tenants.FirstOrDefault(it => it.TenantId == form.TenantId); if (t != null) { form.CopyTo(t); } else { AppConfigTenant.Tenants.Add(form); } await SynchronizeData(); } private async Task SynchronizeData() { var agileConfigServer = ServiceProvider.GetService(); var agileConfigApiVM = new CloudGaming.Core.AgileConfig.AgileConfigApi.AgileConfigApiVM() { IsPatch = false, Json = JsonConvert.SerializeObject(AppConfigTenant) }; if (agileConfigServer != null) { //var model = await agileConfigServer.AgileConfigApi.GetJson(AgileConfig.AppId, AgileConfig.Env); var s = await agileConfigServer.AgileConfigApi.SaveJson(AgileConfig.AppId, AgileConfig.Env, agileConfigApiVM); if (s.Success) { var model = new AgileConfigPublishVM() { log = $"{AccountService.AccountContext.Name}修改配置,{DateTime.Now}自动发布", appId = AgileConfig.AppId }; await agileConfigServer.AgileConfigApi.Publish(AgileConfig.Env, model); } } } }