pg_admin_user_reponsitory.go 4.5 KB
package repository

import (
	"fmt"

	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)

type AdminUserRepository struct {
	transactionContext *transaction.TransactionContext
}

var (
	_ domain.AdminUserRepository = (*AdminUserRepository)(nil)
)

func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &AdminUserRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) {
	result := domain.AdminUser{
		Id:         adminuserModel.Id,
		Account:    adminuserModel.Account,
		AdminName:  adminuserModel.AdminName,
		IsDefault:  adminuserModel.IsDefault,
		CreateAt:   adminuserModel.CreateAt,
		IsUsable:   adminuserModel.IsUsable,
		Password:   adminuserModel.Password,
		Permission: adminuserModel.Permission,
	}
	return result, nil
}

func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindOneQuery) (*domain.AdminUser, error) {
	db := reponsitory.transactionContext.PgDd
	adminuserModel := new(models.AdminUser)
	query := db.Model(adminuserModel)

	if queryOption.AdminUserId > 0 {
		query = query.Where("id=?", queryOption.AdminUserId)
	}
	if len(queryOption.AccountEqual) > 0 {
		query = query.Where("account=?", queryOption.AccountEqual)
	}
	err := query.First()
	if err != nil {
		return nil, err
	}
	adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel)
	return &adminUser, err
}

func (reponsitory AdminUserRepository) updateAdminUser(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
	tx := reponsitory.transactionContext.PgTx
	adminUserModel := &models.AdminUser{
		Id:         adminuser.Id,
		Account:    adminuser.Account,
		AdminName:  adminuser.AdminName,
		Password:   adminuser.Password,
		IsUsable:   adminuser.IsUsable,
		Permission: adminuser.Permission,
	}
	_, err := tx.Model(adminUserModel).
		Where("id=?", adminUserModel.Id).
		Column("admin_name", "account", "password", "is_usable", "permission").
		Update()
	if err != nil {
		return nil, fmt.Errorf("更新用户数据失败:%s", err)
	}
	return adminuser, nil
}

func (reponsitory AdminUserRepository) addAdminUser(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
	tx := reponsitory.transactionContext.PgTx
	adminuserModel := &models.AdminUser{
		Account:    adminuser.Account,
		AdminName:  adminuser.AdminName,
		Password:   adminuser.Password,
		IsDefault:  false,
		IsUsable:   true,
		Permission: adminuser.Permission,
	}
	//添加用户数据
	_, err := tx.Model(adminuserModel).Insert()
	if err != nil {
		return nil, err
	}
	return nil, nil
}

func (reponsitory AdminUserRepository) Save(adminuser *domain.AdminUser) (*domain.AdminUser, error) {
	if adminuser.Id == 0 {
		return reponsitory.addAdminUser(adminuser)
	} else {
		return reponsitory.updateAdminUser(adminuser)
	}
	return adminuser, nil
}

func (reponsitory AdminUserRepository) Find(queryOption domain.AdminUserFindQuery) ([]domain.AdminUser, error) {
	db := reponsitory.transactionContext.PgDd
	adminuserModels := []models.AdminUser{}
	query := db.Model(&adminuserModels)
	if len(queryOption.AccountLike) > 0 {
		query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
	}

	if queryOption.Offset > -1 {
		query = query.Offset(queryOption.Offset)
	}
	if queryOption.Limit > 0 {
		query = query.Limit(queryOption.Limit)
	} else {
		query = query.Limit(20)
	}
	var (
		err             error
		adminuserReturn = make([]domain.AdminUser, 0)
	)
	query = query.Order("admin_user.id DESC ")
	err = query.Select()
	if err != nil {
		return adminuserReturn, err
	}
	for i := range adminuserModels {
		domainAdminUser, err := reponsitory.transformPgModelToDomainModel(&adminuserModels[i])
		if err != nil {
			return adminuserReturn, err
		}
		adminuserReturn = append(adminuserReturn, domainAdminUser)
	}
	return adminuserReturn, nil
}

func (reponsitory AdminUserRepository) CountAll(queryOption domain.AdminUserFindQuery) (int, error) {
	db := reponsitory.transactionContext.PgDd
	adminuserModels := models.AdminUser{}
	query := db.Model(&adminuserModels)
	if len(queryOption.AccountLike) > 0 {
		query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
	}
	cnt, err := query.Count()
	return cnt, err
}