作者 tangxvhui

定时作业,自动创建赛季

@@ -8,6 +8,8 @@ import ( @@ -8,6 +8,8 @@ import (
8 "oppmg/common/config" 8 "oppmg/common/config"
9 "oppmg/common/log" 9 "oppmg/common/log"
10 10
  11 + "oppmg/services/crontab"
  12 +
11 "github.com/astaxie/beego" 13 "github.com/astaxie/beego"
12 "github.com/astaxie/beego/orm" 14 "github.com/astaxie/beego/orm"
13 _ "github.com/go-sql-driver/mysql" 15 _ "github.com/go-sql-driver/mysql"
@@ -23,5 +25,6 @@ func init() { @@ -23,5 +25,6 @@ func init() {
23 func main() { 25 func main() {
24 26
25 log.Debug("%s 应用启动", time.Now().String()) 27 log.Debug("%s 应用启动", time.Now().String())
  28 + crontab.Run()
26 beego.Run() 29 beego.Run()
27 } 30 }
@@ -160,6 +160,7 @@ func init() { @@ -160,6 +160,7 @@ func init() {
160 beego.AddNamespace(nsV1) 160 beego.AddNamespace(nsV1)
161 beego.AddNamespace(nsAuth) 161 beego.AddNamespace(nsAuth)
162 beego.AddNamespace(nsUcenter) 162 beego.AddNamespace(nsUcenter)
  163 +
163 beego.SetStaticPath("/log", beego.AppConfig.String("log_filename")) 164 beego.SetStaticPath("/log", beego.AppConfig.String("log_filename"))
164 beego.SetStaticPath("/file/opp", beego.AppConfig.String("file_save_path")) 165 beego.SetStaticPath("/file/opp", beego.AppConfig.String("file_save_path"))
165 beego.SetStaticPath("/static", "./static") 166 beego.SetStaticPath("/static", "./static")
  1 +package routers
  2 +
  3 +import (
  4 + "oppmg/services/crontab"
  5 +
  6 + "github.com/astaxie/beego"
  7 + "github.com/astaxie/beego/context"
  8 +)
  9 +
  10 +func init() {
  11 + testNs := beego.NewNamespace("/test",
  12 + beego.NSCond(func(ctx *context.Context) bool {
  13 + if beego.BConfig.RunMode == "prod" {
  14 + return false
  15 + }
  16 + return true
  17 + }),
  18 + beego.NSGet("/create_rank_peroid", func(ctx *context.Context) {
  19 + crontab.AutoCreateRankPeriod()
  20 + }),
  21 + )
  22 + beego.AddNamespace(testNs)
  23 +}
  1 +package crontab
  2 +
  3 +import (
  4 + "github.com/astaxie/beego/toolbox"
  5 +)
  6 +
  7 +//定时任务
  8 +
  9 +func Run() {
  10 + //自动创建赛季 每隔一小时执行一次
  11 + taskRankPeriod := toolbox.NewTask("AutoCreateRankPeriod", " 0 1 * * * *", AutoCreateRankPeriod)
  12 + toolbox.AddTask("AutoCreateRankPeriod", taskRankPeriod)
  13 + toolbox.StartTask()
  14 +}
  1 +package crontab
  2 +
  3 +import (
  4 + "fmt"
  5 + "oppmg/common/log"
  6 + "oppmg/models"
  7 + "oppmg/utils"
  8 + "strings"
  9 + "time"
  10 +
  11 + "github.com/astaxie/beego/orm"
  12 +)
  13 +
  14 +//rankTypeAutoPeriod 榜单配置:自动创建赛季
  15 +type RankTypeAutoPeriod struct {
  16 + RankTypeId int64 `orm:"column(rank_type_id)"`
  17 + CompanyId int64 `orm:"column(company_id)"`
  18 + AutoPeriod int64 `orm:"column(auto_period)"`
  19 + CreateTime int64 `orm:"column(create_time)"`
  20 +}
  21 +
  22 +//
  23 +type RankPeriodEndTime struct {
  24 + RankTypeId int64 `orm:"column(rank_type_id)"`
  25 + EndTime int64 `orm:"column(end_time)"`
  26 +}
  27 +
  28 +func AutoCreateRankPeriod() error {
  29 + log.Info("计划任务:【自动创建赛季】")
  30 + //获取需要执行自动创建的榜单记录
  31 + sql1 := `ELECT a.id AS rank_type_id,a.auto_period,UNIX_TIMESTAMP(a.create_at) AS create_time
  32 + ,a.company_id
  33 + FROM rank_type AS a
  34 + JOIN company AS b ON a.company_id=b.id AND b.enable=1 AND b.delete_at=0
  35 + WHERE a.auto_create = 1 AND a.auto_period>0
  36 + AND NOT EXISTS(
  37 + SELECT 1 FROM rank_period AS c WHERE a.id=c.rank_type_id AND c.status<2
  38 + )
  39 + AND a.create_at>?
  40 + LIMIT 1000`
  41 +
  42 + //获取自动创建赛季时需要的开始时间
  43 + sql2 := `SELECT a.rank_type_id,MAX(a.end_time) AS end_time
  44 + FROM rank_period AS a
  45 + WHERE a.status = 2 AND a.rank_type_id IN (%s)
  46 + GROUP BY a.rank_type_id `
  47 + var (
  48 + rankTypeCreateAt int64
  49 + err error
  50 + )
  51 + o := orm.NewOrm()
  52 +LOOP0:
  53 + for {
  54 + var (
  55 + rankTypeList []RankTypeAutoPeriod
  56 + )
  57 + log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录")
  58 + err = utils.ExecuteQueryAll(&rankTypeList, sql1, rankTypeCreateAt)
  59 + if err != nil {
  60 + log.Error("【自动创建赛季】,获取需要执行自动创建的榜单记录失败,err:%s", err)
  61 + break LOOP0
  62 + }
  63 + if len(rankTypeList) == 0 {
  64 + log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录数0,结束任务")
  65 + break LOOP0
  66 + }
  67 + log.Info("【自动创建赛季】,获取需要执行自动创建的榜单记录数%d", len(rankTypeList))
  68 + var rankTypeIds []string
  69 + for i := range rankTypeList {
  70 + rankTypeIds = append(rankTypeIds, fmt.Sprintf("%d", rankTypeList[i].RankTypeId))
  71 + rankTypeCreateAt = rankTypeList[i].CreateTime
  72 + }
  73 + log.Info("【自动创建赛季】,获取自动创建赛季时需要的开始时间")
  74 + runsql2 := fmt.Sprintf(sql2, strings.Join(rankTypeIds, ","))
  75 + var (
  76 + rankPeriodList []RankPeriodEndTime
  77 + )
  78 + rankPeriodMap := make(map[int64]int64)
  79 + err = utils.ExecuteQueryAll(&rankPeriodList, runsql2)
  80 + if err != nil {
  81 + log.Error("【自动创建赛季】,获取自动创建赛季时需要的开始时间失败:%s")
  82 + break LOOP0
  83 + }
  84 + for _, v := range rankPeriodList {
  85 + rankPeriodMap[v.RankTypeId] = v.EndTime
  86 + }
  87 + //开始循环rankTypeList,创建赛季
  88 + var (
  89 + successNum int64
  90 + addRankPeriod []models.RankPeriod
  91 + )
  92 + nowTime := time.Now()
  93 + LOOP1:
  94 + for _, v := range rankTypeList {
  95 + m := models.RankPeriod{
  96 + CompanyId: v.CompanyId,
  97 + RankTypeId: v.RankTypeId,
  98 + CreateAt: nowTime,
  99 + UpdateAt: nowTime,
  100 + Status: models.RANKPERIOD_STATUS_NOT,
  101 + }
  102 + if t, ok := rankPeriodMap[v.RankTypeId]; ok {
  103 + s := time.Unix(t, 0).Format("2006-01-02")
  104 + dayTime, err := time.Parse("2006-01-02", s)
  105 + if err != nil {
  106 + log.Error("【自动创建赛季】,格式化rank_period的end_time失败,id=%d,end_time=%d", v.RankTypeId, t)
  107 + continue LOOP1
  108 + }
  109 + begin := dayTime.Unix() + 24*60*60
  110 + end := begin + v.AutoPeriod*24*60*60 - 1
  111 + m.BeginTime = time.Unix(begin, 0).Local()
  112 + m.EndTime = time.Unix(end, 0).Local()
  113 + m.SeasonName = fmt.Sprintf("自动创建赛季%s", s)
  114 + } else {
  115 + s := nowTime.Format("2006-01-02")
  116 + dayTime, _ := time.Parse("2006-01-02", s)
  117 + begin := dayTime.Unix() + 24*60*60
  118 + end := begin + v.AutoPeriod*24*60*60 - 1
  119 + m.BeginTime = time.Unix(begin, 0).Local()
  120 + m.EndTime = time.Unix(end, 0).Local()
  121 + m.SeasonName = fmt.Sprintf("自动创建赛季%s", s)
  122 + }
  123 + addRankPeriod = append(addRankPeriod, m)
  124 + }
  125 + successNum, err = o.InsertMulti(100, addRankPeriod)
  126 + if err != nil {
  127 + log.Error("【自动创建赛季】,创建赛季出现错误,添加rank_period数据发生错误:%s", err)
  128 + }
  129 + log.Info("【自动创建赛季】,添加rank_period记录数量:%d", successNum)
  130 + }
  131 + return nil
  132 +}