rule_service.go 7.2 KB
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"
	"sort"
)

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, "已存在相同名称的评估规则")
	}

	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
		})
	}

	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())
	}

	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
		})
	}

	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) Get(in *command.GetRuleCommand) (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 := 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})
//
//	total, rules, err := ruleRepository.Find(tool_funs.SimpleStructToMap(in))
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	return tool_funs.SimpleWrapGridMap(total, rules), nil
//}

func (rs *EvaluationRuleService) ListRelCreator(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})

	total, 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(total, ras), nil
}