cooperation_project.go 16.4 KB
package service

import (
	"fmt"
	"github.com/linmadan/egglib-go/core/application"
	"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationProject/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationProject/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/factory"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/domain/service"
	"strconv"
	"time"
)

// CooperationProjectService 共创项目服务
type CooperationProjectService struct {
}

// CheckUndertaker 判断当前勾选的承接对象是否存在用户
func (cooperationProjectService *CooperationProjectService) CheckUndertaker(checkUndertakerQuery *query.CheckUndertakerQuery) (interface{}, error) {
	if err := checkUndertakerQuery.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// CreateCooperationProject 创建共创项目服务
func (cooperationProjectService *CooperationProjectService) CreateCooperationProject(createCooperationProjectCommand *command.CreateCooperationProjectCommand) (interface{}, error) {
	if err := createCooperationProjectCommand.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()
	}()

	// 用户REST服务初始化
	var userService service.UserService
	if value, err := factory.CreateUserService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		userService = value
	}

	// 获取操作人
	var operator *domain.User
	if data, err := userService.OperatorFrom(createCooperationProjectCommand.CompanyId, createCooperationProjectCommand.OrgId, createCooperationProjectCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		operator = data
	}

	// Sponsor
	var sponsor *domain.User
	if data, err := userService.UserFrom(createCooperationProjectCommand.CompanyId, createCooperationProjectCommand.OrgId, createCooperationProjectCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		sponsor = data
	}

	// Publisher
	var publisher *domain.User
	if data, err := userService.UserFrom(createCooperationProjectCommand.CompanyId, createCooperationProjectCommand.OrgId, createCooperationProjectCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		publisher = data
	}

	// 公司REST服务初始化
	var companyService service.CompanyService
	if value, err := factory.CreateCompanyService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		companyService = value
	}

	// 获取公司信息
	var company *domain.Company
	if data, err := companyService.CompanyFrom(createCooperationProjectCommand.CompanyId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		company = data
	}

	// 组织机构REST服务初始化
	var organizationService service.OrgService
	if value, err := factory.CreateOrganizationService(map[string]interface{}{}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organizationService = value
	}

	// 获取组织机构信息
	var organization *domain.Org
	if data, err := organizationService.OrgFrom(createCooperationProjectCommand.CompanyId, createCooperationProjectCommand.OrgId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organization = data
	}

	newCooperationProject := &domain.CooperationProject{
		CooperationProjectNumber:          "",
		CooperationProjectName:            createCooperationProjectCommand.CooperationProjectName,
		CooperationProjectUndertakerTypes: createCooperationProjectCommand.CooperationProjectUndertakerTypes,
		CooperationProjectSponsor:         sponsor,
		CooperationProjectPublisher:       publisher,
		CooperationProjectDescription:     createCooperationProjectCommand.CooperationProjectDescription,
		Company:                           company,
		Org:                               organization,
		Operator:                          operator,
		OperateTime:                       time.Now(),
		Status:                            0,
		UpdatedAt:                         time.Time{},
		DeletedAt:                         time.Time{},
		CreatedAt:                         time.Now(),
	}
	var cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	if cooperationProject, err := cooperationProjectRepository.Save(newCooperationProject); 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 cooperationProject, nil
	}
}

// GetCooperationProject 返回共创项目服务
func (cooperationProjectService *CooperationProjectService) GetCooperationProject(getCooperationProjectQuery *query.GetCooperationProjectQuery) (interface{}, error) {
	if err := getCooperationProjectQuery.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 cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	cooperationProject, err := cooperationProjectRepository.FindOne(map[string]interface{}{"cooperationProjectId": getCooperationProjectQuery.CooperationProjectId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if cooperationProject == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(getCooperationProjectQuery.CooperationProjectId, 10)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return cooperationProject, nil
	}
}

// ListCooperationProject 返回共创项目服务列表
func (cooperationProjectService *CooperationProjectService) ListCooperationProject(listCooperationProjectQuery *query.ListCooperationProjectQuery) (interface{}, error) {
	if err := listCooperationProjectQuery.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 cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	if count, cooperationProjects, err := cooperationProjectRepository.Find(tool_funs.SimpleStructToMap(listCooperationProjectQuery)); 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{}{
			//"grid": map[string]interface{}{
			"total": count,
			"list":  cooperationProjects,
			//},
		}, nil
	}
}

// ReleaseCooperationProject 发布共创项目
func (cooperationProjectService *CooperationProjectService) ReleaseCooperationProject(releaseCooperationProjectCommand *command.ReleaseCooperationProjectCommand) (interface{}, error) {
	if err := releaseCooperationProjectCommand.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()
	}()
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return nil, nil
}

// RemoveCooperationProject 移除共创项目服务
func (cooperationProjectService *CooperationProjectService) RemoveCooperationProject(removeCooperationProjectCommand *command.RemoveCooperationProjectCommand) (interface{}, error) {
	if err := removeCooperationProjectCommand.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 cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	cooperationProject, err := cooperationProjectRepository.FindOne(map[string]interface{}{"cooperationProjectId": removeCooperationProjectCommand.CooperationProjectId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if cooperationProject == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeCooperationProjectCommand.CooperationProjectId, 10)))
	}
	if cooperationProject, err := cooperationProjectRepository.Remove(cooperationProject); 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 cooperationProject, nil
	}
}

// SearchCooperationProject 查询共创项目
func (cooperationProjectService *CooperationProjectService) SearchCooperationProject(searchCooperationProjectQuery *query.SearchCooperationProjectQuery) (interface{}, error) {
	if err := searchCooperationProjectQuery.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 cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	if count, cooperationProjects, err := cooperationProjectRepository.Find(tool_funs.SimpleStructToMap(searchCooperationProjectQuery)); 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{}{
			//"grid": map[string]interface{}{
			"total": count,
			"list":  cooperationProjects,
			//},
		}, nil
	}
}

// UpdateCooperationProject 更新共创项目服务
func (cooperationProjectService *CooperationProjectService) UpdateCooperationProject(updateCooperationProjectCommand *command.UpdateCooperationProjectCommand) (interface{}, error) {
	if err := updateCooperationProjectCommand.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 cooperationProjectRepository domain.CooperationProjectRepository
	if value, err := factory.CreateCooperationProjectRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationProjectRepository = value
	}
	cooperationProject, err := cooperationProjectRepository.FindOne(map[string]interface{}{"cooperationProjectId": updateCooperationProjectCommand.CooperationProjectId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if cooperationProject == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateCooperationProjectCommand.CooperationProjectId)))
	}
	if err := cooperationProject.Update(tool_funs.SimpleStructToMap(updateCooperationProjectCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if cooperationProject, err := cooperationProjectRepository.Save(cooperationProject); 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 cooperationProject, nil
	}
}

func NewCooperationProjectService(options map[string]interface{}) *CooperationProjectService {
	newCooperationProjectService := &CooperationProjectService{}
	return newCooperationProjectService
}