cooperation_contract_change_log.go 15.3 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/cooperationContractChangeLog/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/cooperationContractChangeLog/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"
)

// CooperationContractChangeLogService 共创合约变更日志
type CooperationContractChangeLogService struct {
}

// CreateCooperationContractChangeLog 创建共创合约变更日志
func (cooperationContractChangeLogService *CooperationContractChangeLogService) CreateCooperationContractChangeLog(createCooperationContractChangeLogCommand *command.CreateCooperationContractChangeLogCommand) (interface{}, error) {
	if err := createCooperationContractChangeLogCommand.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 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(createCooperationContractChangeLogCommand.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(createCooperationContractChangeLogCommand.CompanyId, createCooperationContractChangeLogCommand.OrgId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organization = data
	}

	// 用户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(createCooperationContractChangeLogCommand.CompanyId, createCooperationContractChangeLogCommand.OrgId, createCooperationContractChangeLogCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		operator = data
	}

	//  共创合约变更记录仓储初始化
	var cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}

	newCooperationContractChangeLog := &domain.CooperationContractChangeLog{
		IncentivesRule:            createCooperationContractChangeLogCommand.IncentivesRule,
		IncentivesRuleDetail:      createCooperationContractChangeLogCommand.IncentivesRuleDetail,
		OperationType:             createCooperationContractChangeLogCommand.OperationType,
		Undertakers:               createCooperationContractChangeLogCommand.Undertakers,
		CooperationContractNumber: createCooperationContractChangeLogCommand.CooperationContractNumber,
		Company:                   company,
		Org:                       organization,
		Operator:                  operator,
		UpdatedAt:                 time.Time{},
		DeletedAt:                 time.Time{},
		CreatedAt:                 time.Now(),
	}

	if cooperationContractChangeLog, err := cooperationContractChangeLogRepository.Save(newCooperationContractChangeLog); 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 cooperationContractChangeLog, nil
	}
}

// GetCooperationContractChangeLog 返回共创合约变更日志
func (cooperationContractChangeLogService *CooperationContractChangeLogService) GetCooperationContractChangeLog(getCooperationContractChangeLogQuery *query.GetCooperationContractChangeLogQuery) (interface{}, error) {
	if err := getCooperationContractChangeLogQuery.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 cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}
	cooperationContractChangeLog, err := cooperationContractChangeLogRepository.FindOne(map[string]interface{}{"cooperationContractChangeLogId": getCooperationContractChangeLogQuery.CooperationContractChangeLogId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创合约变更记录不存在")
	}
	if cooperationContractChangeLog == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(getCooperationContractChangeLogQuery.CooperationContractChangeLogId, 10)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return cooperationContractChangeLog, nil
	}
}

// ListCooperationContractChangeLog 返回共创合约变更日志列表
func (cooperationContractChangeLogService *CooperationContractChangeLogService) ListCooperationContractChangeLog(listCooperationContractChangeLogQuery *query.ListCooperationContractChangeLogQuery) (interface{}, error) {
	if err := listCooperationContractChangeLogQuery.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 cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}
	if count, cooperationContractChangeLogs, err := cooperationContractChangeLogRepository.Find(tool_funs.SimpleStructToMap(listCooperationContractChangeLogQuery)); 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":  cooperationContractChangeLogs,
			},
		}, nil
	}
}

// RemoveCooperationContractChangeLog 移除共创合约变更日志
func (cooperationContractChangeLogService *CooperationContractChangeLogService) RemoveCooperationContractChangeLog(removeCooperationContractChangeLogCommand *command.RemoveCooperationContractChangeLogCommand) (interface{}, error) {
	if err := removeCooperationContractChangeLogCommand.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 cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}
	cooperationContractChangeLog, err := cooperationContractChangeLogRepository.FindOne(map[string]interface{}{"cooperationContractChangeLogId": removeCooperationContractChangeLogCommand.CooperationContractChangeLogId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创合约变更记录不存在")
	}
	if cooperationContractChangeLog == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(removeCooperationContractChangeLogCommand.CooperationContractChangeLogId, 10)))
	}
	if cooperationContractChangeLog, err := cooperationContractChangeLogRepository.Remove(cooperationContractChangeLog); 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 cooperationContractChangeLog, nil
	}
}

// SearchCooperationContractChangeLog 共创合约变更记录搜索
func (cooperationContractChangeLogService *CooperationContractChangeLogService) SearchCooperationContractChangeLog(searchCooperationContractChangeLogQuery *query.SearchCooperationContractChangeLogQuery) (interface{}, error) {
	if err := searchCooperationContractChangeLogQuery.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 cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}
	if count, cooperationContractChangeLogs, err := cooperationContractChangeLogRepository.Find(tool_funs.SimpleStructToMap(searchCooperationContractChangeLogQuery)); 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":  cooperationContractChangeLogs,
			},
		}, nil
	}
}

// UpdateCooperationContractChangeLog 更新共创合约变更日志
func (cooperationContractChangeLogService *CooperationContractChangeLogService) UpdateCooperationContractChangeLog(updateCooperationContractChangeLogCommand *command.UpdateCooperationContractChangeLogCommand) (interface{}, error) {
	if err := updateCooperationContractChangeLogCommand.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 cooperationContractChangeLogRepository domain.CooperationContractChangeLogRepository
	if value, err := factory.CreateCooperationContractChangeLogRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractChangeLogRepository = value
	}
	cooperationContractChangeLog, err := cooperationContractChangeLogRepository.FindOne(map[string]interface{}{"cooperationContractChangeLogId": updateCooperationContractChangeLogCommand.CooperationContractChangeLogId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创合约变更记录不存在")
	}
	if cooperationContractChangeLog == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", strconv.FormatInt(updateCooperationContractChangeLogCommand.CooperationContractChangeLogId, 10)))
	}
	if err := cooperationContractChangeLog.Update(tool_funs.SimpleStructToMap(updateCooperationContractChangeLogCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if cooperationContractChangeLog, err := cooperationContractChangeLogRepository.Save(cooperationContractChangeLog); 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 cooperationContractChangeLog, nil
	}
}

func NewCooperationContractChangeLogService(options map[string]interface{}) *CooperationContractChangeLogService {
	newCooperationContractChangeLogService := &CooperationContractChangeLogService{}
	return newCooperationContractChangeLogService
}