From 8c0ac9f6e99a6a6220b84e454b3eeb5e1a34e97d Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Sat, 6 Aug 2022 19:06:16 +0800 Subject: [PATCH] add Reverse: for v1.6.1 --- db/db_user_small_public_platoon_relation.go | 21 ++-- .../small_public_platoon_relate_commission.go | 98 ++++++++++++------- svc/public_platoon_relate_commission.go | 7 +- svc/small_public_platoon_relate_commission.go | 20 ++-- 4 files changed, 91 insertions(+), 55 deletions(-) diff --git a/db/db_user_small_public_platoon_relation.go b/db/db_user_small_public_platoon_relation.go index 80a5ea2..5db0008 100644 --- a/db/db_user_small_public_platoon_relation.go +++ b/db/db_user_small_public_platoon_relation.go @@ -84,6 +84,14 @@ func UserSmallPublicPlatoonRelationGetOneByParams(Db *xorm.Engine, params map[st return &m, nil } +func UserSmallPublicPlatoonRelationFindByEmptyPosition(Db *xorm.Engine) (*model.UserSmallPublicPlatoonRelation, error) { + var m model.UserSmallPublicPlatoonRelation + if has, err := Db.Where("uid = -1").Or("uid = -2").Get(&m); err != nil || has == false { + return nil, zhios_order_relate_logx.Error(err) + } + return &m, nil +} + func UserSmallPublicPlatoonRelationGetOneByPid(Db *xorm.Engine, pid string, params map[string]interface{}) (*model.UserSmallPublicPlatoonRelation, error) { var m model.UserSmallPublicPlatoonRelation var query = fmt.Sprintf("%s =?", params["key"]) @@ -95,8 +103,8 @@ func UserSmallPublicPlatoonRelationGetOneByPid(Db *xorm.Engine, pid string, para func UserSmallPublicPlatoonRelationFindByPidLike(Db *xorm.Engine, pid string) ([]model.UserSmallPublicPlatoonRelation, error) { var m []model.UserSmallPublicPlatoonRelation - if err := Db.Where("pid like ?", "%"+pid+"%").OrderBy("position Desc"). - Find(m); err != nil { + if err := Db.Where("father_uid like ?", "%"+pid+"%").OrderBy("id Desc"). + Find(&m); err != nil { return nil, zhios_order_relate_logx.Warn(err) } return m, nil @@ -134,13 +142,10 @@ func UserSmallPublicPlatoonRelationFindByParams(Db *xorm.Engine, params map[stri } } -func UserSmallPublicPlatoonRelationFindByPosition(Db *xorm.Engine, params map[string]interface{}) ([]model.UserSmallPublicPlatoonRelation, error) { +func UserSmallPublicPlatoonRelationFindRecommends(Db *xorm.Engine) ([]model.UserSmallPublicPlatoonRelation, error) { var m []model.UserSmallPublicPlatoonRelation - if params["value"] == nil { - return nil, errors.New("参数有误") - } - var query = fmt.Sprintf("%s >?", params["key"]) - err := Db.Where(query, params["value"]).Find(&m) + var query = fmt.Sprintf("recommend_uid > 0") + err := Db.Where(query).Find(&m) if err != nil { return nil, zhios_order_relate_logx.Error(err) } diff --git a/rule/small_public_platoon_relate_commission.go b/rule/small_public_platoon_relate_commission.go index f3ad2d0..edf9de0 100644 --- a/rule/small_public_platoon_relate_commission.go +++ b/rule/small_public_platoon_relate_commission.go @@ -42,7 +42,7 @@ func AddSmallPublicPlatoonRelateCommission(engine *xorm.Engine, AddSmallPublicPl //判断是否有uid为-2 (代表等待新用户填充) 的记录 userSmallPublicPlatoonRelation, err := db.UserSmallPublicPlatoonRelationGetOneByPid(engine, param.RecommendUid, map[string]interface{}{ "key": "uid", - "value": -1, + "value": -2, }) if err != nil { return nil, err @@ -70,10 +70,7 @@ func AddSmallPublicPlatoonRelateCommission(engine *xorm.Engine, AddSmallPublicPl //3、无推荐人 //3.1 判断是否有uid为-1 (代表等待新用户填充) 的记录 - userSmallPublicPlatoonRelation, err := db.UserSmallPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ - "key": "uid", - "value": -1, - }) + userSmallPublicPlatoonRelation, err := db.UserSmallPublicPlatoonRelationFindByEmptyPosition(engine) if err != nil { return nil, err } @@ -93,7 +90,7 @@ func AddSmallPublicPlatoonRelateCommission(engine *xorm.Engine, AddSmallPublicPl } // 查找当前 user_public_platoon_relation 中 `position` 最大的记录 - var m model.UserPublicPlatoonRelation + var m model.UserSmallPublicPlatoonRelation has, err := engine.Where("recommend_uid = 0").OrderBy("id desc").Get(&m) if err != nil { return nil, err @@ -103,16 +100,13 @@ func AddSmallPublicPlatoonRelateCommission(engine *xorm.Engine, AddSmallPublicPl } // 查找当前大于 `position` 的所有记录 - mlist, err := db.UserSmallPublicPlatoonRelationFindByPosition(engine, map[string]interface{}{ - "key": "position", - "value": m.Position, - }) + mlist, err := db.UserSmallPublicPlatoonRelationFindRecommends(engine) if err != nil { return nil, err } - var mapBigUserSmallPublicPlatoonRelations map[int]interface{} + var mapBigUserSmallPublicPlatoonRelations = map[int]interface{}{} for _, item := range mlist { - mapBigUserSmallPublicPlatoonRelations[item.Position] = item + mapBigUserSmallPublicPlatoonRelations[item.Position] = true } res, err := naturalSmallPublicPlatoon(engine, zhios_order_relate_utils.StrToInt(param.Uid), &m.Position, *userPublicPlatoonSetting, mapBigUserSmallPublicPlatoonRelations) @@ -146,38 +140,65 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl } //2、 查找当前 user_small_public_platoon_relation 中 recommendUid 所有的子记录 - userSmallPublicPlatoonRelations, err := db.UserSmallPublicPlatoonRelationFindByPidLike(engine, strconv.Itoa(recommendUid)) + userSmallPublicPlatoonRelations, err := db.UserSmallPublicPlatoonRelationFindByPidLike(engine, strconv.Itoa(m.Position)) if err != nil { return model.UserSmallPublicPlatoonRelation{}, err } var position, position1, position2, pid int - var level float64 + var level, level1, level2 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{} + var mapUserSmallPublicPlatoonRelations = map[int]interface{}{} for _, item := range userSmallPublicPlatoonRelations { - mapUserSmallPublicPlatoonRelations[item.Position] = item + 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 { //属于 - position = position1 - pid = fatherPosition1 + 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])))) + 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 @@ -188,8 +209,8 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl } } else { //新的一级 - tmpPosition := (userSmallPublicPlatoonRelations[0].Position + 1) - int(math.Pow(float64(userPublicPlatoonSetting.SeveralTimes), float64(smallSearchPositionSonForLevel(userSmallPublicPlatoonRelations[0])))) - position = smallSearchPositionFirstSonPosition(m.Position, userPublicPlatoonSetting.SeveralTimes) + 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 } @@ -197,7 +218,7 @@ func smallPublicPlatoon(engine *xorm.Engine, uid, recommendUid int, userPublicPl } //2、查找对应pid的 user_public_platoon_relation 记录 - var m1 model.UserPublicPlatoonRelation + var m1 model.UserSmallPublicPlatoonRelation if has, err := engine.Where("position=?", pid).Get(&m1); err != nil || has == false { return userSmallPublicPlatoonRelation, err } @@ -244,14 +265,14 @@ func naturalSmallPublicPlatoon(engine *xorm.Engine, uid int, originalPosition *i position1 = *originalPosition + 1 position2 = *originalPosition + userPublicPlatoonSetting.SeveralTimes var level, level1, level2 float64 - makeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level1) - makeSearchLevel(&position2, float64(userPublicPlatoonSetting.SeveralTimes), &level2) - level = level1 + 1 //TODO::根据公式需要 + 1 + smallMakeSearchLevel(&position1, float64(userPublicPlatoonSetting.SeveralTimes), &level1) + smallMakeSearchLevel(&position2, float64(userPublicPlatoonSetting.SeveralTimes), &level2) + level = level1 if level > 2 { if level1 != level2 { - position = reverseDeductionPosition(*originalPosition, getLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes), userPublicPlatoonSetting.SeveralTimes) + position = smallReverseDeductionPosition(*originalPosition, smallGetLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes), userPublicPlatoonSetting.SeveralTimes) } else { - if position1 == getLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes) { + if position1 == smallGetLevelForFirstPosition(int(level), userPublicPlatoonSetting.SeveralTimes) { position = position1 } else { position = position2 @@ -261,13 +282,14 @@ func naturalSmallPublicPlatoon(engine *xorm.Engine, uid int, originalPosition *i position = position1 } if mapBigUserSmallPublicPlatoonRelations[position] != nil { + originalPosition = &position return naturalSmallPublicPlatoon(engine, uid, originalPosition, userPublicPlatoonSetting, mapBigUserSmallPublicPlatoonRelations) } pid := makeSearchPid(position, userPublicPlatoonSetting.SeveralTimes) //2、查找对应pid的 user_public_platoon_relation 记录 - var m1 model.UserPublicPlatoonRelation + var m1 model.UserSmallPublicPlatoonRelation if has, err := engine.Where("position=?", pid).Get(&m1); err != nil || has == false { return userPublicPlatoonRelation, err } @@ -326,11 +348,11 @@ func smallGetLevelForFirstPosition(level, severalTimes int) (position int) { //递归查找等级 func smallMakeSearchLevel(position *int, rows float64, times *float64) (level int) { difference := *position - int(math.Pow(rows, *times)) + *times++ if difference <= 0 { return int(*times) } else { position = &difference - *times++ return smallMakeSearchLevel(position, rows, times) } } @@ -359,10 +381,10 @@ func smallSearchPositionFirstSonPosition(position int, times int) (sonPosition i } //查找当前位位置相当于父级的等级 -func smallSearchPositionSonForLevel(m model.UserSmallPublicPlatoonRelation) (level int) { - fatherUids := strings.Split(m.FatherUid, "-") +func smallSearchPositionSonForLevel(fatherUid string, fatherPosition int) (level int) { + fatherUids := strings.Split(fatherUid, "-") for key, item := range fatherUids { - if item == strconv.Itoa(m.Pid) { + if item == strconv.Itoa(fatherPosition) { level = key + 1 break } @@ -373,7 +395,7 @@ func smallSearchPositionSonForLevel(m model.UserSmallPublicPlatoonRelation) (lev // smallFindWaitForDealUsers 查询待处理的用户 func smallFindWaitForDealUsers(engine *xorm.Engine, page, pageSize int) (err error, resp []int) { now := time.Now().Format("2006-01-02") - lists, err := db.UserPublicPlatoonRelationFindByParamsByPage(engine, map[string]interface{}{ + lists, err := db.UserSmallPublicPlatoonRelationFindByParamsByPage(engine, map[string]interface{}{ "key": "wait_for_settlement_date", "value": now, }, page, pageSize) @@ -389,7 +411,7 @@ func smallFindWaitForDealUsers(engine *xorm.Engine, page, pageSize int) (err err // SmallUpdateWaitForSettlementDate 改变 `wait_for_settlement_date` 待结算时间 func SmallUpdateWaitForSettlementDate(engine *xorm.Engine, userIds []string) (err error) { //Todo:: 将 user_public_platoon_relation 中 wait_for_settlement_date 设置为 30天后 - sql := "update user_public_platoon_amount set wait_for_settlement_date = ? where uid in (" + strings.Join(userIds, ",") + ")" + sql := "update user_small_public_platoon_relation set wait_for_settlement_date = ? where uid in (" + strings.Join(userIds, ",") + ")" waitForSettlementDate := time.Now().AddDate(0, 0, 30).Format("2006-01-02") _, err = engine.Exec(sql, waitForSettlementDate) if err != nil { @@ -528,7 +550,13 @@ func SmallDealCommonWealthPunish(engine *xorm.Engine, uid int, reason string) (e err = errors.New("未查询到公排关系记录") _ = session.Rollback() } - params.Uid = -1 + //TODO::判断是否为推荐用户 + if params.RecommendUid == 0 { + params.Uid = -1 + } else { + params.Uid = -2 + } + updateAffected, err := db.UserPublicPlatoonRelationUpdate(session, params.Id, params) if err != nil { _ = session.Rollback() diff --git a/svc/public_platoon_relate_commission.go b/svc/public_platoon_relate_commission.go index ba48222..87048b5 100644 --- a/svc/public_platoon_relate_commission.go +++ b/svc/public_platoon_relate_commission.go @@ -104,7 +104,12 @@ func BatchGetPublicPlatoonRelateCommissionByOrder(engine *xorm.Engine, masterId if nowBenefitUsers == nil { return nil, errors.New("未查询到父级公排关系记录") } - nowBenefitUid = zhios_order_relate_utils.AnyToString(nowBenefitUsers.Uid) + if nowBenefitUsers.Uid == -1 { + //则为待填充空位用户 + nowBenefitUid = "" + } else { + nowBenefitUid = zhios_order_relate_utils.AnyToString(nowBenefitUsers.Uid) + } //TODO::更新 公排关系记录 中 `return_commission_num` affected, err := db.UserPublicPlatoonRelationUpdate(engine.NewSession(), userPublicPlatoonRelation.Id, userPublicPlatoonRelation, "return_commission_num") diff --git a/svc/small_public_platoon_relate_commission.go b/svc/small_public_platoon_relate_commission.go index 8f90fdd..e63edbd 100644 --- a/svc/small_public_platoon_relate_commission.go +++ b/svc/small_public_platoon_relate_commission.go @@ -16,13 +16,6 @@ import ( // 批量分佣 订单显示 func BatchSmallGetPublicPlatoonRelateCommission(engine *xorm.Engine, masterId string, PublicPlatoonRelateCommissionReqList []*md.SmallPublicPlatoonRelateCommissionReq) (map[string]*md.SmallPublicPlatoonRelateCommissionResp, error) { var resp = make(map[string]*md.SmallPublicPlatoonRelateCommissionResp) - //userPublicPlatoonSetting, err := db.UserPublicPlatoonSettingGetOneByParams(engine, map[string]interface{}{ - // "key": "is_open", - // "value": 1, - //}) - //if err != nil { - // return nil, err - //} for _, param := range PublicPlatoonRelateCommissionReqList { // 根据供应商 rewardOpt, err := db.DbsPlanRewardByPvd(engine, param.Pvd) @@ -66,7 +59,7 @@ func BatchSmallGetPublicPlatoonRelateCommission(engine *xorm.Engine, masterId st //TODO::本次消费产生的共富收益给到谁 var nowBenefitUid string - userPublicPlatoonRelation, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ + userPublicPlatoonRelation, err := db.UserSmallPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ "key": "uid", "value": param.Uid, }) @@ -91,7 +84,7 @@ func BatchSmallGetPublicPlatoonRelateCommission(engine *xorm.Engine, masterId st fatherPosition = fatherPositions[remainder1-1] } - nowBenefitUsers, err := db.UserPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ + nowBenefitUsers, err := db.UserSmallPublicPlatoonRelationGetOneByParams(engine, map[string]interface{}{ "key": "position", "value": fatherPosition, }) @@ -101,10 +94,15 @@ func BatchSmallGetPublicPlatoonRelateCommission(engine *xorm.Engine, masterId st if nowBenefitUsers == nil { return nil, errors.New("未查询到父级公排关系记录") } - nowBenefitUid = zhios_order_relate_utils.AnyToString(nowBenefitUsers.Uid) + if nowBenefitUsers.Uid == -1 || nowBenefitUsers.Uid == -2 { + //则为待填充空位用户 + nowBenefitUid = "" + } else { + nowBenefitUid = zhios_order_relate_utils.AnyToString(nowBenefitUsers.Uid) + } //TODO::更新 公排关系记录 中 `return_commission_num` - affected, err := db.UserPublicPlatoonRelationUpdate(engine.NewSession(), userPublicPlatoonRelation.Id, userPublicPlatoonRelation, "return_commission_num") + affected, err := db.UserSmallPublicPlatoonRelationUpdate(engine.NewSession(), userPublicPlatoonRelation.Id, userPublicPlatoonRelation, "return_commission_num") if err != nil { return nil, err }