You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
5.4 KiB
204 lines
5.4 KiB
package call |
|
|
|
import ( |
|
"fmt" |
|
"reflect" |
|
"server/common" |
|
"server/db" |
|
"server/pb" |
|
"server/util" |
|
"time" |
|
|
|
"github.com/liangdas/mqant/log" |
|
"gorm.io/gorm" |
|
) |
|
|
|
func GetShareInfo(uid int) *common.ShareInfo { |
|
shareInfo := &common.ShareInfo{UID: uid} |
|
db.Mysql().Get(shareInfo) |
|
if shareInfo.ID <= 0 { |
|
info, _ := GetUserXInfo(uid, "channel_id") |
|
shareInfo.ChannelID = info.ChannelID |
|
shareInfo.Share = util.GetShareCode(uid) |
|
shareInfo.Time = time.Now().Unix() |
|
db.Mysql().Create(shareInfo) |
|
} |
|
return shareInfo |
|
} |
|
|
|
// 分享查询 |
|
func ShareBind(share string, isOld bool, uid, cid int) { |
|
// 绑定 |
|
if share == "" || isOld { |
|
return |
|
} |
|
now := time.Now().Unix() |
|
codeInfo := &common.ShareActivityCode{ShareCode: share} |
|
upInfo := &common.ShareInfo{} |
|
db.Mysql().Get(codeInfo) |
|
if codeInfo.Id > 0 { |
|
upInfo.UID = codeInfo.UID |
|
// 判断是否过期 |
|
if now < codeInfo.ExpireAt { |
|
util.Go(func() { |
|
SendShareReward(uid, codeInfo.ActivityId) |
|
}) |
|
} |
|
} else { |
|
// 一级 |
|
upInfo = &common.ShareInfo{Share: share} |
|
} |
|
db.Mysql().Get(upInfo) |
|
if upInfo.ID <= 0 { |
|
return |
|
} |
|
shareInfo := &common.ShareInfo{UID: uid, UP1: upInfo.UID, UP2: upInfo.UP1, UP3: upInfo.UP2, Time: time.Now().Unix(), ChannelID: cid, Share: util.GetShareCode(uid)} |
|
db.Mysql().Create(shareInfo) |
|
|
|
// 更新上级邀请玩家数 |
|
db.Mysql().Update(&common.ShareInfo{UID: upInfo.UID}, map[string]interface{}{"invites": gorm.Expr("invites + 1")}) |
|
} |
|
|
|
// 判断分享,发放有效用户奖励 |
|
func CheckShare(r *common.RechargeOrder) { |
|
reward := GetConfigShareSys().ShareReward |
|
if reward <= 0 { |
|
return |
|
} |
|
if r.Amount < GetConfigShareSys().ShareRecharge { |
|
return |
|
} |
|
share := GetShareInfo(r.UID) |
|
if share.UP1 == 0 { |
|
return |
|
} |
|
// 发放奖励 |
|
db.Mysql().Update(&common.ShareInfo{UID: share.UP1}, map[string]interface{}{ |
|
"invalid_invites": gorm.Expr("invalid_invites + 1"), |
|
"invite_reward": gorm.Expr("invite_reward + ?", reward), |
|
"available_reward": gorm.Expr("available_reward + ?", reward), |
|
}) |
|
} |
|
|
|
// 投注奖励结算 |
|
func ShareSettle(d *pb.InnerAfterSettle) { |
|
shareInfo := &common.ShareInfo{UID: int(d.UID)} |
|
db.Mysql().Get(shareInfo) |
|
if shareInfo.UP1 == 0 { |
|
return |
|
} |
|
db.Mysql().Update(&common.ShareInfo{UID: int(d.UID)}, map[string]interface{}{"bet": gorm.Expr("bet + ?", d.TotalBet)}) |
|
|
|
ref := reflect.ValueOf(shareInfo).Elem() |
|
// 循环查询上级 |
|
for i := 1; i <= 3; i++ { |
|
uid := int(ref.FieldByName(fmt.Sprintf("UP%d", i)).Int()) |
|
if uid == 0 { |
|
break |
|
} |
|
con := GetConfigShareByLevel(i) |
|
if con == nil { |
|
log.Error("unknown config share level:%v", i) |
|
continue |
|
} |
|
// 发奖 |
|
reward := d.TotalBet * con.Per / 1000 |
|
if reward <= 0 { |
|
continue |
|
} |
|
db.Mysql().Update(&common.ShareInfo{UID: uid}, map[string]interface{}{ |
|
"bet_reward": gorm.Expr("bet_reward + ?", reward), |
|
"available_reward": gorm.Expr("available_reward + ?", reward), |
|
}) |
|
} |
|
} |
|
|
|
func PackLevelSql(uid, level int) (sql string) { |
|
if level == 0 { |
|
sql = fmt.Sprintf("up1 = %d or up2 = %d or up3 = %d", uid, uid, uid) |
|
} else { |
|
sql = fmt.Sprintf("up%d = %d", level, uid) |
|
} |
|
return |
|
} |
|
|
|
// GetUserShares 查询玩家下级数量 |
|
func GetUserShares(uid, level int) int64 { |
|
return db.Mysql().Count(&common.ShareInfo{}, PackLevelSql(uid, level)) |
|
} |
|
|
|
// GetUserShareRecharges 查询玩家下级充值人数 |
|
func GetUserShareRecharges(uid, level int) (count int64) { |
|
sql := fmt.Sprintf(`SELECT count(*) as count from |
|
(SELECT uid from share_info WHERE %s)a |
|
INNER JOIN |
|
(SELECT uid from recharge_info WHERE total_recharge > 0)b |
|
on a.uid = b.uid`, PackLevelSql(uid, level)) |
|
err := db.Mysql().C().Raw(sql).Scan(&count).Error |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
return |
|
} |
|
|
|
// GetUserShareValidRecharges 查询玩家下级有效充值人数 |
|
func GetUserShareValidRecharges(uid, level int) (count int64) { |
|
sql := fmt.Sprintf(`SELECT count(*) as count from |
|
(SELECT uid from share_info WHERE %s)a |
|
INNER JOIN |
|
(SELECT uid from recharge_info WHERE total_recharge >= %d)b |
|
on a.uid = b.uid`, PackLevelSql(uid, level), GetConfigShareSys().ShareRecharge) |
|
err := db.Mysql().C().Raw(sql).Scan(&count).Error |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
return |
|
} |
|
|
|
// GetUserShareRechargeAmount 查询玩家下级充值总额 |
|
func GetUserShareRechargeAmount(uid, level int) (count int64) { |
|
sql := fmt.Sprintf(`SELECT sum(b.total_recharge) as count from |
|
(SELECT uid from share_info WHERE %s)a |
|
INNER JOIN |
|
(SELECT uid,total_recharge from recharge_info WHERE total_recharge > 0)b |
|
on a.uid = b.uid`, PackLevelSql(uid, level)) |
|
err := db.Mysql().C().Raw(sql).Scan(&count).Error |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
return |
|
} |
|
|
|
// GetActivityShareCode 根据actid获取share code |
|
func GetActivityShareCode(uid, actId int) (code string, err error) { |
|
now := time.Now() |
|
ret := make([]*common.ShareActivityCode, 0, 1) |
|
_, err = db.Mysql().QueryList(0, 1, fmt.Sprintf("uid = %d and activity_id = %d and expire_at < %d", uid, actId, now.Unix()), "id", &common.ShareActivityCode{}, &ret) |
|
if err != nil { |
|
log.Error("GetActivityShareCode err:%v", err) |
|
return |
|
} |
|
if len(ret) == 0 { |
|
expireTime := util.GetZeroTime(now.AddDate(0, 0, 1)) |
|
code = util.GetShareCode(-uid - actId) |
|
err = db.Mysql().Create(&common.ShareActivityCode{ |
|
UID: uid, |
|
ShareCode: code, |
|
ActivityId: actId, |
|
ExpireAt: expireTime.Unix(), |
|
CreateAt: now.Unix(), |
|
}) |
|
if err != nil { |
|
log.Error("GetActivityShareCode err:%v", err) |
|
return |
|
} |
|
} else { |
|
code = ret[0].ShareCode |
|
} |
|
return |
|
} |
|
|
|
func SendShareReward(uid, actId int) { |
|
switch actId { |
|
} |
|
}
|
|
|