timetask.go 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package timetask
  2. import (
  3. . "config"
  4. . "ent/entity"
  5. . "ent/util"
  6. "log"
  7. qutil "qfw/util"
  8. "strings"
  9. "time"
  10. )
  11. const (
  12. threeday = 259200
  13. )
  14. func Run() {
  15. go syncEntModel()
  16. go updateLimit()
  17. }
  18. //每天0点 检查是否到期
  19. func syncEntModel() {
  20. crontab(false, TimeTaskConfig.SyncEntModel, func() {
  21. defer qutil.Catch()
  22. log.Println("开始同步订阅模式。。。")
  23. now := time.Now()
  24. today_unix := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, time.Local).Unix()
  25. list := Mysql.SelectBySql(`select * from entniche_entmodel where createtime<?`, today_unix)
  26. if list != nil {
  27. for _, v := range *list {
  28. entId := qutil.IntAll(v["ent_id"])
  29. ent := Mysql.SelectBySql(`select model from entniche_info where id=?`, entId)
  30. if ent == nil || len(*ent) == 0 {
  31. log.Println(entId, "同步订阅模式", "没有找到该企业")
  32. continue
  33. }
  34. model := qutil.IntAll((*ent)[0]["model"])
  35. newModel := qutil.IntAll(v["model"])
  36. //由统一订阅变成个人订阅
  37. if model == 1 && newModel == 2 {
  38. log.Println(entId, "同步订阅模式", "新的订阅模式", newModel, "旧的订阅模式", model)
  39. del_ent_customer := Mysql.SelectBySql(`select * from entniche_customer where ent_id=? and state=1`, entId)
  40. 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)
  41. 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)
  42. 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)
  43. if Mysql.UpdateOrDeleteBySql(`delete from entniche_customer where ent_id=? and state=1`, entId) > 0 {
  44. VarBackup.Save(Entniche_customer, del_ent_customer)
  45. VarBackup.Save(Entniche_customer_contact, del_ent_customer_contact)
  46. VarBackup.Save(Entniche_project, del_ent_project)
  47. VarBackup.Save(Entniche_project_track, del_ent_project_track)
  48. }
  49. }
  50. }
  51. }
  52. Mysql.UpdateOrDeleteBySql(`delete from entniche_entmodel where createtime<?`, today_unix)
  53. log.Println("同步订阅模式结束。。。")
  54. })
  55. }
  56. func crontab(flag bool, c string, f func()) {
  57. array := strings.Split(c, ":")
  58. if len(array) != 2 {
  59. log.Fatalln("定时任务参数错误!", c)
  60. }
  61. if flag {
  62. go f()
  63. }
  64. now := time.Now()
  65. t := time.Date(now.Year(), now.Month(), now.Day(), qutil.IntAll(array[0]), qutil.IntAll(array[1]), 0, 0, time.Local)
  66. if t.Before(now) {
  67. t = t.AddDate(0, 0, 1)
  68. }
  69. timer := time.NewTimer(t.Sub(now))
  70. for {
  71. select {
  72. case <-timer.C:
  73. go f()
  74. timer.Reset(24 * time.Hour)
  75. }
  76. }
  77. }
  78. //每天0点更新用户每日导出剩余量
  79. func updateLimit() {
  80. crontab(false, TimeTaskConfig.QuotaUpdateTime, func() {
  81. log.Println("更新用户限额表定时任务。。。")
  82. isOk := Mysql.UpdateOrDeleteBySql("UPDATE entniche_export_limit SET remain_nums = data_limit,export_nums = 0")
  83. if isOk > -1 {
  84. log.Println("更新用户限额表定时结束。。。")
  85. } else {
  86. log.Println("更新用户限额表出错。。。")
  87. }
  88. })
  89. }