@@ -0,0 +1,121 @@ | |||||
package db | |||||
import ( | |||||
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model" | |||||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | |||||
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | |||||
"errors" | |||||
"fmt" | |||||
"reflect" | |||||
"xorm.io/xorm" | |||||
) | |||||
// BatchSelectUserPublicPlatoonDoubleNetworkUserCoinRecords 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `UserPublicPlatoonDoubleNetworkUserCoinRecordFindByParams` 方法 | |||||
func BatchSelectUserPublicPlatoonDoubleNetworkUserCoinRecords(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonDoubleNetworkUserCoinRecord, error) { | |||||
var UserPublicPlatoonDoubleNetworkUserCoinRecordData []model.UserPublicPlatoonDoubleNetworkUserCoinRecord | |||||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]). | |||||
Find(&UserPublicPlatoonDoubleNetworkUserCoinRecordData); err != nil { | |||||
return nil, zhios_order_relate_logx.Warn(err) | |||||
} | |||||
return &UserPublicPlatoonDoubleNetworkUserCoinRecordData, nil | |||||
} | |||||
// UserPublicPlatoonDoubleNetworkUserCoinRecordInsert 插入单条数据 | |||||
func UserPublicPlatoonDoubleNetworkUserCoinRecordInsert(Db *xorm.Engine, UserPublicPlatoonDoubleNetworkUserCoinRecord *model.UserPublicPlatoonDoubleNetworkUserCoinRecord) (int, error) { | |||||
_, err := Db.InsertOne(UserPublicPlatoonDoubleNetworkUserCoinRecord) | |||||
if err != nil { | |||||
return 0, err | |||||
} | |||||
return UserPublicPlatoonDoubleNetworkUserCoinRecord.Id, nil | |||||
} | |||||
// BatchAddUserPublicPlatoonDoubleNetworkUserCoinRecords 批量新增数据 | |||||
func BatchAddUserPublicPlatoonDoubleNetworkUserCoinRecords(Db *xorm.Engine, UserPublicPlatoonDoubleNetworkUserCoinRecordData []*model.UserPublicPlatoonDoubleNetworkUserCoinRecord) (int64, error) { | |||||
affected, err := Db.Insert(UserPublicPlatoonDoubleNetworkUserCoinRecordData) | |||||
if err != nil { | |||||
return 0, err | |||||
} | |||||
return affected, nil | |||||
} | |||||
func GetUserPublicPlatoonDoubleNetworkUserCoinRecordCount(Db *xorm.Engine) int { | |||||
var UserPublicPlatoonDoubleNetworkUserCoinRecord model.UserPublicPlatoonDoubleNetworkUserCoinRecord | |||||
session := Db.Where("") | |||||
count, err := session.Count(&UserPublicPlatoonDoubleNetworkUserCoinRecord) | |||||
if err != nil { | |||||
return 0 | |||||
} | |||||
return int(count) | |||||
} | |||||
// UserPublicPlatoonDoubleNetworkUserCoinRecordDelete 删除记录 | |||||
func UserPublicPlatoonDoubleNetworkUserCoinRecordDelete(Db *xorm.Engine, id interface{}) (int64, error) { | |||||
if reflect.TypeOf(id).Kind() == reflect.Slice { | |||||
return Db.In("id", id).Delete(model.UserPublicPlatoonDoubleNetworkUserCoinRecord{}) | |||||
} else { | |||||
return Db.Where("id = ?", id).Delete(model.UserPublicPlatoonDoubleNetworkUserCoinRecord{}) | |||||
} | |||||
} | |||||
// UserPublicPlatoonDoubleNetworkUserCoinRecordUpdate 更新记录 | |||||
func UserPublicPlatoonDoubleNetworkUserCoinRecordUpdate(Db *xorm.Engine, id interface{}, UserPublicPlatoonDoubleNetworkUserCoinRecord *model.UserPublicPlatoonDoubleNetworkUserCoinRecord, forceColums ...string) (int64, error) { | |||||
var ( | |||||
affected int64 | |||||
err error | |||||
) | |||||
if forceColums != nil { | |||||
affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(UserPublicPlatoonDoubleNetworkUserCoinRecord) | |||||
} else { | |||||
affected, err = Db.Where("id=?", id).Update(UserPublicPlatoonDoubleNetworkUserCoinRecord) | |||||
} | |||||
if err != nil { | |||||
return 0, err | |||||
} | |||||
return affected, nil | |||||
} | |||||
// UserPublicPlatoonDoubleNetworkUserCoinRecordGetOneByParams 通过传入的参数查询数据(单条) | |||||
func UserPublicPlatoonDoubleNetworkUserCoinRecordGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.UserPublicPlatoonDoubleNetworkUserCoinRecord, error) { | |||||
var m model.UserPublicPlatoonDoubleNetworkUserCoinRecord | |||||
var query = fmt.Sprintf("%s =?", params["key"]) | |||||
has, err := Db.Where(query, params["value"]).Get(&m) | |||||
if err != nil { | |||||
return nil, zhios_order_relate_logx.Error(err) | |||||
} | |||||
if has == false { | |||||
return nil, nil | |||||
} | |||||
return &m, nil | |||||
} | |||||
// UserPublicPlatoonDoubleNetworkUserCoinRecordFindByParams 通过传入的参数查询数据(多条) | |||||
func UserPublicPlatoonDoubleNetworkUserCoinRecordFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserPublicPlatoonDoubleNetworkUserCoinRecord, error) { | |||||
var m []model.UserPublicPlatoonDoubleNetworkUserCoinRecord | |||||
if params["value"] == nil { | |||||
return nil, errors.New("参数有误") | |||||
} | |||||
if params["key"] == nil { | |||||
//查询全部数据 | |||||
err := Db.Find(&m) | |||||
if err != nil { | |||||
return nil, zhios_order_relate_logx.Error(err) | |||||
} | |||||
return &m, nil | |||||
} else { | |||||
if reflect.TypeOf(params["value"]).Kind() == reflect.Slice { | |||||
//指定In查询 | |||||
if err := Db.In(zhios_order_relate_utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil { | |||||
return nil, zhios_order_relate_logx.Warn(err) | |||||
} | |||||
return &m, nil | |||||
} else { | |||||
var query = fmt.Sprintf("%s =?", params["key"]) | |||||
err := Db.Where(query, params["value"]).Find(&m) | |||||
if err != nil { | |||||
return nil, zhios_order_relate_logx.Error(err) | |||||
} | |||||
return &m, nil | |||||
} | |||||
} | |||||
} |
@@ -11,6 +11,7 @@ type UserPublicPlatoonDoubleNetworkSetting struct { | |||||
SeveralRows int `json:"several_rows" xorm:"not null default 0 comment('几排') TINYINT(3)"` | SeveralRows int `json:"several_rows" xorm:"not null default 0 comment('几排') TINYINT(3)"` | ||||
OriginatorUid int `json:"originator_uid" xorm:"not null default -1 comment('创始人uid') INT(11)"` | OriginatorUid int `json:"originator_uid" xorm:"not null default -1 comment('创始人uid') INT(11)"` | ||||
SettlementDate string `json:"settlement_date" xorm:"not null default '0000-00-00 00:00:00' comment('结算日期') CHAR(50)"` | SettlementDate string `json:"settlement_date" xorm:"not null default '0000-00-00 00:00:00' comment('结算日期') CHAR(50)"` | ||||
CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` | |||||
Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` | Ext string `json:"ext" xorm:"comment('拓展字段(json存储)') TEXT"` | ||||
CreateAt time.Time `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | 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"` | UpdateAt time.Time `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` | ||||
@@ -0,0 +1,15 @@ | |||||
package model | |||||
import ( | |||||
"time" | |||||
) | |||||
type UserPublicPlatoonDoubleNetworkUserCoinRecord struct { | |||||
Id int `json:"id" xorm:"not null pk autoincr INT(11)"` | |||||
Uid int `json:"uid" xorm:"not null default 0 comment('uid') INT(11)"` | |||||
LastAmount string `json:"last_amount" xorm:"not null default 0.0000 comment('上次金额') DECIMAL(10,4)"` | |||||
Amount string `json:"amount" xorm:"not null default 0.0000 comment('当前金额') DECIMAL(10,4)"` | |||||
CoinId int `json:"coin_id" xorm:"not null default 0 comment('虚拟币id(作用于成长值)') INT(11)"` | |||||
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"` | |||||
} |
@@ -7,8 +7,10 @@ import ( | |||||
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils" | ||||
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx" | ||||
"errors" | "errors" | ||||
"github.com/shopspring/decimal" | |||||
"math" | "math" | ||||
"math/rand" | "math/rand" | ||||
"strconv" | |||||
"strings" | "strings" | ||||
"time" | "time" | ||||
"xorm.io/xorm" | "xorm.io/xorm" | ||||
@@ -168,7 +170,7 @@ func getLevelForFirstPositionDoubleNetwork(level, severalTimes int) (position in | |||||
} | } | ||||
// getLevelForLastPositionDoubleNetwork 每个等级的结束值 | // getLevelForLastPositionDoubleNetwork 每个等级的结束值 | ||||
func etLevelForLastPositionDoubleNetwork(level, severalTimes int) (position int) { | |||||
func getLevelForLastPositionDoubleNetwork(level, severalTimes int) (position int) { | |||||
for n := 0; n <= (level - 1); n++ { | for n := 0; n <= (level - 1); n++ { | ||||
position += int(math.Pow(float64(severalTimes), float64(n))) | position += int(math.Pow(float64(severalTimes), float64(n))) | ||||
} | } | ||||
@@ -339,3 +341,74 @@ func randSeedIntDoubleNetwork(start, end int64, nums int, uniqueMap map[int64]bo | |||||
res = *resp | res = *resp | ||||
return res | return res | ||||
} | } | ||||
func EstimateUserPosition(engine *xorm.Engine, uid int) (total, level, levelRank, rank, previousRow int, diffValue string, err error) { | |||||
//1、查找 `user_public_platoon_setting` 基础设置 | |||||
userPublicPlatoonDoubleNetworkSetting, err := db.UserPublicPlatoonDoubleNetworkSettingGetOneByParams(engine, map[string]interface{}{ | |||||
"key": "is_open", | |||||
"value": 1, | |||||
}) | |||||
if err != nil { | |||||
return | |||||
} | |||||
//2、查询排名 | |||||
rank, total, userAmount, err := CalcUserRank(engine, zhios_order_relate_utils.IntToStr(uid)) | |||||
if err != nil { | |||||
return | |||||
} | |||||
rows := float64(userPublicPlatoonDoubleNetworkSetting.SeveralRows) | |||||
times := float64(userPublicPlatoonDoubleNetworkSetting.SeveralTimes) | |||||
level = makeSearchLevelDoubleNetwork(&rank, rows, ×) | |||||
levelPosition1 := getLevelForFirstPositionDoubleNetwork(level, userPublicPlatoonDoubleNetworkSetting.SeveralTimes) | |||||
levelRank = rank - levelPosition1 + 1 | |||||
//3、计算与前排差距 | |||||
previousRow = level - 1 | |||||
if previousRow > 0 { | |||||
previousRowPosition1 := getLevelForLastPositionDoubleNetwork(previousRow, userPublicPlatoonDoubleNetworkSetting.SeveralTimes) | |||||
previousRowAmount, err1 := GetUserRankAmount(engine, strconv.Itoa(previousRowPosition1)) | |||||
if err1 != nil { | |||||
err = err1 | |||||
return | |||||
} | |||||
userAmountValue, _ := decimal.NewFromString(userAmount) | |||||
previousRowAmountValue, _ := decimal.NewFromString(previousRowAmount) | |||||
diffValue = previousRowAmountValue.Sub(userAmountValue).String() | |||||
} | |||||
return | |||||
} | |||||
func CalcUserRank(engine *xorm.Engine, uid string) (rank, total int, amount string, err error) { | |||||
sql := "SELECT id, uid, amount, @rank := @rank + 1 AS rank FROM `user_public_platoon_double_network_user_coin_record`, (SELECT @rank:=0) r ORDER BY amount DESC;" | |||||
nativeString, _ := db.QueryNativeString(engine, sql) | |||||
total = len(nativeString) | |||||
if total <= 0 { | |||||
err = errors.New("当前无排名数据") | |||||
return | |||||
} | |||||
for _, v := range nativeString { | |||||
if uid == v["uid"] { | |||||
rank = zhios_order_relate_utils.StrToInt(v["rank"]) | |||||
amount = v["amount"] | |||||
break | |||||
} | |||||
} | |||||
return | |||||
} | |||||
func GetUserRankAmount(engine *xorm.Engine, rank string) (amount string, err error) { | |||||
sql := "SELECT id, uid, amount, @rank := @rank + 1 AS rank FROM `user_public_platoon_double_network_user_coin_record`, (SELECT @rank:=0) r ORDER BY amount DESC;" | |||||
nativeString, _ := db.QueryNativeString(engine, sql) | |||||
if len(nativeString) <= 0 { | |||||
err = errors.New("当前无排名数据") | |||||
return | |||||
} | |||||
for _, v := range nativeString { | |||||
if rank == v["rank"] { | |||||
amount = v["amount"] | |||||
break | |||||
} | |||||
} | |||||
return | |||||
} |