212 lines
No EOL
8.5 KiB
C#
212 lines
No EOL
8.5 KiB
C#
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.Enums;
|
||
using Erp.Platform.ListForms;
|
||
using Microsoft.EntityFrameworkCore;
|
||
using Volo.Abp.Domain.Repositories;
|
||
|
||
namespace Erp.Platform.Data.Seeds;
|
||
|
||
public class SeederUtils
|
||
{
|
||
private readonly IRepository<ListForm, Guid> _listFormRepository;
|
||
private readonly IRepository<ListFormField, Guid> _listFormFieldRepository;
|
||
|
||
public SeederUtils(
|
||
IRepository<ListForm, Guid> listFormRepository,
|
||
IRepository<ListFormField, Guid> listFormFieldRepository)
|
||
{
|
||
_listFormRepository = listFormRepository;
|
||
_listFormFieldRepository = listFormFieldRepository;
|
||
}
|
||
|
||
public static string GetDefaultFormCodes(string listFormCode) => "form-" + listFormCode;
|
||
|
||
public async Task CloneFormLayoutAsync(string listFormCode, string subFormsJson)
|
||
{
|
||
var newListFormCode = GetDefaultFormCodes(listFormCode);
|
||
var cloneForm = await CloneListFormWithFieldsAsync(listFormCode, newListFormCode);
|
||
|
||
if (cloneForm != null)
|
||
{
|
||
await SetSubFormsJsonAsync(cloneForm, subFormsJson);
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine($"ListForm with code {listFormCode} not found!");
|
||
}
|
||
}
|
||
|
||
public async Task<ListForm> CloneListFormWithFieldsAsync(string listFormCode, string newListFormCode, bool copyFormFields = true)
|
||
{
|
||
var listForm = await _listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == listFormCode);
|
||
if (listForm == null)
|
||
{
|
||
Console.WriteLine($"ListForm with code {listFormCode} not found!");
|
||
|
||
return null;
|
||
}
|
||
|
||
var targetForm = await _listFormRepository.FirstOrDefaultAsync(f => f.ListFormCode == newListFormCode);
|
||
if (targetForm == null)
|
||
{
|
||
var insertForm = new ListForm
|
||
{
|
||
ListFormCode = newListFormCode,
|
||
SubFormsJson = listForm.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);
|
||
}
|
||
|
||
if (copyFormFields)
|
||
{
|
||
var targetFormFields = await _listFormFieldRepository.GetListAsync(f => f.ListFormCode == listFormCode);
|
||
if (targetFormFields != null && targetFormFields.Count > 0)
|
||
{
|
||
var clonedFields = new List<ListFormField>();
|
||
foreach (var f in targetFormFields)
|
||
{
|
||
var newField = new ListFormField
|
||
{
|
||
ListFormCode = newListFormCode,
|
||
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);
|
||
}
|
||
}
|
||
|
||
return targetForm;
|
||
}
|
||
|
||
private async Task SetSubFormsJsonAsync(ListForm listForm, string subFormsJson)
|
||
{
|
||
listForm.ListFormType = ListFormTypeEnum.Form;
|
||
listForm.SubFormsJson = subFormsJson;
|
||
await _listFormRepository.UpdateAsync(listForm, autoSave: true);
|
||
|
||
//Eksik olan form alanlarını 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(
|
||
listForm.ListFormCode,
|
||
parentFieldName,
|
||
dbType
|
||
);
|
||
|
||
var childListFormCode = subForm.Code;
|
||
var childFieldName = sf.ChildFieldName;
|
||
|
||
await InsertListFormFieldAsync(
|
||
childListFormCode,
|
||
childFieldName,
|
||
dbType
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private async Task InsertListFormFieldAsync(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);
|
||
}
|
||
}
|
||
} |