From 53def8bceb5498ebcb2f4854442fe2fbb70cb3cd Mon Sep 17 00:00:00 2001
From: ZhengZ <75157243@qq.com>
Date: Mon, 14 Nov 2022 14:13:22 +0800
Subject: [PATCH] 周期下的 模板和规则数据  生成

---
 pkg/application/evaluation_cycle/cycle_service.go                        | 41 +++++++++++++++++++++++++++++++++++++----
 pkg/domain/evaluation_template.go                                        | 13 +++++++------
 pkg/infrastructure/repository/pg_evaluation_cycle_template_repository.go |  5 +++++
 pkg/infrastructure/repository/pg_evaluation_rule_repository.go           |  4 ++++
 4 files changed, 53 insertions(+), 10 deletions(-)

diff --git a/pkg/application/evaluation_cycle/cycle_service.go b/pkg/application/evaluation_cycle/cycle_service.go
index abafaa3..a02d514 100644
--- a/pkg/application/evaluation_cycle/cycle_service.go
+++ b/pkg/application/evaluation_cycle/cycle_service.go
@@ -30,6 +30,7 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
 	cycleRepository := factory.CreateEvaluationCycleRepository(map[string]interface{}{"transactionContext": transactionContext})
 	cycleTemplateRepository := factory.CreateEvaluationCycleTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
 	templateRepository := factory.CreateEvaluationTemplateRepository(map[string]interface{}{"transactionContext": transactionContext})
+	ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
 
 	// 检测名称重复
 	count, err := cycleRepository.Count(map[string]interface{}{"name": in.Name, "companyId": in.CompanyId})
@@ -48,6 +49,7 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
 		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "模板不存在, 请重新选择")
 	}
 
+	// 生成新周期数据
 	newCycle := &domain.EvaluationCycle{
 		Id:        0,
 		Name:      in.Name,
@@ -62,11 +64,41 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
 		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
 	}
 
+	// 获取所有模板中的规则对象数据
+	ruleIds := make([]int64, 0)
+	ruleMap := map[int64]*domain.EvaluationRule{}
+	for i := range templates {
+		v := templates[i]
+		for j := range v.LinkNodes {
+			node := v.LinkNodes[j]
+			for k := range node.NodeContents {
+				nodeContent := node.NodeContents[k]
+				ruleIds = append(ruleIds, nodeContent.RuleId)
+			}
+		}
+	}
+	_, rules, err := ruleRepository.Find(map[string]interface{}{"ids": ruleIds, "companyId": in.CompanyId})
+	for i := range rules {
+		ruleMap[rules[i].Id] = rules[i]
+	}
+
 	ctAdapter := &adapter.CycleTemplateAdapter{}
 	ctAdapter.EvaluationCycle = cycle
-
 	for i := range templates {
 		v := templates[i]
+
+		// 对评估模板中的评估规则进行数据赋值
+		for j := range v.LinkNodes {
+			node := v.LinkNodes[j]
+			for k := range node.NodeContents {
+				nodeContent := node.NodeContents[k]
+				if rule, ok := ruleMap[nodeContent.RuleId]; ok {
+					nodeContent.Rule = rule
+				}
+			}
+		}
+
+		// 插入周期中的模板数据
 		cycleTemplate := &domain.EvaluationCycleTemplate{
 			Id:       0,
 			Name:     v.Name,
@@ -78,10 +110,11 @@ func (rs *EvaluationCycleService) Create(in *command.CreateCycleCommand) (interf
 			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
 		}
 
+		// 输出模板简单信息
 		ctAdapter.TemplateSimples = append(ctAdapter.TemplateSimples, &domain.TemplateSimple{
-			Id:        v.Id,
-			Name:      v.Name,
-			CreatedAt: v.CreatedAt,
+			Id:        cycleTemplate.Id,
+			Name:      cycleTemplate.Name,
+			CreatedAt: cycleTemplate.CreatedAt,
 		})
 	}
 
diff --git a/pkg/domain/evaluation_template.go b/pkg/domain/evaluation_template.go
index 88ac429..e91694c 100644
--- a/pkg/domain/evaluation_template.go
+++ b/pkg/domain/evaluation_template.go
@@ -31,12 +31,13 @@ type EntryItem struct {
 
 // NodeContent 评估内容
 type NodeContent struct {
-	Category    string       `json:"category" comment:"类别"`
-	Name        string       `json:"name" comment:"名称"`
-	RuleId      string       `json:"ruleId" comment:"评估规则ID"`
-	PromptTitle string       `json:"promptTitle" comment:"提示项标题"`
-	PromptText  string       `json:"promptText" comment:"提示项正文"`
-	EntryItems  []*EntryItem `json:"entryItems" comment:"填写项"`
+	Category    string          `json:"category" comment:"类别"`
+	Name        string          `json:"name" comment:"名称"`
+	RuleId      int64           `json:"ruleId" comment:"评估规则ID"`
+	Rule        *EvaluationRule `json:"rule" comment:"评估规则对象"`
+	PromptTitle string          `json:"promptTitle" comment:"提示项标题"`
+	PromptText  string          `json:"promptText" comment:"提示项正文"`
+	EntryItems  []*EntryItem    `json:"entryItems" comment:"填写项"`
 }
 
 //// NodeAllInvite 360°邀请
diff --git a/pkg/infrastructure/repository/pg_evaluation_cycle_template_repository.go b/pkg/infrastructure/repository/pg_evaluation_cycle_template_repository.go
index 3b81c10..648e328 100644
--- a/pkg/infrastructure/repository/pg_evaluation_cycle_template_repository.go
+++ b/pkg/infrastructure/repository/pg_evaluation_cycle_template_repository.go
@@ -58,9 +58,14 @@ func (repo *EvaluationCycleTemplateRepository) Insert(d *domain.EvaluationCycleT
 		d.Id = id
 		d.CreatedAt = time.Now()
 		d.UpdatedAt = d.CreatedAt
+		// 模板对象ID也更新
+		if d.Template != nil {
+			d.Template.Id = id
+		}
 	} else {
 		d.UpdatedAt = time.Now()
 	}
+
 	m := repo.TransformToModel(d)
 	tx := repo.transactionContext.PgTx
 	var err error
diff --git a/pkg/infrastructure/repository/pg_evaluation_rule_repository.go b/pkg/infrastructure/repository/pg_evaluation_rule_repository.go
index 14b8ccd..95df901 100644
--- a/pkg/infrastructure/repository/pg_evaluation_rule_repository.go
+++ b/pkg/infrastructure/repository/pg_evaluation_rule_repository.go
@@ -123,6 +123,10 @@ func (repo *EvaluationRuleRepository) Find(queryOptions map[string]interface{}) 
 		query.Where("name LIKE ? or remark LIKE ?", v, v)
 	}
 
+	if v, ok := queryOptions["ids"]; ok {
+		query.Where("id in(?)", pg.In(v))
+	}
+
 	if v, ok := queryOptions["name"]; ok {
 		query.Where("name = ?", v)
 	}
--
libgit2 0.24.0