task_nature.go 10.2 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/factory"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/taskNature/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/taskNature/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)

// 任务性质服务
type TaskNatureService struct {
}

// 创建任务性质
func (taskNatureService *TaskNatureService) CreateTaskNature(createTaskNatureCommand *command.CreateTaskNatureCommand) (interface{}, error) {
	if err := createTaskNatureCommand.ValidateCommand(); 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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	newTaskNature := &domain.TaskNature{
		CompanyId:      createTaskNatureCommand.CompanyId,
		TaskNatureName: createTaskNatureCommand.TaskNatureName,
	}
	var taskNatureRepository domain.TaskNatureRepository
	if value, err := factory.CreateTaskNatureRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskNatureRepository = value
	}
	if count, _, err := taskNatureRepository.Find(map[string]interface{}{
		"taskNatureName": createTaskNatureCommand.TaskNatureName,
		"companyId":      createTaskNatureCommand.CompanyId,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
		}
	}
	if taskNature, err := taskNatureRepository.Save(newTaskNature); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return taskNature, nil
	}
}

// 返回任务性质
func (taskNatureService *TaskNatureService) GetTaskNature(getTaskNatureQuery *query.GetTaskNatureQuery) (interface{}, error) {
	if err := getTaskNatureQuery.ValidateQuery(); 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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var taskNatureRepository domain.TaskNatureRepository
	if value, err := factory.CreateTaskNatureRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskNatureRepository = value
	}
	taskNature, err := taskNatureRepository.FindOne(map[string]interface{}{"taskNatureId": getTaskNatureQuery.TaskNatureId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if taskNature == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getTaskNatureQuery.TaskNatureId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return taskNature, nil
	}
}

// 更新任务性质
func (taskNatureService *TaskNatureService) UpdateTaskNature(updateTaskNatureCommand *command.UpdateTaskNatureCommand) (interface{}, error) {
	if err := updateTaskNatureCommand.ValidateCommand(); 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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var taskNatureRepository domain.TaskNatureRepository
	if value, err := factory.CreateTaskNatureRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskNatureRepository = value
	}
	taskNature, err := taskNatureRepository.FindOne(map[string]interface{}{"taskNatureId": updateTaskNatureCommand.TaskNatureId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if taskNature == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateTaskNatureCommand.TaskNatureId)))
	}
	if count, taskNatures, err := taskNatureRepository.Find(map[string]interface{}{
		"taskNatureName": updateTaskNatureCommand.TaskNatureName,
		"companyId":      taskNature.CompanyId,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 && taskNatures[0].TaskNatureId != taskNature.TaskNatureId {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
		}
	}
	if err := taskNature.Update(tool_funs.SimpleStructToMap(updateTaskNatureCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if taskNature, err := taskNatureRepository.Save(taskNature); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return taskNature, nil
	}
}

// 移除任务性质
func (taskNatureService *TaskNatureService) RemoveTaskNature(removeTaskNatureCommand *command.RemoveTaskNatureCommand) (interface{}, error) {
	if err := removeTaskNatureCommand.ValidateCommand(); 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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var taskNatureRepository domain.TaskNatureRepository
	if value, err := factory.CreateTaskNatureRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskNatureRepository = value
	}
	taskNature, err := taskNatureRepository.FindOne(map[string]interface{}{"taskNatureId": removeTaskNatureCommand.TaskNatureId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if taskNature == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeTaskNatureCommand.TaskNatureId)))
	}
	var taskRepository domain.TaskRepository
	if value, err := factory.CreateTaskRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskRepository = value
	}
	if count, _, err := taskRepository.Find(map[string]interface{}{
		"taskNatures": []int{taskNature.TaskNatureId},
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "该标签已被使用,不可删除")
		}
	}
	if taskNature, err := taskNatureRepository.Remove(taskNature); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return taskNature, nil
	}
}

// 返回任务性质列表
func (taskNatureService *TaskNatureService) ListTaskNature(listTaskNatureQuery *query.ListTaskNatureQuery) (interface{}, error) {
	if err := listTaskNatureQuery.ValidateQuery(); 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())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var taskNatureRepository domain.TaskNatureRepository
	if value, err := factory.CreateTaskNatureRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		taskNatureRepository = value
	}
	if count, taskNatures, err := taskNatureRepository.Find(tool_funs.SimpleStructToMap(listTaskNatureQuery)); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return map[string]interface{}{
			"count":       count,
			"taskNatures": taskNatures,
		}, nil
	}
}

func NewTaskNatureService(options map[string]interface{}) *TaskNatureService {
	newTaskNatureService := &TaskNatureService{}
	return newTaskNatureService
}