diff --git a/core/db/db_migration/tables.go b/core/db/db_migration/tables.go index e97e26a..10540ad 100644 --- a/core/db/db_migration/tables.go +++ b/core/db/db_migration/tables.go @@ -33,6 +33,8 @@ func (m *MigrationFunctions) migrateTables() error { tablesmigration.ApprovalAdditionalApproverTableMigrate, tablesmigration.ApprovalTasksTableMigrate, tablesmigration.ApprovalCommentsTableMigrate, + tablesmigration.SuggestionsTableMigrate, + tablesmigration.SuggestionMessagesTableMigrate, } for _, migrate := range migrations { diff --git a/core/db/db_migration/tables/providers.go b/core/db/db_migration/tables/providers.go index f2e1115..9c2b05b 100644 --- a/core/db/db_migration/tables/providers.go +++ b/core/db/db_migration/tables/providers.go @@ -20,6 +20,8 @@ type LocalApprovalStepProvider typescore.ApprovalStep type ApprovalAdditionalApprover typescore.ApprovalAdditionalApprover type ApprovalTaskProvider typescore.ApprovalTask type ApprovalCommentsProvider typescore.ApprovalComment +type LocalSuggestionProvider typescore.Suggestion +type LocalSuggestionMessagesProvider typescore.SuggestionMessage func (LocalUserProvider) TableName() string { return dbcoretablenames.TableNameUsers.ToString() @@ -66,3 +68,9 @@ func (ApprovalTaskProvider) TableName() string { func (ApprovalCommentsProvider) TableName() string { return dbcoretablenames.TableNameApprovalComments.ToString() } +func (LocalSuggestionProvider) TableName() string { + return dbcoretablenames.TableNameSuggestions.ToString() +} +func (LocalSuggestionMessagesProvider) TableName() string { + return dbcoretablenames.TableNameSuggestionMessages.ToString() +} diff --git a/core/db/db_migration/tables/suggestion_messages.go b/core/db/db_migration/tables/suggestion_messages.go new file mode 100644 index 0000000..ed424bc --- /dev/null +++ b/core/db/db_migration/tables/suggestion_messages.go @@ -0,0 +1,34 @@ +package tablesmigration + +import ( + "gorm.io/gorm" + "time" +) + +func SuggestionMessagesTableMigrate(db *gorm.DB) error { + hasTable := db.Migrator().HasTable(&LocalSuggestionProvider{}) + + if err := db.AutoMigrate(&LocalSuggestionProvider{}); err != nil { + return err + } + + if !hasTable { + time.Sleep(5 * time.Second) + + db.Exec(`ALTER TABLE suggestion_messages ADD CONSTRAINT fk_suggestion_messages_suggestion_id FOREIGN KEY (suggestion_id) REFERENCES suggestions(id) ON DELETE CASCADE ON UPDATE CASCADE`) + db.Exec(`ALTER TABLE suggestion_messages ADD CONSTRAINT fk_suggestion_messages_user_id FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE`) + + execComments(db, ` + COMMENT ON TABLE "suggestion_messages" IS 'Сообщения переписки в рамках обсуждения предложения'; + COMMENT ON COLUMN "suggestion_messages".id IS 'Внутренний уникальный идентификатор сообщения'; + COMMENT ON COLUMN "suggestion_messages".suggestion_id IS 'ID предложения, к которому относится сообщение (ссылка на suggestions.id)'; + COMMENT ON COLUMN "suggestion_messages".user_id IS 'ID автора сообщения (ссылка на users.id)'; + COMMENT ON COLUMN "suggestion_messages".message IS 'Текст сообщения'; + COMMENT ON COLUMN "suggestion_messages".file_data IS 'Прикреплённый файл в формате base64 (опционально)'; + COMMENT ON COLUMN "suggestion_messages".file_name IS 'Оригинальное имя прикреплённого файла (опционально)'; + COMMENT ON COLUMN "suggestion_messages".created_at IS 'Дата и время отправки сообщения'; + `) + } + + return nil +} diff --git a/core/db/db_migration/tables/suggestions.go b/core/db/db_migration/tables/suggestions.go new file mode 100644 index 0000000..fadb44d --- /dev/null +++ b/core/db/db_migration/tables/suggestions.go @@ -0,0 +1,34 @@ +package tablesmigration + +import ( + "gorm.io/gorm" + "time" +) + +func SuggestionsTableMigrate(db *gorm.DB) error { + hasTable := db.Migrator().HasTable(&LocalSuggestionProvider{}) + + if err := db.AutoMigrate(&LocalSuggestionProvider{}); err != nil { + return err + } + + if !hasTable { + time.Sleep(5 * time.Second) + + db.Exec(`ALTER TABLE suggestions ADD CONSTRAINT fk_suggestions_initiator_id FOREIGN KEY (initiator_id) REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE`) + db.Exec(`ALTER TABLE suggestions ADD CONSTRAINT fk_suggestions_director_id FOREIGN KEY (director_id) REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE`) + + execComments(db, ` + COMMENT ON TABLE "suggestions" IS 'Предложения и обращения сотрудников к руководству'; + COMMENT ON COLUMN "suggestions".id IS 'Внутренний уникальный идентификатор предложения'; + COMMENT ON COLUMN "suggestions".initiator_id IS 'ID сотрудника, создавшего предложение (ссылка на users.id)'; + COMMENT ON COLUMN "suggestions".director_id IS 'ID руководителя, к которому обращено предложение (ссылка на users.id)'; + COMMENT ON COLUMN "suggestions".subject IS 'Тема или заголовок предложения'; + COMMENT ON COLUMN "suggestions".status IS 'Статус предложения: open (открыто) | closed (закрыто)'; + COMMENT ON COLUMN "suggestions".created_at IS 'Дата и время создания предложения'; + COMMENT ON COLUMN "suggestions".updated_at IS 'Дата и время последнего изменения предложения'; + `) + } + + return nil +} diff --git a/core/db/table_names/table_names.go b/core/db/table_names/table_names.go index 0cf7c7e..a971908 100644 --- a/core/db/table_names/table_names.go +++ b/core/db/table_names/table_names.go @@ -18,6 +18,8 @@ const ( TableNameApprovalAdditionalApprovers TableName = "approval_additional_approvers" TableNameApprovalTasks TableName = "approval_tasks" TableNameApprovalComments TableName = "approval_comments" + TableNameSuggestions TableName = "suggestions" + TableNameSuggestionMessages TableName = "suggestion_messages" ) func (t TableName) ToString() string { diff --git a/core/typescore/suggestion.go b/core/typescore/suggestion.go new file mode 100644 index 0000000..eb287ea --- /dev/null +++ b/core/typescore/suggestion.go @@ -0,0 +1,30 @@ +package typescore + +import "time" + +type Suggestion struct { + ID *uint64 `gorm:"column:id;index;type:bigint;autoIncrement;not null;unique" json:"id" db:"id"` + InitiatorID *uint64 `gorm:"type:bigint;not null;index;column:initiator_id" json:"initiator_id" db:"initiator_id"` + DirectorID *uint64 `gorm:"type:bigint;not null;index;column:director_id" json:"director_id" db:"director_id"` + Subject *string `gorm:"type:text;not null;column:subject" json:"subject" db:"subject"` + Status *string `gorm:"type:text;not null;default:'open';column:status" json:"status" db:"status"` // open, closed + CreatedAt *time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;column:created_at" json:"created_at" db:"created_at"` + UpdatedAt *time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;column:updated_at" json:"updated_at" db:"updated_at"` + + Initiator *User `gorm:"-" json:"initiator,omitempty"` + Director *User `gorm:"-" json:"director,omitempty"` + Messages []*SuggestionMessage `gorm:"-" json:"messages,omitempty"` +} + +type SuggestionMessage struct { + ID *uint64 `gorm:"column:id;index;type:bigint;autoIncrement;not null;unique" json:"id" db:"id"` + SuggestionID *uint64 `gorm:"type:bigint;not null;index;column:suggestion_id" json:"suggestion_id" db:"suggestion_id"` + UserID *uint64 `gorm:"type:bigint;not null;index;column:user_id" json:"user_id" db:"user_id"` + Message *string `gorm:"type:text;not null;column:message" json:"message" db:"message"` + FileData *string `gorm:"type:text;column:file_data" json:"file_data,omitempty" db:"file_data"` // base64 + FileName *string `gorm:"type:text;column:file_name" json:"file_name,omitempty" db:"file_name"` + CreatedAt *time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP;column:created_at" json:"created_at" db:"created_at"` + + Suggestion *Suggestion `gorm:"-" json:"suggestion,omitempty"` + User *User `gorm:"-" json:"user,omitempty"` +}