审查视图

pkg/infrastructure/repository/pg_evaluation_project_repository.go 6.4 KB
1 2 3 4 5
package repository

import (
	"errors"
	"fmt"
tangxvhui authored
6 7
	"time"
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
	"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"
)

type EvaluationProjectRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

func NewEvaluationProjectRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationProjectRepository {
	return &EvaluationProjectRepository{transactionContext: transactionContext}
}

func (repo *EvaluationProjectRepository) TransformToDomain(m *models.EvaluationProject) domain.EvaluationProject {
25 26 27 28 29 30 31 32 33 34 35 36
	// 历史旧数据,默认输出转成1
	if m.Template != nil {
		for i := range m.Template.LinkNodes {
			node := m.Template.LinkNodes[i]
			for j := range node.NodeContents {
				if node.NodeContents[j].Required == 0 {
					node.NodeContents[j].Required = domain.NodeRequiredYes
				}
			}
		}
	}
37
	return domain.EvaluationProject{
tangxvhui authored
38 39 40 41 42 43 44 45 46 47 48 49
		Id:           m.Id,
		Name:         m.Name,
		Describe:     m.Describe,
		CompanyId:    m.CompanyId,
		CycleId:      m.CycleId,
		CreatorId:    m.CreatorId,
		State:        m.State,
		SummaryState: domain.ProjectSummaryState(m.SummaryState),
		HrBp:         m.HrBp,
		Pmp:          m.Pmp,
		PmpIds:       m.PmpIds,
		Recipients:   m.Recipients,
50
		PrincipalId:  m.PrincipalId,
tangxvhui authored
51 52 53 54 55 56
		Template:     m.Template,
		BeginTime:    m.BeginTime,
		EndTime:      m.EndTime,
		CreatedAt:    m.CreatedAt.Local(),
		UpdatedAt:    m.UpdatedAt.Local(),
		DeletedAt:    m.DeletedAt,
57 58 59 60 61
	}
}

func (repo *EvaluationProjectRepository) TransformToModel(d *domain.EvaluationProject) models.EvaluationProject {
	return models.EvaluationProject{
tangxvhui authored
62 63 64 65 66 67 68 69 70 71 72 73
		Id:           d.Id,
		Name:         d.Name,
		Describe:     d.Describe,
		CompanyId:    d.CompanyId,
		CycleId:      d.CycleId,
		CreatorId:    d.CreatorId,
		State:        d.State,
		SummaryState: int(d.SummaryState),
		HrBp:         d.HrBp,
		Pmp:          d.Pmp,
		PmpIds:       d.PmpIds,
		Recipients:   d.Recipients,
74
		PrincipalId:  d.PrincipalId,
tangxvhui authored
75 76 77 78 79 80
		Template:     d.Template,
		BeginTime:    d.BeginTime,
		EndTime:      d.EndTime,
		CreatedAt:    d.CreatedAt,
		UpdatedAt:    d.UpdatedAt,
		DeletedAt:    d.DeletedAt,
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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	}
}

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

func (repo *EvaluationProjectRepository) Insert(d *domain.EvaluationProject) (*domain.EvaluationProject, 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 *EvaluationProjectRepository) Remove(d *domain.EvaluationProject) (*domain.EvaluationProject, 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 *EvaluationProjectRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationProject, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationProject)
	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
}
郑周 authored
146
func (repo *EvaluationProjectRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationProject, error) {
147 148
	tx := repo.transactionContext.PgTx
	var m []*models.EvaluationProject
149
	query := tx.Model(&m).Where("deleted_at isnull")
150
郑周 authored
151 152 153 154
	if len(excludeColumns) > 0 {
		query.ExcludeColumn(excludeColumns...)
	}
郑周 authored
155 156 157 158
	if v, ok := queryOptions["ids"]; ok {
		query.Where("id in (?)", pg.In(v))
	}
郑周 authored
159
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
郑周 authored
160
		query.Where("name LIKE ?", v)
161 162
	}
163 164
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
165 166
	}
郑周 authored
167 168 169 170 171 172 173 174
	if v, ok := queryOptions["cycleId"]; ok {
		query.Where("cycle_id = ?", v)
	}

	if v, ok := queryOptions["state"]; ok && v.(int) >= 0 {
		query.Where("state = ?", v)
	}
tangxvhui authored
175 176 177 178 179 180 181 182 183 184 185 186 187 188
	if v, ok := queryOptions["summaryState"]; ok {
		query.Where("summary_state=?", v)
	}

	if v, ok := queryOptions["beginTime"]; ok {
		t := v.(time.Time)
		query.Where("begin_time>=?", t)
	}

	if v, ok := queryOptions["endTime"]; ok {
		t := v.(time.Time)
		query.Where("end_time<=?", t)
	}
郑周 authored
189 190 191 192 193 194 195
	if v, ok := queryOptions["pmpIds"].([]string); ok && len(v) > 0 {
		query.WhereGroup(func(query *pg.Query) (*pg.Query, error) {
			for i := range v {
				query.WhereOr("pmp_ids @> ?", `"`+v[i]+`"`)
			}
			return query, nil
		})
郑周 authored
196 197
	}
郑周 authored
198 199
	if v, ok := queryOptions["limit"].(int64); ok {
		query.Limit(int(v))
200
	}
郑周 authored
201 202
	if v, ok := queryOptions["offset"].(int64); ok {
		query.Offset(int(v))
203
	}
郑周 authored
204 205
	// 按创建时间降序
	query.Order("created_at DESC")
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var arrays []*domain.EvaluationProject
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, &d)
	}
	return int64(count), arrays, nil
}

func (repo *EvaluationProjectRepository) Count(queryOptions map[string]interface{}) (int64, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.EvaluationProject)
	query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
	query.Where("deleted_at isnull")
225 226
	if v, ok := queryOptions["id"]; ok {
		query.Where("id = ?", v)
227 228
	}
229 230
	if v, ok := queryOptions["notId"]; ok {
		query.Where("id != ?", v)
231 232
	}
郑周 authored
233
	if v, ok := queryOptions["name"].(string); ok && len(v) > 0 {
234
		query.Where("name = ?", v)
235 236
	}
237 238
	if v, ok := queryOptions["companyId"]; ok {
		query.Where("company_id = ?", v)
239 240
	}
郑周 authored
241 242 243 244
	if v, ok := queryOptions["cycleId"]; ok {
		query.Where("cycle_id = ?", v)
	}
245 246 247 248 249 250
	count, err := query.Count()
	if err != nil {
		return 0, err
	}
	return int64(count), nil
}