|
|
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"
|
|
|
"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/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, _ = PartnerInfoService.Find(map[string]interface{}{"account": phone, "status": 1, "sortByCreateTime": "ASC"})
|
|
|
_, svr.Users, _ = 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
|
|
|
}
|
|
|
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
|
|
|
partnerCategory []*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)
|
|
|
|
|
|
totalBonus, e := OrderDao.OrderBonusStatics(domain.OrderBonusQuery{InPartnerIds: doGetPartnerIds()})
|
|
|
if e != nil {
|
|
|
return nil, e
|
|
|
}
|
|
|
_, partnerCategory, _ = PartnerCategoryInfoRepository.Find(map[string]interface{}{"sortById": domain.ASC})
|
|
|
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})
|
|
|
item := &Company{
|
|
|
CompanyBase: newCompanyBase(c),
|
|
|
IncomePercent: computeBonusPercent(totalBonus.Bonus, bonus.Bonus),
|
|
|
DividendMoney: bonus.Bonus,
|
|
|
JoinWays: svr.GetJoinWays(partnerCategory, partner, bonus.Bonus),
|
|
|
}
|
|
|
companys = append(companys, item)
|
|
|
}
|
|
|
|
|
|
response := make(map[string]interface{})
|
|
|
response["id"] = protocolx.AdminTypePartner
|
|
|
response["name"] = svr.PartnerInfo[0].PartnerName
|
|
|
response["companys"] = companys
|
|
|
return response, nil
|
|
|
}
|
|
|
func (svr *PgLoginService) ManagerStaticInfo() (interface{}, error) {
|
|
|
if len(svr.Users) == 0 {
|
|
|
return nil, nil
|
|
|
}
|
|
|
var (
|
|
|
companyList []*domain.Company
|
|
|
)
|
|
|
doGetCompanyIds := func() []int64 {
|
|
|
var companies []int64
|
|
|
for i := range svr.PartnerInfo {
|
|
|
companies = append(companies, svr.PartnerInfo[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
|
|
|
// }
|
|
|
//}
|
|
|
item := newCompanyBase(c)
|
|
|
companys = append(companys, item)
|
|
|
}
|
|
|
|
|
|
response := make(map[string]interface{})
|
|
|
response["id"] = protocolx.AdminTypePartner
|
|
|
response["name"] = svr.PartnerInfo[0].PartnerName
|
|
|
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{}{"companies": companies, "status": 1, "sortByCreateTime": domain.ASC}); e != nil {
|
|
|
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)
|
|
|
}
|
|
|
}
|
|
|
return joinWays
|
|
|
}
|
|
|
func newCompanyBase(company *domain.Company) protocol.CompanyBase {
|
|
|
return protocol.CompanyBase{
|
|
|
Id: company.Id,
|
|
|
Name: company.Name,
|
|
|
ShortName: "TODO",
|
|
|
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
|
|
|
}
|
|
|
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,
|
|
|
}
|
|
|
} |
...
|
...
|
|