183 lines
5.4 KiB
C#
183 lines
5.4 KiB
C#
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;
|
|
|
|
/// <summary>
|
|
/// 服务 AppConfigService
|
|
/// </summary>
|
|
public class AppConfigService(IServiceProvider serviceProvider)
|
|
: ApplicationService(serviceProvider)// ApplicationGameService<AppConfig, Guid, AppConfig, AppConfig>(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; }
|
|
/// <summary>
|
|
/// 获取列表数据
|
|
/// </summary>
|
|
/// <param name="pagingSearchInput"></param>
|
|
/// <returns></returns>
|
|
public async Task<PagingView> FindListAsync(PagingSearchInput<AppConfig> 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<AppConfig, AppConfig, AppConfig>(query, pagingSearchInput);
|
|
result.Size = pagingSearchInput.Size;
|
|
result.PageCount = 1;
|
|
result.Page = pagingSearchInput.Page;
|
|
result.Total = AppConfigTenant.Tenants.Count;
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 根据id数组删除
|
|
/// </summary>
|
|
/// <param name="ids"></param>
|
|
/// <returns></returns>
|
|
public async Task DeleteListAsync(List<Guid> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询表单数据
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object?>> FindFormAsync(Guid? id)
|
|
{
|
|
|
|
var res = new Dictionary<string, object?>();
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="form"></param>
|
|
/// <returns></returns>
|
|
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<AppConfig>();
|
|
}
|
|
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<AgileConfigServer>();
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
} |