|
|
package repository
|
|
|
|
|
|
import (
|
|
|
"fmt"
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
|
|
|
"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"github.com/linmadan/egglib-go/utils/snowflake"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/transform"
|
|
|
)
|
|
|
|
|
|
type UsersRepository struct {
|
|
|
transactionContext *pgTransaction.TransactionContext
|
|
|
}
|
|
|
|
|
|
func (repository *UsersRepository) nextIdentify() (int64, error) {
|
|
|
IdWorker, err := snowflake.NewIdWorker(1)
|
|
|
if err != nil {
|
|
|
return 0, err
|
|
|
}
|
|
|
id, err := IdWorker.NextId()
|
|
|
return id, err
|
|
|
}
|
|
|
func (repository *UsersRepository) Save(users *domain.Users) (*domain.Users, error) {
|
|
|
sqlBuildFields := []string{
|
|
|
"users_id",
|
|
|
"company_id",
|
|
|
"users_base_id",
|
|
|
"users_type",
|
|
|
"users_code",
|
|
|
"organization_id",
|
|
|
"department_id",
|
|
|
"users_info",
|
|
|
"users_org",
|
|
|
"users_role",
|
|
|
"favorite_menus",
|
|
|
"cooperation_info",
|
|
|
"enable_status",
|
|
|
"ext",
|
|
|
"created_at",
|
|
|
"updated_at",
|
|
|
}
|
|
|
insertFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
insertPlaceHoldersSnippet := sqlbuilder.SqlPlaceHoldersSnippet(sqlBuildFields)
|
|
|
returningFieldsSnippet := sqlbuilder.SqlFieldsSnippet(sqlBuildFields)
|
|
|
updateFields := sqlbuilder.RemoveSqlFields(sqlBuildFields, "users_id")
|
|
|
updateFieldsSnippet := sqlbuilder.SqlUpdateFieldsSnippet(updateFields)
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
if users.Identify() == nil {
|
|
|
usersId, err := repository.nextIdentify()
|
|
|
if err != nil {
|
|
|
return users, err
|
|
|
} else {
|
|
|
users.UsersId = usersId
|
|
|
}
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&users.UsersId,
|
|
|
&users.CompanyId,
|
|
|
&users.UsersBaseId,
|
|
|
&users.UsersType,
|
|
|
&users.UsersCode,
|
|
|
&users.OrganizationId,
|
|
|
&users.DepartmentId,
|
|
|
pg.Array(&users.UsersInfo),
|
|
|
pg.Array(&users.UsersOrg),
|
|
|
pg.Array(&users.UsersRole),
|
|
|
pg.Array(&users.FavoriteMenus),
|
|
|
&users.CooperationInfo,
|
|
|
&users.EnableStatus,
|
|
|
&users.Ext,
|
|
|
&users.CreatedAt,
|
|
|
&users.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("INSERT INTO userss (%s) VALUES (%s) RETURNING %s", insertFieldsSnippet, insertPlaceHoldersSnippet, returningFieldsSnippet),
|
|
|
users.UsersId,
|
|
|
users.CompanyId,
|
|
|
users.UsersBaseId,
|
|
|
users.UsersType,
|
|
|
users.UsersCode,
|
|
|
users.OrganizationId,
|
|
|
users.DepartmentId,
|
|
|
pg.Array(users.UsersInfo),
|
|
|
pg.Array(users.UsersOrg),
|
|
|
pg.Array(users.UsersRole),
|
|
|
pg.Array(users.FavoriteMenus),
|
|
|
users.CooperationInfo,
|
|
|
users.EnableStatus,
|
|
|
users.Ext,
|
|
|
users.CreatedAt,
|
|
|
users.UpdatedAt,
|
|
|
); err != nil {
|
|
|
return users, err
|
|
|
}
|
|
|
} else {
|
|
|
if _, err := tx.QueryOne(
|
|
|
pg.Scan(
|
|
|
&users.UsersId,
|
|
|
&users.CompanyId,
|
|
|
&users.UsersBaseId,
|
|
|
&users.UsersType,
|
|
|
&users.UsersCode,
|
|
|
&users.OrganizationId,
|
|
|
&users.DepartmentId,
|
|
|
pg.Array(&users.UsersInfo),
|
|
|
pg.Array(&users.UsersOrg),
|
|
|
pg.Array(&users.UsersRole),
|
|
|
pg.Array(&users.FavoriteMenus),
|
|
|
&users.CooperationInfo,
|
|
|
&users.EnableStatus,
|
|
|
&users.Ext,
|
|
|
&users.CreatedAt,
|
|
|
&users.UpdatedAt,
|
|
|
),
|
|
|
fmt.Sprintf("UPDATE userss SET %s WHERE users_id=? RETURNING %s", updateFieldsSnippet, returningFieldsSnippet),
|
|
|
users.CompanyId,
|
|
|
users.UsersBaseId,
|
|
|
users.UsersType,
|
|
|
users.UsersCode,
|
|
|
users.OrganizationId,
|
|
|
users.DepartmentId,
|
|
|
pg.Array(users.UsersInfo),
|
|
|
pg.Array(users.UsersOrg),
|
|
|
pg.Array(users.UsersRole),
|
|
|
pg.Array(users.FavoriteMenus),
|
|
|
users.CooperationInfo,
|
|
|
users.EnableStatus,
|
|
|
users.Ext,
|
|
|
users.CreatedAt,
|
|
|
users.UpdatedAt,
|
|
|
users.Identify(),
|
|
|
); err != nil {
|
|
|
return users, err
|
|
|
}
|
|
|
}
|
|
|
return users, nil
|
|
|
}
|
|
|
func (repository *UsersRepository) Remove(users *domain.Users) (*domain.Users, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
usersModel := new(models.Users)
|
|
|
usersModel.UsersId = users.Identify().(int64)
|
|
|
if _, err := tx.Model(usersModel).WherePK().Delete(); err != nil {
|
|
|
return users, err
|
|
|
}
|
|
|
return users, nil
|
|
|
}
|
|
|
func (repository *UsersRepository) FindOne(queryOptions map[string]interface{}) (*domain.Users, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
usersModel := new(models.Users)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(usersModel), queryOptions)
|
|
|
query.SetWhereByQueryOption("users.users_id = ?", "usersId")
|
|
|
if err := query.First(); err != nil {
|
|
|
if err.Error() == "pg: no rows in result set" {
|
|
|
return nil, fmt.Errorf("没有此资源")
|
|
|
} else {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
if usersModel.UsersId == 0 {
|
|
|
return nil, nil
|
|
|
} else {
|
|
|
return transform.TransformToUsersDomainModelFromPgModels(usersModel)
|
|
|
}
|
|
|
}
|
|
|
func (repository *UsersRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.Users, error) {
|
|
|
tx := repository.transactionContext.PgTx
|
|
|
var usersModels []*models.Users
|
|
|
userss := make([]*domain.Users, 0)
|
|
|
query := sqlbuilder.BuildQuery(tx.Model(&usersModels), queryOptions)
|
|
|
query.SetOffsetAndLimit(20)
|
|
|
query.SetOrderDirect("users_id", "DESC")
|
|
|
if count, err := query.SelectAndCount(); err != nil {
|
|
|
return 0, userss, err
|
|
|
} else {
|
|
|
for _, usersModel := range usersModels {
|
|
|
if users, err := transform.TransformToUsersDomainModelFromPgModels(usersModel); err != nil {
|
|
|
return 0, userss, err
|
|
|
} else {
|
|
|
userss = append(userss, users)
|
|
|
}
|
|
|
}
|
|
|
return int64(count), userss, nil
|
|
|
}
|
|
|
}
|
|
|
func NewUsersRepository(transactionContext *pgTransaction.TransactionContext) (*UsersRepository, error) {
|
|
|
if transactionContext == nil {
|
|
|
return nil, fmt.Errorf("transactionContext参数不能为nil")
|
|
|
} else {
|
|
|
return &UsersRepository{
|
|
|
transactionContext: transactionContext,
|
|
|
}, nil
|
|
|
}
|
|
|
} |
...
|
...
|
|