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 CloneListFormWithFieldsAsync( IRepository listFormRepository, IRepository listFormFieldRepository, string sourceListFormCode, string targetListFormCode, string subFormsJson = "") { // load source form var sourceForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == sourceListFormCode); if (sourceForm == 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, ShowActivity = sourceForm.ShowActivity, 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 == sourceListFormCode); if (fields != null && fields.Count > 0) { var clonedFields = new List(); 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 GetFormCodes(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; } }