pg_users_repository.go 6.9 KB
package repository

import (
	"errors"
	"fmt"
	"time"

	"github.com/go-pg/pg/v10"
	"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,
		AccessPartners: m.AccessPartners,
		AdminType:      m.AdminType,
		IsSenior:       m.IsSenior,
	}, 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,
		AdminType:      u.AdminType,
		IsSenior:       u.IsSenior,
	}
	_, 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,
		AdminType:      u.AdminType,
		IsSenior:       u.IsSenior,
	}
	_, 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
		hasCondition bool
	)
	query := tx.Model(&m)
	query = query.Where("delete_at ISNULL")
	if queryOptions.Id > 0 {
		hasCondition = true
		query = query.Where("id=?", queryOptions.Id)
	}
	if len(queryOptions.Phone) > 0 {
		hasCondition = true
		query = query.Where("phone=?", queryOptions.Phone)
	}
	if queryOptions.CompanyId > 0 {
		hasCondition = true
		query = query.Where("company_id=?", queryOptions.CompanyId)
	}
	if queryOptions.OpenId > 0 {
		hasCondition = true
		query = query.Where("open_id=?", queryOptions.OpenId)
	}
	if !hasCondition {
		return domain.Users{}, errors.New("UsersRepository.FindOne 必须要有查询条件")
	}
	err = query.First()
	if err != nil {
		return domain.Users{}, err
	}
	return reponsitory.transformPgModelToDomainModel(&m)
}

//FindOneWithExist 查询一个用户,
//当数据不存在时不返回错误,替代的是返回bool,true表示数据存在,false表示数据不存在
func (reponsitory UsersRepository) FindOneWithExist(queryOptions domain.UsersFindOneQuery) (*domain.Users, bool, error) {
	var (
		err          error
		tx           = reponsitory.transactionContext.PgTx
		m            models.Users
		hasCondition bool
	)
	query := tx.Model(&m)
	query = query.Where("delete_at ISNULL")
	if queryOptions.Id > 0 {
		hasCondition = true
		query = query.Where("id=?", queryOptions.Id)
	}
	if len(queryOptions.Phone) > 0 {
		hasCondition = true
		query = query.Where("phone=?", queryOptions.Phone)
	}
	if queryOptions.CompanyId > 0 {
		hasCondition = true
		query = query.Where("company_id=?", queryOptions.CompanyId)
	}
	if queryOptions.OpenId > 0 {
		hasCondition = true
		query = query.Where("open_id=?", queryOptions.OpenId)
	}
	if !hasCondition {
		return nil, false, errors.New("UsersRepository.FindOne 必须要有查询条件")
	}
	err = query.First()
	if err != nil && err == pg.ErrNoRows {
		return nil, false, nil
	}
	if err != nil {
		return nil, false, err
	}
	searchResult, err := reponsitory.transformPgModelToDomainModel(&m)
	return &searchResult, true, err
}

func (reponsitory UsersRepository) Find(queryOption domain.UsersFindQuery) (int, []domain.Users, error) {
	db := reponsitory.transactionContext.PgTx
	usersModels := []models.Users{}
	query := db.Model(&usersModels)
	query.Where("delete_at ISNULL")
	if queryOption.CompanyId > 0 {
		query = query.Where("company_id=?", queryOption.CompanyId)
	}
	if queryOption.ChargeStatus > 0 {
		query = query.Where("charge_status=?", queryOption.ChargeStatus)
	}
	if queryOption.AdminType > 0 {
		query = query.Where("admin_type=?", queryOption.AdminType)
	}
	if len(queryOption.UserNameMatch) > 0 {
		query = query.Where("name like ?", "%"+queryOption.UserNameMatch+"%")
	}
	if queryOption.Offset >= 0 {
		query = query.Offset(queryOption.Offset)
	}
	if queryOption.Limit > 0 {
		query = query.Limit(queryOption.Limit)
	} else {
		query = query.Limit(1000)
	}
	var (
		err         error
		usersReturn = make([]domain.Users, 0)
		cnt         int
	)
	//query = query.Order("id DESC")
	query = query.Order("admin_type 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
}