作者 郑周

1.周期模型 重构

package command
import (
"github.com/beego/beego/v2/core/validation"
"time"
)
type CreateCycleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
CreatorId int64 `cname:"创建人ID" json:"creatorId"`
Name string `cname:"周期名称" json:"name" valid:"Required"`
TimeStart *time.Time `cname:"起始时间" json:"timeStart"`
TimeEnd *time.Time `cname:"截至时间" json:"timeEnd"`
KpiCycle int `cname:"考核周期(0日、1周、2月)" json:"kpiCycle" valid:"Required"`
TemplateIds []string `cname:"周期使用模板ID" json:"templateIds"`
}
func (in *CreateCycleCommand) 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.TimeStart == nil {
validation.SetError("timeStart", "请选择考核周期的开始时间")
return
}
if in.TimeEnd == nil {
validation.SetError("timeEnd", "请选择考核周期的结束时间")
return
}
if len(in.TemplateIds) == 0 {
validation.SetError("templates", "请选择周期内使用的评估模板")
return
}
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type DeleteCycleCommand struct {
Id int64 `cname:"周期ID" json:"id,string" valid:"Required"`
}
func (in *DeleteCycleCommand) Valid(*validation.Validation) {
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type GetCycleCommand struct {
Id int64 `cname:"周期Id" json:"id,string" valid:"Required"`
}
func (in *GetCycleCommand) Valid(*validation.Validation) {
}
... ...
package command
import "github.com/beego/beego/v2/core/validation"
type QueryCycleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
}
func (in *QueryCycleCommand) Valid(validation *validation.Validation) {
if in.CompanyId == 0 {
validation.SetError("companyId", "公司ID无效")
return
}
}
... ...
package command
import (
"github.com/beego/beego/v2/core/validation"
"time"
)
type UpdateCycleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
Id int64 `cname:"周期ID" json:"id,string" valid:"Required"`
Name string `cname:"周期名称" json:"name" valid:"Required"`
TimeStart *time.Time `cname:"起始时间" json:"timeStart"`
TimeEnd *time.Time `cname:"截至时间" json:"timeEnd"`
KpiCycle int `cname:"考核周期(0日、1周、2月)" json:"kpiCycle" valid:"Required"`
TemplateIds []string `cname:"周期使用模板ID" json:"templateIds"`
}
func (in *UpdateCycleCommand) Valid(validation *validation.Validation) {
if len(in.Name) > 40 {
validation.SetError("name", "角色名称最大长度40个字符")
return
}
if in.TimeStart == nil {
validation.SetError("timeStart", "请选择考核周期的开始时间")
return
}
if in.TimeEnd == nil {
validation.SetError("timeEnd", "请选择考核周期的结束时间")
return
}
if len(in.TemplateIds) == 0 {
validation.SetError("templates", "请选择周期内使用的评估模板")
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_cycle/command"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/utils"
)
type EvaluationCycleService struct {
}
func NewEvaluationCycleService() *EvaluationCycleService {
newRoleService := &EvaluationCycleService{}
return newRoleService
}
// Create 创建
func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
// 检测名称重复
count, err := cycleRepository.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, "名称已存在")
}
_, templates, err := templateRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "ids": in.TemplateIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(templates) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "模板不存在, 请重新选择")
}
newCycle := &domain.EvaluationCycle{
Id: 0,
Name: in.Name,
TimeStart: in.TimeStart,
TimeEnd: in.TimeEnd,
CompanyId: in.CompanyId,
CreatorId: in.CreatorId,
KpiCycle: in.KpiCycle,
Templates: templates,
}
rule, err := cycleRepository.Insert(newCycle)
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 *EvaluationCycleService) Update(in *command.UpdateCycleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
// 检测名称重复(排除自己)
count, err := cycleRepository.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, "名称已存在")
}
cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
_, templates, err := templateRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "ids": in.TemplateIds})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if len(templates) == 0 {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "模板不存在, 请重新选择")
}
cycle.Name = in.Name
cycle.TimeStart = in.TimeStart
cycle.TimeEnd = in.TimeEnd
cycle.KpiCycle = in.KpiCycle
cycle.Templates = templates
cycle, err = cycleRepository.Insert(cycle)
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 cycle, nil
}
func (rs *EvaluationCycleService) Get(in *command.GetCycleCommand) (interface{}, error) {
// Get 不需要事务
if err := utils.ValidateCommand(in); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return cycle, nil
}
func (rs *EvaluationCycleService) Remove(in *command.DeleteCycleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
cycle, err := cycleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if _, err := cycleRepository.Remove(cycle); 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 cycle, nil
}
func (rs *EvaluationCycleService) List(in *command.QueryCycleCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
// FIXME 总数量是否使用Count获取一个总数量
count, cycles, err := cycleRepository.Find(tool_funs.SimpleStructToMap(in), "templates")
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
return tool_funs.SimpleWrapGridMap(count, cycles), nil
}
... ...
... ... @@ -4,6 +4,7 @@ import "github.com/beego/beego/v2/core/validation"
type QueryRuleCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
NameOrRemark string `cname:"规则名称或备注" json:"nameOrRemark"`
PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
}
... ...
... ... @@ -150,8 +150,12 @@ func (rs *EvaluationRuleService) List(in *command.QueryRuleCommand) (interface{}
transactionContext.RollbackTransaction()
}()
ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
inMap := tool_funs.SimpleStructToMap(in)
if len(in.NameOrRemark) > 0 {
inMap["nameOrRemark"] = "%" + in.NameOrRemark + "%"
}
// FIXME 总数量是否使用Count获取一个总数量
count, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in))
count, rules, err := ruleRepository.Find(inMap)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
... ... @@ -4,6 +4,7 @@ import "github.com/beego/beego/v2/core/validation"
type QueryTemplateCommand struct {
CompanyId int64 `cname:"公司ID" json:"companyId"`
Name string `cname:"模板名称" json:"name"`
PageNumber int `cname:"分页页码" json:"pageNumber" valid:"Required"`
PageSize int `cname:"分页数量" json:"pageSize" valid:"Required"`
}
... ...
... ... @@ -11,7 +11,7 @@ type UpdateTemplateCommand struct {
CreatorId int64 `cname:"创建人ID" json:"creatorId"`
Name string `cname:"模板名称" json:"name" valid:"Required"`
Describe string `cname:"模板描述" json:"remark"`
LinkNodes []domain.LinkNode `cname:"评估流程" json:"linkNodes"`
LinkNodes []*domain.LinkNode `cname:"评估流程" json:"linkNodes"`
}
func (in *UpdateTemplateCommand) Valid(validation *validation.Validation) {
... ...
... ... @@ -37,28 +37,28 @@ func (rs *EvaluationTemplateService) Create(in *command.CreateTemplateCommand) (
return nil, application.ThrowError(application.BUSINESS_ERROR, "名称已存在")
}
linkNodes := make([]domain.LinkNode, 0)
linkNodes = append(linkNodes, domain.LinkNode{
linkNodes := make([]*domain.LinkNode, 0)
linkNodes = append(linkNodes, &domain.LinkNode{
Type: domain.LinkNodeAssessment,
Name: "填写自评反馈",
KpiCycle: domain.KpiCycleDay,
})
linkNodes = append(linkNodes, domain.LinkNode{
linkNodes = append(linkNodes, &domain.LinkNode{
Type: domain.LinkNodeAllInvite,
Name: "360°邀请",
KpiCycle: domain.KpiCycleDay,
})
linkNodes = append(linkNodes, domain.LinkNode{
linkNodes = append(linkNodes, &domain.LinkNode{
Type: domain.LinkNodeAssessment,
Name: "360°评估",
KpiCycle: domain.KpiCycleDay,
})
linkNodes = append(linkNodes, domain.LinkNode{
linkNodes = append(linkNodes, &domain.LinkNode{
Type: domain.LinkNodeAssessment,
Name: "上级评估",
KpiCycle: domain.KpiCycleDay,
})
linkNodes = append(linkNodes, domain.LinkNode{
linkNodes = append(linkNodes, &domain.LinkNode{
Type: domain.LinkNodeViewResult,
Name: "绩效结果查看",
KpiCycle: domain.KpiCycleDay,
... ... @@ -174,8 +174,13 @@ func (rs *EvaluationTemplateService) List(in *command.QueryTemplateCommand) (int
transactionContext.RollbackTransaction()
}()
templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
inMap := tool_funs.SimpleStructToMap(in)
if len(in.Name) > 0 {
inMap["name"] = "%" + in.Name + "%"
}
// FIXME 总数量是否使用Count获取一个总数量
count, templates, err := templateRepository.Find(tool_funs.SimpleStructToMap(in), true)
count, templates, err := templateRepository.Find(inMap, "linkNodes")
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ... @@ -197,7 +202,7 @@ func (rs *EvaluationTemplateService) ListForEnable(in *command.AllEnableTemplate
"state": domain.TemplateStateEnable,
"offset": 0,
"limit": 9999999,
}, true)
}, "linkNodes")
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
... ...
... ... @@ -12,7 +12,7 @@ type EvaluationCycle struct {
CompanyId int64 `json:"companyId,string" comment:"公司ID"`
CreatorId int64 `json:"creatorId,string" comment:"创建人ID"`
KpiCycle int `json:"state" comment:"考核周期(0日、1周、2月)"`
Template []EvaluationTemplate `json:"template" comment:"周期使用模板"`
Templates []*EvaluationTemplate `json:"templates" comment:"周期使用模板"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
... ... @@ -22,6 +22,6 @@ 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)
Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*EvaluationCycle, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
... ... @@ -10,7 +10,7 @@ const (
)
type Rating struct {
Levels []RatingLevel `json:"levels" comment:"配置等级"`
Levels []*RatingLevel `json:"levels" comment:"配置等级"`
}
type RatingLevel struct {
... ... @@ -25,7 +25,7 @@ type Score struct {
Max float64 `json:"max" comment:"评分上限"`
IntervalState int `json:"intervalState" comment:"按分数子区间匹配等级(0关闭、1开启)"`
DecimalPlaces int `json:"decimalPlaces" comment:"保留小数点(0不保留、1保留一位、以此类推)"`
Levels []ScoreLevel `json:"levels" comment:"配置等级区间"`
Levels []*ScoreLevel `json:"levels" comment:"配置等级区间"`
}
type ScoreLevel struct {
... ...
... ... @@ -72,7 +72,7 @@ type EvaluationTemplate struct {
CompanyId int64 `json:"companyId,string" comment:"公司ID"`
CreatorId int64 `json:"creatorId,string" comment:"创建人ID"`
State int `json:"state" comment:"状态(0待完成配置、1待启用、2启用、3停用)"`
LinkNodes []LinkNode `json:"linkNodes" comment:"评估流程"`
LinkNodes []*LinkNode `json:"linkNodes" comment:"评估流程"`
CreatedAt time.Time `json:"createdAt" comment:"创建时间"`
UpdatedAt time.Time `json:"updatedAt" comment:"更新时间"`
DeletedAt *time.Time `json:"deletedAt" comment:"删除时间"`
... ... @@ -90,6 +90,6 @@ 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{}, simplify bool) (int64, []*EvaluationTemplate, error)
Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*EvaluationTemplate, error)
Count(queryOptions map[string]interface{}) (int64, error)
}
... ...
... ... @@ -14,7 +14,7 @@ type EvaluationCycle struct {
CompanyId int64 `comment:"公司ID"`
CreatorId int64 `comment:"创建人ID"`
KpiCycle int `comment:"考核周期(0日、1周、2月)"`
Template []domain.EvaluationTemplate `comment:"周期使用模板"`
Templates []*domain.EvaluationTemplate `comment:"周期使用模板"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
... ...
... ... @@ -13,7 +13,7 @@ type EvaluationTemplate struct {
CompanyId int64 `comment:"公司ID"`
CreatorId int64 `comment:"创建人ID"`
State int `comment:"状态(0待完成配置、1待启用、2启用、3停用)"`
LinkNodes []domain.LinkNode `comment:"评估流程"`
LinkNodes []*domain.LinkNode `comment:"评估流程"`
CreatedAt time.Time `comment:"创建时间"`
UpdatedAt time.Time `comment:"更新时间"`
DeletedAt *time.Time `comment:"删除时间"`
... ...
... ... @@ -29,7 +29,7 @@ func (repo *EvaluationCycleRepository) TransformToDomain(m *models.EvaluationCyc
CompanyId: m.CompanyId,
CreatorId: m.CreatorId,
KpiCycle: m.KpiCycle,
Template: m.Template,
Templates: m.Templates,
CreatedAt: m.CreatedAt,
UpdatedAt: m.UpdatedAt,
DeletedAt: m.DeletedAt,
... ... @@ -45,7 +45,7 @@ func (repo *EvaluationCycleRepository) TransformToModel(d *domain.EvaluationCycl
CompanyId: d.CompanyId,
CreatorId: d.CreatorId,
KpiCycle: d.KpiCycle,
Template: d.Template,
Templates: d.Templates,
CreatedAt: d.CreatedAt,
UpdatedAt: d.UpdatedAt,
DeletedAt: d.DeletedAt,
... ... @@ -114,12 +114,16 @@ func (repo *EvaluationCycleRepository) FindOne(queryOptions map[string]interface
return &u, nil
}
func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.EvaluationCycle, error) {
func (repo *EvaluationCycleRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (int64, []*domain.EvaluationCycle, error) {
tx := repo.transactionContext.PgTx
var m []*models.EvaluationCycle
query := tx.Model(&m).
Where("deleted_at isnull").
Limit(20)
// 排除掉保存模板的列[templates], 减少数据赋值
query := tx.Model(&m).Where("deleted_at isnull").Limit(20)
if len(excludeColumns) > 0 {
query.ExcludeColumn(excludeColumns...)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
... ...
... ... @@ -119,6 +119,10 @@ func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{})
var m []*models.EvaluationRule
query := tx.Model(&m).Where("deleted_at isnull").Limit(20)
if nameOrRemark, ok := queryOptions["nameOrRemark"]; ok && len(nameOrRemark.(string)) > 0 {
query.Where("name LIKE ? or remark LIKE ?", nameOrRemark, nameOrRemark)
}
if name, ok := queryOptions["name"]; ok {
query.Where("name = ?", name)
}
... ...
... ... @@ -112,15 +112,23 @@ func (repo *EvaluationTemplateRepository) FindOne(queryOptions map[string]interf
return &u, nil
}
func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}, simplify bool) (int64, []*domain.EvaluationTemplate, error) {
func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface{}, excludeColumns ...string) (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 len(excludeColumns) > 0 {
query.ExcludeColumn(excludeColumns...)
}
if v, ok := queryOptions["ids"]; ok {
query.Where("id in(?)", pg.In(v))
}
if name, ok := queryOptions["name"]; ok && len(name.(string)) > 0 {
query.Where("name LIKE ?", name)
}
if companyId, ok := queryOptions["companyId"]; ok {
... ... @@ -145,9 +153,6 @@ func (repo *EvaluationTemplateRepository) Find(queryOptions map[string]interface
var arrays []*domain.EvaluationTemplate
for _, v := range m {
d := repo.TransformToDomain(v)
if simplify { // 去掉流程数据避免数据量过大
d.LinkNodes = make([]domain.LinkNode, 0)
}
arrays = append(arrays, &d)
}
return int64(count), arrays, nil
... ...