cooperation_mode.go 6.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationMode/command"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationMode/dto"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/cooperationMode/query"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_cooperation"
)

// 共创模式服务
type CooperationModeService struct {
}

//CreateCooperationMode 创建共创模式服务
func (cooperationModeService *CooperationModeService) CreateCooperationMode(createCooperationModeCommand *command.CreateCooperationModeCommand) (interface{}, error) {
	if err := createCooperationModeCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(createCooperationModeCommand.Operator)
	result, err := creationCooperationGateway.CooperationModeAdd(allied_creation_cooperation.ReqCooperationModeAdd{
		CooperationModeName:   createCooperationModeCommand.CooperationModeName,
		CooperationModeNumber: createCooperationModeCommand.CooperationModeNumber,
		Remarks:               createCooperationModeCommand.Remarks,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := struct {
		CooperationModeId int `json:"cooperationModeId,string," valid:"Required"`
		command.CreateCooperationModeCommand
	}{
		CooperationModeId:            result.CooperationModeId,
		CreateCooperationModeCommand: *createCooperationModeCommand,
	}
	return data, nil
}

//GetCooperationMode 返回共创模式服务
func (cooperationModeService *CooperationModeService) GetCooperationMode(getCooperationModeQuery *query.GetCooperationModeQuery) (interface{}, error) {
	if err := getCooperationModeQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}

	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(getCooperationModeQuery.Operator)
	result, err := creationCooperationGateway.CooperationModeGet(allied_creation_cooperation.ReqCooperationModeGet{
		CooperationModeId: getCooperationModeQuery.CooperationModeId,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	data := dto.CooperationModeItem{
		CooperationModeId:     result.CooperationModeId,
		CooperationModeName:   result.CooperationModeName,
		Remarks:               result.Remarks,
		Status:                result.Status,
		CooperationModeNumber: result.CooperationModeNumber,
		Org:                   result.Org,
	}
	return data, nil
}

//ListCooperationMode 返回共创模式服务列表
func (cooperationModeService *CooperationModeService) ListCooperationMode(listCooperationModeQuery *query.ListCooperationModeQuery) (int64, interface{}, error) {
	if err := listCooperationModeQuery.ValidateQuery(); err != nil {
		return 0, nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(listCooperationModeQuery.Operator)
	result, err := creationCooperationGateway.CooperationModesSearch(allied_creation_cooperation.ReqCooperationModesSearch{
		PageNumber:          listCooperationModeQuery.PageNumber,
		PageSize:            listCooperationModeQuery.PageSize,
		CooperationModeName: listCooperationModeQuery.CooperationModeName,
		OrganizationName:    listCooperationModeQuery.OrganizationName,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var (
		listData []dto.CooperationModeItem
		item     dto.CooperationModeItem
	)
	for _, v := range result.List {
		item = dto.CooperationModeItem{
			CooperationModeId:     v.CooperationModeId,
			CooperationModeName:   v.CooperationModeName,
			Remarks:               v.Remarks,
			Status:                v.Status,
			CooperationModeNumber: v.CooperationModeNumber,
			Org:                   v.Org,
		}
		listData = append(listData, item)
	}
	return int64(result.Total), listData, nil
}

//RemoveCooperationMode 移除共创模式服务
func (cooperationModeService *CooperationModeService) RemoveCooperationMode(removeCooperationModeCommand *command.RemoveCooperationModeCommand) (interface{}, error) {
	if err := removeCooperationModeCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(removeCooperationModeCommand.Operator)
	_, err := creationCooperationGateway.CooperationModeRemove(allied_creation_cooperation.ReqCooperationModeRemove{
		CooperationModeId: removeCooperationModeCommand.CooperationModeId,
	})
	//TODO 改为批量删除
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return removeCooperationModeCommand, nil
}

//UpdateCooperationMode 更新共创模式
func (cooperationModeService *CooperationModeService) UpdateCooperationMode(updateCooperationModeCommand *command.UpdateCooperationModeCommand) (interface{}, error) {
	if err := updateCooperationModeCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(updateCooperationModeCommand.Operator)
	_, err := creationCooperationGateway.CooperationModeUpdate(allied_creation_cooperation.ReqCooperationModeUpdate{
		CooperationModeId:   updateCooperationModeCommand.CooperationModeId,
		CooperationModeName: updateCooperationModeCommand.CooperationModeName,
		Remarks:             updateCooperationModeCommand.Remarks,
		Status:              updateCooperationModeCommand.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return updateCooperationModeCommand, nil
}

// EnableCooperationMode 禁用启用共创模式
func (cooperationModeService *CooperationModeService) EnableCooperationMode(updateCooperationModeCommand *command.UpdateCooperationModeCommand) (interface{}, error) {
	if err := updateCooperationModeCommand.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	//TODO gateway 待补充
	return nil, nil
}

func NewCooperationModeService(options map[string]interface{}) *CooperationModeService {
	newCooperationModeService := &CooperationModeService{}
	return newCooperationModeService
}