审查视图

pkg/infrastructure/repository/pg_evaluation_rule_repository.go 5.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package repository

import (
	"errors"
	"fmt"
	"github.com/go-pg/pg/v10"
	"github.com/linmadan/egglib-go/persistent/pg/sqlbuilder"
	pgTransaction "github.com/linmadan/egglib-go/transaction/pg"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/infrastructure/pg/models"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
	"time"
)

type EvaluationRuleRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewEvaluationRuleRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationRuleRepository {
	return &EvaluationRuleRepository{transactionContext: transactionContext}
}

func (repo *EvaluationRuleRepository) TransformToDomain(m *models.EvaluationRule) domain.EvaluationRule {
	return domain.EvaluationRule{
		Id:        m.Id,
		Name:      m.Name,
		Remark:    m.Remark,
		CompanyId: m.CompanyId,
		CreatorId: m.CreatorId,
		Type:      m.Type,
郑周 authored
31
		SysType:   m.SysType,
32 33
		Rating:    m.Rating,
		Score:     m.Score,
34 35
		CreatedAt: m.CreatedAt.Local(), // PG库为UTC需要转成本地时间
		UpdatedAt: m.UpdatedAt.Local(),
36 37 38 39 40 41 42 43 44 45 46 47
		DeletedAt: m.DeletedAt,
	}
}

func (repo *EvaluationRuleRepository) TransformToModel(d *domain.EvaluationRule) models.EvaluationRule {
	return models.EvaluationRule{
		Id:        d.Id,
		Name:      d.Name,
		Remark:    d.Remark,
		CompanyId: d.CompanyId,
		CreatorId: d.CreatorId,
		Type:      d.Type,
郑周 authored
48
		SysType:   d.SysType,
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		Rating:    d.Rating,
		Score:     d.Score,
		CreatedAt: d.CreatedAt,
		UpdatedAt: d.UpdatedAt,
		DeletedAt: d.DeletedAt,
	}
}

func (repo *EvaluationRuleRepository) nextIdentify() (int64, error) {
	return utils.NewSnowflakeId()
}

func (repo *EvaluationRuleRepository) Insert(d *domain.EvaluationRule) (*domain.EvaluationRule, error) {
	var isCreate = d.Id == 0
	if isCreate {
		id, err := repo.nextIdentify()
		if err != nil {
			return d, err
		}
		d.Id = id
		d.CreatedAt = time.Now()
		d.UpdatedAt = d.CreatedAt
	} else {
		d.UpdatedAt = time.Now()
	}
	m := repo.TransformToModel(d)
	tx := repo.transactionContext.PgTx
	var err error
	if isCreate {
		_, err = tx.Model(&m).Returning("id").Insert()
	} else {
		_, err = tx.Model(&m).Returning("id").WherePK().Update() // 更新和删除必须增加条件
	}
	if err != nil {
		return nil, err
	}
	d.Id = m.Id
	return d, nil
}

func (repo *EvaluationRuleRepository) Remove(d *domain.EvaluationRule) (*domain.EvaluationRule, error) {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	m := repo.TransformToModel(d)
	m.DeletedAt = &nowTime
	if _, err := tx.Model(&m).WherePK().Update(); err != nil {
		return d, err
	}
	return d, nil
}

func (repo *EvaluationRuleRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationRule, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationRule)
	query := tx.Model(m)
	query.Where("deleted_at isnull")
	if id, ok := queryOptions["id"]; ok {
		query.Where("id=?", id)
	}
	if err := query.First(); err != nil {
		if errors.Is(err, pg.ErrNoRows) {
			return nil, fmt.Errorf("没有此资源")
		} else {
			return nil, err
		}
	}
	u := repo.TransformToDomain(m)
	return &u, nil
}

func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationRule, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.EvaluationRule
122
	query := tx.Model(&m).Where("deleted_at isnull")
123
124 125
	if v, ok := queryOptions["nameOrRemark"]; ok && len(v.(string)) > 0 {
		query.Where("name LIKE ? or remark LIKE ?", v, v)
郑周 authored
126 127
	}
128 129 130 131
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in(?)", pg.In(v))
	}
郑周 authored
132
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
133
		query.Where("name = ?", v)
134 135
	}
136 137
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
138 139
	}
郑周 authored
140 141 142 143 144 145 146 147
	if v, ok := queryOptions["creatorId"]; ok && v.(int64) > 0 {
		query.Where("creator_id = ?", v)
	}

	if v, ok := queryOptions["type"]; ok && v.(int) >= 0 {
		query.Where("type = ?", v)
	}
郑周 authored
148 149 150 151
	if v, ok := queryOptions["sysType"]; ok && v.(int) >= 0 {
		query.Where("sys_type = ?", v)
	}
郑周 authored
152 153
	if v, ok := queryOptions["limit"].(int64); ok {
		query.Limit(int(v))
154
	}
郑周 authored
155 156
	if v, ok := queryOptions["offset"].(int64); ok {
		query.Offset(int(v))
157 158
	}
郑周 authored
159 160 161
	// 按创建时间降序
	query.Order("created_at DESC")
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var arrays []*domain.EvaluationRule
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, &d)
	}
	return int64(count), arrays, nil
}

func (repo *EvaluationRuleRepository) Count(queryOptions map[string]interface{}) (int64, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationRule)
	query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
	query.Where("deleted_at isnull")
180 181
	if v, ok := queryOptions["id"]; ok {
		query.Where("id = ?", v)
182 183
	}
184 185
	if v, ok := queryOptions["notId"]; ok {
		query.Where("id != ?", v)
186 187
	}
郑周 authored
188
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
189
		query.Where("name = ?", v)
190 191
	}
192 193
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
194 195 196 197 198 199 200 201
	}

	count, err := query.Count()
	if err != nil {
		return 0, err
	}
	return int64(count), nil
}