erp-platform/api/src/Erp.Platform.DbMigrator/Seeds/SeederUtils.cs
2025-11-14 23:59:46 +03:00

142 lines
No EOL
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Erp.Platform.Entities;
using Volo.Abp.Domain.Repositories;
namespace Erp.Platform.Data.Seeds;
public class SeederUtils : IDisposable
{
public async Task<ListForm> CloneListFormWithFieldsAsync(
IRepository<ListForm, Guid> listFormRepository,
IRepository<ListFormField, Guid> listFormFieldRepository,
string listFormCode,
string subFormsJson = "")
{
// load source form
var sourceForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
if (sourceForm == null)
{
return null;
}
string targetListFormCode = GetDefaultFormCodes(listFormCode);
var targetForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == targetListFormCode);
if (targetForm != null)
{
return null;
}
// create a shallow copy of the source form and set the target code
var clonedForm = new ListForm
{
ListFormType = sourceForm.ListFormType,
IsSubForm = sourceForm.IsSubForm,
ShowNote = sourceForm.ShowNote,
LayoutJson = sourceForm.LayoutJson,
SubFormsJson = subFormsJson ?? sourceForm.SubFormsJson,
CultureName = sourceForm.CultureName,
ListFormCode = targetListFormCode,
Name = sourceForm.Name,
Title = sourceForm.Title,
DataSourceCode = sourceForm.DataSourceCode,
IsTenant = sourceForm.IsTenant,
IsBranch = sourceForm.IsBranch,
IsOrganizationUnit = sourceForm.IsOrganizationUnit,
Description = sourceForm.Description,
SelectCommandType = sourceForm.SelectCommandType,
SelectCommand = sourceForm.SelectCommand,
KeyFieldName = sourceForm.KeyFieldName,
KeyFieldDbSourceType = sourceForm.KeyFieldDbSourceType,
DefaultFilter = sourceForm.DefaultFilter,
SortMode = sourceForm.SortMode,
PermissionJson = sourceForm.PermissionJson,
DeleteCommand = sourceForm.DeleteCommand,
DeleteFieldsDefaultValueJson = sourceForm.DeleteFieldsDefaultValueJson,
EditingOptionJson = sourceForm.EditingOptionJson,
EditingFormJson = sourceForm.EditingFormJson,
InsertFieldsDefaultValueJson = sourceForm.InsertFieldsDefaultValueJson,
FormFieldsDefaultValueJson = sourceForm.FormFieldsDefaultValueJson,
CommandColumnJson = sourceForm.CommandColumnJson,
InsertServiceAddress = sourceForm.InsertServiceAddress,
UpdateServiceAddress = sourceForm.UpdateServiceAddress,
FilterRowJson = sourceForm.FilterRowJson,
HeaderFilterJson = sourceForm.HeaderFilterJson,
SearchPanelJson = sourceForm.SearchPanelJson,
GroupPanelJson = sourceForm.GroupPanelJson,
SelectionJson = sourceForm.SelectionJson,
ColumnOptionJson = sourceForm.ColumnOptionJson,
PagerOptionJson = sourceForm.PagerOptionJson,
};
// insert cloned form
var insertedForm = await listFormRepository.InsertAsync(clonedForm);
// copy fields
var fields = await listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode);
if (fields != null && fields.Count > 0)
{
var clonedFields = new List<ListFormField>();
foreach (var f in fields)
{
var newField = new ListFormField
{
ListFormCode = targetListFormCode,
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);
}
await listFormFieldRepository.InsertManyAsync(clonedFields);
}
return insertedForm;
}
public string GetDefaultFormCodes(string listCode)
{
return listCode.Replace("list-", "form-");
}
private bool _disposed;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
// If the class ever holds disposable fields, dispose them here.
// Currently SeederUtils does not own any IDisposable resources.
}
_disposed = true;
}
}