1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package timetask
- import (
- . "config"
- . "ent/entity"
- . "ent/util"
- "log"
- qutil "qfw/util"
- "strings"
- "time"
- )
- const (
- threeday = 259200
- )
- func Run() {
- go syncEntModel()
- go updateLimit()
- }
- //每天0点 检查是否到期
- func syncEntModel() {
- crontab(false, TimeTaskConfig.SyncEntModel, func() {
- defer qutil.Catch()
- log.Println("开始同步订阅模式。。。")
- now := time.Now()
- today_unix := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local).Unix()
- list := Mysql.SelectBySql(`select * from entniche_entmodel where createtime<?`, today_unix)
- if list != nil {
- for _, v := range *list {
- entId := qutil.IntAll(v["ent_id"])
- ent := Mysql.SelectBySql(`select model from entniche_info where id=?`, entId)
- if ent == nil || len(*ent) == 0 {
- log.Println(entId, "同步订阅模式", "没有找到该企业")
- continue
- }
- model := qutil.IntAll((*ent)[0]["model"])
- newModel := qutil.IntAll(v["model"])
- //由统一订阅变成个人订阅
- if model == 1 && newModel == 2 {
- log.Println(entId, "同步订阅模式", "新的订阅模式", newModel, "旧的订阅模式", model)
- del_ent_customer := Mysql.SelectBySql(`select * from entniche_customer where ent_id=? and state=1`, entId)
- del_ent_customer_contact := Mysql.SelectBySql(`select a.* from entniche_customer_contact a inner join entniche_customer b on (b.ent_id=? and a.customer_id=b.id and b.state=1)`, entId)
- del_ent_project := Mysql.SelectBySql(`select a.* from entniche_project a inner join entniche_customer b on (b.ent_id=? and b.state=1 and a.customer_id=b.id)`, entId)
- del_ent_project_track := Mysql.SelectBySql(`select a.* from entniche_project_track a inner join entniche_project b on (a.project_id=b.id) inner join entniche_customer c on (c.ent_id=? and c.state=1 and b.customer_id=c.id)`, entId)
- if Mysql.UpdateOrDeleteBySql(`delete from entniche_customer where ent_id=? and state=1`, entId) > 0 {
- VarBackup.Save(Entniche_customer, del_ent_customer)
- VarBackup.Save(Entniche_customer_contact, del_ent_customer_contact)
- VarBackup.Save(Entniche_project, del_ent_project)
- VarBackup.Save(Entniche_project_track, del_ent_project_track)
- }
- }
- }
- }
- Mysql.UpdateOrDeleteBySql(`delete from entniche_entmodel where createtime<?`, today_unix)
- 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(), qutil.IntAll(array[0]), qutil.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)
- }
- }
- }
- //每天0点更新用户每日导出剩余量
- func updateLimit() {
- crontab(false, TimeTaskConfig.QuotaUpdateTime, func() {
- log.Println("更新用户限额表定时任务。。。")
- isOk := Mysql.UpdateOrDeleteBySql("UPDATE entniche_export_limit SET remain_nums = data_limit,export_nums = 0")
- if isOk > -1 {
- log.Println("更新用户限额表定时结束。。。")
- } else {
- log.Println("更新用户限额表出错。。。")
- }
- })
- }
|