approveflow/app/infrastructure/flow_repository.go

105 lines
4.3 KiB
Go

package infrastructure
import (
FlowDefinitionModule "approveflow/app/provider/flow_definition"
"context"
"github.com/Superdanda/hade/app/provider/database_connect"
"github.com/Superdanda/hade/framework"
"github.com/Superdanda/hade/framework/contract"
"github.com/Superdanda/hade/framework/provider/repository"
"gorm.io/gorm"
)
type FlowRepositoryImpl struct {
container framework.Container
db *gorm.DB
contract.OrmRepository[FlowDefinitionModule.ApprovalFlow, int64]
FlowDefinitionModule.FlowRepository
}
func (u *FlowRepositoryImpl) GetGenericRepository() contract.GenericRepository[FlowDefinitionModule.ApprovalFlow, int64] {
repositoryService := u.container.MustMake(contract.RepositoryKey).(contract.RepositoryService)
return repositoryService.GetGenericRepositoryByKey(FlowDefinitionModule.FlowRepositoryKey).(contract.GenericRepository[FlowDefinitionModule.ApprovalFlow, int64])
}
func NewOrmFlowRepositoryImplAndRegister(container framework.Container) {
// 获取必要的服务对象
connectService := container.MustMake(database_connect.DatabaseConnectKey).(database_connect.Service)
infrastructureService := container.MustMake(contract.InfrastructureKey).(contract.InfrastructureService)
repositoryService := container.MustMake(contract.RepositoryKey).(contract.RepositoryService)
connect := connectService.DefaultDatabaseConnect()
FlowRepositoryOrmService := &FlowRepositoryImpl{container: container, db: connect}
infrastructureService.RegisterOrmRepository(FlowDefinitionModule.FlowRepositoryKey, FlowRepositoryOrmService)
// 注册通用仓储对象
repository.RegisterRepository[FlowDefinitionModule.ApprovalFlow, int64](repositoryService, FlowDefinitionModule.FlowRepositoryKey, FlowRepositoryOrmService)
}
func (u *FlowRepositoryImpl) SaveToDB(entity *FlowDefinitionModule.ApprovalFlow) error {
// 先保存 ApprovalFlow 本身
if entity.ID != 0 {
// 使用FullSaveAssociations 方法来确保模型的整体状态,包括其所有关联都反映在了数据库中,从在应用中保持数据的完整性和一致性
return u.db.Session(&gorm.Session{FullSaveAssociations: true}).Updates(entity).Error
}
return u.db.Save(entity).Error
}
func (u *FlowRepositoryImpl) FindByIDFromDB(id int64) (*FlowDefinitionModule.ApprovalFlow, error) {
entity := &FlowDefinitionModule.ApprovalFlow{}
err := u.db.Preload("Steps").Preload("Steps.Rules").Preload("Steps.DynamicConfig").Preload("Steps.PathConfigs").First(entity, id).Error
return entity, err
}
func (u *FlowRepositoryImpl) FindByIDsFromDB(ids []int64) ([]*FlowDefinitionModule.ApprovalFlow, error) {
var entities []*FlowDefinitionModule.ApprovalFlow
err := u.db.Where("id IN ?", ids).Find(&entities).Error
return entities, err
}
func (u *FlowRepositoryImpl) GetPrimaryKey(entity *FlowDefinitionModule.ApprovalFlow) int64 {
return entity.ID
}
func (u *FlowRepositoryImpl) GetBaseField() string {
return FlowDefinitionModule.FlowRepositoryKey
}
func (u *FlowRepositoryImpl) GetFieldQueryFunc(fieldName string) (func(value string) ([]*FlowDefinitionModule.ApprovalFlow, error), bool) {
switch fieldName {
// 根据您的实际情况添加字段查询函数
default:
return nil, false
}
}
func (u *FlowRepositoryImpl) GetFieldInQueryFunc(fieldName string) (func(values []string) ([]*FlowDefinitionModule.ApprovalFlow, error), bool) {
switch fieldName {
// 根据您的实际情况添加字段批量查询函数
default:
return nil, false
}
}
func (u *FlowRepositoryImpl) GetFieldValueFunc(fieldName string) (func(entity *FlowDefinitionModule.ApprovalFlow) string, bool) {
switch fieldName {
// 根据您的实际情况添加获取字段值的函数
default:
return nil, false
}
}
// SaveFlow 保存审批流程模板
func (u *FlowRepositoryImpl) SaveFlow(ctx context.Context, flow *FlowDefinitionModule.ApprovalFlow) error {
return u.GetGenericRepository().Save(ctx, flow)
}
// GetFlowByID 根据流程 ID 获取审批流程模板
func (u *FlowRepositoryImpl) GetFlowByID(ctx context.Context, id int64) (*FlowDefinitionModule.ApprovalFlow, error) {
return u.GetGenericRepository().FindByID(ctx, id)
}
func (u *FlowRepositoryImpl) ListFlows(ctx context.Context, pageNum, pageSize int, flow *FlowDefinitionModule.ApprovalFlow) ([]*FlowDefinitionModule.ApprovalFlow, int64, error) {
return ListFlows[FlowDefinitionModule.ApprovalFlow](u.db, pageNum, pageSize, flow)
}