partner_info.go 8.0 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/command"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/partnerInfo/query"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)

// 客户价值服务
type PartnerInfoService struct {
}

func NewPartnerInfoService(options map[string]interface{}) *PartnerInfoService {
	newPartnerInfoService := &PartnerInfoService{}
	return newPartnerInfoService
}

// 创建客户价值
func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command.CreatePartnerInfoCommand) (data interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = command.ValidateCommand(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	//检查账号是否存在
	var (
		partnerinfoDao *dao.PartnerInfoDao
	)
	if v, err := factory.CreatePartnerInfoDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	} else {
		partnerinfoDao = v
	}
	ok, err := partnerinfoDao.PartnerAccountExist(command.Account)
	if err != nil {
		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	if ok {
		return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在")
	}

	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	newPartnerInfo := &domain.PartnerInfo{
		Account:         command.Account,
		PartnerName:     command.PartnerName,
		Password:        command.Password,
		Status:          command.Status,
		PartnerCategory: command.PartnerCategory,
		RegionInfo:      command.RegionInfo,
		Salesman:        command.Salesman,
		CooperateTime:   command.CooperateTime,
	}
	if data, err = PartnerInfoRepository.Save(newPartnerInfo); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	return
}

// GetPartnerInfo 返回合伙人
func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(command *query.GetPartnerInfoQuery) (data *domain.PartnerInfo, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = command.ValidateQuery(); err != nil {
		return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	data, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{UserId: command.Id})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	err = transactionContext.CommitTransaction()
	return
}

// 更新客户价值
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(updatePartnerInfoCommand *command.UpdatePartnerInfoCommand) (err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = updatePartnerInfoCommand.ValidateCommand(); err != nil {
		return application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var partnerInfoRepository domain.PartnerInfoRepository
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
		UserId: updatePartnerInfoCommand.Id,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	partnerInfo.PartnerCategory = updatePartnerInfoCommand.PartnerCategory
	partnerInfo.Salesman = updatePartnerInfoCommand.Salesman
	partnerInfo.Status = updatePartnerInfoCommand.Status
	partnerInfo.RegionInfo = updatePartnerInfoCommand.RegionInfo
	if _, err = partnerInfoRepository.Save(partnerInfo); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	transactionContext.CommitTransaction()
	return
}

// 返回客户价值列表
func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(listPartnerInfoQuery *query.ListPartnerInfoQuery) (int, []domain.PartnerInfo, error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		partnerInfos          []domain.PartnerInfo
		count                 int
		err                   error
	)
	if err = listPartnerInfoQuery.ValidateQuery(); err != nil {
		return 0, nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		if err != nil {
			transactionContext.RollbackTransaction()
		}
	}()
	var partnerInfoRepository domain.PartnerInfoRepository
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	queryOption := domain.PartnerFindQuery{
		Offset:          listPartnerInfoQuery.Offset,
		Limit:           listPartnerInfoQuery.Limit,
		PartnerCategory: []int{listPartnerInfoQuery.Partnertype},
		PartnerName:     listPartnerInfoQuery.PartnerName,
	}
	// RegionInfo
	if len(listPartnerInfoQuery.RegionInfo) > 0 {
		queryOption.RegionInfo = &domain.RegionInfo{RegionName: listPartnerInfoQuery.RegionInfo}
	}
	if partnerInfos, err = partnerInfoRepository.Find(queryOption); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if count, err = partnerInfoRepository.CountAll(queryOption); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	return count, partnerInfos, nil
}

func (PartnerInfoService *PartnerInfoService) UpdateStatus(command command.StatusPartnerInfoCommand) (err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = command.ValidateCommand(); err != nil {
		return application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var partnerInfoRepository domain.PartnerInfoRepository
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
		UserId: command.Id,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	partnerInfo.Status = command.Status
	if _, err = partnerInfoRepository.Save(partnerInfo); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	transactionContext.CommitTransaction()
	return
}