contract_undertaker_feedback.go 15.7 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/contractUndertakerFeedback/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/application/contractUndertakerFeedback/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"
	"time"
)

// ContractUndertakerFeedbackService 共创合约反馈服务
type ContractUndertakerFeedbackService struct {
}

// CreateContractUndertakerFeedback 创建共创合约反馈服务
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) CreateContractUndertakerFeedback(createContractUndertakerFeedbackCommand *command.CreateContractUndertakerFeedbackCommand) (interface{}, error) {
	if err := createContractUndertakerFeedbackCommand.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 undertaker *domain.Undertaker
	if data, err := userService.UndertakerFrom(createContractUndertakerFeedbackCommand.CompanyId, createContractUndertakerFeedbackCommand.OrgId, createContractUndertakerFeedbackCommand.UserId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		undertaker = 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(createContractUndertakerFeedbackCommand.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(createContractUndertakerFeedbackCommand.CompanyId, createContractUndertakerFeedbackCommand.OrgId); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		organization = data
	}

	// 获取项目合约信息
	var cooperationContractRepository domain.CooperationContractRepository
	if value, err := factory.CreateCooperationContractRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		cooperationContractRepository = value
	}
	cooperationContract, err := cooperationContractRepository.FindOne(map[string]interface{}{"cooperationContractNumber": createContractUndertakerFeedbackCommand.CooperationContractNumber})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "共创合约不存在")
	}
	if cooperationContract == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", createContractUndertakerFeedbackCommand.CooperationContractNumber))
	} else {
		newContractUndertakerFeedback := &domain.ContractUndertakerFeedback{
			FeedbackAttachment:        createContractUndertakerFeedbackCommand.FeedbackAttachment,
			FeedbackContent:           createContractUndertakerFeedbackCommand.FeedbackContent,
			CooperationContractNumber: createContractUndertakerFeedbackCommand.CooperationContractNumber,
			CooperationContractName:   cooperationContract.CooperationContractName,
			CooperationModeName:       cooperationContract.CooperationMode.CooperationModeName,
			ContractUndertaker:        undertaker,
			Company:                   company,
			Org:                       organization,
			UpdatedAt:                 time.Time{},
			DeletedAt:                 time.Time{},
			CreatedAt:                 time.Now(),
		}

		var contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
		if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
			"transactionContext": transactionContext,
		}); err != nil {
			return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
		} else {
			contractUndertakerFeedbackRepository = value
		}
		if contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.Save(newContractUndertakerFeedback); 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 contractUndertakerFeedback, nil
		}
	}
}

// GetContractUndertakerFeedback 返回共创合约反馈服务
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) GetContractUndertakerFeedback(getContractUndertakerFeedbackQuery *query.GetContractUndertakerFeedbackQuery) (interface{}, error) {
	if err := getContractUndertakerFeedbackQuery.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 contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
	if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		contractUndertakerFeedbackRepository = value
	}
	contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.FindOne(map[string]interface{}{"contractUndertakerFeedbackId": getContractUndertakerFeedbackQuery.FeedbackId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "合约反馈信息不存在")
	}
	if contractUndertakerFeedback == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getContractUndertakerFeedbackQuery.FeedbackId)))
	} else {
		if err := transactionContext.CommitTransaction(); err != nil {
			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
		}
		return contractUndertakerFeedback, nil
	}
}

// ListContractUndertakerFeedback 返回共创合约反馈服务列表
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) ListContractUndertakerFeedback(listContractUndertakerFeedbackQuery *query.ListContractUndertakerFeedbackQuery) (interface{}, error) {
	if err := listContractUndertakerFeedbackQuery.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 contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
	if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		contractUndertakerFeedbackRepository = value
	}
	if count, contractUndertakerFeedbacks, err := contractUndertakerFeedbackRepository.Find(tool_funs.SimpleStructToMap(listContractUndertakerFeedbackQuery)); 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":  contractUndertakerFeedbacks,
			},
		}, nil
	}
}

// RemoveContractUndertakerFeedback 移除共创合约反馈服务
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) RemoveContractUndertakerFeedback(removeContractUndertakerFeedbackCommand *command.RemoveContractUndertakerFeedbackCommand) (interface{}, error) {
	if err := removeContractUndertakerFeedbackCommand.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 contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
	if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		contractUndertakerFeedbackRepository = value
	}
	contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.FindOne(map[string]interface{}{"contractUndertakerFeedbackId": removeContractUndertakerFeedbackCommand.FeedbackId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "反馈信息不存在")
	}
	if contractUndertakerFeedback == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removeContractUndertakerFeedbackCommand.FeedbackId)))
	}
	if contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.Remove(contractUndertakerFeedback); 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 contractUndertakerFeedback, nil
	}
}

// SearchContractUndertakerFeedback 查询共创承接方反馈信息
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) SearchContractUndertakerFeedback(searchContractUndertakerFeedbackQuery *query.SearchContractUndertakerFeedbackQuery) (interface{}, error) {
	if err := searchContractUndertakerFeedbackQuery.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 contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
	if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		contractUndertakerFeedbackRepository = value
	}
	if count, contractUndertakerFeedbacks, err := contractUndertakerFeedbackRepository.Find(tool_funs.SimpleStructToMap(searchContractUndertakerFeedbackQuery)); 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":  contractUndertakerFeedbacks,
			},
		}, nil
	}
}

// UpdateContractUndertakerFeedback 更新共创合约反馈服务
func (contractUndertakerFeedbackService *ContractUndertakerFeedbackService) UpdateContractUndertakerFeedback(updateContractUndertakerFeedbackCommand *command.UpdateContractUndertakerFeedbackCommand) (interface{}, error) {
	if err := updateContractUndertakerFeedbackCommand.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 contractUndertakerFeedbackRepository domain.ContractUndertakerFeedbackRepository
	if value, err := factory.CreateContractUndertakerFeedbackRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	} else {
		contractUndertakerFeedbackRepository = value
	}
	contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.FindOne(map[string]interface{}{"contractUndertakerFeedbackId": updateContractUndertakerFeedbackCommand.FeedbackId})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "反馈信息不存在")
	}
	if contractUndertakerFeedback == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updateContractUndertakerFeedbackCommand.FeedbackId)))
	}
	if err := contractUndertakerFeedback.Update(tool_funs.SimpleStructToMap(updateContractUndertakerFeedbackCommand)); err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	if contractUndertakerFeedback, err := contractUndertakerFeedbackRepository.Save(contractUndertakerFeedback); 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 contractUndertakerFeedback, nil
	}
}

func NewContractUndertakerFeedbackService(options map[string]interface{}) *ContractUndertakerFeedbackService {
	newContractUndertakerFeedbackService := &ContractUndertakerFeedbackService{}
	return newContractUndertakerFeedbackService
}