rank_type.go 1.9 KB
package models

import (
	"fmt"
	"time"

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

type RankType struct {
	Id           int64     `orm:"column(id);pk"`
	CompanyId    int64     `orm:"column(company_id)"`
	Name         string    `orm:"column(name)"`
	EnableStatus int8      `orm:"column(enable_status)"`
	CreateAt     time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
	UpdateAt     time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
	Type         int8      `orm:"column(type)"`
	AutoCreate   int8      `orm:"column(auto_create)"` //是否自动创建赛季 【0:否】【1:是】
	AutoPeriod   int       `orm:"column(auto_period)"`
}

func (t *RankType) TableName() string {
	return "rank_type"
}

func init() {
	orm.RegisterModel(new(RankType))
}

//榜单类型
const (
	RANK_TYPE_SEASON int8 = 1 //季赛榜
	RANK_TYPE_YEAR   int8 = 2 //年榜
)

//榜单状态
const (
	RANK_STATUS_YES int8 = 2 //启用
	RANK_STATUS_NO  int8 = 1 //禁用
)

// AddRank insert a new Rank into database and returns
// last inserted Id on success.
func AddRank(m *RankType) (id int64, err error) {
	o := orm.NewOrm()
	nowTime := time.Now()
	m.CreateAt = nowTime
	m.UpdateAt = nowTime
	id, err = o.Insert(m)
	return
}

// GetRankById retrieves Rank by Id. Returns error if
// Id doesn't exist
func GetRankById(id int64) (v *RankType, err error) {
	o := orm.NewOrm()
	v = &RankType{Id: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateRank updates Rank by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankById(m *RankType, cols []string, om ...orm.Ormer) (err error) {
	var o orm.Ormer
	if len(om) > 0 {
		o = om[0]
	} else {
		o = orm.NewOrm()
	}
	m.UpdateAt = time.Now()
	if len(cols) > 0 {
		cols = append(cols, "UpdateAt")
	}
	var num int64
	if num, err = o.Update(m, cols...); err == nil {
		fmt.Println("Number of records updated in database:", num)
	}

	return
}