pg_users_repository.go 4.9 KB
package repository

import (
	"errors"
	"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 map[string]interface{}) (domain.Users, error) {
	if len(queryOptions) == 0 {
		return domain.Users{}, errors.New("queryOptions is empty")
	}
	var (
		err error
		tx  = reponsitory.transactionContext.PgTx
		m   models.Users
	)
	query := tx.Model(&m)
	query.Where("deleted_at ISNULL")
	if v, ok := queryOptions["Id"]; ok {
		query = query.Where("id=?", v)
	}
	if v, ok := queryOptions["phone"]; ok {
		query = query.Where("phone=?", v)
	}
	err = query.First()
	if err != nil {
		return domain.Users{}, err
	}
	return reponsitory.transformPgModelToDomainModel(&m)
}

func (reponsitory UsersRepository) Find(queryOption map[string]interface{}) (int, []domain.Users, error) {
	db := reponsitory.transactionContext.PgTx
	usersModels := []models.Users{}
	query := db.Model(&usersModels)
	query.Where("deleted_at ISNULL")
	if v, ok := queryOption["CompanyId"]; ok {
		query = query.Where("company_id=?", v)
	}
	if v, ok := queryOption["ChargeStatus"]; ok {
		query = query.Where("charge_status=?", v)
	}
	if v, ok := queryOption["AdminType"]; ok {
		query = query.Where("admin_type=?", v)
	}
	if v, ok := queryOption["Offset"]; ok {
		offset := v.(int)
		query = query.Offset(offset)
	}
	if v, ok := queryOption["Limit"]; ok {
		limit := v.(int)
		if limit > 0 {
			query = query.Limit(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
}