pg_login_services.go 9.0 KB
package domain_service

import (
	"github.com/tiptok/gocomm/xa/eda"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/dao"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/repository"
	http_gateway "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/log"
	"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
	protocolx "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol/auth"
	"strings"
)

type PgLoginService struct {
	Phone string
	eda.EventCenterPublisher
	PartnerInfo        []*domain.PartnerInfo
	Users              []*domain.Users
	transactionContext *transaction.TransactionContext
}

func (svr *PgLoginService) Init(phone string) (err error) {
	var (
		PartnerInfoService, _ = repository.NewPartnerInfoRepository(svr.transactionContext)
		UsersRepository, _    = repository.NewUsersRepository(svr.transactionContext)
	)
	svr.Phone = phone
	_, svr.PartnerInfo, err = PartnerInfoService.Find(map[string]interface{}{"account": phone, "status": 1, "sortByCreateTime": "ASC"})
	_, svr.Users, err = UsersRepository.Find(map[string]interface{}{"phone": phone, "status": 1, "sortByCreateTime": "ASC"})
	return nil
}

//合伙人登录
func (svr *PgLoginService) PartnerLogin(phone string, password string) (err error) {
	if len(svr.PartnerInfo) == 0 {
		//子账号
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	if !strings.EqualFold(svr.PartnerInfo[0].Password, password) {
		err = protocol.NewCustomMessage(1, "密码输入有误!")
		return
	}
	return
}

//管理层登录
func (svr *PgLoginService) ManagerLogin(phone string, password string) (err error) {
	if len(svr.Users) == 0 {
		//子账号
		err = protocol.NewErrWithMessage(502, err) //账号不存在
		return
	}
	var openid int64
	ucenerSvr := http_gateway.NewHttplibUCenterApiServiceGateway()
	openid, err = ucenerSvr.ServerLogin(phone, password, 1)
	if err == nil && openid > 0 {
		_, err = svr.transactionContext.PgTx.Exec("update users set open_id=? where phone=?", openid, phone)
	}
	return
}

//合伙人统计信息
func (svr *PgLoginService) PartnerStaticInfo() (interface{}, error) {
	if len(svr.PartnerInfo) == 0 {
		return nil, nil
	}
	var (
		OrderDao, _                      = dao.NewOrderBaseDao(svr.transactionContext)
		PartnerCategoryInfoRepository, _ = repository.NewPartnerCategoryInfoRepository(svr.transactionContext)
		companyList                      []*domain.Company
		allPartnerCategory               []*domain.PartnerCategoryInfo
	)
	doGetCompanyIds := func() []int64 {
		var companies []int64
		for i := range svr.PartnerInfo {
			companies = append(companies, svr.PartnerInfo[i].CompanyId)
		}
		return companies
	}
	doGetPartnerIds := func() []int64 {
		var array []int64
		for i := range svr.PartnerInfo {
			array = append(array, svr.PartnerInfo[i].Id)
		}
		return array
	}
	companyList = svr.GetCompanyList(doGetCompanyIds)
	if len(companyList) == 0 {
		return nil, nil
	}
	totalBonus, e := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: doGetPartnerIds(), OrderType: domain.OrderReal})
	if e != nil {
		return nil, e
	}
	_, allPartnerCategory, e = PartnerCategoryInfoRepository.Find(map[string]interface{}{"sortById": domain.ASC})
	if e != nil {
		log.Error(e)
		return nil, e
	}
	var companys = make([]*Company, 0)
	for i := range companyList {
		c := companyList[i]
		var partner *domain.PartnerInfo
		for j := range svr.PartnerInfo {
			if svr.PartnerInfo[j].CompanyId == c.Id {
				partner = svr.PartnerInfo[j]
				break
			}
		}
		if partner == nil {
			continue
		}
		bonus, _ := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{PartnerId: partner.Id, OrderType: domain.OrderReal})
		item := &Company{
			CompanyBase:   newCompanyBase(c),
			IncomePercent: computeBonusPercent(totalBonus.Bonus, bonus.Bonus*100),
			DividendMoney: utils.Decimal(bonus.Bonus),
			JoinWays:      svr.GetJoinWays(allPartnerCategory, partner, bonus.Bonus),
		}
		//当所有公司的总收入都为0时,
		//初始总收入=公司数*1
		//否则计算的比例始终都为0
		if totalBonus.Bonus == 0 {
			item.IncomePercent = computeBonusPercent(totalBonus.Bonus+float64(len(companyList)), bonus.Bonus+1) * 100
		}
		companys = append(companys, item)
	}

	response := make(map[string]interface{})
	response["id"] = protocolx.AdminTypePartner
	response["name"] = protocolx.AdminTypePartnerName
	response["companys"] = companys
	return response, nil
}
func (svr *PgLoginService) ManagerStaticInfo() (interface{}, error) {
	if len(svr.Users) == 0 {
		return nil, nil
	}
	var (
		companyList []*domain.Company
		//adminApiGateway = http_gateway.NewHttplibBusinessAdminApiServiceGateway()
	)
	doGetCompanyIds := func() []int64 {
		var companies []int64
		for i := range svr.Users {
			companies = append(companies, svr.Users[i].CompanyId)
		}
		return companies
	}
	companyList = svr.GetCompanyList(doGetCompanyIds)
	var companys = make([]protocol.CompanyBase, 0)
	for i := range companyList {
		c := companyList[i]

		//通过企业平台 校验模块权限
		//var user *domain.Users
		//for j := range svr.Users {
		//	if svr.Users[j].CompanyId == c.Id {
		//		user = svr.Users[j]
		//		break
		//	}
		//}
		//if user != nil {
		//	if code, e := adminApiGateway.UserAuth(user.Id, constant.BUSINESS_ADMIN_PLATFORM_ID); e != nil || code != 0 {
		//		log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e.Error())
		//		continue
		//	} else {
		//		log.Debug("【检查权限】", svr.Phone, "【公司】", c.Id, user.Id, code, e)
		//	}
		//}
		item := newCompanyBase(c)
		companys = append(companys, item)
	}

	response := make(map[string]interface{})
	response["id"] = protocolx.AdminTypeManager
	response["name"] = protocolx.AdminTypeManagerName
	response["companys"] = companys
	return response, nil
}
func (svr *PgLoginService) GetCompanyList(funcGetCompanyIds func() []int64) []*domain.Company {
	var (
		CompanyRepository, _ = repository.NewCompanyRepository(svr.transactionContext)
		companyList          []*domain.Company
	)
	var companies []int64 = funcGetCompanyIds()
	if len(companies) == 0 {
		return companyList
	}
	if _, v, e := CompanyRepository.Find(map[string]interface{}{"inCompanyIds": companies, "status": 1, "sortByCreateTime": domain.ASC}); e != nil {
		log.Error(e)
		return companyList
	} else {
		companyList = v
	}
	return companyList
}
func (svr *PgLoginService) GetJoinWays(partnerCategory []*domain.PartnerCategoryInfo, partnerInfo *domain.PartnerInfo, bonus float64) []joinWay {
	searchCategory := func(partnerCategory []*domain.PartnerCategoryInfo, id int64) *domain.PartnerCategoryInfo {
		for i := range partnerCategory {
			if partnerCategory[i].Id == id {
				return partnerCategory[i]
			}
		}
		return nil
	}
	//var (
	//	totalBonus                 float64
	//	businessBonus              float64
	//	BusinessBonusRepository, _ = repository.NewBusinessBonusRepository(svr.transactionContext)
	//)
	//for i := range partnerInfo.PartnerCategoryInfos {
	//	c := partnerInfo.PartnerCategoryInfos[i]
	//	switch c.Id {
	//	case 1:
	//		totalBonus += bonus
	//	case 2:
	//		if one, e := BusinessBonusRepository.FindOne(map[string]interface{}{"partner_id": partnerInfo.Id}); e == nil {
	//			businessBonus = one.Bonus
	//			totalBonus += businessBonus
	//		}
	//	}
	//}
	var joinWays []joinWay
	for i := range partnerCategory {
		c := partnerCategory[i]
		searchC := searchCategory(partnerInfo.PartnerCategoryInfos, c.Id)
		if searchC != nil {
			item := joinWay{
				Type: int(c.Id),
				Name: c.Name,
			}
			//if c.Id == 1 {
			//	item.Percent = computeBonusPercent(totalBonus, bonus) * 100
			//} else if c.Id == 2 {
			//	item.Percent = computeBonusPercent(totalBonus, businessBonus) * 100
			//}
			joinWays = append(joinWays, item)
		}
	}

	for i := range joinWays {
		joinWays[i].Percent = computeBonusPercent(float64(len(joinWays)), 1) * 100
	}
	return joinWays
}
func newCompanyBase(company *domain.Company) protocol.CompanyBase {
	return protocol.CompanyBase{
		Id:        company.Id,
		Name:      company.Name,
		ShortName: company.Abbreviation,
		Image:     protocol.NewImage(company.Logo),
		Phone:     company.Phone,
	}
}
func computeBonusPercent(totalBonus float64, bonus float64) float64 {
	if totalBonus == 0 {
		return 0
	}
	return utils.Decimal(bonus / totalBonus)
}

type Company struct {
	protocol.CompanyBase
	// 收入占比
	IncomePercent float64 `json:"incomePercent"`
	// 分红金额
	DividendMoney float64   `json:"dividendMoney"`
	JoinWays      []joinWay `json:"joinWays"`
}
type incomeInfo struct {
	// 收入占比
	IncomePercent float64 `json:"incomePercent"`
	// 分红金额
	DividendMoney float64 `json:"dividendMoney"`
}
type joinWay struct {
	Type    int     `json:"type"`
	Name    string  `json:"name"`
	Percent float64 `json:"percent"`
}

func NewPgLoginService(ctx *transaction.TransactionContext) *PgLoginService {
	return &PgLoginService{
		transactionContext: ctx,
	}
}