CloudGamingAdmin/admin-server/CloudGaming.Shared.Admin/ApplicationServices/ApplicationGameService.cs
2024-12-04 14:22:48 +08:00

82 lines
2.7 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CloudGaming.Shared.Admin.ApplicationServices
{
/// <summary>
/// 游戏抽象类
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TSearchDto"></typeparam>
/// <typeparam name="TSaveFormDto"></typeparam>
/// <param name="serviceProvider"></param>
public abstract class ApplicationGameService<TEntity, TKey, TSearchDto, TSaveFormDto>(IServiceProvider serviceProvider) : ApplicationService<TEntity, TKey, TSearchDto, TSaveFormDto>(serviceProvider)
where TEntity : class, new()
where TSearchDto : class, new()
where TSaveFormDto : class, new()
{
/// <summary>
/// 查询表单数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override async Task<Dictionary<string, object?>> FindFormAsync(TKey id)
{
var res = new Dictionary<string, object?>();
var form = await Repository.FindByIdAsync(id);
form = form.NullSafe();
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" 开头 Create creat
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;
}
}
}