|
|
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
|
+} |