partner_info.go 6.9 KB
package service

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

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

// 创建客户价值
func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(command *command.CreatePartnerInfoCommand) (data *domain.PartnerInfo, err error) {
	var (
		count                 int64
		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()
	}()

	newPartnerInfo := &command.PartnerInfo
	newPartnerInfo.CreateAt = time.Now()
	newPartnerInfo.UpdateAt = time.Now()

	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil {
		return nil, err
	}
	if count, _, err = PartnerInfoRepository.Find(map[string]interface{}{"account": command.Account}); err != nil {
		return nil, err
	}
	if count > 0 {
		return nil, fmt.Errorf("标签不可重复名称")
	}

	if data, err = PartnerInfoRepository.Save(newPartnerInfo); err != nil {
		return
	}
	err = transactionContext.CommitTransaction()
	return
}

// 返回客户价值
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, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()
	var PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil {
		return nil, err
	}
	data, err = PartnerInfoRepository.FindOne(utils.ObjectJsonToMap(command))
	if err != nil {
		return nil, err
	}
	if data == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(command.Id)))
	}
	err = transactionContext.CommitTransaction()
	return
}

// 更新客户价值
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(command *command.UpdatePartnerInfoCommand) (data *domain.PartnerInfo, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfo           *domain.PartnerInfo
	)
	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 PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil {
		return nil, err
	}
	PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": command.Id})
	if err != nil {
		return nil, err
	}
	if PartnerInfo == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(command.Id)))
	}
	//if count, PartnerInfos, err := PartnerInfoRepository.Find(map[string]interface{}{
	//	"PartnerInfoName": updatePartnerInfoCommand.PartnerInfoName,
	//	"companyId":         PartnerInfo.CompanyId,
	//}); err != nil {
	//	return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	//} else {
	//	if count > 0 && PartnerInfos[0].PartnerInfoId != PartnerInfo.PartnerInfoId {
	//		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, "标签不可重复名称")
	//	}
	//}
	if err := PartnerInfo.Update(tool_funs.SimpleStructToMap(command)); err != nil {
		return nil, err
	}

	if data, err = PartnerInfoRepository.Save(PartnerInfo); err != nil {
		return nil, err
	}
	err = transactionContext.CommitTransaction()
	return
}

// 移除客户价值
func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(command *command.RemovePartnerInfoCommand) (data *domain.PartnerInfo, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfo           *domain.PartnerInfo
	)
	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 PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil {
		return nil, err
	}
	PartnerInfo, err = PartnerInfoRepository.FindOne(map[string]interface{}{"Id": command.Id})
	if err != nil {
		return nil, err
	}
	if PartnerInfo == nil {
		return nil, application.ThrowError(application.RES_NO_FIND_ERROR, fmt.Sprintf("%s", string(command.Id)))
	}
	if data, err = PartnerInfoRepository.Remove(PartnerInfo); err != nil {
		return nil, err
	}
	err = transactionContext.CommitTransaction()
	return
}

// 返回客户价值列表
func (PartnerInfoService *PartnerInfoService) ListPartnerInfo(command *query.ListPartnerInfoQuery) (data interface{}, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
		PartnerInfos          []*domain.PartnerInfo
		count                 int64
	)
	if err = command.ValidateQuery(); 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 PartnerInfoRepository domain.PartnerInfoRepository
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{"transactionContext": transactionContext}); err != nil {
		return nil, err
	}
	if count, PartnerInfos, err = PartnerInfoRepository.Find(tool_funs.SimpleStructToMap(command)); err != nil {
		return nil, err
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return nil, err
	}
	return map[string]interface{}{
		"count":        count,
		"partnerInfos": PartnerInfos,
	}, nil
}

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