erp-platform/api/src/Erp.Platform.DbMigrator/Seeds/SeederUtils.cs

212 lines
8.5 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Erp.Platform.Entities;
2025-11-26 11:04:31 +00:00
using Erp.Platform.Enums;
using Erp.Platform.ListForms;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories;
namespace Erp.Platform.Data.Seeds;
2025-11-26 11:04:31 +00:00
public class SeederUtils
{
2025-11-26 11:04:31 +00:00
private readonly IRepository<ListForm, Guid> _listFormRepository;
private readonly IRepository<ListFormField, Guid> _listFormFieldRepository;
public SeederUtils(
IRepository<ListForm, Guid> listFormRepository,
2025-11-26 11:04:31 +00:00
IRepository<ListFormField, Guid> listFormFieldRepository)
{
_listFormRepository = listFormRepository;
_listFormFieldRepository = listFormFieldRepository;
}
public static string GetDefaultFormCodes(string listFormCode) => "form-" + listFormCode;
public async Task CloneFormLayoutAsync(string listFormCode, string subFormsJson)
{
var newListFormCode = GetDefaultFormCodes(listFormCode);
var cloneForm = await CloneListFormWithFieldsAsync(listFormCode, newListFormCode);
if (cloneForm != null)
{
await SetSubFormsJsonAsync(cloneForm, subFormsJson);
}
2025-11-27 21:00:02 +00:00
else
{
Console.WriteLine($"ListForm with code {listFormCode} not found!");
}
2025-11-26 11:04:31 +00:00
}
public async Task<ListForm> CloneListFormWithFieldsAsync(string listFormCode, string newListFormCode, bool copyFormFields = true)
{
2025-11-26 11:04:31 +00:00
var listForm = await _listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
if (listForm == null)
{
2025-11-27 21:00:02 +00:00
Console.WriteLine($"ListForm with code {listFormCode} not found!");
2025-11-13 09:36:30 +00:00
return null;
}
2025-11-26 11:04:31 +00:00
var targetForm = await _listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == newListFormCode);
if (targetForm == null)
2025-11-13 09:36:30 +00:00
{
var insertForm = new ListForm
{
2025-11-26 11:04:31 +00:00
ListFormCode = newListFormCode,
SubFormsJson = listForm.SubFormsJson,
ListFormType = listForm.ListFormType,
IsSubForm = listForm.IsSubForm,
ShowNote = listForm.ShowNote,
LayoutJson = listForm.LayoutJson,
CultureName = listForm.CultureName,
Name = listForm.Name,
Title = listForm.Title,
DataSourceCode = listForm.DataSourceCode,
IsTenant = listForm.IsTenant,
IsBranch = listForm.IsBranch,
IsOrganizationUnit = listForm.IsOrganizationUnit,
Description = listForm.Description,
SelectCommandType = listForm.SelectCommandType,
SelectCommand = listForm.SelectCommand,
KeyFieldName = listForm.KeyFieldName,
KeyFieldDbSourceType = listForm.KeyFieldDbSourceType,
DefaultFilter = listForm.DefaultFilter,
SortMode = listForm.SortMode,
PermissionJson = listForm.PermissionJson,
DeleteCommand = listForm.DeleteCommand,
DeleteFieldsDefaultValueJson = listForm.DeleteFieldsDefaultValueJson,
EditingOptionJson = listForm.EditingOptionJson,
EditingFormJson = listForm.EditingFormJson,
InsertFieldsDefaultValueJson = listForm.InsertFieldsDefaultValueJson,
FormFieldsDefaultValueJson = listForm.FormFieldsDefaultValueJson,
CommandColumnJson = listForm.CommandColumnJson,
InsertServiceAddress = listForm.InsertServiceAddress,
UpdateServiceAddress = listForm.UpdateServiceAddress,
FilterRowJson = listForm.FilterRowJson,
HeaderFilterJson = listForm.HeaderFilterJson,
SearchPanelJson = listForm.SearchPanelJson,
GroupPanelJson = listForm.GroupPanelJson,
SelectionJson = listForm.SelectionJson,
ColumnOptionJson = listForm.ColumnOptionJson,
PagerOptionJson = listForm.PagerOptionJson,
};
// insert cloned form
2025-11-26 11:04:31 +00:00
targetForm = await _listFormRepository.InsertAsync(insertForm, autoSave: true);
}
2025-11-26 11:04:31 +00:00
if (copyFormFields)
{
2025-11-26 11:04:31 +00:00
var targetFormFields = await _listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode);
if (targetFormFields != null && targetFormFields.Count > 0)
{
2025-11-26 11:04:31 +00:00
var clonedFields = new List<ListFormField>();
foreach (var f in targetFormFields)
{
2025-11-26 11:04:31 +00:00
var newField = new ListFormField
{
ListFormCode = newListFormCode,
CultureName = f.CultureName,
SourceDbType = f.SourceDbType,
FieldName = f.FieldName,
Width = f.Width,
ListOrderNo = f.ListOrderNo,
Visible = f.Visible,
IsActive = f.IsActive,
IsDeleted = f.IsDeleted,
AllowSearch = f.AllowSearch,
LookupJson = f.LookupJson,
ColumnCustomizationJson = f.ColumnCustomizationJson,
PermissionJson = f.PermissionJson,
PivotSettingsJson = f.PivotSettingsJson,
ValidationRuleJson = f.ValidationRuleJson,
EditorOptions = f.EditorOptions,
SortIndex = f.SortIndex,
SortDirection = f.SortDirection,
};
clonedFields.Add(newField);
}
2025-11-26 11:04:31 +00:00
await _listFormFieldRepository.InsertManyAsync(clonedFields, autoSave: true);
}
}
2025-11-26 11:04:31 +00:00
return targetForm;
}
private async Task SetSubFormsJsonAsync(ListForm listForm, string subFormsJson)
{
listForm.ListFormType = ListFormTypeEnum.Form;
listForm.SubFormsJson = subFormsJson;
await _listFormRepository.UpdateAsync(listForm, autoSave: true);
//Eksik olan form alanlarını ekle
var subForms = JsonSerializer.Deserialize<List<SubFormDto>>(subFormsJson);
if (subForms != null)
{
foreach (var subForm in subForms)
{
foreach (var sf in subForm.Relation)
{
var parentFieldName = sf.ParentFieldName;
var dbType = sf.DbType;
await InsertListFormFieldAsync(
2025-11-26 11:04:31 +00:00
listForm.ListFormCode,
parentFieldName,
dbType
);
var childListFormCode = subForm.Code;
var childFieldName = sf.ChildFieldName;
await InsertListFormFieldAsync(
childListFormCode,
childFieldName,
dbType
);
}
}
}
}
2025-11-26 11:04:31 +00:00
private async Task InsertListFormFieldAsync(string listFormCode, string fieldName, DbType dbType)
{
2025-11-26 11:04:31 +00:00
var form = await _listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
var fieldQuery = await _listFormFieldRepository.GetQueryableAsync();
var formField = await fieldQuery.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode && f.FieldName == fieldName);
if (formField == null)
{
var listOrderNo = await fieldQuery.Where(f => f.ListFormCode == listFormCode).MaxAsync(a => a.ListOrderNo) ?? 0;
var field = new ListFormField
{
CultureName = PlatformConsts.DefaultLanguage,
ListFormCode = listFormCode,
SourceDbType = dbType,
FieldName = fieldName,
ListOrderNo = listOrderNo + 1,
Visible = false,
IsActive = true,
IsDeleted = false,
SortIndex = 0,
PermissionJson = JsonSerializer.Serialize(new ListFormFieldPermissionDto
{
C = form.Name + ".Create",
R = form.Name,
U = form.Name + ".Update",
E = true,
I = false,
Deny = false
}),
};
2025-11-26 11:04:31 +00:00
await _listFormFieldRepository.InsertAsync(field, autoSave: true);
}
}
}