using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text.Json; using System.Threading.Tasks; using Erp.Platform.Entities; using Erp.Platform.ListForms; using Microsoft.EntityFrameworkCore; using Volo.Abp.Domain.Repositories; namespace Erp.Platform.Data.Seeds; public class SeederUtils : IDisposable { public async Task CloneListFormWithFieldsAsync( IRepository listFormRepository, IRepository listFormFieldRepository, string listFormCode, string subFormsJson) { // load source form var listForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode); if (listForm == null) { return null; } string formCode = GetDefaultFormCodes(listFormCode); var targetForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == formCode); if (targetForm == null) { // create a shallow copy of the source form and set the target code var insertForm = new ListForm { ListFormCode = formCode, SubFormsJson = subFormsJson, //Özelleştirilmiş 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 targetForm = await listFormRepository.InsertAsync(insertForm, autoSave: true); } // copy fields var targetFormFields = await listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode); if (targetFormFields != null && targetFormFields.Count > 0) { var clonedFields = new List(); foreach (var f in targetFormFields) { var newField = new ListFormField { ListFormCode = formCode, 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, autoSave: true); } //Subform içerisindeki ParentFieldName ve ChildFieldName alanlarına göre eksik olan fieldları ekle var subForms = JsonSerializer.Deserialize>(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( listFormRepository, listFormFieldRepository, listFormCode, parentFieldName, dbType ); var childListFormCode = subForm.Code; var childFieldName = sf.ChildFieldName; await InsertListFormFieldAsync( listFormRepository, listFormFieldRepository, childListFormCode, childFieldName, dbType ); } } } return targetForm; } private async Task InsertListFormFieldAsync( IRepository listFormRepository, IRepository listFormFieldRepository, string listFormCode, string fieldName, DbType dbType ) { 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 }), }; await listFormFieldRepository.InsertAsync(field, autoSave: true); } } 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; } }