cooperation_mode.go 8.2 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) {

	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,"`
		command.CreateCooperationModeCommand
	}{
		CooperationModeId:            result.CooperationModeId,
		CreateCooperationModeCommand: *createCooperationModeCommand,
	}
	return data, nil
}

//GetCooperationMode 返回共创模式服务
func (cooperationModeService *CooperationModeService) GetCooperationMode(getCooperationModeQuery *query.GetCooperationModeQuery) (interface{}, 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,
		AuthFlag:              result.Org.OrgId == int(getCooperationModeQuery.Operator.OrgId),
	}
	return data, nil
}

//ListCooperationMode 返回共创模式服务列表
func (cooperationModeService *CooperationModeService) ListCooperationMode(listCooperationModeQuery *query.ListCooperationModeQuery) (int64, interface{}, 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,
		CompanyId:           listCooperationModeQuery.Operator.CompanyId,
		UserId:              0,
		OrgIds:              listCooperationModeQuery.Operator.OrgIds,
		OrgId:               0,
		UserBaseId:          0,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var (
		listData = make([]dto.CooperationModeItem, 0)
		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,
			AuthFlag:              v.Org.OrgId == int(listCooperationModeQuery.Operator.OrgId),
		}
		listData = append(listData, item)
	}
	return int64(result.Total), listData, nil
}

//RemoveCooperationMode 移除共创模式服务
func (cooperationModeService *CooperationModeService) RemoveCooperationMode(removeCooperationModeCommand *command.RemoveCooperationModeCommand) (interface{}, error) {

	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(removeCooperationModeCommand.Operator)
	_, err := creationCooperationGateway.CooperationModeBatchRemove(allied_creation_cooperation.ReqCooperationModeBatchRemove{
		CooperationModeIds: removeCooperationModeCommand.CooperationModeId,
	})
	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) {

	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.OperateCooperationModeCommand) (interface{}, error) {

	creationCooperationGateway := allied_creation_cooperation.NewHttplibAlliedCreationCooperation(updateCooperationModeCommand.Operator)
	_, err := creationCooperationGateway.CooperationModeOperate(allied_creation_cooperation.ReqOperateCooperationMode{
		CooperationModeId: updateCooperationModeCommand.CooperationModeId,
		Status:            updateCooperationModeCommand.Status,
	})
	if err != nil {
		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
	}
	return updateCooperationModeCommand, nil
}

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

//CooperationModeSelector 返回共创模式可选列表
func (cooperationModeService *CooperationModeService) CooperationModeSelector(listCooperationModeQuery *query.ListCooperationModeQuery) (int64, interface{}, 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,
		CompanyId:           listCooperationModeQuery.Operator.CompanyId,
		UserId:              0,
		OrgIds:              nil,
		OrgId:               listCooperationModeQuery.Operator.OrgId,
		UserBaseId:          0,
	})
	if err != nil {
		return 0, nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	var (
		listData = make([]dto.CooperationModeItem, 0)
		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,
			AuthFlag:              v.Org.OrgId == int(listCooperationModeQuery.Operator.OrgId),
		}
		listData = append(listData, item)
	}
	return int64(result.Total), listData, nil
}