pg_users_repository.go 4.6 KB
package repository

import (
	"fmt"
	"time"

	"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 UsersRepository struct {
	transactionContext *transaction.TransactionContext
}

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

func NewUsersRepository(transactionContext *transaction.TransactionContext) (*UsersRepository, error) {
	if transactionContext == nil {
		return nil, fmt.Errorf("transactionContext参数不能为nil")
	}
	return &UsersRepository{transactionContext: transactionContext}, nil
}

func (repository UsersRepository) transformPgModelToDomainModel(m *models.Users) (domain.Users, error) {
	return domain.Users{
		Id:           m.Id,
		CompanyId:    m.CompanyId,
		OpenId:       m.OpenId,
		Name:         m.Name,
		Sex:          m.Sex,
		JobNum:       m.JobNum,
		Phone:        m.Phone,
		PrivatePhone: m.PrivatePhone,
		Email:        m.Email,
		ExtensionNum: m.ExtensionNum,
		EntryTime:    m.EntryTime,
		Workspace:    m.Workspace,
		Status:       m.Status,
		Avatar:       m.Avatar,
		Remarks:      m.Remarks,
		ChargeStatus: m.ChargeStatus,
		CreateAt:     m.CreateAt,
		UpdateAt:     m.UpdateAt,
		Permission:   m.Permission,
	}, nil
}

func (reponsitory UsersRepository) Add(u *domain.Users) error {
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
	)
	m := &models.Users{
		Id:             u.Id,
		CompanyId:      u.CompanyId,
		OpenId:         u.OpenId,
		Name:           u.Name,
		Sex:            u.Sex,
		JobNum:         u.JobNum,
		Phone:          u.Phone,
		PrivatePhone:   u.PrivatePhone,
		Email:          u.Email,
		ExtensionNum:   u.ExtensionNum,
		EntryTime:      u.EntryTime,
		Workspace:      u.Workspace,
		Status:         u.Status,
		Avatar:         u.Avatar,
		Remarks:        u.Remarks,
		ChargeStatus:   u.ChargeStatus,
		CreateAt:       u.CreateAt,
		UpdateAt:       u.UpdateAt,
		Permission:     u.Permission,
		AccessPartners: u.AccessPartners,
	}
	_, err = tx.Model(m).Insert()
	return err
}

func (reponsitory UsersRepository) Edit(u *domain.Users) error {
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
	)
	m := &models.Users{
		Id:             u.Id,
		CompanyId:      u.CompanyId,
		OpenId:         u.OpenId,
		Name:           u.Name,
		Sex:            u.Sex,
		JobNum:         u.JobNum,
		Phone:          u.Phone,
		PrivatePhone:   u.PrivatePhone,
		Email:          u.Email,
		ExtensionNum:   u.ExtensionNum,
		EntryTime:      u.EntryTime,
		Workspace:      u.Workspace,
		Status:         u.Status,
		Avatar:         u.Avatar,
		Remarks:        u.Remarks,
		ChargeStatus:   u.ChargeStatus,
		CreateAt:       u.CreateAt,
		UpdateAt:       u.UpdateAt,
		Permission:     u.Permission,
		AccessPartners: u.AccessPartners,
	}
	_, err = tx.Model(m).WherePK().Update()
	return err
}

func (reponsitory UsersRepository) FindOne(queryOptions domain.UsersFindOneQuery) (domain.Users, error) {
	var (
		err      error
		tx       = reponsitory.transactionContext.PgTx
		m        models.Users
		hasWhere bool
	)
	query := tx.Model(&m)
	if queryOptions.Id > 0 {
		query = query.Where("id=?", queryOptions.Id)
		hasWhere = true
	}
	if len(queryOptions.Phone) > 0 {
		query = query.Where("phone=?", queryOptions.Id)
		hasWhere = true
	}
	if !hasWhere {
		return domain.Users{}, fmt.Errorf("queryOptions 中没有搜索条件")
	}
	err = query.First()
	if err != nil {
		return domain.Users{}, err
	}
	return reponsitory.transformPgModelToDomainModel(&m)
}

func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) {
	db := reponsitory.transactionContext.PgTx
	usersModels := []models.Users{}
	query := db.Model(&usersModels)
	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
		usersReturn = make([]domain.Users, 0)
		cnt         int
	)
	query = query.Order("id DESC")
	cnt, err = query.SelectAndCount()
	if err != nil {
		return 0, usersReturn, err
	}
	for i := range usersModels {
		domainUsers, err := reponsitory.transformPgModelToDomainModel(&usersModels[i])
		if err != nil {
			return 0, usersReturn, err
		}
		usersReturn = append(usersReturn, domainUsers)
	}
	return cnt, usersReturn, nil
}

func (reponsitory UsersRepository) Remove(ids []int64) error {
	if len(ids) == 0 {
		return nil
	}
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
	)
	_, err = tx.Model(&models.Users{}).
		Set("delete_at=?", time.Now()).
		WhereIn("id in(?)", ids).
		Update()
	return err
}