审查视图

pkg/application/evaluation_rule/rule_service.go 8.8 KB
1 2 3 4 5 6 7 8 9
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"
郑周 authored
10
	"sort"
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
)

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 {
郑周 authored
38
		return nil, application.ThrowError(application.BUSINESS_ERROR, "已存在相同名称的评估规则")
39
	}
郑周 authored
40 41 42 43 44 45 46

	if in.Type == domain.EvaluationTypeRating { // 按等级量化值排序
		sort.SliceStable(in.Rating.Levels, func(i, j int) bool {
			return in.Rating.Levels[i].QuantizedValue < in.Rating.Levels[j].QuantizedValue
		})
	}
47 48 49 50 51 52 53
	newRule := &domain.EvaluationRule{
		Id:        0,
		Name:      in.Name,
		Remark:    in.Remark,
		CompanyId: in.CompanyId,
		CreatorId: in.CreatorId,
		Type:      in.Type,
郑周 authored
54
		SysType:   domain.EvaluationSysTypeCommon,
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
		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 {
郑周 authored
86
		return nil, application.ThrowError(application.BUSINESS_ERROR, "已存在相同名称的评估规则")
87 88 89 90 91 92 93
	}

	rule, err := ruleRepository.FindOne(map[string]interface{}{"id": in.Id})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
郑周 authored
94 95 96 97 98 99
	if in.Type == domain.EvaluationTypeRating { // 按等级量化值排序
		sort.SliceStable(in.Rating.Levels, func(i, j int) bool {
			return in.Rating.Levels[i].QuantizedValue < in.Rating.Levels[j].QuantizedValue
		})
	}
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	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
}
116
func (rs *EvaluationRuleService) Get(in *command.GetRuleCommand) (interface{}, error) {
117
	transactionContext, err := factory.ValidateStartTransaction(in)
118
	if err != nil {
119
		return nil, err
120
	}
121 122 123
	defer func() {
		transactionContext.RollbackTransaction()
	}()
124 125 126 127 128
	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())
	}
129 130 131 132

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
133 134 135
	return rule, nil
}
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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())
	}
郑周 authored
151 152 153 154
	if rule.SysType == domain.EvaluationSysTypeSystem {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "系统默认规则不可删除")
	}
155 156 157 158 159 160 161 162 163 164
	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
}
165
func (rs *EvaluationRuleService) ListRelCreator(in *command.QueryRuleCommand) (interface{}, error) {
郑周 authored
166
	transactionContext, err := factory.ValidateStartTransaction(in)
167 168 169 170 171 172 173
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	ruleRepository := factory.CreateEvaluationRuleRepository(map[string]interface{}{"transactionContext": transactionContext})
174 175
	userRepository := factory.CreateUserRepository(map[string]interface{}{"transactionContext": transactionContext})
176 177 178 179 180 181 182 183 184 185 186 187 188
	// 查询系统默认规则
	_, sysRules, err := ruleRepository.Find(map[string]interface{}{"companyId": in.CompanyId, "sysType": domain.EvaluationSysTypeSystem, "limit": 1})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	// 不存在,生成默认规则
	if len(sysRules) == 0 {
		newRule := domain.GenerateSysRule(in.CompanyId)
		if _, err := ruleRepository.Insert(newRule); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
郑周 authored
189
	total, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in))
190 191 192 193 194 195 196 197 198 199 200 201
	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)
	}
郑周 authored
202
203 204 205 206 207 208 209 210 211 212 213 214
	_, 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
		}
	}
郑周 authored
215 216 217 218 219

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
郑周 authored
220
	return tool_funs.SimpleWrapGridMap(total, ras), nil
221
}
郑周 authored
222 223

func (rs *EvaluationRuleService) ListCreator(in *command.QueryCreatorCommand) (interface{}, error) {
郑周 authored
224
	transactionContext, err := factory.ValidateStartTransaction(in)
郑周 authored
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
	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})

	_, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in))
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}

	// 获取所有创建人ID
	creatorMap := map[int64]int64{}
	for i := range rules {
		creatorMap[rules[i].CreatorId] = rules[i].CreatorId
	}
	creatorIds := make([]int64, 0)
	for k := range creatorMap {
		creatorIds = append(creatorIds, k)
	}
	_, users, _ := userRepository.Find(map[string]interface{}{"ids": creatorIds, "limit": len(creatorIds)})
	cas := make([]*adapter.CreatorAdapter, 0)
	for i := range users {
		ca := &adapter.CreatorAdapter{
			Id:   users[i].Id,
			Name: users[i].Name,
		}
		cas = append(cas, ca)
	}
郑周 authored
257 258 259 260

	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
郑周 authored
261 262
	return map[string]interface{}{"list": cas}, nil
}