Browse Source

1

tags/v1.7.3
DengBiao 2 years ago
parent
commit
58577d86da
3 changed files with 140 additions and 62 deletions
  1. +19
    -0
      db/db_user_small_public_platoon_relation.go
  2. +1
    -0
      db/model/user_small_public_platoon_relation.go
  3. +120
    -62
      rule/small_public_platoon_relate_commission.go

+ 19
- 0
db/db_user_small_public_platoon_relation.go View File

@@ -101,6 +101,15 @@ func UserSmallPublicPlatoonRelationGetOneByPid(Db *xorm.Engine, recommendUid str
return &m, nil
}

func UserSmallPublicPlatoonRelationFindByPid(Db *xorm.Engine, pid int) ([]model.UserSmallPublicPlatoonRelation, error) {
var m []model.UserSmallPublicPlatoonRelation
if err := Db.Where("pid = ?", pid).OrderBy("position Desc").
Find(&m); err != nil {
return nil, zhios_order_relate_logx.Warn(err)
}
return m, nil
}

func UserSmallPublicPlatoonRelationFindByPidLike(Db *xorm.Engine, pid string) ([]model.UserSmallPublicPlatoonRelation, error) {
var m []model.UserSmallPublicPlatoonRelation
if err := Db.Where("father_uid like ?", "%"+pid+"%").OrderBy("id Desc").
@@ -110,6 +119,16 @@ func UserSmallPublicPlatoonRelationFindByPidLike(Db *xorm.Engine, pid string) ([
return m, nil
}

func UserSmallPublicPlatoonRelationFindCountByPosition(Db *xorm.Engine, startPosition, endPosition int64) ([]model.UserSmallPublicPlatoonRelation, int64, error) {
var m []model.UserSmallPublicPlatoonRelation
var count int64
if count, err := Db.Where("position >= ?", startPosition).And("position <= ?", endPosition).OrderBy("has_son_num Desc").
FindAndCount(&m); err != nil {
return nil, count, zhios_order_relate_logx.Warn(err)
}
return m, count, nil
}

// UserSmallPublicPlatoonRelationFindByParams 通过传入的参数查询数据(多条)
func UserSmallPublicPlatoonRelationFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserSmallPublicPlatoonRelation, error) {
var m []model.UserSmallPublicPlatoonRelation


+ 1
- 0
db/model/user_small_public_platoon_relation.go View File

@@ -18,4 +18,5 @@ type UserSmallPublicPlatoonRelation struct {
WaitForSettlementDate string `json:"wait_for_settlement_date" xorm:"not null default '' comment('待结算时间(0000-00)') CHAR(50)"`
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
HasSonNum int `json:"has_son_num" xorm:"not null default 0 comment('拥有直属下级数量') TINYINT(2)"`
}

+ 120
- 62
rule/small_public_platoon_relate_commission.go View File

@@ -146,81 +146,96 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl
}

//2、 查找当前 user_small_public_platoon_relation 中 recommendUid 所有的子记录
userSmallPublicPlatoonRelations, err := db.UserSmallPublicPlatoonRelationFindByPidLike(engine, strconv.Itoa(m.Position))
userSmallPublicPlatoonRelations, err := db.UserSmallPublicPlatoonRelationFindByPid(engine, m.Position)
if err != nil {
return model.UserSmallPublicPlatoonRelation{}, err
}

var position, position1, position2, pid int
var level, level1, level2 float64
var position, pid int
var level float64
if len(userSmallPublicPlatoonRelations) == 0 {
//证明是第一个
position = smallSearchPositionFirstSonPosition(m.Position, userPublicPlatoonSetting.SeveralTimes)
level = float64(m.Level) + 1
pid = m.Position
} else {
var mapUserSmallPublicPlatoonRelations = map[int]interface{}{}
for _, item := range userSmallPublicPlatoonRelations {
mapUserSmallPublicPlatoonRelations[item.Position] = &item
}
mapUserSmallPublicPlatoonRelations[m.Position] = m

position1 = userSmallPublicPlatoonRelations[0].Position + userPublicPlatoonSetting.SeveralTimes
position2 = userSmallPublicPlatoonRelations[0].Position + 1
level = float64(userSmallPublicPlatoonRelations[0].Level)
smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level1)
smallMakeSearchLevel(&position2, float64(userPublicPlatoonSetting.SeveralTimes), &level2)

//判断 position1 的上级是否还属于
fatherPosition1 := smallMakeSearchPid(position1, userPublicPlatoonSetting.SeveralTimes)
fatherPosition2 := smallMakeSearchPid(position2, userPublicPlatoonSetting.SeveralTimes)
if mapUserSmallPublicPlatoonRelations[fatherPosition1] != nil {
//属于
if level != level1 {
if mapUserSmallPublicPlatoonRelations[fatherPosition2] != nil {
//同级
tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1 + userPublicPlatoonSetting.SeveralTimes) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
if mapUserSmallPublicPlatoonRelations[tmpPosition] != nil {
position = position2
pid = fatherPosition2
level = level2
} else {
//回归
position = tmpPosition
pid = smallMakeSearchPid(position, userPublicPlatoonSetting.SeveralTimes)
}
} else {
//新的一级
tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
position = smallSearchPositionFirstSonPosition(tmpPosition, userPublicPlatoonSetting.SeveralTimes)
smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level)
pid = tmpPosition
}
} else {
position = position1
pid = fatherPosition1
level = level1
//TODO::判断直属下级是否排满
if len(userSmallPublicPlatoonRelations) == userPublicPlatoonSetting.SeveralTimes {
var fatherLevel float64
smallMakeSearchLevel(&m.Position, float64(userPublicPlatoonSetting.SeveralTimes), &fatherLevel)
position, pid, level, err = smallFindSuitablePosition(engine, &m.Position, float64(userPublicPlatoonSetting.SeveralTimes), int(fatherLevel))
if err != nil {
return model.UserSmallPublicPlatoonRelation{}, err
}
} else {
if mapUserSmallPublicPlatoonRelations[fatherPosition2] != nil {
//同级
tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1 + userPublicPlatoonSetting.SeveralTimes) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
if mapUserSmallPublicPlatoonRelations[tmpPosition] != nil {
position = position2
pid = fatherPosition2
} else {
//回归
position = tmpPosition
pid = smallMakeSearchPid(position, userPublicPlatoonSetting.SeveralTimes)
}
} else {
//新的一级
tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
position = smallSearchPositionFirstSonPosition(tmpPosition, userPublicPlatoonSetting.SeveralTimes)
smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level)
pid = tmpPosition
}
position = m.Position + 1
level = float64(m.Level) + 1
pid = m.Position
}

//var mapUserSmallPublicPlatoonRelations = map[int]interface{}{}
//for _, item := range userSmallPublicPlatoonRelations {
// mapUserSmallPublicPlatoonRelations[item.Position] = &item
//}
//mapUserSmallPublicPlatoonRelations[m.Position] = m
//
//position1 = userSmallPublicPlatoonRelations[0].Position + userPublicPlatoonSetting.SeveralTimes
//position2 = userSmallPublicPlatoonRelations[0].Position + 1
//level = float64(userSmallPublicPlatoonRelations[0].Level)
//smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level1)
//smallMakeSearchLevel(&position2, float64(userPublicPlatoonSetting.SeveralTimes), &level2)
//
////判断 position1 的上级是否还属于
//fatherPosition1 := smallMakeSearchPid(position1, userPublicPlatoonSetting.SeveralTimes)
//fatherPosition2 := smallMakeSearchPid(position2, userPublicPlatoonSetting.SeveralTimes)
//if mapUserSmallPublicPlatoonRelations[fatherPosition1] != nil {
// //属于
// if level != level1 {
// if mapUserSmallPublicPlatoonRelations[fatherPosition2] != nil {
// //同级
// tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1 + userPublicPlatoonSetting.SeveralTimes) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
// if mapUserSmallPublicPlatoonRelations[tmpPosition] != nil {
// position = position2
// pid = fatherPosition2
// level = level2
// } else {
// //回归
// position = tmpPosition
// pid = smallMakeSearchPid(position, userPublicPlatoonSetting.SeveralTimes)
// }
// } else {
// //新的一级
// tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
// position = smallSearchPositionFirstSonPosition(tmpPosition, userPublicPlatoonSetting.SeveralTimes)
// smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level)
// pid = tmpPosition
// }
// } else {
// position = position1
// pid = fatherPosition1
// level = level1
// }
//}
//else {
// if mapUserSmallPublicPlatoonRelations[fatherPosition2] != nil {
// //同级
// tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1 + userPublicPlatoonSetting.SeveralTimes) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
// if mapUserSmallPublicPlatoonRelations[tmpPosition] != nil {
// position = position2
// pid = fatherPosition2
// } else {
// //回归
// position = tmpPosition
// pid = smallMakeSearchPid(position, userPublicPlatoonSetting.SeveralTimes)
// }
// } else {
// //新的一级
// tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0].FatherUid, m.Position))))
// position = smallSearchPositionFirstSonPosition(tmpPosition, userPublicPlatoonSetting.SeveralTimes)
// smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level)
// pid = tmpPosition
// }
//}
}

//2、查找对应pid的 user_public_platoon_relation 记录
@@ -243,10 +258,22 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl

//唯一标识符(父级id-uid-等级-位置)
var uniqueIdentifier = zhios_order_relate_utils.IntToStr(pid) + "-" + zhios_order_relate_utils.IntToStr(uid) + "-" + zhios_order_relate_utils.IntToStr(int(level)) + "-" + zhios_order_relate_utils.IntToStr(position)
<<<<<<< HEAD
if userPublicPlatoonSetting.SettleDay == 0 {
userPublicPlatoonSetting.SettleDay = 30
}
// 3、插入 user_public_platoon_relation 记录
=======
// 3、改变直属父级的 has_son_num 数量
update, err := engine.Where("position = ?", pid).SetExpr("has_son_num", "has_son_num + 1").Update(new(model.UserSmallPublicPlatoonRelation))
if err != nil {
return userSmallPublicPlatoonRelation, err
}
if update == 0 {
return userSmallPublicPlatoonRelation, errors.New("更新has_son_num数据出错")
}
// 4、插入 user_public_platoon_relation 记录
>>>>>>> 64fd8a9 (update)
now := time.Now()
userSmallPublicPlatoonRelation.Uid = uid
userSmallPublicPlatoonRelation.FatherUid = fatherUid
@@ -259,6 +286,7 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl
userSmallPublicPlatoonRelation.JoinAt = now
userSmallPublicPlatoonRelation.CreateAt = now //
userSmallPublicPlatoonRelation.UpdateAt = now
userSmallPublicPlatoonRelation.HasSonNum = 0
_, err = db.UserSmallPublicPlatoonRelationInsert(engine, &userSmallPublicPlatoonRelation)
if err != nil {
return model.UserSmallPublicPlatoonRelation{}, err
@@ -347,6 +375,36 @@ func smallReverseDeductionPosition(calcPosition, levelFirstPosition, severalTime
return
}


func smallFindSuitablePosition(engine *xorm.Engine, position *int, severalTimes float64, fatherLevel int) (suitablePosition, pid int, level float64, err error) {
firstSonPosition := smallSearchPositionFirstSonPosition(*position, int(severalTimes))
var tempLevel float64
smallMakeSearchLevel(&firstSonPosition, severalTimes, &tempLevel)
firstSonLevel := int(tempLevel)
endSonPosition := firstSonPosition + int(math.Pow(severalTimes, float64(fatherLevel-firstSonLevel))) - 1
list, _, err := db.UserSmallPublicPlatoonRelationFindCountByPosition(engine, int64(firstSonPosition), int64(endSonPosition))
if err != nil {
return 0, 0, 0, err
}
if list[0].HasSonNum == int(severalTimes) {
//TODO::则证明该层直属下级无空位
position = &firstSonPosition
return smallFindSuitablePosition(engine, position, severalTimes, fatherLevel)
} else if list[0].HasSonNum == 0 {
//TODO::新的一层
suitablePosition = smallSearchPositionFirstSonPosition(*position, int(severalTimes))
pid = firstSonPosition
level = float64(firstSonLevel + 1)
} else {
userSmallPublicPlatoonRelations, err := db.UserSmallPublicPlatoonRelationFindByPid(engine, list[0].Position)
if err != nil {
return 0, 0, 0, err
}
return userSmallPublicPlatoonRelations[0].Position + 1, userSmallPublicPlatoonRelations[0].Pid, float64(userSmallPublicPlatoonRelations[0].Level), nil
}
return
}

// smallGetLevelForFirstPosition 返回当前等级的起始值
func smallGetLevelForFirstPosition(level, severalTimes int) (position int) {
position = position + 1


Loading…
Cancel
Save