2025-11-12 21:19:32 +00:00
|
|
|
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,
|
2025-11-13 09:36:30 +00:00
|
|
|
string listFormCode,
|
2025-11-12 21:19:32 +00:00
|
|
|
string subFormsJson = "")
|
|
|
|
|
{
|
|
|
|
|
// load source form
|
2025-11-13 09:36:30 +00:00
|
|
|
var sourceForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
|
2025-11-12 21:19:32 +00:00
|
|
|
if (sourceForm == null)
|
|
|
|
|
{
|
2025-11-13 09:36:30 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string targetListFormCode = GetDefaultFormCodes(listFormCode);
|
|
|
|
|
|
|
|
|
|
var targetForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == targetListFormCode);
|
|
|
|
|
if (targetForm != null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
2025-11-12 21:19:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create a shallow copy of the source form and set the target code
|
|
|
|
|
var clonedForm = new ListForm
|
|
|
|
|
{
|
|
|
|
|
ListFormType = sourceForm.ListFormType,
|
|
|
|
|
IsSubForm = sourceForm.IsSubForm,
|
2025-11-14 20:59:46 +00:00
|
|
|
ShowNote = sourceForm.ShowNote,
|
2025-11-12 21:19:32 +00:00
|
|
|
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
|
2025-11-13 09:36:30 +00:00
|
|
|
var fields = await listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode);
|
2025-11-12 21:19:32 +00:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-13 09:36:30 +00:00
|
|
|
public string GetDefaultFormCodes(string listCode)
|
2025-11-12 21:19:32 +00:00
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|