company_v2.go 5.3 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"
	userx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/user"
	"time"
)

func PartnersV2(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 = getPartnersV2(header.UserId, request, transactionContext); err != nil {
		log.Error(err)
		return
	}
	if request.PageIndex == 0 {
		rsp.Timestamp = time.Now().Unix() * 1000
	}
	if rsp.Partners == nil {
		rsp.Partners = []struct{}{}
	}
	err = transactionContext.CommitTransaction()
	return
}

func getPartnersV2(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
	}

	var company *domain.Company
	var CompanyRepository, _ = factory.CreateCompanyRepository(transactionContext)
	if company, err = CompanyRepository.FindOne(map[string]interface{}{"id": user.CompanyId}); 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 := newQueryOptionV2(request)
	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
			mapPartners["joinWays"] = newJoinWays(mapCategoryInfo, p)
			mapPartners["dividend"] = utils.Decimal(p.Bonus)
			mapPartners["ordersCount"] = p.Total
			mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
			mapPartners["phone"] = p.Account
			mapPartners["company"] = newCompany(company, p)
			array = append(array, mapPartners)
		}
		return array, nil
	}
	return nil, nil
}

// 查询参数
func newQueryOptionV2(request *protocolx.PartnersRequest) map[string]interface{} {
	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
	}
	if request.Keyword != "" {
		queryOption["keyword"] = request.Keyword
	}
	queryOption["limit"] = request.PageSize
	queryOption["offset"] = request.PageSize * request.PageIndex

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

func newCompany(company *domain.Company, p *domain.PartnerStatics) userx.Company {
	partnerCompany := userx.Company{
		Id:               company.Id,
		Name:             company.Name,
		Phone:            company.Phone,
		SerialNo:         p.Id,
		CareerSerialNo:   "",
		BusinessSerialNo: "",
		DevelopSerialNo:  "",
		AppSerialNo:      "",
		CooperateTime:    p.CooperateTime.Unix() * 1000,
		MiniProgram:      nil,
		District:         map[string]interface{}{"id": p.RegionInfo.RegionId, "name": p.RegionInfo.RegionName},
	}

	//获取合伙人编号
	for _, partnerCategoryInfo := range p.PartnerCategoryInfos {
		if partnerCategoryInfo.Id == 1 {
			partnerCompany.CareerSerialNo = partnerCategoryInfo.Code
		} else if partnerCategoryInfo.Id == 2 {
			partnerCompany.BusinessSerialNo = partnerCategoryInfo.Code
		} else if partnerCategoryInfo.Id == 3 {
			partnerCompany.DevelopSerialNo = partnerCategoryInfo.Code
		} else if partnerCategoryInfo.Id == 4 {
			partnerCompany.AppSerialNo = partnerCategoryInfo.Code
		}
	}

	if len(p.Salesman) > 0 {
		partnerCompany.Salesman = map[string]interface{}{"uname": p.Salesman[0].Name, "phone": p.Salesman[0].Telephone}
	} else {
		partnerCompany.Salesman = map[string]interface{}{}
	}
	return partnerCompany
}