ice_gametop.go 3.98 KB
package sdkdb

import (
	"errors"
	"fmt"
	"log"
	"strconv"

	"github.com/astaxie/beego/orm"
)

type IceGameTop struct {
	UID     string `orm:"size(36);pk;unique"`
	GTID    string `orm:"size(36);unique"`
	GID     string `orm:"size(36);unique"`
	TopType string `orm:"size(1);unique"`
	MyKey   string `orm:"size(20)"`
}

func (self *IceGameTop) String() string {
	return fmt.Sprintf("UID[%s] GTID[%s] GID[%s] TopType[%s] MyKey[%s]", self.UID, self.GTID, self.GID, self.TopType, self.MyKey)
}

func IceGameTopInit() {
	// 注册模型
	orm.RegisterModel(new(IceGameTop))
}

/**
 *  查询所有数据
 */
func IceGameTopMysqlQueryTotal() (iceGameTops []*IceGameTop, err error) {
	o := orm.NewOrm()

	var datalist []orm.Params
	_, err = o.Raw("SELECT * FROM ice_gametop").Values(&datalist)

	iceGameTops = []*IceGameTop{}
	for _, data := range datalist {
		igt := new(IceGameTop)
		igt.UID = data["UID"].(string)
		igt.GTID = data["GTID"].(string)
		igt.GID = data["GID"].(string)
		igt.TopType = data["TOPTYPE"].(string)
		igt.MyKey = data["MYKEY"].(string)

		iceGameTops = append(iceGameTops, igt)
	}

	return
}

/**
 *  查询一条数据
 */
func IceGameTopMysqlQueryOne(uid string, gtid string, gid string, toptype string) (iceGameTop *IceGameTop, num int64, err error) {
	o := orm.NewOrm()

	var datalist []orm.Params
	num, err = o.Raw("SELECT * FROM ice_gametop where uid=? and gtid=? and gid=? and toptype=?", uid, gtid, gid, toptype).Values(&datalist)

	if num != 0 {
		iceGameTop = new(IceGameTop)
		iceGameTop.UID = datalist[0]["UID"].(string)
		iceGameTop.GTID = datalist[0]["GTID"].(string)
		iceGameTop.GID = datalist[0]["GID"].(string)
		iceGameTop.TopType = datalist[0]["TOPTYPE"].(string)
		iceGameTop.MyKey = datalist[0]["MYKEY"].(string)
	}

	return
}

/**
 *  提交分数,如果不存在则insert,如果存在则update
 */
func IceGameTopMysqlSubmitScore(uid string, gtid string, gid string, score string) (err error) {
	toptypes, _ := IceGameTopSetQueryToptype(gtid, gid)
	//update time 2017-09-07 15:30:17 update by 徐振浩
	//update start
	if len(toptypes) == 0 {
		err = errors.New("gametop name or game name not exist")
		return
	}
	toptype := toptypes[0]
	//	myorm, _ := DBBegin()
	myorm := orm.NewOrm()

	// 先获得原有总榜分数
	igt, num, _ := IceGameTopMysqlQueryOne(uid, gtid, gid, toptype)
	var value string
	if num != 0 {
		value = igt.MyKey // 总榜分
	} else {
		value = "0"
	}
	value_float64, _ := strconv.ParseFloat(value, 64) // 总榜原有分数
	score_float64, _ := strconv.ParseFloat(score, 64)
	//判断排行榜类型,如果为顺序排序,则高分刷新,如果为倒序排序,则低分刷新
	sortType, err := IceGameTopSetQuerySortType(gtid, gid)
	log.Println(sortType)
	if err != nil {
		return
	}
	if sortType == "0" {
		if value_float64-score_float64 > 0.000001 || value == "0" {
			sqlstr := "INSERT INTO ice_gametop (uid,gtid,gid,toptype,mykey) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE mykey=?"
			res, err := myorm.Raw(sqlstr, uid, gtid, gid, toptype, score_float64, score_float64).Exec()
			if err != nil {
				log.Printf("IceGameTopInsertOrUpdate err[%v]", err)
			}
			num, _ := res.RowsAffected()
			log.Printf("IceGameTopInsertOrUpdate affected nums[%d]", num)
		}
	} else {
		if score_float64-value_float64 > 0.000001 {
			sqlstr := "INSERT INTO ice_gametop (uid,gtid,gid,toptype,mykey) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE mykey=?"
			res, err := myorm.Raw(sqlstr, uid, gtid, gid, toptype, score_float64, score_float64).Exec()
			if err != nil {
				log.Printf("IceGameTopInsertOrUpdate err[%v]", err)
			}
			num, _ := res.RowsAffected()
			log.Printf("IceGameTopInsertOrUpdate affected nums[%d]", num)
		}
	}
	//updat end
	return
}

func IceGameTopCleanScore() (err error) {
	myorm, _ := DBBegin()

	sqlstr := "UPDATE ice_gametop set mykey=? where toptype <> 0"
	res, err := myorm.Raw(sqlstr, 0).Exec()
	if err != nil {
		log.Printf("IceGameTopCleanScore err[%v]", err)
	}

	num, _ := res.RowsAffected()
	log.Printf("IceGameTopCleanScore affected nums[%d]", num)

	DBCommit(myorm)

	return
}