223 lines
7.2 KiB
C#
223 lines
7.2 KiB
C#
using MiaoYu.Repository.ChatAI.Admin.Entities;
|
|
using MiaoYu.Repository.ChatAI.Admin.Entities.Apps;
|
|
|
|
using NPOI.SS.Formula.Functions;
|
|
|
|
using TencentCloud.Tci.V20190318.Models;
|
|
namespace MiaoYu.Api.Admin.ApplicationServices.Apps;
|
|
|
|
/// <summary>
|
|
/// 人物表 服务 T_CharacterService
|
|
/// </summary>
|
|
public class T_CharacterService(
|
|
IRepository<T_Character> defaultRepository,
|
|
IRepository<T_Character_Label> T_Character_Label,
|
|
IRepository<T_Character_Label_Relation> T_Character_Label_Relation,
|
|
IRepository<T_Character_Personality> T_Character_Personality,
|
|
IRepository<T_Character_Personality_Relation> T_Character_Personality_Relation,
|
|
IRepository<T_Character_Type> T_Character_Type,
|
|
IRepository<T_Character_Type_Intimacy> T_Character_Type_Intimacy,
|
|
IRepository<T_Image_Config> imageRepository
|
|
)
|
|
: ApplicationService<IRepository<T_Character>>(defaultRepository)
|
|
{
|
|
//public T_CharacterService()
|
|
// : base(defaultRepository)
|
|
//{
|
|
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 获取列表数据
|
|
/// </summary>
|
|
/// <param name="pagingSearchInput"></param>
|
|
/// <returns></returns>
|
|
public async Task<PagingView> FindListAsync(PagingSearchInput<T_Character> pagingSearchInput)
|
|
{
|
|
var query = this._defaultRepository.Select
|
|
|
|
//项目
|
|
.WhereIf(pagingSearchInput.Search?.TenantId != null,
|
|
w => w.TenantId == pagingSearchInput.Search.TenantId)
|
|
.WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search?.Name),
|
|
w => w.Name.Contains(pagingSearchInput.Search.Name ?? ""))
|
|
|
|
|
|
.OrderByDescending(w => w.Id)
|
|
.Select(w => new
|
|
{
|
|
w.Id,
|
|
w.Name,
|
|
w.Biography,
|
|
w.TenantId,
|
|
w.Prologue,
|
|
w.ModelConfigId,
|
|
w.Visibility,
|
|
w.CreateTime,
|
|
w.UpdateTime,
|
|
w.Gender,
|
|
w.System,
|
|
w.BgImg,
|
|
w.IconImg,
|
|
//标签
|
|
Lables = T_Character_Label.Select.Where(l =>
|
|
T_Character_Label_Relation.Select.Any(ll => ll.CharacterId == w.Id && ll.CharacterLabelId == l.Id)
|
|
).Select(l => new
|
|
{
|
|
Name = l.LabelName,
|
|
Code = l.LabelValue ?? l.LabelName,
|
|
Value = l.Id
|
|
}).ToList(),
|
|
//分类
|
|
Types =
|
|
T_Character_Type.Select.Where(t =>
|
|
T_Character_Type_Intimacy.Select.Any(tt => tt.CharacterId == w.Id && tt.TypeId == t.Id)
|
|
)
|
|
.Select(t => new
|
|
{
|
|
Name = t.Name,
|
|
Code = t.Name,
|
|
Value = t.Id
|
|
}).ToList(),
|
|
//性格
|
|
Personas =
|
|
T_Character_Personality.Select.Where(p =>
|
|
T_Character_Personality_Relation.Select.Any(pp => pp.CharacterId == w.Id && pp.PersonalityId == p.Id)
|
|
)
|
|
.Select(p => new
|
|
{
|
|
Name = p.Name,
|
|
Code = p.Value ?? p.Name,
|
|
Value = p.Id
|
|
}).ToList(),
|
|
|
|
})
|
|
;
|
|
|
|
var result = await _defaultRepository.AsPagingViewAsync(query, pagingSearchInput);
|
|
var imageIds = new List<int>();
|
|
var characteIds = new List<int>();
|
|
|
|
result.DataSource.ForEach(it =>
|
|
{
|
|
characteIds.Add(int.Parse(it["Id"]?.ToString() ?? "0"));
|
|
if (it.TryGetValue("BgImg", out var bgImg))
|
|
{
|
|
if (int.TryParse(bgImg.ToString(), out var t))
|
|
{
|
|
imageIds.Add(t);
|
|
}
|
|
}
|
|
if (it.TryGetValue("IconImg", out var IconImg))
|
|
{
|
|
if (int.TryParse(IconImg.ToString(), out var t))
|
|
{
|
|
imageIds.Add(t);
|
|
}
|
|
}
|
|
|
|
});
|
|
var imageList = imageRepository.GetAllList(it => imageIds.Contains(it.ImageId));
|
|
|
|
|
|
result.DataSource.ForEach(it =>
|
|
{
|
|
setImageId(it, imageList, "BgImg", "BgImgUrl");
|
|
setImageId(it, imageList, "IconImg", "IconImgUrl");
|
|
});
|
|
return result;
|
|
}
|
|
|
|
private void setImageId(Dictionary<string, object?> it, List<T_Image_Config> imageList, string key, string keyurl)
|
|
{
|
|
it.TryAdd(keyurl, "");
|
|
if (it.TryGetValue(key, out var bgImg))
|
|
{
|
|
if (int.TryParse(bgImg.ToString(), out var t))
|
|
{
|
|
var img = imageList.FirstOrDefault(it => it.ImageId == t);
|
|
if (img != null)
|
|
{
|
|
it[keyurl] = img.Url ?? "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据id数组删除
|
|
/// </summary>
|
|
/// <param name="ids">ids</param>
|
|
/// <returns></returns>
|
|
public async Task DeleteListAsync(List<int> ids)
|
|
{
|
|
await this._defaultRepository.DeleteByIdsAsync(ids);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查询表单数据
|
|
/// </summary>
|
|
/// <param name="id">id</param>
|
|
/// <returns></returns>
|
|
public async Task<Dictionary<string, object>> FindFormAsync(int id)
|
|
{
|
|
var res = new Dictionary<string, object>();
|
|
var form = await this._defaultRepository.FindByIdAsync(id);
|
|
form = form.NullSafe();
|
|
//if (form.CreateTime == null || form.CreateTime == DateTime.MinValue)
|
|
//{
|
|
// form.CreateTime = DateTime.Now;
|
|
//}
|
|
//if (form.UpdateTime == null || form.UpdateTime == DateTime.MinValue)
|
|
//{
|
|
// form.UpdateTime = DateTime.Now;
|
|
//}
|
|
res[nameof(id)] = id;
|
|
res[nameof(form)] = form;
|
|
return res;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存数据
|
|
/// </summary>
|
|
/// <param name="form">form</param>
|
|
/// <returns></returns>
|
|
public Task SaveFormAsync(T_Character form)
|
|
{
|
|
return this._defaultRepository.InsertOrUpdateAsync(form);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 导出Excel
|
|
/// </summary>
|
|
/// <param name="pagingSearchInput"></param>
|
|
/// <returns></returns>
|
|
public async Task<byte[]> ExportExcelAsync(PagingSearchInput<T_Character> pagingSearchInput)
|
|
{
|
|
pagingSearchInput.Page = -1;
|
|
var tableViewModel = await this.FindListAsync(pagingSearchInput);
|
|
return ExcelUtil.ExportExcelByPagingView(tableViewModel, null, "Id");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改状态
|
|
/// </summary>
|
|
/// <param name="state"></param>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
|
|
public async Task<bool> UpdateState(bool state, int id)
|
|
{
|
|
var form = await this._defaultRepository.FindByIdAsync(id);
|
|
if (form == null)
|
|
{
|
|
return false;
|
|
}
|
|
form.Visibility = state;
|
|
await SaveFormAsync(form);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
} |