using CloudGaming.Shared.Admin.ApplicationServices;
using HZY.Framework.Core.Utils;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace CloudGaming.Code.DataBaseModel.ApplicationServices
{
///
/// 游戏抽象类
///
///
///
///
///
///
public abstract class ApplicationUserService(IServiceProvider serviceProvider) : ApplicationService(serviceProvider)
where TEntity : class, new()
where TSearchDto : class, new()
where TSaveFormDto : class, new()
{
///
/// 查询表单数据
///
///
///
public override async Task> FindFormAsync(TKey id)
{
var res = new Dictionary();
var form = await Repository.FindByIdAsync(id);
form = form.NullSafe();
if (form is DefaultGameAppEntity entity)
{
if (entity.TenantId == Guid.Empty)
{
var app = serviceProvider.GetRequiredService();
entity.TenantId = app.TenantId;
}
}
if (id is Guid newId)
{
res[nameof(id)] = newId == Guid.Empty ? "" : newId;
}
else if (id is int newIntId)
{
res[nameof(id)] = newIntId == 0 ? "" : newIntId;
}
else if (id is long newLongId)
{
res[nameof(id)] = newLongId == 0 ? "" : newLongId;
}
else
{
res[nameof(id)] = id;
}
// 使用反射获取 form 对象的所有属性
if (form != null)
{
var formType = form.GetType();
var properties = formType.GetProperties();
foreach (var property in properties)
{
// 判断属性类型是否为 DateTime,且名称是否以 "Create" 开头
if (property.PropertyType == typeof(DateTime) && (property.Name.StartsWith("Create") || property.Name.StartsWith("Update")))
{
// 获取属性的值
var value = property.GetValue(form);
// 如果值为 null 或默认值,则重新赋值当前时间
if (value == null || (DateTime)value == default(DateTime))
{
property.SetValue(form, DateTime.Now);
}
}
}
}
res[nameof(form)] = form;
return res;
}
}
}