company_v2.go 5.5 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"
)

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 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 CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
	if company, err = CompanyResponsitory.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 := 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

			mapPartners["dividend"] = utils.Decimal(p.Bonus)
			mapPartners["ordersCount"] = p.Total
			mapPartners["ordersMoney"] = utils.Decimal(p.Amount)
			mapPartners["phone"] = p.Account

			partnerCompany := userx.Company{
				Id:            company.Id,
				Name:          company.Name,
				Phone:         company.Phone,
				SerialNo:      p.Id,
				CooperateTime: p.CooperateTime.Unix() * 1000,
				MiniProgram:   nil,
				District:      map[string]interface{}{"id": p.RegionInfo.RegionId, "name": p.RegionInfo.RegionName},
			}
			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{}{}
			}
			mapPartners["company"] = partnerCompany

			array = append(array, mapPartners)
		}
		return array, nil
	}
	return nil, nil
}

func funcPartnerCompanyInfo(companyId int64, transactionContext *transaction.TransactionContext) (partnerCompany userx.Company, err error) {
	var (
		CompanyResponsitory, _ = factory.CreateCompanyRepository(transactionContext)
		company                *domain.Company
	)
	if company, err = CompanyResponsitory.FindOne(map[string]interface{}{"id": companyId}); err != nil {
		return
	}

	partnerCompany = userx.Company{
		Id:    company.Id,
		Name:  company.Name,
		Phone: company.Phone,
		//SerialNo:      partnerInfo.Id,
		//CooperateTime: partnerInfo.CooperateTime.Unix() * 1000,
		MiniProgram: nil,
		//District:      map[string]interface{}{"id": partnerInfo.RegionInfo.RegionId, "name": partnerInfo.RegionInfo.RegionName},
	}

	//if len(partnerInfo.Salesman) > 0 {
	//	u.CooperateCompany.Salesman = map[string]interface{}{"uname": partnerInfo.Salesman[0].Name, "phone": partnerInfo.Salesman[0].Telephone}
	//} else {
	//	u.CooperateCompany.Salesman = map[string]interface{}{}
	//}
	//rspMap["user"] = u
	//rsp = rspMap
	return
}