approveflow/app/infrastructure/Instance_repository.go

102 lines
4.0 KiB
Go
Raw Normal View History

2024-11-14 17:02:41 +08:00
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 {
return u.db.Save(entity).Error
}
func (u *InstanceRepositoryImpl) FindByIDFromDB(id int64) (*InstanceModel.ApprovalInstance, error) {
entity := &InstanceModel.ApprovalInstance{}
err := u.db.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).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)
}