2025-11-12 21:19:32 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
2025-11-18 11:48:51 +00:00
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text.Json;
|
2025-11-12 21:19:32 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Erp.Platform.Entities;
|
2025-11-18 11:48:51 +00:00
|
|
|
|
using Erp.Platform.ListForms;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-11-12 21:19:32 +00:00
|
|
|
|
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-18 11:48:51 +00:00
|
|
|
|
string subFormsJson)
|
2025-11-12 21:19:32 +00:00
|
|
|
|
{
|
|
|
|
|
|
// load source form
|
2025-11-18 11:48:51 +00:00
|
|
|
|
var listForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
|
|
|
|
|
|
if (listForm == null)
|
2025-11-12 21:19:32 +00:00
|
|
|
|
{
|
2025-11-13 09:36:30 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 11:48:51 +00:00
|
|
|
|
string formCode = GetDefaultFormCodes(listFormCode);
|
2025-11-13 09:36:30 +00:00
|
|
|
|
|
2025-11-18 11:48:51 +00:00
|
|
|
|
var targetForm = await listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == formCode);
|
|
|
|
|
|
if (targetForm == null)
|
2025-11-13 09:36:30 +00:00
|
|
|
|
{
|
2025-11-18 11:48:51 +00:00
|
|
|
|
// create a shallow copy of the source form and set the target code
|
|
|
|
|
|
var insertForm = new ListForm
|
|
|
|
|
|
{
|
|
|
|
|
|
ListFormCode = formCode,
|
|
|
|
|
|
SubFormsJson = subFormsJson, //Özelleştirilmiş SubFormsJson
|
2025-11-12 21:19:32 +00:00
|
|
|
|
|
2025-11-18 11:48:51 +00:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2025-11-12 21:19:32 +00:00
|
|
|
|
|
|
|
|
|
|
// copy fields
|
2025-11-18 11:48:51 +00:00
|
|
|
|
var targetFormFields = await listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode);
|
|
|
|
|
|
if (targetFormFields != null && targetFormFields.Count > 0)
|
2025-11-12 21:19:32 +00:00
|
|
|
|
{
|
|
|
|
|
|
var clonedFields = new List<ListFormField>();
|
2025-11-18 11:48:51 +00:00
|
|
|
|
foreach (var f in targetFormFields)
|
2025-11-12 21:19:32 +00:00
|
|
|
|
{
|
|
|
|
|
|
var newField = new ListFormField
|
|
|
|
|
|
{
|
2025-11-18 11:48:51 +00:00
|
|
|
|
ListFormCode = formCode,
|
2025-11-12 21:19:32 +00:00
|
|
|
|
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-18 11:48:51 +00:00
|
|
|
|
await listFormFieldRepository.InsertManyAsync(clonedFields, autoSave: true);
|
2025-11-12 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-18 11:48:51 +00:00
|
|
|
|
//Subform içerisindeki ParentFieldName ve ChildFieldName alanlarına göre eksik olan fieldları 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(
|
|
|
|
|
|
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<ListForm, Guid> listFormRepository,
|
|
|
|
|
|
IRepository<ListFormField, Guid> 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);
|
|
|
|
|
|
}
|
2025-11-12 21:19:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|