作者 郑周

1.评估规则 ruleService 业务编码

package adapter
import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type RuleAdapter struct {
*domain.EvaluationRule
CreatorName string `json:"creatorName" comment:"创建人名称"`
}
... ...
package command
import (
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type CreateRuleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
CreatorId int64 `cname:"创建人ID" json:"creatorId"`
Name string `cname:"规则名称" json:"name" valid:"Required"`
Remark string `cname:"规则备注" json:"remark"`
Type int `cname:"评估方式" json:"type"`
Rating *domain.Rating `cname:"评级" json:"rating"`
Score *domain.Score `cname:"评分" json:"score"`
}
func (in *CreateRuleCommand) Valid(validation *validation.Validation) {
if in.CompanyId == 0 {
validation.SetError("companyId", "公司ID无效")
return
}
if in.CreatorId == 0 {
validation.SetError("creatorId", "创建人ID无效")
return
}
if len(in.Name) > 40 {
validation.SetError("name", "角色名称最大长度40个字符")
return
}
if in.Type == domain.EvaluationTypeRating {
if nil == in.Rating || len(in.Rating.Levels) == 0 {
validation.SetError("rating", "评级内容不能为空")
return
}
}
if in.Type == domain.EvaluationTypeScore {
if nil == in.Score {
validation.SetError("rating", "评分内容不能为空")
return
}
}
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type DeleteRuleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
Id int64 `cname:"规则ID" json:"id,string" valid:"Required"`
}
func (in *DeleteRuleCommand) Valid(validation *validation.Validation) {
if in.CompanyId == 0 {
validation.SetError("companyId", "公司ID无效")
return
}
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type QueryRuleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
}
func (in *QueryRuleCommand) Valid(validation *validation.Validation) {
if in.CompanyId == 0 {
validation.SetError("companyId", "公司ID无效")
return
}
}
... ...
package command
import (
"github.com/beego/beego/v2/core/validation"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type UpdateRuleCommand struct {
Id int64 `cname:"规则ID" json:"id,string" valid:"Required"`
CompanyId int64 `cname:"公司ID" json:"companyId"`
CreatorId int64 `cname:"创建人ID" json:"creatorId"`
Name string `cname:"规则名称" json:"name" valid:"Required"`
Remark string `cname:"规则备注" json:"remark"`
Type int `cname:"评估方式" json:"type"`
Rating *domain.Rating `cname:"评级" json:"rating"`
Score *domain.Score `cname:"评分" json:"score"`
}
func (in *UpdateRuleCommand) Valid(validation *validation.Validation) {
if in.CompanyId == 0 {
validation.SetError("companyId", "公司ID无效")
return
}
if in.CreatorId == 0 {
validation.SetError("creatorId", "创建人ID无效")
return
}
if len(in.Name) > 40 {
validation.SetError("name", "角色名称最大长度40个字符")
return
}
if in.Type == domain.EvaluationTypeRating {
if nil == in.Rating || len(in.Rating.Levels) == 0 {
validation.SetError("rating", "评级内容不能为空")
return
}
}
if in.Type == domain.EvaluationTypeScore {
if nil == in.Score {
validation.SetError("rating", "评分内容不能为空")
return
}
}
}
... ...
package service
import (
"github.com/linmadan/egglib-go/core/application"
"github.com/linmadan/egglib-go/utils/tool_funs"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_rule/adapter"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/evaluation_rule/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
type EvaluationRuleService struct {
}
func NewEvaluationRuleService() *EvaluationRuleService {
newRoleService := &EvaluationRuleService{}
return newRoleService
}
// Create 创建
func (rs *EvaluationRuleService) Create(in *command.CreateRuleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
// 检测名称重复
count, err := ruleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count > 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "名称已存在")
}
newRule := &domain.EvaluationRule{
Id: 0,
Name: in.Name,
Remark: in.Remark,
CompanyId: in.CompanyId,
CreatorId: in.CreatorId,
Type: in.Type,
Rating: in.Rating,
Score: in.Score,
}
rule, err := ruleRepository.Insert(newRule)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rule, nil
}
func (rs *EvaluationRuleService) Update(in *command.UpdateRuleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
// 检测名称重复(排除自己)
count, err := ruleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId, "notId": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if count > 0 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "名称已存在")
}
rule, err := ruleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
rule.Name = in.Name
rule.Remark = in.Remark
rule.Type = in.Type
rule.Rating = in.Rating
rule.Score = in.Score
rule, err = ruleRepository.Insert(rule)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rule, nil
}
func (rs *EvaluationRuleService) Remove(in *command.DeleteRuleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
rule, err := ruleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if _, err := ruleRepository.Remove(rule); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rule, nil
}
func (rs *EvaluationRuleService) List(in *command.QueryRuleCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
// Fixme 总数量是否使用Count获取一个总数量
count, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in))
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
ras := make([]*adapter.RuleAdapter, 0)
creatorIds := make([]int64, 0)
for i := range rules {
ra := &adapter.RuleAdapter{}
ra.EvaluationRule = rules[i]
ras = append(ras, ra)
creatorIds = append(creatorIds, rules[i].CreatorId)
}
_, users, _ := userRepository.Find(map[string]interface{}{"ids": creatorIds, "limit": len(creatorIds)})
userNameMap := map[int64]string{}
if users != nil {
for i := range users {
userNameMap[users[i].Id] = users[i].Name
}
}
for i := range ras {
if v, ok := userNameMap[ras[i].CreatorId]; ok {
ras[i].CreatorName = v
}
}
return tool_funs.SimpleWrapGridMap(count, ras), nil
}
... ...
... ... @@ -80,3 +80,35 @@ func CreateReceivedMessageRepository(options map[string]interface{}) domain.Rece
}
return repository.NewReceivedMessageRepository(transactionContext)
}
func CreateEvaluationRuleRepository(options map[string]interface{}) domain.EvaluationRuleRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewEvaluationRuleRepository(transactionContext)
}
func CreateEvaluationTemplateRepository(options map[string]interface{}) domain.EvaluationTemplateRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewEvaluationTemplateRepository(transactionContext)
}
func CreateEvaluationCycleRepository(options map[string]interface{}) domain.EvaluationCycleRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewEvaluationCycleRepository(transactionContext)
}
func CreateEvaluationProjectRepository(options map[string]interface{}) domain.EvaluationProjectRepository {
var transactionContext *pg.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*pg.TransactionContext)
}
return repository.NewEvaluationProjectRepository(transactionContext)
}
... ...
... ... @@ -4,13 +4,6 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)
//type RoleContainUser struct {
// RoleId int64 `json:"roleId,string"`
// UserId int64 `json:"userId,string"`
// UserName string `json:"userName"`
// UserEmail string `json:"userEmail"`
//}
type RoleUserAdapter struct {
domain.Role
Users []*domain.RoleContainUser `json:"users"`
... ...
... ... @@ -17,3 +17,11 @@ type EvaluationCycle struct {
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
type EvaluationCycleRepository interface {
Insert(cycle *EvaluationCycle) (*EvaluationCycle, error)
Remove(cycle *EvaluationCycle) (*EvaluationCycle, error)
FindOne(queryOptions map[string]interface{}) (*EvaluationCycle, error)
Find(queryOptions map[string]interface{}) (int64, []*EvaluationCycle, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
... ... @@ -18,3 +18,11 @@ type EvaluationProject struct {
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
type EvaluationProjectRepository interface {
Insert(project *EvaluationProject) (*EvaluationProject, error)
Remove(project *EvaluationProject) (*EvaluationProject, error)
FindOne(queryOptions map[string]interface{}) (*EvaluationProject, error)
Find(queryOptions map[string]interface{}) (int64, []*EvaluationProject, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
... ... @@ -10,7 +10,7 @@ const (
)
type Rating struct {
Levels []RatingLevel `comment:"配置等级"`
Levels []RatingLevel `json:"levels" comment:"配置等级"`
}
type RatingLevel struct {
... ... @@ -48,3 +48,11 @@ type EvaluationRule struct {
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
}
type EvaluationRuleRepository interface {
Insert(rule *EvaluationRule) (*EvaluationRule, error)
Remove(rule *EvaluationRule) (*EvaluationRule, error)
FindOne(queryOptions map[string]interface{}) (*EvaluationRule, error)
Find(queryOptions map[string]interface{}) (int64, []*EvaluationRule, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
... ... @@ -68,6 +68,13 @@ type EvaluationTemplate struct {
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
//Link EvaluationLink `json:"link" comment:"评估流程"`
}
type EvaluationTemplateRepository interface {
Insert(template *EvaluationTemplate) (*EvaluationTemplate, error)
Remove(template *EvaluationTemplate) (*EvaluationTemplate, error)
FindOne(queryOptions map[string]interface{}) (*EvaluationTemplate, error)
Find(queryOptions map[string]interface{}) (int64, []*EvaluationTemplate, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"time"
)
... ... @@ -13,7 +14,7 @@ type EvaluationCycle struct {
CompanyId int64 `comment:"公司ID"`
CreatorId int64 `comment:"创建人ID"`
KpiCycle int `comment:"考核周期(0日、1周、2月)"`
Template []EvaluationTemplate `comment:"周期使用模板"`
Template []domain.EvaluationTemplate `comment:"周期使用模板"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
... ...
package models
import (
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"time"
)
... ... @@ -14,7 +15,7 @@ type EvaluationProject struct {
HrBp int `comment:"HR角色权限"`
Pms []string `comment:"项目管理员ID"`
Recipients []string `comment:"被评估人ID"`
Template *EvaluationTemplate `comment:"评估模板"`
Template *domain.EvaluationTemplate `comment:"评估模板"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
... ...
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 EvaluationCycleRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewEvaluationCycleRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationCycleRepository {
return &EvaluationCycleRepository{transactionContext: transactionContext}
}
func (repo *EvaluationCycleRepository) TransformToDomain(m *models.EvaluationCycle) domain.EvaluationCycle {
return domain.EvaluationCycle{
Id: m.Id,
Name: m.Name,
TimeStart: m.TimeStart,
TimeEnd: m.TimeEnd,
CompanyId: m.CompanyId,
CreatorId: m.CreatorId,
KpiCycle: m.KpiCycle,
Template: m.Template,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
}
}
func (repo *EvaluationCycleRepository) TransformToModel(d *domain.EvaluationCycle) models.EvaluationCycle {
return models.EvaluationCycle{
Id: d.Id,
Name: d.Name,
TimeStart: d.TimeStart,
TimeEnd: d.TimeEnd,
CompanyId: d.CompanyId,
CreatorId: d.CreatorId,
KpiCycle: d.KpiCycle,
Template: d.Template,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
}
}
func (repo *EvaluationCycleRepository) nextIdentify() (int64, error) {
return utils.NewSnowflakeId()
}
func (repo *EvaluationCycleRepository) Insert(d *domain.EvaluationCycle) (*domain.EvaluationCycle, 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 *EvaluationCycleRepository) Remove(d *domain.EvaluationCycle) (*domain.EvaluationCycle, 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 *EvaluationCycleRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationCycle, error) {
tx := repo.transactionContext.PgTx
m := new(models.EvaluationCycle)
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 *EvaluationCycleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationCycle, error) {
tx := repo.transactionContext.PgTx
var m []*models.EvaluationCycle
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var arrays []*domain.EvaluationCycle
for _, v := range m {
d := repo.TransformToDomain(v)
arrays = append(arrays, &d)
}
return int64(count), arrays, nil
}
func (repo *EvaluationCycleRepository) Count(queryOptions map[string]interface{}) (int64, error) {
tx := repo.transactionContext.PgTx
m := new(models.EvaluationCycle)
query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
query.Where("deleted_at isnull")
if id, ok := queryOptions["id"]; ok {
query.Where("id = ?", id)
}
if notId, ok := queryOptions["notId"]; ok {
query.Where("id != ?", notId)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
count, err := query.Count()
if err != nil {
return 0, err
}
return int64(count), nil
}
... ...
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 EvaluationProjectRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewEvaluationProjectRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationProjectRepository {
return &EvaluationProjectRepository{transactionContext: transactionContext}
}
func (repo *EvaluationProjectRepository) TransformToDomain(m *models.EvaluationProject) domain.EvaluationProject {
return domain.EvaluationProject{
Id: m.Id,
Name: m.Name,
Describe: m.Describe,
CompanyId: m.CompanyId,
CreatorId: m.CreatorId,
HrBp: m.HrBp,
Pms: m.Pms,
Recipients: m.Recipients,
Template: m.Template,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
}
}
func (repo *EvaluationProjectRepository) TransformToModel(d *domain.EvaluationProject) models.EvaluationProject {
return models.EvaluationProject{
Id: d.Id,
Name: d.Name,
Describe: d.Describe,
CompanyId: d.CompanyId,
CreatorId: d.CreatorId,
HrBp: d.HrBp,
Pms: d.Pms,
Recipients: d.Recipients,
Template: d.Template,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
}
}
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
}
func (repo *EvaluationProjectRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationProject, error) {
tx := repo.transactionContext.PgTx
var m []*models.EvaluationProject
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
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")
if id, ok := queryOptions["id"]; ok {
query.Where("id = ?", id)
}
if notId, ok := queryOptions["notId"]; ok {
query.Where("id != ?", notId)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
count, err := query.Count()
if err != nil {
return 0, err
}
return int64(count), nil
}
... ...
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,
Rating: m.Rating,
Score: m.Score,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
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,
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
query := tx.Model(&m).Where("deleted_at isnull").Limit(20)
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
query.Count()
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")
if id, ok := queryOptions["id"]; ok {
query.Where("id = ?", id)
}
if notId, ok := queryOptions["notId"]; ok {
query.Where("id != ?", notId)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
count, err := query.Count()
if err != nil {
return 0, err
}
return int64(count), nil
}
... ...
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 EvaluationTemplateRepository struct {
transactionContext *pgTransaction.TransactionContext
}
func NewEvaluationTemplateRepository(transactionContext *pgTransaction.TransactionContext) *EvaluationTemplateRepository {
return &EvaluationTemplateRepository{transactionContext: transactionContext}
}
func (repo *EvaluationTemplateRepository) TransformToDomain(m *models.EvaluationTemplate) domain.EvaluationTemplate {
return domain.EvaluationTemplate{
Id: m.Id,
Name: m.Name,
Describe: m.Describe,
CompanyId: m.CompanyId,
CreatorId: m.CreatorId,
State: m.State,
Link: m.Link,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
}
}
func (repo *EvaluationTemplateRepository) TransformToModel(d *domain.EvaluationTemplate) models.EvaluationTemplate {
return models.EvaluationTemplate{
Id: d.Id,
Name: d.Name,
Describe: d.Describe,
CompanyId: d.CompanyId,
CreatorId: d.CreatorId,
State: d.State,
Link: d.Link,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
}
}
func (repo *EvaluationTemplateRepository) nextIdentify() (int64, error) {
return utils.NewSnowflakeId()
}
func (repo *EvaluationTemplateRepository) Insert(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, 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 *EvaluationTemplateRepository) Remove(d *domain.EvaluationTemplate) (*domain.EvaluationTemplate, 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 *EvaluationTemplateRepository) FindOne(queryOptions map[string]interface{}) (*domain.EvaluationTemplate, error) {
tx := repo.transactionContext.PgTx
m := new(models.EvaluationTemplate)
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 *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationTemplate, error) {
tx := repo.transactionContext.PgTx
var m []*models.EvaluationTemplate
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
if v, ok := queryOptions["limit"].(int); ok {
query.Limit(v)
}
if v, ok := queryOptions["offset"].(int); ok {
query.Offset(v)
}
count, err := query.SelectAndCount()
if err != nil {
return 0, nil, err
}
var arrays []*domain.EvaluationTemplate
for _, v := range m {
d := repo.TransformToDomain(v)
arrays = append(arrays, &d)
}
return int64(count), arrays, nil
}
func (repo *EvaluationTemplateRepository) Count(queryOptions map[string]interface{}) (int64, error) {
tx := repo.transactionContext.PgTx
m := new(models.EvaluationTemplate)
query := sqlbuilder.BuildQuery(tx.Model(m), queryOptions)
query.Where("deleted_at isnull")
if id, ok := queryOptions["id"]; ok {
query.Where("id = ?", id)
}
if notId, ok := queryOptions["notId"]; ok {
query.Where("id != ?", notId)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
query.Where("company_id = ?", companyId)
}
count, err := query.Count()
if err != nil {
return 0, err
}
return int64(count), nil
}
... ...