ice_gametopset.go 4.32 KB
package sdkdb

import (
	"errors"
	"fmt"
	"log"

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

type IceGameTopSet struct {
	GTID       string `orm:"size(36);pk;unique"`
	GID        string `orm:"size(36);unique"`
	TOPTYPE    string `orm:"size(1);unique"`
	GTNAME     string `orm:"size(255)"`
	SORCETYPE  string `orm:"size(1)"`
	SORCESUB   string `orm:"size(1)"`
	SORCEORDER string `orm:"size(1)"`
}

func (self *IceGameTopSet) String() string {
	return fmt.Sprintf("GTID[%s] GID[%s] TOPTYPE[%s] GTNAME[%s] SORCETYPE[%s] SORCESUB[%s] SORCEORDER[%s]", self.GTID, self.GID, self.TOPTYPE, self.GTNAME, self.SORCETYPE, self.SORCESUB, self.SORCEORDER)
}

func IceGameTopSetInit() {
	// 注册模型
	orm.RegisterModel(new(IceGameTopSet))
}

/**
 *  查询所有数据
 */
func IceGameTopSetQueryTotal() (iceGameTopSets []*IceGameTopSet, err error) {
	o := orm.NewOrm()

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

	iceGameTopSets = []*IceGameTopSet{}
	for _, data := range datalist {
		igt := new(IceGameTopSet)
		igt.GTID = data["GTID"].(string)
		igt.GID = data["GID"].(string)
		igt.TOPTYPE = data["TOPTYPE"].(string)
		igt.GTNAME = data["GTNAME"].(string)
		igt.SORCETYPE = data["SORCETYPE"].(string)
		igt.SORCESUB = data["SORCESUB"].(string)
		igt.SORCEORDER = data["SORCEORDER"].(string)

		iceGameTopSets = append(iceGameTopSets, igt)
	}

	return
}

/**
 *  查询一条数据
 */
func IceGameTopSetQueryOne(gtid string, gid string, toptype string) (iceGameTopSet *IceGameTopSet, num int64, err error) {
	o := orm.NewOrm()

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

	if num != 0 {
		iceGameTopSet = new(IceGameTopSet)
		iceGameTopSet.GTID = datalist[0]["GTID"].(string)
		iceGameTopSet.GID = datalist[0]["GID"].(string)
		iceGameTopSet.TOPTYPE = datalist[0]["TOPTYPE"].(string)
		iceGameTopSet.GTNAME = datalist[0]["GTNAME"].(string)
		iceGameTopSet.SORCETYPE = datalist[0]["SORCETYPE"].(string)
		iceGameTopSet.SORCESUB = datalist[0]["SORCESUB"].(string)
		iceGameTopSet.SORCEORDER = datalist[0]["SORCEORDER"].(string)
	}

	return
}

/**
 *  查询一个排行榜ID的所有排行类型
 */
func IceGameTopSetQueryToptype(gtid string, gid string) (topTypes []string, err error) {
	o := orm.NewOrm()

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

	topTypes = []string{}
	for _, data := range datalist {
		topType := data["toptype"].(string)
		topTypes = append(topTypes, topType)
	}

	return
}

func IceGameTopSetQuerySortType(gtid string, gid string) (sortType string, err error) {
	o := orm.NewOrm()

	var datalist []orm.Params
	_, err = o.Raw("select sorceorder from ice_gametopset where gid=? and gtid=?", gid, gtid).Values(&datalist)

	if len(datalist) == 0 {
		err = errors.New("gamename or gametopname not exist")
		return
	}
	sortType = datalist[0]["sorceorder"].(string)
	return

}

func IceGameTopSetQuery(gtid string, gid string) (iceGameTopSets []*IceGameTopSet, err error) {
	o := orm.NewOrm()

	var datalist []orm.Params
	_, err = o.Raw("SELECT GTID,GID,TOPTYPE,GTNAME,SORCETYPE,SORCESUB,SORCEORDER FROM ice_gametopset where gtid=? and gid=?", gtid, gid).Values(&datalist)

	iceGameTopSets = []*IceGameTopSet{}
	for _, data := range datalist {
		igts := new(IceGameTopSet)
		igts.GTID = data["GTID"].(string)
		igts.GID = data["GID"].(string)
		igts.TOPTYPE = data["TOPTYPE"].(string)
		igts.GTNAME = data["GTNAME"].(string)
		igts.SORCETYPE = data["SORCETYPE"].(string)
		igts.SORCESUB = data["SORCESUB"].(string)
		igts.SORCEORDER = data["SORCEORDER"].(string)

		iceGameTopSets = append(iceGameTopSets, igts)
	}

	return
}

/**
 *  创建排行榜
 */
func IceGameTopSetInsert(gtid string, gid string, toptype string, gtname string, sorcetype string, sorcesub string, sorceorder string) (err error) {
	_, num, err := IceGameTopSetQueryOne(gtid, gid, toptype)

	if num == 0 {
		myorm, _ := DBBegin()

		res, err := myorm.Raw("insert into ice_gametopset(gtid,gid,toptype,gtname,sorcetype,sorcesub,sorceorder) values(?,?,?,?,?,?,?)", gtid, gid, toptype, gtname, sorcetype, sorcesub, sorceorder).Exec()
		if err == nil {
			num, _ := res.RowsAffected()
			log.Printf("mysql row affected nums[%d]", num)
		}

		DBCommit(myorm)
	} else {
		err = errors.New("排行榜已存在")
	}

	return
}