From aa80ba91be6ee60c765b5e6d72cf69fdc966ae05 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Thu, 15 Aug 2024 10:46:55 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20find=5Fb?= =?UTF-8?q?y=5Ffunctions.json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/templates/configs_/findby_functions.json | 2 +- .../configs_/findby_functions_rapira.json | 2 +- .../configs_/findmassby_functions.json | 2 +- .../configs_/findmassby_functions_rapira.json | 2 +- bin/templates/configs_/readme.txt | 18 +- .../db/crud/crud_tables_findby_function.go_ | 2 +- .../crud/crud_tables_findby_function_test.go_ | 2 +- .../pkg/db/crud/crud_tables_findmassby.go_ | 11 + .../crud/crud_tables_findmassby_function.go_ | 31 ++ .../crud_tables_findmassby_function_test.go_ | 15 + .../db/crud/crud_tables_findmassby_test.go_ | 8 + .../create_files/crud_tables/crud_tables.go | 16 + .../crud_tables/crud_tables_findby.go | 29 +- .../crud_tables/crud_tables_findmassby.go | 299 ++++++++++++++++++ internal/load_configs/load_configs.go | 10 +- internal/types/types.go | 13 +- 16 files changed, 424 insertions(+), 38 deletions(-) create mode 100644 internal/create_files/crud_tables/crud_tables_findmassby.go diff --git a/bin/templates/configs_/findby_functions.json b/bin/templates/configs_/findby_functions.json index 9e26dfe..0637a08 100644 --- a/bin/templates/configs_/findby_functions.json +++ b/bin/templates/configs_/findby_functions.json @@ -1 +1 @@ -{} \ No newline at end of file +[] \ No newline at end of file diff --git a/bin/templates/configs_/findby_functions_rapira.json b/bin/templates/configs_/findby_functions_rapira.json index 31897dc..581d6b0 100644 --- a/bin/templates/configs_/findby_functions_rapira.json +++ b/bin/templates/configs_/findby_functions_rapira.json @@ -1 +1 @@ -{"connections":["branch_id","is_legal"]} \ No newline at end of file +[{"Table":"connections","Columns":["branch_id","is_legal"]}] diff --git a/bin/templates/configs_/findmassby_functions.json b/bin/templates/configs_/findmassby_functions.json index 9e26dfe..0637a08 100644 --- a/bin/templates/configs_/findmassby_functions.json +++ b/bin/templates/configs_/findmassby_functions.json @@ -1 +1 @@ -{} \ No newline at end of file +[] \ No newline at end of file diff --git a/bin/templates/configs_/findmassby_functions_rapira.json b/bin/templates/configs_/findmassby_functions_rapira.json index be64027..001dfae 100644 --- a/bin/templates/configs_/findmassby_functions_rapira.json +++ b/bin/templates/configs_/findmassby_functions_rapira.json @@ -1 +1 @@ -{"connections":["branch_id"]} \ No newline at end of file +[{"Table":"connections","Columns":["branch_id"]}] \ No newline at end of file diff --git a/bin/templates/configs_/readme.txt b/bin/templates/configs_/readme.txt index b49e725..2a5dae3 100644 --- a/bin/templates/configs_/readme.txt +++ b/bin/templates/configs_/readme.txt @@ -67,21 +67,15 @@ example: ------------------------------------------------------------------------ findby_functions.json -Need for automatic create functions searching 1 row in table filtered by column_name -Fill "TableName":["column_name"] -Fill "TableName":["column_name","column2_name"] +Need for automatic create functions searching 1 row in table filtered by column_name, example: -{ - "lawsuits": ["contract_id"] -} +[{"Table":"table_name1","Columns":["column_name1"]}] +[{"Table":"table_name1","Columns":["column_name1","column_name2"]}] ------------------------------------------------------------------------ findmassby_function.json -Need for automatic create functions searching many rows in table filtered by column_name -Fill "TableName":["column_name"] -Fill "TableName":["column_name","column2_name"] +Need for automatic create functions searching many rows in table filtered by column_name, example: -{ - "lawsuits": ["contract_id"] -} +[{"Table":"table_name1","Columns":["column_name1"]}] +[{"Table":"table_name1","Columns":["column_name1","column_name2"]}] diff --git a/bin/templates/pkg/db/crud/crud_tables_findby_function.go_ b/bin/templates/pkg/db/crud/crud_tables_findby_function.go_ index 22401af..6c4ac16 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findby_function.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findby_function.go_ @@ -19,7 +19,7 @@ func FindBy_FieldNamesWithUnderline_ctx(ctx context.Context, db *gorm.DB, m *law tx := db.WithContext(ctx) - tx := tx.Where("ColumnName = ?", m.FieldName) + tx = tx.Where("ColumnName = ?", m.FieldName) tx = tx.First(m) err = tx.Error diff --git a/bin/templates/pkg/db/crud/crud_tables_findby_function_test.go_ b/bin/templates/pkg/db/crud/crud_tables_findby_function_test.go_ index f17c107..40eba9f 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findby_function_test.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findby_function_test.go_ @@ -9,7 +9,7 @@ func TestFindBy_FieldNamesWithUnderline(t *testing.T) { Otvet.FieldName = 0 err := crud.FindBy_FieldNamesWithUnderline(&Otvet) if err != nil { - t.Log("crud_"+TableName+"_test.TestFindBy_FieldNamesWithUnderline() FieldNamesWithUnderline=", FieldNames_TEST, " error: ", err) + t.Log("crud_"+TableName+"_test.TestFindBy_FieldNamesWithUnderline() FieldNamesWithComma:", FieldNamesDefault, " error: ", err) } } diff --git a/bin/templates/pkg/db/crud/crud_tables_findmassby.go_ b/bin/templates/pkg/db/crud/crud_tables_findmassby.go_ index 7c4c42f..fa9143c 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findmassby.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findmassby.go_ @@ -1 +1,12 @@ package crud_lawsuit_status_types + +import ( + "context" + "errors" + "github.com/ManyakRus/starter/postgres_gorm" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "gorm.io/gorm" + "time" +) + diff --git a/bin/templates/pkg/db/crud/crud_tables_findmassby_function.go_ b/bin/templates/pkg/db/crud/crud_tables_findmassby_function.go_ index e69de29..de0d009 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findmassby_function.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findmassby_function.go_ @@ -0,0 +1,31 @@ +// FindMassBy_FieldNamesWithUnderline - находит запись в БД по FieldNamesWithPlus +func (crud Crud_DB) FindMassBy_FieldNamesWithUnderline(m *lawsuit_status_types.LawsuitStatusType) ([]lawsuit_status_types.LawsuitStatusType, error) { + Otvet := make([]files.File, 0) + var err error + + ctxMain := context.Background() + ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(db_constants.TIMEOUT_DB_SECONDS)) + defer ctxCancelFunc() + + db := postgres_gorm.GetConnection() + + Otvet, err = FindMassBy_FieldNamesWithUnderline_ctx(ctx, db, m) + + return Otvet, err +} + +// FindMassBy_FieldNamesWithUnderline_ctx - находит запись в БД по FieldNamesWithPlus +func FindMassBy_FieldNamesWithUnderline_ctx(ctx context.Context, db *gorm.DB, m *lawsuit_status_types.LawsuitStatusType) ([]lawsuit_status_types.LawsuitStatusType, error) { + var err error + Otvet := make([]files.File, 0) + + tx := db.WithContext(ctx) + + tx = tx.Where("ColumnName = ?", m.FieldName) + tx = tx.Order("ColumnNamesWithComma") + tx = tx.Find(m) + err = tx.Error + + return Otvet, err +} + diff --git a/bin/templates/pkg/db/crud/crud_tables_findmassby_function_test.go_ b/bin/templates/pkg/db/crud/crud_tables_findmassby_function_test.go_ index e69de29..d875c4e 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findmassby_function_test.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findmassby_function_test.go_ @@ -0,0 +1,15 @@ +func TestFindMassBy_FieldNamesWithUnderline(t *testing.T) { + config_main.LoadEnv() + + postgres_gorm.Connect() + defer postgres_gorm.CloseConnection() + + crud := Crud_DB{} + Otvet := lawsuit_status_types.LawsuitStatusType{} + Otvet.FieldName = 0 + Otvet, err := crud.FindMassBy_FieldNamesWithUnderline(&Otvet) + if err != nil { + t.Log("crud_"+TableName+"_test.TestFindMassBy_FieldNamesWithUnderline() FieldNamesWithComma:", FieldNamesDefault, " error: ", err) + } +} + diff --git a/bin/templates/pkg/db/crud/crud_tables_findmassby_test.go_ b/bin/templates/pkg/db/crud/crud_tables_findmassby_test.go_ index 7c4c42f..7b20fba 100644 --- a/bin/templates/pkg/db/crud/crud_tables_findmassby_test.go_ +++ b/bin/templates/pkg/db/crud/crud_tables_findmassby_test.go_ @@ -1 +1,9 @@ package crud_lawsuit_status_types + +import ( + "github.com/ManyakRus/starter/config_main" + "github.com/ManyakRus/starter/postgres_gorm" + "gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/object_model/entities/lawsuit_status_types" + "testing" +) + diff --git a/internal/create_files/crud_tables/crud_tables.go b/internal/create_files/crud_tables/crud_tables.go index 25c41b0..4677180 100644 --- a/internal/create_files/crud_tables/crud_tables.go +++ b/internal/create_files/crud_tables/crud_tables.go @@ -81,17 +81,33 @@ func CreateAllFiles(MapAll map[string]*types.Table) error { } } + // err = CreateFilesFindBy(Table1) if err != nil { log.Error("CreateFilesFindBy() table: ", Table1.Name, " error: ", err) return err } + // err = CreateFilesFindByTest(Table1) if err != nil { log.Error("CreateFilesFindByTest() table: ", Table1.Name, " error: ", err) return err } + + // + err = CreateFilesFindMassBy(Table1) + if err != nil { + log.Error("CreateFilesFindMassBy() table: ", Table1.Name, " error: ", err) + return err + } + + // + err = CreateFilesFindMassByTest(Table1) + if err != nil { + log.Error("CreateFilesFindMassByTest() table: ", Table1.Name, " error: ", err) + return err + } } } diff --git a/internal/create_files/crud_tables/crud_tables_findby.go b/internal/create_files/crud_tables/crud_tables_findby.go index 71ae927..98ba45d 100644 --- a/internal/create_files/crud_tables/crud_tables_findby.go +++ b/internal/create_files/crud_tables/crud_tables_findby.go @@ -15,7 +15,7 @@ import ( func CreateFilesFindBy(Table1 *types.Table) error { var err error - if len(types.MapFindBy) == 0 { + if len(types.MassFindBy) == 0 { return err } @@ -108,11 +108,11 @@ func CreateFilesFindBy(Table1 *types.Table) error { func CreateFilesFindByTable(Table1 *types.Table, TextTemplateFunction string) string { Otvet := "" - for TableName1, MassColumns1 := range types.MapFindBy { - if TableName1 != Table1.Name { + for _, TableColumns1 := range types.MassFindBy { + if TableColumns1.TableName != Table1.Name { continue } - Otvet1 := CreateFilesFindByTable1(Table1, TextTemplateFunction, MassColumns1) + Otvet1 := CreateFilesFindByTable1(Table1, TextTemplateFunction, TableColumns1.MassColumnNames) Otvet = Otvet + Otvet1 } @@ -125,13 +125,11 @@ func CreateFilesFindByTable1(Table1 *types.Table, TextTemplateFunction string, M // FieldNamesWithUnderline := "" - - // FieldNamesWithComma := "" + TextWhere := "" // - TextFind := "\t" + `tx := tx.Where("ColumnName = ?", m.FieldName)` + "\n" - TextWhere := "" + TextFind := "\t" + `tx = tx.Where("ColumnName = ?", m.FieldName)` + "\n" Underline := "" Plus := "" for _, ColumnName1 := range MassColumns1 { @@ -139,7 +137,7 @@ func CreateFilesFindByTable1(Table1 *types.Table, TextTemplateFunction string, M if ok == false { log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false") } - TextWhere = TextWhere + "\t" + `tx := tx.Where("` + ColumnName1 + ` = ?", m.` + Column1.NameGo + `)` + "\n" + TextWhere = TextWhere + "\t" + `tx = tx.Where("` + ColumnName1 + ` = ?", m.` + Column1.NameGo + `)` + "\n" FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo FieldNamesWithComma = FieldNamesWithComma + Plus + Column1.NameGo Underline = "_" @@ -156,7 +154,7 @@ func CreateFilesFindByTable1(Table1 *types.Table, TextTemplateFunction string, M func CreateFilesFindByTest(Table1 *types.Table) error { var err error - if len(types.MapFindBy) == 0 { + if len(types.MassFindBy) == 0 { return err } @@ -246,11 +244,11 @@ func CreateFilesFindByTest(Table1 *types.Table) error { func CreateFilesFindByTestTable(Table1 *types.Table, TextTemplateFunction string) string { Otvet := "" - for TableName1, MassColumns1 := range types.MapFindBy { - if TableName1 != Table1.Name { + for _, TableColumns1 := range types.MassFindBy { + if TableColumns1.TableName != Table1.Name { continue } - Otvet1 := CreateFilesFindByTestTable1(Table1, TextTemplateFunction, MassColumns1) + Otvet1 := CreateFilesFindByTestTable1(Table1, TextTemplateFunction, TableColumns1.MassColumnNames) Otvet = Otvet + Otvet1 } @@ -263,6 +261,7 @@ func CreateFilesFindByTestTable1(Table1 *types.Table, TextTemplateFunction strin // FieldNamesWithUnderline := "" + FieldNamesWithComma := "" // TextAssignFind := "\t" + `Otvet.FieldName = 0` + "\n" @@ -279,6 +278,7 @@ func CreateFilesFindByTestTable1(Table1 *types.Table, TextTemplateFunction strin DefaultValue := create_files.FindTextDefaultValue(Column1.TypeGo) TextAssign = TextAssign + "\t" + `Otvet.` + Column1.NameGo + ` = ` + DefaultValue + "\n" FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo + FieldNamesWithComma = FieldNamesWithComma + Comma + Column1.NameGo TextFieldName_TEST = TextFieldName_TEST + Comma + DefaultValue Underline = "_" @@ -286,7 +286,8 @@ func CreateFilesFindByTestTable1(Table1 *types.Table, TextTemplateFunction strin } Otvet = strings.ReplaceAll(Otvet, TextAssignFind, TextAssign) Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline) - Otvet = strings.ReplaceAll(Otvet, "FieldNames_TEST", TextFieldName_TEST) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithComma", FieldNamesWithComma) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesDefault", TextFieldName_TEST) return Otvet } diff --git a/internal/create_files/crud_tables/crud_tables_findmassby.go b/internal/create_files/crud_tables/crud_tables_findmassby.go new file mode 100644 index 0000000..ecc6735 --- /dev/null +++ b/internal/create_files/crud_tables/crud_tables_findmassby.go @@ -0,0 +1,299 @@ +package crud_tables + +import ( + "github.com/ManyakRus/crud_generator/internal/config" + "github.com/ManyakRus/crud_generator/internal/constants" + "github.com/ManyakRus/crud_generator/internal/create_files" + "github.com/ManyakRus/crud_generator/internal/types" + "github.com/ManyakRus/starter/log" + "github.com/ManyakRus/starter/micro" + "os" + "strings" +) + +// CreateFilesFindMassBy - создаёт 1 файл в папке crud +func CreateFilesFindMassBy(Table1 *types.Table) error { + var err error + + if len(types.MassFindMassBy) == 0 { + return err + } + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + + FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD + TableName + FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + "_findmassby.go" + + //создадим каталог + ok, err := micro.FileExists(DirReadyTable) + if ok == false { + err = os.MkdirAll(DirReadyTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirReadyTable, " error: ", err) + } + } + + //загрузим шаблон файла + bytes, err := os.ReadFile(FilenameTemplateCrud) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + } + TextCrud := string(bytes) + + //загрузим шаблон файла функции + FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_FILENAME + bytes, err = os.ReadFile(FilenameTemplateCrudFunction) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err) + } + TextTemplatedFunction := string(bytes) + + //заменим имя пакета на новое + TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable) + + ModelName := Table1.NameGo + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud) + + ModelTableURL := create_files.FindModelTableURL(TableName) + TextCrud = create_files.AddImport(TextCrud, ModelTableURL) + + ConstantsURL := create_files.FindDBConstantsURL() + TextCrud = create_files.AddImport(TextCrud, ConstantsURL) + + } + + //создание функций + TextCrudFunc := CreateFilesFindMassByTable(Table1, TextTemplatedFunction) + if TextCrudFunc == "" { + return err + } + TextCrud = TextCrud + TextCrudFunc + + //создание текста + TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud + + //замена импортов на новые URL + TextCrud = create_files.ReplaceServiceURLImports(TextCrud) + + //uuid + TextCrud = create_files.CheckAndAddImportUUID_FromText(TextCrud) + + //alias + TextCrud = create_files.CheckAndAddImportAlias(TextCrud) + + //удаление пустого импорта + TextCrud = create_files.DeleteEmptyImport(TextCrud) + + //удаление пустых строк + TextCrud = create_files.DeleteEmptyLines(TextCrud) + + //запись файла + err = os.WriteFile(FilenameReady, []byte(TextCrud), constants.FILE_PERMISSIONS) + + return err +} + +// CreateFilesFindMassByTable - создаёт текст всех функций +func CreateFilesFindMassByTable(Table1 *types.Table, TextTemplateFunction string) string { + Otvet := "" + + for _, TableColumns1 := range types.MassFindMassBy { + if TableColumns1.TableName != Table1.Name { + continue + } + Otvet1 := CreateFilesFindMassByTable1(Table1, TextTemplateFunction, TableColumns1.MassColumnNames) + Otvet = Otvet + Otvet1 + } + + return Otvet +} + +// CreateFilesFindMassByTable1 - создаёт текст всех функций +func CreateFilesFindMassByTable1(Table1 *types.Table, TextTemplateFunction string, MassColumns1 []string) string { + Otvet := TextTemplateFunction + + // + FieldNamesWithUnderline := "" + FieldNamesWithComma := "" + ColumnNamesWithComma := "" + + // + TextFind := "\t" + `tx = tx.Where("ColumnName = ?", m.FieldName)` + "\n" + TextWhere := "" + Underline := "" + Plus := "" + Comma := "" + for _, ColumnName1 := range MassColumns1 { + Column1, ok := Table1.MapColumns[ColumnName1] + if ok == false { + log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false") + } + TextWhere = TextWhere + "\t" + `tx = tx.Where("` + ColumnName1 + ` = ?", m.` + Column1.NameGo + `)` + "\n" + FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo + FieldNamesWithComma = FieldNamesWithComma + Plus + Column1.NameGo + ColumnNamesWithComma = ColumnNamesWithComma + Comma + Column1.Name + + Underline = "_" + Plus = "+" + Comma = ", " + } + Otvet = strings.ReplaceAll(Otvet, TextFind, TextWhere) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithPlus", FieldNamesWithComma) + Otvet = strings.ReplaceAll(Otvet, "ColumnNamesWithComma", ColumnNamesWithComma) + + return Otvet +} + +// CreateFilesFindMassByTest - создаёт 1 файл в папке crud +func CreateFilesFindMassByTest(Table1 *types.Table) error { + var err error + + if len(types.MassFindMassBy) == 0 { + return err + } + + //чтение файлов + DirBin := micro.ProgramDir_bin() + DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile() + DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile() + DirTemplatesCrud := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + DirReadyCrud := DirReady + config.Settings.TEMPLATE_FOLDERNAME_CRUD + micro.SeparatorFile() + + FilenameTemplateCrud := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FILENAME + TableName := strings.ToLower(Table1.Name) + DirReadyTable := DirReadyCrud + config.Settings.PREFIX_CRUD + TableName + FilenameReady := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_CRUD + TableName + "_findmassby_test.go" + + //создадим каталог + ok, err := micro.FileExists(DirReadyTable) + if ok == false { + err = os.MkdirAll(DirReadyTable, 0777) + if err != nil { + log.Panic("Mkdir() ", DirReadyTable, " error: ", err) + } + } + + //загрузим шаблон файла + bytes, err := os.ReadFile(FilenameTemplateCrud) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrud, " error: ", err) + } + TextCrud := string(bytes) + + //загрузим шаблон файла функции + FilenameTemplateCrudFunction := DirTemplatesCrud + config.Settings.TEMPLATES_CRUD_TABLE_FINDMASSBY_FUNCTION_TEST_FILENAME + bytes, err = os.ReadFile(FilenameTemplateCrudFunction) + if err != nil { + log.Panic("ReadFile() ", FilenameTemplateCrudFunction, " error: ", err) + } + TextTemplatedFunction := string(bytes) + + //заменим имя пакета на новое + TextCrud = create_files.ReplacePackageName(TextCrud, DirReadyTable) + + ModelName := Table1.NameGo + //заменим импорты + if config.Settings.USE_DEFAULT_TEMPLATE == true { + TextCrud = create_files.DeleteTemplateRepositoryImports(TextCrud) + + ModelTableURL := create_files.FindModelTableURL(TableName) + TextCrud = create_files.AddImport(TextCrud, ModelTableURL) + + } + + //создание функций + TextCrudFunc := CreateFilesFindMassByTestTable(Table1, TextTemplatedFunction) + if TextCrudFunc == "" { + return err + } + TextCrud = TextCrud + TextCrudFunc + + //создание текста + TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_MODEL, ModelName) + TextCrud = strings.ReplaceAll(TextCrud, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name) + TextCrud = config.Settings.TEXT_MODULE_GENERATED + TextCrud + + //замена импортов на новые URL + TextCrud = create_files.ReplaceServiceURLImports(TextCrud) + + //uuid + TextCrud = create_files.CheckAndAddImportUUID_FromText(TextCrud) + + //alias + TextCrud = create_files.CheckAndAddImportAlias(TextCrud) + + //удаление пустого импорта + TextCrud = create_files.DeleteEmptyImport(TextCrud) + + //удаление пустых строк + TextCrud = create_files.DeleteEmptyLines(TextCrud) + + //запись файла + err = os.WriteFile(FilenameReady, []byte(TextCrud), constants.FILE_PERMISSIONS) + + return err +} + +// CreateFilesFindMassByTestTable - создаёт текст всех функций +func CreateFilesFindMassByTestTable(Table1 *types.Table, TextTemplateFunction string) string { + Otvet := "" + + for _, TableColumns1 := range types.MassFindMassBy { + if TableColumns1.TableName != Table1.Name { + continue + } + Otvet1 := CreateFilesFindMassByTestTable1(Table1, TextTemplateFunction, TableColumns1.MassColumnNames) + Otvet = Otvet + Otvet1 + } + + return Otvet +} + +// CreateFilesFindMassByTestTable1 - создаёт текст всех функций +func CreateFilesFindMassByTestTable1(Table1 *types.Table, TextTemplateFunction string, MassColumns1 []string) string { + Otvet := TextTemplateFunction + + // + FieldNamesWithUnderline := "" + FieldNamesWithComma := "" + + // + TextAssignFind := "\t" + `Otvet.FieldName = 0` + "\n" + TextAssign := "" + TextFieldName_TEST := "" + + Underline := "" + Comma := "" + for _, ColumnName1 := range MassColumns1 { + Column1, ok := Table1.MapColumns[ColumnName1] + if ok == false { + log.Panic(Table1.Name + " .MapColumns[" + ColumnName1 + "] = false") + } + DefaultValue := create_files.FindTextDefaultValue(Column1.TypeGo) + TextAssign = TextAssign + "\t" + `Otvet.` + Column1.NameGo + ` = ` + DefaultValue + "\n" + FieldNamesWithUnderline = FieldNamesWithUnderline + Underline + Column1.NameGo + FieldNamesWithComma = FieldNamesWithComma + Comma + Column1.NameGo + TextFieldName_TEST = TextFieldName_TEST + Comma + DefaultValue + + Underline = "_" + Comma = ", " + } + Otvet = strings.ReplaceAll(Otvet, TextAssignFind, TextAssign) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithUnderline", FieldNamesWithUnderline) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesWithComma", FieldNamesWithComma) + Otvet = strings.ReplaceAll(Otvet, "FieldNamesDefault", TextFieldName_TEST) + + return Otvet +} diff --git a/internal/load_configs/load_configs.go b/internal/load_configs/load_configs.go index 3bdf646..a5aa1dc 100644 --- a/internal/load_configs/load_configs.go +++ b/internal/load_configs/load_configs.go @@ -220,8 +220,14 @@ func LoadFindBy() { log.Panic(TextError) } + //MassFindBy1 := types.TableNameColumnNames{} + //MassFindBy1.TableName = "TableName" + //MassFindBy1.MassColumnNames = []string{"ColumnName"} + //types.MassFindBy = append(types.MassFindBy, MassFindBy1) + //bytes, _ = json.Marshal(types.MassFindBy) //удалить + //json в map - err = json.Unmarshal(bytes, &types.MapFindBy) + err = json.Unmarshal(bytes, &types.MassFindBy) if err != nil { log.Panic("Unmarshal() error: ", err) } @@ -243,7 +249,7 @@ func LoadFindMassBy() { } //json в map - err = json.Unmarshal(bytes, &types.MapFindMassBy) + err = json.Unmarshal(bytes, &types.MassFindMassBy) if err != nil { log.Panic("Unmarshal() error: ", err) } diff --git a/internal/types/types.go b/internal/types/types.go index e946356..08d72e7 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -52,8 +52,13 @@ var MapModelCrudDeleteFunctions = make(map[string]string, 0) // MapRenameFunctions - карта функций которые будут переименованый в файлах crud var MapRenameFunctions = make(map[string][]ReplaceStruct, 0) -// MapFindBy - карта функций которые будут созданы для поиска 1 строки в таблице -var MapFindBy = make(map[string][]string, 0) +type TableNameColumnNames struct { + TableName string `json:"Table"` + MassColumnNames []string `json:"Columns"` +} -// MapFindMassBy - карта функций которые будут созданы для поиска много строк в таблице -var MapFindMassBy = make(map[string][]string, 0) +// MassFindBy - карта функций которые будут созданы для поиска 1 строки в таблице +var MassFindBy = make([]TableNameColumnNames, 0) + +// MassFindMassBy - карта функций которые будут созданы для поиска много строк в таблице +var MassFindMassBy = make([]TableNameColumnNames, 0)