审查视图

pkg/infrastructure/repository/pg_company_repository.go 3.1 KB
tangxvhui authored
1 2 3
package repository

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

type CompanyRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

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

func (repo *CompanyRepository) Insert(u *domain.Company) (*domain.Company, error) {
tangxvhui authored
25
	companyModel := models.Company{
tangxvhui authored
26 27 28 29 30
		Id:            u.Id,
		Logo:          u.Logo,
		Name:          u.Name,
		Status:        u.Status,
		ChargeUserIds: u.ChargeUserIds,
庄敏学 authored
31 32 33
		UpdatedAt:     u.UpdatedAt,
		CreatedAt:     u.CreatedAt,
		DeletedAt:     u.DeletedAt,
tangxvhui authored
34 35
	}
	tx := repo.transactionContext.PgTx
tangxvhui authored
36 37 38 39 40 41 42 43 44 45
	_, err := tx.Model(&companyModel).Insert()
	if err != nil {
		return nil, err
	}
	u.Id = companyModel.Id
	return u, nil
}

func (repo *CompanyRepository) Update(u *domain.Company) (*domain.Company, error) {
	companyModel := models.Company{
tangxvhui authored
46 47 48 49 50
		Id:            u.Id,
		Logo:          u.Logo,
		Name:          u.Name,
		Status:        u.Status,
		ChargeUserIds: u.ChargeUserIds,
庄敏学 authored
51 52 53
		UpdatedAt:     u.UpdatedAt,
		CreatedAt:     u.CreatedAt,
		DeletedAt:     u.DeletedAt,
tangxvhui authored
54 55 56 57 58
	}
	tx := repo.transactionContext.PgTx
	_, err := tx.Model(&companyModel).WherePK().Update()
	if err != nil {
		return nil, err
tangxvhui authored
59 60
	}
	return u, nil
tangxvhui authored
61 62
}
tangxvhui authored
63 64
func (repo *CompanyRepository) Remove(u *domain.Company) (*domain.Company, error) {
	nowTime := time.Now()
庄敏学 authored
65
	u.DeletedAt = &nowTime
tangxvhui authored
66
	_, err := repo.Update(u)
tangxvhui authored
67
	return u, err
tangxvhui authored
68 69 70
}

func (repo *CompanyRepository) FindOne(queryOptions map[string]interface{}) (*domain.Company, error) {
tangxvhui authored
71 72 73 74 75 76 77 78
	tx := repo.transactionContext.PgTx
	companyModel := models.Company{}
	query := tx.Model(&companyModel)
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
	err := query.First()
	if err == pg.ErrNoRows {
tangxvhui authored
79
		return nil, ErrNoRows
tangxvhui authored
80 81 82 83 84 85
	}
	if err != nil {
		return nil, err
	}
	result := repo.TransformToCompanyDomain(&companyModel)
	return result, nil
tangxvhui authored
86 87
}
tangxvhui authored
88 89 90 91 92
func (repo *CompanyRepository) Find(queryOptions map[string]interface{}) (int, []*domain.Company, error) {
	tx := repo.transactionContext.PgTx
	companyModel := []models.Company{}
	query := tx.Model(&companyModel).
		Limit(20)
tangxvhui authored
93 94 95
	if v, ok := queryOptions["id"]; ok {
		query.Where("id=?", v)
	}
tangxvhui authored
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	if v, ok := queryOptions["limit"]; ok {
		query.Limit(v.(int))
	}
	if v, ok := queryOptions["offset"]; ok {
		query.Offset(v.(int))
	}
	cnt, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var resultList []*domain.Company
	for i := range companyModel {
		result := repo.TransformToCompanyDomain(&companyModel[i])
		resultList = append(resultList, result)
	}
	return cnt, resultList, nil
}

func (repo *CompanyRepository) TransformToCompanyDomain(m *models.Company) *domain.Company {
	return &domain.Company{
庄敏学 authored
116 117 118 119 120 121 122
		Id:        m.Id,
		Logo:      m.Logo,
		Name:      m.Name,
		Status:    m.Status,
		UpdatedAt: m.UpdatedAt,
		CreatedAt: m.CreatedAt,
		DeletedAt: m.DeletedAt,
tangxvhui authored
123
	}
tangxvhui authored
124
}