package timetask import ( "fmt" "log" "strconv" "strings" "time" "app.yhyue.com/moapp/jybase/common" "bp.jydev.jianyu360.cn/BaseService/userCenter/entity" ) //已到期企业定时任务 func Run() { if entity.ConfigJson.IsRun { go checkEntIsExpire() log.Println(entity.ConfigJson.CheckEntIsExpire) } } func checkEntIsExpire() { crontab(false, entity.ConfigJson.CheckEntIsExpire, func() { defer common.Catch() log.Println("定时任务更新认证已到期企业开始") now := time.Now().Format(entity.Date_Full_Layout) rdata := entity.Mysql.SelectBySql(`select id,name,phone,auth_endTime from entniche_info where auth_endTime < ? and auth_status =1;`, now) if len(*rdata) > 0 && rdata != nil { log.Println(fmt.Sprintf("本轮更新已到期企业数量为:%v。", len(*rdata))) for _, v := range *rdata { log.Println(fmt.Sprintf("已到期企业id:%v, 企业名称: %s, 企业管理员手机号:%s", v["id"], v["name"], v["phone"])) } } //更新企业 ok := entity.Mysql.UpdateOrDeleteBySql(`UPDATE entniche_info SET auth_status=-2 where auth_endTime < ? and auth_status =1 `, now) if ok < 0 { log.Println("更新已到期企业失败") } log.Println("定时任务更新认证已到期企业结束") }) } // func crontab(flag bool, c string, f func()) { array := strings.Split(c, ":") if len(array) != 2 { log.Fatalln("定时任务参数错误!", c) } if flag { go f() } now := time.Now() t := time.Date(now.Year(), now.Month(), now.Day(), common.IntAll(array[0]), common.IntAll(array[1]), 0, 0, time.Local) if t.Before(now) { t = t.AddDate(0, 0, 1) } timer := time.NewTimer(t.Sub(now)) for { select { case <-timer.C: go f() timer.Reset(24 * time.Hour) } } } // func crontabByTicker(flag bool, c string, f func()) { if c == "0" { log.Println("定时任务参数有误:", c) return } countdown := 5 * 60 if strings.Contains(c, "h") { cStr := strings.Split(c, "h")[0] countdown, _ = strconv.Atoi(cStr) countdown = countdown * 60 * 60 } else { countdown, _ = strconv.Atoi(c) countdown = countdown * 60 } if flag { go f() } ticker := time.NewTicker(time.Duration(countdown) * time.Second) go func(t *time.Ticker) { for { select { case <-t.C: go f() fmt.Println("get ticker", time.Now().Format("2006-01-02 15:04:05")) } } }(ticker) }