company.go 3.8 KB
package company

import (
	"github.com/tiptok/gocomm/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/company"
	"strings"
)

func Property(header *protocol.RequestHeader, request *protocolx.PropertyRequest) (rsp *protocolx.PropertyResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.PropertyResponse{}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	//用户查询区域
	if (request.Action & 0x01) > 0 {
		rsp.Districts, _ = getDistricts(header.UserId, transactionContext)
	}
	//查询合伙人类型
	if (request.Action & 0x02) > 0 {
		rsp.JoinWays, _ = getJoinWays(transactionContext)
	}
	err = transactionContext.CommitTransaction()
	return
}

func Partners(header *protocol.RequestHeader, request *protocolx.PartnersRequest) (rsp *protocolx.PartnersResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.PartnersResponse{}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

func Statistics(header *protocol.RequestHeader, request *protocolx.StatisticsRequest) (rsp *protocolx.StatisticsResponse, err error) {
	var (
		transactionContext, _ = factory.CreateTransactionContext(nil)
	)
	rsp = &protocolx.StatisticsResponse{}
	if err = transactionContext.StartTransaction(); err != nil {
		log.Error(err)
		return nil, err
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	err = transactionContext.CommitTransaction()
	return
}

// 获取区域列表
func getDistricts(userId int64, transactionContext *transaction.TransactionContext) (districts []protocolx.Districts, err error) {
	var (
		user                     *domain.Users
		partnerInfos             []*domain.PartnerInfo
		UsersRepository, _       = factory.CreateUsersRepository(transactionContext)
		PartnerInfoRepository, _ = factory.CreatePartnerInfoRepositoryIn(transactionContext)
	)
	if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
		log.Error(err)
		return
	}
	if user == nil || len(user.AccessPartners) == 0 {
		return
	}
	if _, partnerInfos, err = PartnerInfoRepository.Find(map[string]interface{}{
		"inPartnerIds":     user.AccessPartnerIds(),
		"sortByCreateTime": domain.ASC},
	); err != nil {
		return
	}
	for i := range partnerInfos {
		partner := partnerInfos[i]
		include := false
		if partner.RegionInfo == nil {
			continue
		}
		for j := range districts {
			if strings.TrimSpace(districts[j].Name) == strings.TrimSpace(partner.RegionInfo.RegionName) {
				include = true
				break
			}
		}
		if !include {
			districts = append(districts, protocolx.Districts{
				Id:   partner.RegionInfo.RegionId,
				Name: partner.RegionInfo.RegionName,
			})
		}
	}
	return
}

// 获取合伙人类型列表
func getJoinWays(transactionContext *transaction.TransactionContext) (joinWays []protocolx.JoinWays, err error) {
	var (
		CategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext)
		categories                []*domain.PartnerCategoryInfo
	)
	if _, categories, err = CategoryInfoRepository.Find(nil); err != nil && len(categories) == 0 {
		return
	}
	for i := range categories {
		c := categories[i]
		joinWays = append(joinWays, protocolx.JoinWays{Type: c.Id, Name: c.Name})
	}
	return
}