审查视图

pkg/infrastructure/repository/pg_staff_assess_repository.go 5.7 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
package repository

import (
	"errors"
	"fmt"
	"time"

	"github.com/go-pg/pg/v10"
	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"
)

type StaffAssessRepository struct {
	transactionContext *pgTransaction.TransactionContext
}

var _ domain.StaffAssessRepository = (*StaffAssessRepository)(nil)

func NewStaffAssessRepository(transactionContext *pgTransaction.TransactionContext) *StaffAssessRepository {
	return &StaffAssessRepository{transactionContext: transactionContext}
}

func (repo *StaffAssessRepository) TransformToDomain(d *models.StaffAssess) *domain.StaffAssess {
	return &domain.StaffAssess{
Your Name authored
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
		Id:                    d.Id,
		CompanyId:             d.CompanyId,
		EvaluationProjectId:   d.EvaluationProjectId,
		EvaluationProjectName: d.EvaluationProjectName,
		CycleId:               d.CycleId,
		CycleName:             d.CycleName,
		StaffAssessTaskId:     d.StaffAssessTaskId,
		TargetUser:            d.TargetUser,
		TargetDepartment:      d.TargetDepartment,
		Executor:              d.Executor,
		Types:                 domain.StaffAssessType(d.Types),
		LinkNodeId:            d.LinkNodeId,
		LinkNodeName:          d.LinkNodeName,
		Status:                domain.StaffAssessStatus(d.Status),
		BeginTime:             d.BeginTime,
		EndTime:               d.EndTime,
42 43
		CreatedAt:             d.CreatedAt,
		UpdatedAt:             d.UpdatedAt,
Your Name authored
44
		DeletedAt:             d.DeletedAt,
45 46 47 48 49
	}
}

func (repo *StaffAssessRepository) Save(d *domain.StaffAssess) (*domain.StaffAssess, error) {
	saveModel := models.StaffAssess{
Your Name authored
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
		Id:                    d.Id,
		CompanyId:             d.CompanyId,
		EvaluationProjectId:   d.EvaluationProjectId,
		EvaluationProjectName: d.EvaluationProjectName,
		CycleId:               d.CycleId,
		CycleName:             d.CycleName,
		StaffAssessTaskId:     d.StaffAssessTaskId,
		TargetUser:            d.TargetUser,
		TargetDepartment:      d.TargetDepartment,
		Executor:              d.Executor,
		Types:                 string(d.Types),
		LinkNodeId:            d.LinkNodeId,
		LinkNodeName:          d.LinkNodeName,
		Status:                string(d.Status),
		BeginTime:             d.BeginTime,
		EndTime:               d.EndTime,
tangxvhui authored
66 67
		CreatedAt:             d.CreatedAt,
		UpdatedAt:             d.UpdatedAt,
Your Name authored
68
		DeletedAt:             d.DeletedAt,
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
	}
	tx := repo.transactionContext.PgTx
	var err error
	if saveModel.Id == 0 {
		_, err = tx.Model(&saveModel).Insert()
		if err != nil {
			return nil, err
		}
	} else {
		_, err = tx.Model(&saveModel).WherePK().Update()
		if err != nil {
			return nil, err
		}
	}
	d.Id = saveModel.Id
	return d, nil
}

func (repo *StaffAssessRepository) Remove(id int) error {
	tx := repo.transactionContext.PgTx
	nowTime := time.Now()
	_, err := tx.Model(&models.StaffAssess{}).
		Where("id=?", id).
		Set("deleted_at=?", nowTime).
		Update()
	return err
}

func (repo *StaffAssessRepository) FindOne(queryOptions map[string]interface{}) (*domain.StaffAssess, error) {
	tx := repo.transactionContext.PgTx
	m := new(models.StaffAssess)
	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 *StaffAssessRepository) Find(queryOptions map[string]interface{}) (int, []*domain.StaffAssess, error) {
	tx := repo.transactionContext.PgTx
	var m []*models.StaffAssess
	query := tx.Model(&m).
tangxvhui authored
120 121
		Where("staff_assess.deleted_at isnull").
		Join(`JOIN staff_assess_task ON staff_assess_task."id" = staff_assess."staff_assess_task_id" and staff_assess_task."deleted_at" isnull`)
122
	if companyId, ok := queryOptions["companyId"]; ok {
tangxvhui authored
123
		query.Where("staff_assess.company_id = ?", companyId)
124 125 126 127 128 129 130
	}
	if v, ok := queryOptions["limit"].(int); ok {
		query.Limit(v)
	}
	if v, ok := queryOptions["offset"].(int); ok {
		query.Offset(v)
	}
郑周 authored
131 132 133
	if v, ok := queryOptions["id"]; ok {
		query.Where("staff_assess.id=?", v)
	}
tangxvhui authored
134
	if v, ok := queryOptions["targetUserId"]; ok {
tangxvhui authored
135
		query.Where(`staff_assess.target_user->>'userId'='?'`, v)
Your Name authored
136
	}
郑周 authored
137 138 139
	if v, ok := queryOptions["targetUserIds"]; ok {
		query.Where(`staff_assess.target_user->>'userId' in(?)`, pg.In(v))
	}
tangxvhui authored
140
	if v, ok := queryOptions["targetUserName"].(string); ok {
tangxvhui authored
141
		query.Where(`staff_assess.target_user->>'userName' like ?`, fmt.Sprintf("%%%v%%", v))
Your Name authored
142
	}
tangxvhui authored
143
	if v, ok := queryOptions["executorId"]; ok {
tangxvhui authored
144
		query.Where(`staff_assess.executor->>'userId'='?'`, v)
Your Name authored
145
	}
tangxvhui authored
146
	if v, ok := queryOptions["cycleId"]; ok {
tangxvhui authored
147
		query.Where(`staff_assess.cycle_id=?`, v)
Your Name authored
148
	}
Your Name authored
149
	if v, ok := queryOptions["staffAssessTaskId"]; ok {
tangxvhui authored
150
		query.Where(`staff_assess.staff_assess_task_id=?`, v)
Your Name authored
151
	}
郑周 authored
152 153
	if v, ok := queryOptions["types"]; ok {
		query.Where(`staff_assess.types=?`, v)
Your Name authored
154
	}
tangxvhui authored
155
	if v, ok := queryOptions["typesList"].([]string); ok {
tangxvhui authored
156
		query.Where("staff_assess.types in(?)", pg.In(v))
Your Name authored
157
	}
tangxvhui authored
158
	if v, ok := queryOptions["status"]; ok {
tangxvhui authored
159
		query.Where("staff_assess.status=?", v)
Your Name authored
160
	}
tangxvhui authored
161
	if v, ok := queryOptions["endTime"]; ok {
tangxvhui authored
162
		query.Where("staff_assess.end_time<=?", v)
tangxvhui authored
163
	}
tangxvhui authored
164 165 166
	if v, ok := queryOptions["beginDay"]; ok {
		query.Where("to_char(staff_assess.begin_time at time zone 'PRC','YYYY-MM-DD')= ? ", v)
	}
tangxvhui authored
167 168 169
	if v, ok := queryOptions["evaluationProjectId"]; ok {
		query.Where("staff_assess.evaluation_project_id=?", v)
	}
tangxvhui authored
170
	query.Order("staff_assess.id DESC")
171 172 173 174 175 176 177 178 179 180 181
	count, err := query.SelectAndCount()
	if err != nil {
		return 0, nil, err
	}
	var arrays []*domain.StaffAssess
	for _, v := range m {
		d := repo.TransformToDomain(v)
		arrays = append(arrays, d)
	}
	return count, arrays, nil
}