approveflow/app/infrastructure/Instance_repository.go

107 lines
4.6 KiB
Go

package infrastructure
import (
"approveflow/app/provider/database_connect"
InstanceModule "approveflow/app/provider/flow_instance"
InstanceModel "approveflow/app/provider/flow_instance/model"
"context"
"github.com/Superdanda/hade/framework"
"github.com/Superdanda/hade/framework/contract"
"github.com/Superdanda/hade/framework/provider/repository"
"gorm.io/gorm"
)
type InstanceRepositoryImpl struct {
container framework.Container
db *gorm.DB
contract.OrmRepository[InstanceModel.ApprovalInstance, int64]
InstanceModule.FlowInstanceRepository
}
func (u *InstanceRepositoryImpl) GetGenericRepository() contract.GenericRepository[InstanceModel.ApprovalInstance, int64] {
repositoryService := u.container.MustMake(contract.RepositoryKey).(contract.RepositoryService)
return repositoryService.GetGenericRepositoryByKey(InstanceModule.InstanceRepositoryKey).(contract.GenericRepository[InstanceModel.ApprovalInstance, int64])
}
func NewOrmInstanceRepositoryImplAndRegister(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()
InstanceRepositoryOrmService := &InstanceRepositoryImpl{container: container, db: connect}
infrastructureService.RegisterOrmRepository(InstanceModule.InstanceRepositoryKey, InstanceRepositoryOrmService)
// 注册通用仓储对象
repository.RegisterRepository[InstanceModel.ApprovalInstance, int64](repositoryService, InstanceModule.InstanceRepositoryKey, InstanceRepositoryOrmService)
}
func (u *InstanceRepositoryImpl) SaveToDB(entity *InstanceModel.ApprovalInstance) 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 *InstanceRepositoryImpl) FindByIDFromDB(id int64) (*InstanceModel.ApprovalInstance, error) {
entity := &InstanceModel.ApprovalInstance{}
err := u.db.Preload("CurrentStepIDs").Preload("StatusEvents").Preload("Steps").Preload("Steps.StatusEvents").Preload("Steps.Records").Preload("Steps.InstancePathConfigs").First(entity, id).Error
return entity, err
}
func (u *InstanceRepositoryImpl) FindByIDsFromDB(ids []int64) ([]*InstanceModel.ApprovalInstance, error) {
var entities []*InstanceModel.ApprovalInstance
err := u.db.Where("id IN ?", ids).Preload("StatusEvents").Preload("CurrentStepIDs").Preload("Steps").Preload("Steps.StatusEvents").Preload("Steps.Records").Preload("Steps.InstancePathConfigs").Find(&entities).Error
return entities, err
}
func (u *InstanceRepositoryImpl) GetPrimaryKey(entity *InstanceModel.ApprovalInstance) int64 {
return entity.ID
}
func (u *InstanceRepositoryImpl) GetBaseField() string {
return InstanceModule.FlowInstanceKey
}
func (u *InstanceRepositoryImpl) GetFieldQueryFunc(fieldName string) (func(value string) ([]*InstanceModel.ApprovalInstance, error), bool) {
switch fieldName {
// 根据您的实际情况添加字段查询函数
default:
return nil, false
}
}
func (u *InstanceRepositoryImpl) GetFieldInQueryFunc(fieldName string) (func(values []string) ([]*InstanceModel.ApprovalInstance, error), bool) {
switch fieldName {
// 根据您的实际情况添加字段批量查询函数
default:
return nil, false
}
}
func (u *InstanceRepositoryImpl) GetFieldValueFunc(fieldName string) (func(entity *InstanceModel.ApprovalInstance) string, bool) {
switch fieldName {
// 根据您的实际情况添加获取字段值的函数
default:
return nil, false
}
}
// SaveInstance 保存审批流程实例
func (u *InstanceRepositoryImpl) SaveInstance(ctx context.Context, flow *InstanceModel.ApprovalInstance) error {
return u.GetGenericRepository().Save(ctx, flow)
}
// GetInstanceByID 根据流程 ID 获取审批流程实例
func (u *InstanceRepositoryImpl) GetInstanceByID(ctx context.Context, id int64) (*InstanceModel.ApprovalInstance, error) {
return u.GetGenericRepository().FindByID(ctx, id)
}
// GetInstancePage 实例分页查询
func (u *InstanceRepositoryImpl) GetInstancePage(ctx context.Context, pageNum, pageSize int, instance *InstanceModel.ApprovalInstance) ([]*InstanceModel.ApprovalInstance, int64, error) {
return ListFlows[InstanceModel.ApprovalInstance](u.db, pageNum, pageSize, instance)
}