partner.go 9.9 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	//"github.com/linmadan/egglib-go/utils/tool_funs"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
	//"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/partner/command"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/partner/query"
	//"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/domain"
)

// 合伙人服务
type PartnerService struct {
}

//// 创建合伙人增删改查
//func (partnerService *PartnerService) CreatePartner(createPartnerCommand *command.CreatePartnerCommand) (interface{}, error) {
//	if err := createPartnerCommand.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()
//	}()
//	newPartner := &domain.Partner{}
//	var partnerRepository partner.PartnerRepository
//	if value, err := factory.CreatePartnerRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	} else {
//		partnerRepository = value
//	}
//	if partner, err := partnerRepository.Save(newPartner); 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 partner, nil
//	}
//}
//
//// 返回合伙人增删改查
//func (partnerService *PartnerService) GetPartner(getPartnerQuery *query.GetPartnerQuery) (interface{}, error) {
//	if err := getPartnerQuery.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 partnerRepository partner.PartnerRepository
//	if value, err := factory.CreatePartnerRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	} else {
//		partnerRepository = value
//	}
//	partner, err := partnerRepository.FindOne(map[string]interface{}{"partnerId": getPartnerQuery.PartnerId})
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	if partner == nil {
//		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(getPartnerQuery.PartnerId)))
//	} else {
//		if err := transactionContext.CommitTransaction(); err != nil {
//			return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
//		}
//		return partner, nil
//	}
//}
//
//// 返回合伙人增删改查列表
//func (partnerService *PartnerService) ListPartner(listPartnerQuery *query.ListPartnerQuery) (interface{}, error) {
//	if err := listPartnerQuery.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 partnerRepository partner.PartnerRepository
//	if value, err := factory.CreatePartnerRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	} else {
//		partnerRepository = value
//	}
//	if count, partners, err := partnerRepository.Find(tool_funs.SimpleStructToMap(listPartnerQuery)); 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{}{
//			"count":    count,
//			"partners": partners,
//		}, nil
//	}
//}

// 合伙人分类列表
func (partnerService *PartnerService) PartnerCategories(partnerCategoriesQuery *query.PartnerCategoriesQuery) (interface{}, error) {
	if err := partnerCategoriesQuery.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()
	}()
	PartnerCategoryRepository, err := factory.CreatePartnerCategoryRepository(map[string]interface{}{"transactionContext": transactionContext})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	total, categories, err := PartnerCategoryRepository.Find(map[string]interface{}{})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{"total": total, "categories": categories}, nil
}

//// 移除合伙人增删改查
//func (partnerService *PartnerService) RemovePartner(removePartnerCommand *command.RemovePartnerCommand) (interface{}, error) {
//	if err := removePartnerCommand.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 partnerRepository partner.PartnerRepository
//	if value, err := factory.CreatePartnerRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	} else {
//		partnerRepository = value
//	}
//	partner, err := partnerRepository.FindOne(map[string]interface{}{"partnerId": removePartnerCommand.PartnerId})
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	if partner == nil {
//		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(removePartnerCommand.PartnerId)))
//	}
//	if partner, err := partnerRepository.Remove(partner); 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 partner, nil
//	}
//}
//
//// 更新合伙人增删改查
//func (partnerService *PartnerService) UpdatePartner(updatePartnerCommand *command.UpdatePartnerCommand) (interface{}, error) {
//	if err := updatePartnerCommand.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 partnerRepository partner.PartnerRepository
//	if value, err := factory.CreatePartnerRepository(map[string]interface{}{
//		"transactionContext": transactionContext,
//	}); err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	} else {
//		partnerRepository = value
//	}
//	partner, err := partnerRepository.FindOne(map[string]interface{}{"partnerId": updatePartnerCommand.PartnerId})
//	if err != nil {
//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
//	}
//	if partner == nil {
//		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(updatePartnerCommand.PartnerId)))
//	}
//	if err := partner.Update(tool_funs.SimpleStructToMap(updatePartnerCommand)); err != nil {
//		return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
//	}
//	if partner, err := partnerRepository.Save(partner); 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 partner, nil
//	}
//}

func NewPartnerService(options map[string]interface{}) *PartnerService {
	newPartnerService := &PartnerService{}
	return newPartnerService
}