审查视图

pkg/infrastructure/repository/pg_user_repository.go 5.3 KB
tangxvhui authored
1 2 3
package repository

import (
tangxvhui authored
4
	"errors"
Your Name authored
5 6
	"fmt"
tangxvhui authored
7
	"github.com/go-pg/pg/v10"
tangxvhui authored
8 9
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
tangxvhui authored
10
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
tangxvhui authored
11 12 13 14 15 16 17 18
)

type UserRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.UserRepository = (*UserRepository)(nil)
tangxvhui authored
19 20 21 22 23 24 25
func NewUserRepository(tx *pgTransaction.TransactionContext) *UserRepository {
	return &UserRepository{
		transactionContext: tx,
	}
}

func (repo *UserRepository) Insert(user *domain.User) (*domain.User, error) {
tangxvhui authored
26
	userModel := models.User{
庄敏学 authored
27 28 29 30 31 32
		Id:           user.Id,
		Account:      user.Account,
		AvatarUrl:    user.AvatarUrl,
		CompanyId:    user.CompanyId,
		AdminType:    user.AdminType,
		Name:         user.Name,
Your Name authored
33
		Email:        user.Email,
庄敏学 authored
34
		Status:       user.Status,
Your Name authored
35 36 37
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
郑周 authored
38
		ParentId:     user.ParentId,
庄敏学 authored
39
		CreatedAt:    user.CreatedAt,
Your Name authored
40
		UpdatedAt:    user.UpdatedAt,
庄敏学 authored
41
		DeletedAt:    user.DeletedAt,
tangxvhui authored
42 43
	}
	tx := repo.transactionContext.PgTx
tangxvhui authored
44 45 46 47 48 49 50 51 52 53
	_, err := tx.Model(&userModel).Insert()
	if err != nil {
		return nil, err
	}
	user.Id = userModel.Id
	return user, nil
}

func (repo *UserRepository) Update(user *domain.User) (*domain.User, error) {
	userModel := models.User{
庄敏学 authored
54 55 56 57 58 59 60 61
		Id:           user.Id,
		Account:      user.Account,
		AvatarUrl:    user.AvatarUrl,
		CompanyId:    user.CompanyId,
		AdminType:    user.AdminType,
		Name:         user.Name,
		Email:        user.Email,
		Status:       user.Status,
Your Name authored
62 63 64
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
郑周 authored
65
		ParentId:     user.ParentId,
庄敏学 authored
66
		CreatedAt:    user.CreatedAt,
Your Name authored
67
		UpdatedAt:    user.UpdatedAt,
庄敏学 authored
68
		DeletedAt:    user.DeletedAt,
tangxvhui authored
69 70 71 72 73
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&userModel).WherePK().Update()
	if err != nil {
		return nil, err
tangxvhui authored
74
	}
tangxvhui authored
75 76 77
	return user, nil
}
tangxvhui authored
78 79 80
func (repo *UserRepository) Remove(userId []int64) error {
	tx := repo.transactionContext.PgTx
	uModel := models.User{}
庄敏学 authored
81
	_, err := tx.Model(&uModel).Where("id in (?)", pg.In(userId)).Delete()
tangxvhui authored
82
	return err
tangxvhui authored
83 84 85
}

func (repo *UserRepository) FindOne(queryOptions map[string]interface{}) (*domain.User, error) {
tangxvhui authored
86 87
	tx := repo.transactionContext.PgTx
	userModel := models.User{}
Your Name authored
88
	query := tx.Model(&userModel).Where("deleted_at isnull")
tangxvhui authored
89 90 91
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
庄敏学 authored
92 93 94
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
	}
tangxvhui authored
95
	err := query.First()
tangxvhui authored
96 97
	if errors.Is(err, pg.ErrNoRows) {
		return nil, fmt.Errorf("没有找到user数据")
tangxvhui authored
98 99 100 101 102 103
	}
	if err != nil {
		return nil, err
	}
	result := repo.TransformToUserDomain(&userModel)
	return result, nil
tangxvhui authored
104 105
}
tangxvhui authored
106 107 108
func (repo *UserRepository) Find(queryOptions map[string]interface{}) (int, []*domain.User, error) {
	tx := repo.transactionContext.PgTx
	userModel := []models.User{}
郑周 authored
109
	query := tx.Model(&userModel).Where("deleted_at isnull")
tangxvhui authored
110 111 112
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
tangxvhui authored
113 114 115 116 117 118 119 120 121 122 123
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id=?", v)
	}
	if v, ok := queryOptions["adminType"]; ok {
		query.Where("admin_type=?", v)
	}
	if v, ok := queryOptions["account"]; ok {
		query.Where("account like ?", v)
tangxvhui authored
124
	}
郑周 authored
125 126 127
	if v, ok := queryOptions["accounts"]; ok {
		query.Where("account in(?)", pg.In(v))
	}
128 129 130
	if v, ok := queryOptions["status"]; ok {
		query.Where("status=?", v)
	}
郑周 authored
131 132 133
	if v, ok := queryOptions["departmentId"]; ok {
		query.Where(`department_id @>'[?]'`, v)
	}
郑周 authored
134
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
Your Name authored
135 136
		query.Where("name like ?", fmt.Sprintf("%%%v%%", v))
	}
137 138 139 140
	// 模糊搜索条名称 + 账号
	if v, ok := queryOptions["search"].(string); ok && len(v) > 0 {
		query.Where("name like ? or account like ?", fmt.Sprintf("%%%v%%", v), fmt.Sprintf("%%%v%%", v))
	}
141 142 143
	if v, ok := queryOptions["names"]; ok {
		query.Where("name in(?)", pg.In(v))
	}
144
tangxvhui authored
145 146 147 148
	if v, ok := queryOptions["parentId"]; ok {
		query.Where("parent_id=?", v)
	}
tangxvhui authored
149
	if v, ok := queryOptions["offset"]; ok {
郑周 authored
150 151 152 153 154 155
		if value, ok := v.(int); ok {
			query.Offset(value)
		} else if value, ok := v.(int64); ok {
			query.Offset(int(value))
		}
		//query.Offset(v.(int))
tangxvhui authored
156
	}
tangxvhui authored
157
	if v, ok := queryOptions["limit"]; ok {
郑周 authored
158 159 160 161 162 163
		if value, ok := v.(int); ok {
			query.Limit(value)
		} else if value, ok := v.(int64); ok {
			query.Limit(int(value))
		}
		//query.Limit(v.(int))
tangxvhui authored
164
	}
165 166 167
	// 按创建时间升序
	query.Order("created_at ASC")
tangxvhui authored
168 169 170 171 172 173 174 175 176 177 178 179 180 181
	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{
Your Name authored
182 183 184 185 186 187 188 189 190 191 192
		Id:           user.Id,
		Account:      user.Account,
		AvatarUrl:    user.AvatarUrl,
		CompanyId:    user.CompanyId,
		AdminType:    user.AdminType,
		Name:         user.Name,
		Email:        user.Email,
		Status:       user.Status,
		DepartmentId: user.DepartmentId,
		PositionId:   user.PositionId,
		EntryTime:    user.EntryTime,
郑周 authored
193
		ParentId:     user.ParentId,
Your Name authored
194 195 196
		UpdatedAt:    user.UpdatedAt,
		DeletedAt:    user.DeletedAt,
		CreatedAt:    user.CreatedAt,
tangxvhui authored
197
	}
tangxvhui authored
198
}