作者 tangxvhui

定时作业,自动创建赛季

... ... @@ -8,6 +8,8 @@ import (
"oppmg/common/config"
"oppmg/common/log"
"oppmg/services/crontab"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
... ... @@ -23,5 +25,6 @@ func init() {
func main() {
log.Debug("%s 应用启动", time.Now().String())
crontab.Run()
beego.Run()
}
... ...
... ... @@ -160,6 +160,7 @@ func init() {
beego.AddNamespace(nsV1)
beego.AddNamespace(nsAuth)
beego.AddNamespace(nsUcenter)
beego.SetStaticPath("/log", beego.AppConfig.String("log_filename"))
beego.SetStaticPath("/file/opp", beego.AppConfig.String("file_save_path"))
beego.SetStaticPath("/static", "./static")
... ...
package routers
import (
"oppmg/services/crontab"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
)
func init() {
testNs := beego.NewNamespace("/test",
beego.NSCond(func(ctx *context.Context) bool {
if beego.BConfig.RunMode == "prod" {
return false
}
return true
}),
beego.NSGet("/create_rank_peroid", func(ctx *context.Context) {
crontab.AutoCreateRankPeriod()
}),
)
beego.AddNamespace(testNs)
}
... ...
package crontab
import (
"github.com/astaxie/beego/toolbox"
)
//定时任务
func Run() {
//自动创建赛季 每隔一小时执行一次
taskRankPeriod := toolbox.NewTask("AutoCreateRankPeriod", " 0 1 * * * *", AutoCreateRankPeriod)
toolbox.AddTask("AutoCreateRankPeriod", taskRankPeriod)
toolbox.StartTask()
}
... ...
package crontab
import (
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/utils"
"strings"
"time"
"github.com/astaxie/beego/orm"
)
//rankTypeAutoPeriod 榜单配置:自动创建赛季
type RankTypeAutoPeriod struct {
RankTypeId int64 `orm:"column(rank_type_id)"`
CompanyId int64 `orm:"column(company_id)"`
AutoPeriod int64 `orm:"column(auto_period)"`
CreateTime int64 `orm:"column(create_time)"`
}
//
type RankPeriodEndTime struct {
RankTypeId int64 `orm:"column(rank_type_id)"`
EndTime int64 `orm:"column(end_time)"`
}
func AutoCreateRankPeriod() error {
log.Info("计划任务:【自动创建赛季】")
//获取需要执行自动创建的榜单记录
sql1 := `ELECT a.id AS rank_type_id,a.auto_period,UNIX_TIMESTAMP(a.create_at) AS create_time
,a.company_id
FROM rank_type AS a
JOIN company AS b ON a.company_id=b.id AND b.enable=1 AND b.delete_at=0
WHERE a.auto_create = 1 AND a.auto_period>0
AND NOT EXISTS(
SELECT 1 FROM rank_period AS c WHERE a.id=c.rank_type_id AND c.status<2
)
AND a.create_at>?
LIMIT 1000`
//获取自动创建赛季时需要的开始时间
sql2 := `SELECT a.rank_type_id,MAX(a.end_time) AS end_time
FROM rank_period AS a
WHERE a.status = 2 AND a.rank_type_id IN (%s)
GROUP BY a.rank_type_id `
var (
rankTypeCreateAt int64
err error
)
o := orm.NewOrm()
LOOP0:
for {
var (
rankTypeList []RankTypeAutoPeriod
)
log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录")
err = utils.ExecuteQueryAll(&rankTypeList, sql1, rankTypeCreateAt)
if err != nil {
log.Error("【自动创建赛季】,获取需要执行自动创建的榜单记录失败,err:%s", err)
break LOOP0
}
if len(rankTypeList) == 0 {
log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录数0,结束任务")
break LOOP0
}
log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录数%d", len(rankTypeList))
var rankTypeIds []string
for i := range rankTypeList {
rankTypeIds = append(rankTypeIds, fmt.Sprintf("%d", rankTypeList[i].RankTypeId))
rankTypeCreateAt = rankTypeList[i].CreateTime
}
log.Info("【自动创建赛季】,获取自动创建赛季时需要的开始时间")
runsql2 := fmt.Sprintf(sql2, strings.Join(rankTypeIds, ","))
var (
rankPeriodList []RankPeriodEndTime
)
rankPeriodMap := make(map[int64]int64)
err = utils.ExecuteQueryAll(&rankPeriodList, runsql2)
if err != nil {
log.Error("【自动创建赛季】,获取自动创建赛季时需要的开始时间失败:%s")
break LOOP0
}
for _, v := range rankPeriodList {
rankPeriodMap[v.RankTypeId] = v.EndTime
}
//开始循环rankTypeList,创建赛季
var (
successNum int64
addRankPeriod []models.RankPeriod
)
nowTime := time.Now()
LOOP1:
for _, v := range rankTypeList {
m := models.RankPeriod{
CompanyId: v.CompanyId,
RankTypeId: v.RankTypeId,
CreateAt: nowTime,
UpdateAt: nowTime,
Status: models.RANKPERIOD_STATUS_NOT,
}
if t, ok := rankPeriodMap[v.RankTypeId]; ok {
s := time.Unix(t, 0).Format("2006-01-02")
dayTime, err := time.Parse("2006-01-02", s)
if err != nil {
log.Error("【自动创建赛季】,格式化rank_period的end_time失败,id=%d,end_time=%d", v.RankTypeId, t)
continue LOOP1
}
begin := dayTime.Unix() + 24*60*60
end := begin + v.AutoPeriod*24*60*60 - 1
m.BeginTime = time.Unix(begin, 0).Local()
m.EndTime = time.Unix(end, 0).Local()
m.SeasonName = fmt.Sprintf("自动创建赛季%s", s)
} else {
s := nowTime.Format("2006-01-02")
dayTime, _ := time.Parse("2006-01-02", s)
begin := dayTime.Unix() + 24*60*60
end := begin + v.AutoPeriod*24*60*60 - 1
m.BeginTime = time.Unix(begin, 0).Local()
m.EndTime = time.Unix(end, 0).Local()
m.SeasonName = fmt.Sprintf("自动创建赛季%s", s)
}
addRankPeriod = append(addRankPeriod, m)
}
successNum, err = o.InsertMulti(100, addRankPeriod)
if err != nil {
log.Error("【自动创建赛季】,创建赛季出现错误,添加rank_period数据发生错误:%s", err)
}
log.Info("【自动创建赛季】,添加rank_period记录数量:%d", successNum)
}
return nil
}
... ...