project_belong.go 10.6 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/projectBelong/command"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/application/projectBelong/query"
	"gitlab.fjmaimaimai.com/linmadan/mmm-worth/pkg/domain"
)

// 项目归属服务
type ProjectBelongService struct {
}

// 创建项目归属
func (projectBelongService *ProjectBelongService) CreateProjectBelong(createProjectBelongCommand *command.CreateProjectBelongCommand) (interface{}, error) {
	if err := createProjectBelongCommand.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()
	}()
	newProjectBelong := &domain.ProjectBelong{
		CompanyId:         createProjectBelongCommand.CompanyId,
		ProjectBelongName: createProjectBelongCommand.ProjectBelongName,
	}
	var projectBelongRepository domain.ProjectBelongRepository
	if value, err := factory.CreateProjectBelongRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectBelongRepository = value
	}
	if count, _, err := projectBelongRepository.Find(map[string]interface{}{
		"projectBelongName": createProjectBelongCommand.ProjectBelongName,
		"companyId":         createProjectBelongCommand.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 projectBelong, err := projectBelongRepository.Save(newProjectBelong); 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 projectBelong, nil
	}
}

// 返回项目归属
func (projectBelongService *ProjectBelongService) GetProjectBelong(getProjectBelongQuery *query.GetProjectBelongQuery) (interface{}, error) {
	if err := getProjectBelongQuery.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 projectBelongRepository domain.ProjectBelongRepository
	if value, err := factory.CreateProjectBelongRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectBelongRepository = value
	}
	projectBelong, err := projectBelongRepository.FindOne(map[string]interface{}{"projectBelongId": getProjectBelongQuery.ProjectBelongId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectBelong == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getProjectBelongQuery.ProjectBelongId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return projectBelong, nil
	}
}

// 更新项目归属
func (projectBelongService *ProjectBelongService) UpdateProjectBelong(updateProjectBelongCommand *command.UpdateProjectBelongCommand) (interface{}, error) {
	if err := updateProjectBelongCommand.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 projectBelongRepository domain.ProjectBelongRepository
	if value, err := factory.CreateProjectBelongRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectBelongRepository = value
	}
	projectBelong, err := projectBelongRepository.FindOne(map[string]interface{}{"projectBelongId": updateProjectBelongCommand.ProjectBelongId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectBelong == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateProjectBelongCommand.ProjectBelongId)))
	}
	if count, projectBelongs, err := projectBelongRepository.Find(map[string]interface{}{
		"projectBelongName": updateProjectBelongCommand.ProjectBelongName,
		"companyId":         projectBelong.CompanyId,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		if count > 0 && projectBelongs[0].ProjectBelongId != projectBelong.ProjectBelongId {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
		}
	}
	if err := projectBelong.Update(tool_funs.SimpleStructToMap(updateProjectBelongCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if projectBelong, err := projectBelongRepository.Save(projectBelong); 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 projectBelong, nil
	}
}

// 移除项目归属
func (projectBelongService *ProjectBelongService) RemoveProjectBelong(removeProjectBelongCommand *command.RemoveProjectBelongCommand) (interface{}, error) {
	if err := removeProjectBelongCommand.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 projectBelongRepository domain.ProjectBelongRepository
	if value, err := factory.CreateProjectBelongRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectBelongRepository = value
	}
	projectBelong, err := projectBelongRepository.FindOne(map[string]interface{}{"projectBelongId": removeProjectBelongCommand.ProjectBelongId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if projectBelong == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeProjectBelongCommand.ProjectBelongId)))
	}
	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{}{
		"projectBelongs": []int{projectBelong.ProjectBelongId},
	}); 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 projectBelong, err := projectBelongRepository.Remove(projectBelong); 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 projectBelong, nil
	}
}

// 返回项目归属列表
func (projectBelongService *ProjectBelongService) ListProjectBelong(listProjectBelongQuery *query.ListProjectBelongQuery) (interface{}, error) {
	if err := listProjectBelongQuery.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 projectBelongRepository domain.ProjectBelongRepository
	if value, err := factory.CreateProjectBelongRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		projectBelongRepository = value
	}
	if count, projectBelongs, err := projectBelongRepository.Find(tool_funs.SimpleStructToMap(listProjectBelongQuery)); 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,
			"projectBelongs": projectBelongs,
		}, nil
	}
}

func NewProjectBelongService(options map[string]interface{}) *ProjectBelongService {
	newProjectBelongService := &ProjectBelongService{}
	return newProjectBelongService
}