company.go 9.9 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/infrastructure/utils"
	"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()
	}()
	if rsp.Partners, err = getPartners(header.UserId, request, transactionContext); err != nil {
		log.Error(err)
		return
	}
	if rsp.Partners == nil {
		rsp.Partners = []struct{}{}
	}
	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()
	}()
	rsp.Statistics, err = getStatistics(header.UserId, transactionContext)
	if err != nil {
		log.Error(err)
		return
	}
	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
}

// 获取公司统计
func getStatistics(userId int64, transactionContext *transaction.TransactionContext) (interface{}, error) {
	var (
		UsersRepository, _ = factory.CreateUsersRepository(transactionContext)
		PartnerInfoDao, _  = factory.CreatePartnerInfoDao(transactionContext)
		OrderBaseDao, _    = factory.CreateOrderBaseDao(transactionContext)
		//BusinessBonusDao, _ = factory.CreateBusinessBonusDao(transactionContext)
		//user *domain.Users
		partnerIds []int64
	)
	if user, e := UsersRepository.FindOne(map[string]interface{}{"id": userId}); e != nil || user == nil {
		return struct {
		}{}, nil
	} else {
		partnerIds = user.AccessPartnerIds()
	}
	Statistics := make(map[string]interface{})
	//Statistics["developDividend"] = 0
	//Statistics["developOrdersMoney"] = 0
	//Statistics["appDividend"] = 0
	//Statistics["appOrdersMoney"] = 0
	if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Career}); e == nil {
		Statistics["careerCount"] = count
	}
	if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Business}); e == nil {
		Statistics["businessCount"] = count
	}
	if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.Develop}); e == nil {
		Statistics["developCount"] = count
	}
	if count, e := PartnerInfoDao.PartnerStatic(map[string]interface{}{"inPartnerIds": partnerIds, "inPartnerCategory": domain.App}); e == nil {
		Statistics["appCount"] = count
	}
	if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Career}); e == nil {
		Statistics["careerOrdersMoney"] = bonus.TotalOrderAmount
		Statistics["careerDividend"] = bonus.Bonus
	}
	if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Business}); e == nil {
		Statistics["businessOrdersMoney"] = bonus.TotalOrderAmount
		Statistics["businessDividend"] = bonus.Bonus
	}
	if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.Develop}); e == nil {
		Statistics["developOrdersMoney"] = bonus.TotalOrderAmount
		Statistics["developDividend"] = bonus.Bonus
	}
	if bonus, e := OrderBaseDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, OrderTypes: domain.UserOrderTypes(domain.Career), PartnerCategoryId: domain.App}); e == nil {
		Statistics["appOrdersMoney"] = bonus.TotalOrderAmount
		Statistics["appDividend"] = bonus.Bonus
	}
	//if businessBonus, e := BusinessBonusDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: partnerIds, IsDisable: 1}); e == nil {
	//	Statistics["businessDividend"] = businessBonus.Bonus
	//	Statistics["businessOrdersMoney"] = businessBonus.TotalOrderAmount
	//}

	Statistics["totalCount"] = len(partnerIds)
	return Statistics, nil
}

func getPartners(userId int64, request *protocolx.PartnersRequest, transactionContext *transaction.TransactionContext) (interface{}, error) {
	var (
		UsersRepository, _               = factory.CreateUsersRepository(transactionContext)
		PartnerInfo, _                   = factory.CreatePartnerInfoDao(transactionContext)
		user                             *domain.Users
		err                              error
		PartnerCategoryInfoRepository, _ = factory.CreatePartnerCategoryInfoRepository(transactionContext)
	)
	if user, err = UsersRepository.FindOne(map[string]interface{}{"id": userId}); err != nil {
		return nil, err
	}
	if len(user.AccessPartnerIds()) == 0 {
		return nil, nil
	}
	mapCategoryInfo := make(map[int64]*domain.PartnerCategoryInfo)
	if _, categories, e := PartnerCategoryInfoRepository.Find(nil); e == nil {
		for i := range categories {
			mapCategoryInfo[categories[i].Id] = categories[i]
		}
	}

	queryOption := make(map[string]interface{})

	if len(request.Districts) > 0 {
		var districts []string
		for i := 0; i < len(request.Districts); i++ {
			districts = append(districts, request.Districts[i].Name)
		}
		queryOption["districts"] = districts
	}
	if len(request.JoinWays) > 0 {
		var joinWays []int64
		for i := 0; i < len(request.JoinWays); i++ {
			joinWays = append(joinWays, request.JoinWays[i].Type)
		}
		queryOption["joinWays"] = joinWays
	}
	if request.StartTime > 0 {
		queryOption["startTime"] = request.StartTime / 1000
	}
	if request.EndTime > 0 {
		queryOption["endTime"] = request.EndTime / 1000
	}
	queryOption["limit"] = request.PageSize
	queryOption["offset"] = request.PageSize * request.PageIndex

	queryOption["sortByBonus"] = domain.DESC
	if request.SortBy != 0 {
		queryOption["sortByBonus"] = domain.ASC
	}

	if partners, e := PartnerInfo.Partners(user.AccessPartnerIds(), queryOption); e == nil {
		var array []interface{}
		for i := range partners {
			mapPartners := make(map[string]interface{})
			p := partners[i]
			mapPartners["uid"] = p.Id
			mapPartners["uname"] = p.PartnerName

			var joinWays []protocolx.JoinWays
			for j := range p.PartnerCategoryInfos {
				c := p.PartnerCategoryInfos[j]
				if v, ok := mapCategoryInfo[c.Id]; ok {
					joinWays = append(joinWays, protocolx.JoinWays{Type: v.Id, Name: v.Name})
				}
			}
			mapPartners["joinWays"] = joinWays
			if p.RegionInfo != nil {
				mapPartners["district"] = protocolx.Districts{Id: p.RegionInfo.RegionId, Name: p.RegionInfo.RegionName}
			}
			mapPartners["cooperationTime"] = p.CooperateTime.Unix() * 1000
			mapPartners["dividend"] = utils.Decimal(p.Bonus)
			mapPartners["ordersCount"] = p.Total
			mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
			array = append(array, mapPartners)
		}
		return array, nil
	}
	return nil, nil
}