rank_period.go 3.2 KB
package models

import (
	"fmt"
	"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
	"opp/internal/utils"
	"time"

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

type RankPeriod struct {
	Id            int       `orm:"column(id);auto" description:"主键"`
	CompanyId     int       `orm:"column(company_id);null" description:"公司编号 表company.id"`
	RankTypeId    int       `orm:"column(rank_type_id)" description:"表rank_type.id 榜单类型编号"`
	SeasonName    string    `orm:"column(season_name);size(50);null" description:"赛季名称"`
	BeginTime     time.Time `orm:"column(begin_time);type(timestamp);null" description:"开始时间"`
	EndTime       time.Time `orm:"column(end_time);type(timestamp);null" description:"结束时间"`
	CreateAt      time.Time `orm:"column(create_at);type(timestamp);null" description:"创建时间"`
	UpdateAt      time.Time `orm:"column(update_at);type(timestamp);null" description:"更新时间"`
	Status        int8      `orm:"column(status);null" description:"状态 0:未开始 1:开始 2:结束 "`
	RankRangeBack string    `orm:"column(rank_range_back);null" description:"赛季结束时,备份赛季参与人设置"`
	RankItemBack  string    `orm:"column(rank_item_back);null" description:"赛季结束时,备份赛季评比项设置"`
}

func (t *RankPeriod) TableName() string {
	return "rank_period"
}

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

// AddRankPeriod insert a new RankPeriod into database and returns
// last inserted Id on success.
func AddRankPeriod(m *RankPeriod) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(m)
	return
}

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

// UpdateRankPeriod updates RankPeriod by Id and returns error if
// the record to be updated doesn't exist
func UpdateRankPeriodById(m *RankPeriod) (err error) {
	o := orm.NewOrm()
	v := RankPeriod{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteRankPeriod deletes RankPeriod by Id and returns error if
// the record to be deleted doesn't exist
func DeleteRankPeriod(id int) (err error) {
	o := orm.NewOrm()
	v := RankPeriod{Id: id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&RankPeriod{Id: id}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

//获取进行中的赛季周期 状态 0:未开始 1:开始 2:结束
func GetRankPeriods(companyId int, rankTypeId int, status []int) (v []*RankPeriod, err error) {
	sql := mybeego.NewSqlExutor()
	sql.Table((&RankPeriod{}).TableName())
	if companyId > 0 {
		sql.Where(fmt.Sprintf("company_id=%v", companyId))
	}
	if rankTypeId > 0 {
		sql.Where(fmt.Sprintf("rank_type_id=%v", rankTypeId))
	}
	if len(status) > 0 {
		sql.Where(fmt.Sprintf("status in (%v)", utils.JoinInts(status, ",")))
	}
	sql.Order("end_time")
	_, err = sql.Querys(&v)
	return
}