108 lines
3.7 KiB
C#
108 lines
3.7 KiB
C#
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
|
||
{
|
||
/// <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 ApplicationUserService<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 (form is DefaultGameAppEntity entity)
|
||
{
|
||
if (entity.TenantId == Guid.Empty)
|
||
{
|
||
var app = serviceProvider.GetRequiredService<AppConfig>();
|
||
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);
|
||
}
|
||
}
|
||
if (property.PropertyType == typeof(DateTime) && property.Name.StartsWith("Expiration"))
|
||
{
|
||
// 获取属性的值
|
||
var value = property.GetValue(form);
|
||
|
||
// 如果值为 null 或默认值,则重新赋值当前时间
|
||
if (value == null || (DateTime)value == default(DateTime))
|
||
{
|
||
property.SetValue(form, DateTime.Now.Date);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
res[nameof(form)] = form;
|
||
return res;
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
|