assess_cache_service.go 1.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/application/staff_assess/command"
	"gitlab.fjmaimaimai.com/allied-creation/performance/pkg/domain"
)

// StaffAssessCacheService 保存待提交的评估数据(缓存)
type StaffAssessCacheService struct{}

func NewStaffAssessCacheService() *StaffAssessCacheService {
	newService := &StaffAssessCacheService{}
	return newService
}

func (srv StaffAssessCacheService) SaveAssessCache(in *command.SaveAssessCacheCommand) (map[string]interface{}, error) {
	transactionContext, err := factory.ValidateStartTransaction(in)
	if err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	cacheRepository := factory.CreateStaffAssessCacheRepository(map[string]interface{}{"transactionContext": transactionContext})
	_, caches, err := cacheRepository.Find(map[string]interface{}{"assessId": in.AssessId, "limit": 1})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	var cache *domain.StaffAssessCache
	if len(caches) == 0 {
		cache = &domain.StaffAssessCache{}
		cache.Id = 0
		cache.AssessId = in.AssessId
		cache.AssessContent = in.AssessContent
	} else {
		cache = caches[0]
		cache.AssessContent = in.AssessContent
	}
	if _, err = cacheRepository.Save(cache); 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 map[string]interface{}{"assessId": cache.Id}, nil
}