partner_info.go 13.2 KB
package service

import (
	"fmt"

	"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
}

/**
 * @Author SteveChan
 * @Description //TODO 创建合伙人,判断编号是否重复
 * @Date 15:42 2020/12/29
 * @Param
 * @return
 **/
func (PartnerInfoService *PartnerInfoService) CreatePartnerInfo(cmd *command.CreatePartnerInfoCommand) (data *domain.PartnerInfo, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = cmd.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 partnerInfoDao, err = factory.CreatePartnerInfoDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	ok, err := partnerInfoDao.PartnerAccountExist(cmd.Account, cmd.CompanyId)
	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
		categoryRepository    domain.PartnerCategoryRepository
		categories            []domain.PartnerCategory
	)
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var ids []int64
	for _, partnerCategory := range cmd.PartnerCategory {
		ids = append(ids, partnerCategory.Id)
	}
	_, categories, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
		Ids: ids,
	})
	if err != nil {
		e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}

	newPartnerInfo := domain.PartnerInfo{
		Partner: domain.Partner{
			Account:     cmd.Account,
			PartnerName: cmd.PartnerName,
		},
		PartnerCategory:      0,
		Password:             cmd.Password,
		Status:               cmd.Status,
		RegionInfo:           *cmd.RegionInfo,
		Salesman:             cmd.Salesman,
		CooperateTime:        cmd.CooperateTime,
		CompanyId:            cmd.CompanyId,
		PartnerCategoryInfos: categories,
	}
	if err = partnerInfoRepository.Save(&newPartnerInfo); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	//var businessBonusSrv service.BusinessBonusService
	// if businessBonusSrv, err = factory.CreateBusinessBonusService(map[string]interface{}{
	// 	"transactionContext": transactionContext,
	// }); err != nil {
	// 	return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	// }
	// err = businessBonusSrv.EnableOrDisable(newPartnerInfo.Partner.Id)
	// if err != nil {
	// 	e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", newPartnerInfo.Partner.Id, err)
	// 	return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	// }
	err = transactionContext.CommitTransaction()
	return &newPartnerInfo, nil
}

/**
 * @Author SteveChan
 * @Description //TODO 返回合伙人,增加合伙人编号字段
 * @Date 15:43 2020/12/29
 * @Param
 * @return
 **/
func (PartnerInfoService *PartnerInfoService) GetPartnerInfo(q query.GetPartnerInfoQuery) (data *domain.PartnerInfo, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = q.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
		categoryRepository    domain.PartnerCategoryRepository
		categories            []domain.PartnerCategory
		partnerData           *domain.PartnerInfo
	)
	if PartnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	partnerData, err = PartnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
		UserId: q.Id, CompanyId: q.CompanyId,
	})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if !partnerData.IsCompany(q.CompanyId) {
		return nil, lib.ThrowError(lib.BUSINESS_ERROR, "企业信息异常操作")
	}
	if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var categoryIds []int64
	for _, v := range partnerData.PartnerCategoryInfos {
		categoryIds = append(categoryIds, v.Id)
	}
	if len(categoryIds) > 0 {
		_, categories, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
			Ids: categoryIds,
		})
		if err != nil {
			return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
		}
	}
	partnerData.PartnerCategoryInfos = categories
	err = transactionContext.CommitTransaction()
	return partnerData, nil
}

//UpdatePartnerInfo 更新合伙人
func (PartnerInfoService *PartnerInfoService) UpdatePartnerInfo(cmd *command.UpdatePartnerInfoCommand) (err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = cmd.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
		categoryRepository    domain.PartnerCategoryRepository
		categorys             []domain.PartnerCategory
	)
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	if categoryRepository, err = factory.CreatePartnerCategoryRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	var ids []int64
	for _, partnerCategory := range cmd.PartnerCategory {
		ids = append(ids, partnerCategory.Id)
	}
	_, categorys, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{
		Ids: ids,
	})
	if err != nil {
		e := fmt.Sprintf("获取合伙人分类数据失败:%s", err)
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	partnerInfo, err := partnerInfoRepository.FindOne(domain.PartnerFindOneQuery{
		UserId:    cmd.Id,
		CompanyId: cmd.CompanyId,
	})
	if err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if !partnerInfo.IsCompany(cmd.CompanyId) {
		return lib.ThrowError(lib.BUSINESS_ERROR, "异常操作")
	}
	partnerInfo.Salesman = cmd.Salesman
	partnerInfo.Status = cmd.Status
	partnerInfo.RegionInfo = *cmd.RegionInfo
	partnerInfo.CooperateTime = cmd.CooperateTime
	partnerInfo.PartnerCategoryInfos = categorys
	if err = partnerInfoRepository.Save(partnerInfo); err != nil {
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	//到0.6.0 彻底删除
	// var businessBonusSrv service.BusinessBonusService
	// if businessBonusSrv, err = factory.CreateBusinessBonusService(map[string]interface{}{
	// 	"transactionContext": transactionContext,
	// }); err != nil {
	// 	return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	// }
	// err = businessBonusSrv.EnableOrDisable(partnerInfo.Partner.Id)
	// if err != nil {
	// 	e := fmt.Sprintf("更新业务分红(partner_id=%d)数据失败:%s", partnerInfo.Partner.Id, err)
	// 	return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	// }
	transactionContext.CommitTransaction()
	return
}

// ListPartnerInfo 合伙人列表
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() {
		transactionContext.RollbackTransaction()
	}()

	var (
		partnerInfoRepository domain.PartnerInfoRepository
		categoryRepository    domain.PartnerCategoryRepository
		categories            []domain.PartnerCategory
	)
	if partnerInfoRepository, err = factory.CreatePartnerInfoRepository(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return 0, nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	if categoryRepository, err = factory.CreatePartnerCategoryRepository(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,
		PartnerName: listPartnerInfoQuery.PartnerName,
		CompanyId:   listPartnerInfoQuery.CompanyId,
	}
	if listPartnerInfoQuery.PartnerType > 0 {
		queryOption.PartnerCategory = []int{listPartnerInfoQuery.PartnerType}
	}
	// 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())
	}
	_, categories, err = categoryRepository.Find(domain.PartnerCategoryFindQuery{})
	if err != nil {
		return count, partnerInfos, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}
	categoriesMap := make(map[int64]domain.PartnerCategory)
	for i := range categories {
		categoriesMap[categories[i].Id] = categories[i]
	}
	for i := range partnerInfos {
		var categoryInPartner []domain.PartnerCategory
		for _, vv := range partnerInfos[i].PartnerCategoryInfos {
			if categoryData, ok := categoriesMap[vv.Id]; ok {
				categoryInPartner = append(categoryInPartner, categoryData)
			}
		}
		partnerInfos[i].PartnerCategoryInfos = categoryInPartner
	}
	if err = transactionContext.CommitTransaction(); err != nil {
		return 0, nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	return count, partnerInfos, nil
}

/**
 * @Author SteveChan
 * @Description //TODO 移除合伙人
 * @Date 16:40 2020/12/29
 * @Param
 * @return
 **/
func (PartnerInfoService *PartnerInfoService) RemovePartnerInfo(cmd command.RemovePartnerInfoCommand) (err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = cmd.ValidateCommand(); err != nil {
		return application.ThrowError(application.ARG_ERROR, err.Error())
	}
	if err = transactionContext.StartTransaction(); err != nil {
		return err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	return nil
}

func (PartnerInfoService *PartnerInfoService) UpdateStatus(cmd command.StatusPartnerInfoCommand) (err error) {
	if len(cmd.Ids) == 0 {
		return nil
	}
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	if err = cmd.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 (
		partnerInfoDao *dao.PartnerInfoDao
	)
	if partnerInfoDao, err = factory.CreatePartnerInfoDao(map[string]interface{}{
		"transactionContext": transactionContext,
	}); err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	err = partnerInfoDao.UpdatePartnerStatus(cmd.Ids, cmd.CompanyId, cmd.Status)
	if err != nil {
		e := fmt.Sprintf("更新合伙人(id=%v)的数据失败;%s", cmd.Ids, err)
		return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, e)
	}
	transactionContext.CommitTransaction()
	return
}