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