using MiaoYu.Repository.Admin.Entities.LowCode;
using MiaoYu.Repository.ChatAI.Admin.Entities;
namespace MiaoYu.Api.Admin.ApplicationServices.DevelopmentTools.LowCode.Impl;
///
/// 服务 Low_Code_TableService
///
public class LowCodeTableService : ApplicationService>
{
private readonly IRepository _lowCodeTableInfoRepository;
private readonly LowCodeTableInfoService _lowCodeTableInfoService;
private readonly IDatabaseTableService _databaseTableService;
private readonly ICodeGenerationService _codeGenerationService;
public LowCodeTableService(
IRepository defaultRepository,
LowCodeTableInfoService lowCodeTableInfoService,
IRepository lowCodeTableInfoRepository,
IDatabaseTableService databaseTableService,
ICodeGenerationService codeGenerationService) : base(defaultRepository)
{
_lowCodeTableInfoService = lowCodeTableInfoService;
_lowCodeTableInfoRepository = lowCodeTableInfoRepository;
_databaseTableService = databaseTableService;
_codeGenerationService = codeGenerationService;
}
///
/// 获取列表数据
///
///
///
public async Task FindListAsync(PagingSearchInput pagingSearchInput)
{
var query = _defaultRepository.SelectNoTracking
.WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.TableName),
w => w.TableName.Contains(pagingSearchInput.Search.TableName))
.WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.EntityName),
w => w.EntityName.Contains(pagingSearchInput.Search.EntityName))
.WhereIf(!string.IsNullOrWhiteSpace(pagingSearchInput.Search.DisplayName),
w => w.DisplayName.Contains(pagingSearchInput.Search.DisplayName))
.OrderByDescending(w => w.CreationTime)
.ThenByDescending(w => w.LastModificationTime)
.Select(w => new
{
w.Id,
w.TableName,
w.DisplayName,
w.EntityName,
w.Remark,
w.LastModificationTime,
w.CreationTime,
w.DataBase,
})
;
//获取一下数据用于缓存
_databaseTableService.GetAllTables();
var result = await _defaultRepository.AsPagingViewAsync(query, pagingSearchInput);
//覆盖值
result
.FormatValue(query, w => w.CreationTime, (oldValue) => oldValue.ToString("yyyy-MM-dd"))
.FormatValue(query, w => w.LastModificationTime, (oldValue) => oldValue?.ToString("yyyy-MM-dd"))
;
return result;
}
///
/// 根据id数组删除
///
///
///
public async Task DeleteListAsync(List ids)
{
_databaseTableService.ClearAllTablesByCache();
//删除子表
//await this._lowCodeTableInfoRepository.DeleteBulkAsync(w => ids.Contains(w.Low_Code_TableId));
await _lowCodeTableInfoRepository.DeleteAsync(w => ids.Contains(w.Low_Code_TableId));
//删除主表
await _defaultRepository.DeleteByIdsAsync(ids);
}
///
/// 同步表
///
public async Task SynchronizationAsync()
{
var allTables = _databaseTableService.GetAllTableInfos();
var oldAllTables = await _defaultRepository.ToListAllAsync();
#region 同步表
var insertList = new List();
var updateList = new List();
var ids = new List();
foreach (var item in allTables)
{
var table = oldAllTables.Find(w => w.TableName == item.Name);
var id = Guid.NewGuid();
if (table == null)
{
var lowCodeTable = new LowCodeTable
{
Id = id,
DisplayName = item.Comment,
TableName = item.Name,
EntityName = item.Name.ToLineConvertHump()
};
if (item.Schema.Contains(".MiaoYuChat"))
{
item.Schema = item.Schema.Replace(".MiaoYuChat", "");
lowCodeTable.Schema = item.Schema;
lowCodeTable.DataBase = "MiaoYuChat";
lowCodeTable.EntityName = item.Name;
}
insertList.Add(lowCodeTable);
}
else
{
id = table.Id;
if (item.Schema.Contains(".MiaoYuChat"))
{
item.Schema = item.Schema.Replace(".MiaoYuChat", "");
table.Schema = item.Schema;
table.DataBase = "MiaoYuChat";
table.EntityName = item.Name;
}
updateList.Add(table);
}
ids.Add(id);
}
if (insertList.Count > 0)
{
await _defaultRepository.InsertRangeAsync(insertList);
}
if (updateList.Count > 0)
{
await _defaultRepository.UpdateRangeAsync(updateList);
}
foreach (var item in ids)
{
await _lowCodeTableInfoService.SynchronizationColumnByTableIdAsync(item, true);
}
_databaseTableService.ClearAllTablesByCache();
#endregion
}
///
/// 变更数据
///
///
///
public async Task ChangeAsync(List lowCodeTables)
{
_databaseTableService.ClearAllTablesByCache();
var oldLowCodeTables =
await _defaultRepository.ToListAsync(w => lowCodeTables.Select(w => w.Id).Contains(w.Id));
var updateList = new List();
foreach (var item in lowCodeTables)
{
var lowCodeTable = oldLowCodeTables.Find(w => w.Id == item.Id);
lowCodeTable.DisplayName = item.DisplayName;
lowCodeTable.EntityName = item.EntityName;
lowCodeTable.Remark = item.Remark;
updateList.Add(lowCodeTable);
}
await _defaultRepository.UpdateRangeAsync(updateList);
}
///
/// 查询表单数据
///
///
///
public async Task> FindFormAsync(Guid id)
{
var res = new Dictionary();
var form = (await _defaultRepository.FindByIdAsync(id)).NullSafe();
_codeGenerationService.FillPathByLowCodeTable(form);
res[nameof(id)] = id == Guid.Empty ? "" : id;
res[nameof(form)] = form;
res["menu"] = $"views/apps/{form.TableName}s/Index.vue";
res["router"] = $"/apps/{form.TableName.ToLower()}s";
return res;
}
///
/// 保存数据
///
///
///
[Transactional]
public virtual async Task SaveFormAsync(LowCodeTable form)
{
await _defaultRepository.InsertOrUpdateAsync(form);
}
}