...
|
...
|
@@ -2,9 +2,12 @@ package repository |
|
|
|
|
|
import (
|
|
|
"errors"
|
|
|
"time"
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
|
|
pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
|
|
|
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
|
|
|
)
|
|
|
|
|
|
type UserRepository struct {
|
...
|
...
|
@@ -14,23 +17,97 @@ type UserRepository struct { |
|
|
var _ domain.UserRepository = (*UserRepository)(nil)
|
|
|
|
|
|
func (repo *UserRepository) Save(user *domain.User) (*domain.User, error) {
|
|
|
userModel := models.User{
|
|
|
Id: user.Id,
|
|
|
Account: user.Account,
|
|
|
AvatarUrl: user.AvatarUrl,
|
|
|
CompanyId: user.CompanyId,
|
|
|
IsPrincipal: user.IsPrincipal,
|
|
|
AdminType: user.AdminType,
|
|
|
Name: user.Name,
|
|
|
RoleId: user.RoleId,
|
|
|
Status: user.Status,
|
|
|
UpdateAt: user.UpdateAt,
|
|
|
CreateAt: user.CreateAt,
|
|
|
DeleteAt: user.DeleteAt,
|
|
|
}
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
if userModel.Id == 0 {
|
|
|
_, err := tx.Model(&userModel).Insert()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
user.Id = userModel.Id
|
|
|
} else {
|
|
|
_, err := tx.Model(&userModel).WherePK().Update()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
}
|
|
|
return user, nil
|
|
|
}
|
|
|
|
|
|
func (repo *UserRepository) Remove(user *domain.User) (*domain.User, error) {
|
|
|
// tx := repository.transactionContext.PgTx
|
|
|
// userModel := new(models.User)
|
|
|
// userModel.UserId = user.Identify().(int64)
|
|
|
// if _, err := tx.Model(userModel).WherePK().Delete(); err != nil {
|
|
|
// return user, err
|
|
|
// }
|
|
|
return user, errors.New("no support")
|
|
|
nowTime := time.Now()
|
|
|
user.DeleteAt = &nowTime
|
|
|
_, err := repo.Save(user)
|
|
|
return user, err
|
|
|
}
|
|
|
|
|
|
func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
|
|
|
return nil, nil
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
userModel := models.User{}
|
|
|
query := tx.Model(&userModel)
|
|
|
if v, ok := queryOptions["id"]; ok {
|
|
|
query.Where("id=?", v)
|
|
|
}
|
|
|
err := query.First()
|
|
|
if err == pg.ErrNoRows {
|
|
|
return nil, errors.New("user 不存在")
|
|
|
}
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
result := repo.TransformToUserDomain(&userModel)
|
|
|
return result, nil
|
|
|
}
|
|
|
|
|
|
func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.User, error) {
|
|
|
return 0, nil, nil
|
|
|
func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*domain.User, error) {
|
|
|
tx := repo.transactionContext.PgTx
|
|
|
userModel := []models.User{}
|
|
|
query := tx.Model(&userModel).
|
|
|
Limit(20)
|
|
|
if v, ok := queryOptions["limit"]; ok {
|
|
|
query.Limit(v.(int))
|
|
|
}
|
|
|
if v, ok := queryOptions["offset"]; ok {
|
|
|
query.Offset(v.(int))
|
|
|
}
|
|
|
cnt, err := query.SelectAndCount()
|
|
|
if err != nil {
|
|
|
return 0, nil, err
|
|
|
}
|
|
|
var resultList []*domain.User
|
|
|
for i := range userModel {
|
|
|
result := repo.TransformToUserDomain(&userModel[i])
|
|
|
resultList = append(resultList, result)
|
|
|
}
|
|
|
return cnt, resultList, nil
|
|
|
}
|
|
|
|
|
|
func (repo *UserRepository) TransformToUserDomain(user *models.User) *domain.User {
|
|
|
return &domain.User{
|
|
|
Id: user.Id,
|
|
|
Account: user.Account,
|
|
|
AvatarUrl: user.AvatarUrl,
|
|
|
CompanyId: user.CompanyId,
|
|
|
IsPrincipal: user.IsPrincipal,
|
|
|
AdminType: user.AdminType,
|
|
|
Name: user.Name,
|
|
|
RoleId: user.RoleId,
|
|
|
Status: user.Status,
|
|
|
UpdateAt: user.UpdateAt,
|
|
|
CreateAt: user.CreateAt,
|
|
|
DeleteAt: user.DeleteAt,
|
|
|
}
|
|
|
} |
...
|
...
|
|