urgent_task_conf.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package task
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go.uber.org/zap"
  7. "gorm.io/gorm"
  8. "log-server/global"
  9. "log-server/model/common/request"
  10. "log-server/model/task"
  11. "log-server/service/cache"
  12. "strings"
  13. "time"
  14. )
  15. type UrgentTaskService struct {
  16. }
  17. func (s *UrgentTaskService) CreateUrgentTask(task1 task.UrgentTaskConf) (err error) {
  18. //根据平均期望,将故障较大的放前面可以减少查询次数。那么需要先查是否有task_id以及create_date相等的
  19. var urgentEntity task.UrgentTaskConf
  20. var centralEntity task.CentralControlConf
  21. date := time.Now().Format("2006-01-02")
  22. task1.CreateDate = date
  23. task1.Url = strings.Trim(task1.Url, " ")
  24. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("task_id = ? and create_date = ?", task1.TaskId, date).First(&urgentEntity).Error
  25. if !errors.Is(err, gorm.ErrRecordNotFound) {
  26. return errors.New("任务已存在,请勿重复添加")
  27. }
  28. //查找中控配置是否存在
  29. err = global.GVA_DB.Model(&task.CentralControlConf{}).Where("task_id", task1.TaskId).First(&centralEntity).Error
  30. //若记录不存在,则报错返回
  31. if errors.Is(err, gorm.ErrRecordNotFound) {
  32. return errors.New("请先添加此任务中控配置")
  33. }
  34. //创建紧急任务
  35. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Omit("create_time", "update_time").Create(&task1).Error
  36. if err != nil {
  37. return err
  38. }
  39. codeSplit := strings.Split(task1.PcCode, ",")
  40. ctx := context.Background()
  41. for _, code := range codeSplit {
  42. key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, code)
  43. global.GVA_REDIS.Set(ctx, key, task1.TaskId, time.Hour*6)
  44. }
  45. return
  46. }
  47. func (s *UrgentTaskService) DeleteUrgentTask(task1 task.UrgentTaskConf) (err error) {
  48. //查找此任务是否存在
  49. var entity task.UrgentTaskConf
  50. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("id", task1.Id).First(&entity).Error
  51. if errors.Is(err, gorm.ErrRecordNotFound) {
  52. return errors.New("此任务不存在")
  53. }
  54. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete(&entity).Error
  55. if err != nil {
  56. return err
  57. }
  58. codeSplit := strings.Split(entity.PcCode, ",")
  59. date := entity.CreateDate[:10]
  60. ctx := context.Background()
  61. for _, code := range codeSplit {
  62. key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, code)
  63. global.GVA_REDIS.Del(ctx, key)
  64. }
  65. return
  66. }
  67. //批量删除
  68. func (s *UrgentTaskService) DeleteUrgentTaskByIds(ids []int) (err error) {
  69. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete("id in ?", ids).Error
  70. return err
  71. }
  72. func (s *UrgentTaskService) UpdateUrgentTask(task1 task.UrgentTaskConf, isDelPcCode int) (err error) {
  73. //查找此task_id是否存在,如果存在不更新,否则更新
  74. var entity task.UrgentTaskConf
  75. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("task_id = ? and id = ?", task1.TaskId, task1.Id).First(&entity).Error
  76. if errors.Is(err, gorm.ErrRecordNotFound) {
  77. return errors.New("没有找到数据")
  78. }
  79. upPcCode := ""
  80. if isDelPcCode != 1 {
  81. if task1.PcCode != "" {
  82. upPcCode = task1.PcCode
  83. task1.PcCode = entity.PcCode + "," + task1.PcCode
  84. }
  85. }
  86. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("id", task1.Id).Update("pc_code", task1.PcCode).Error
  87. if err != nil {
  88. return err
  89. }
  90. date := entity.CreateDate[:10]
  91. if upPcCode != "" {
  92. ctx := context.Background()
  93. upCodeSplit := strings.Split(upPcCode, ",")
  94. for _, upCode := range upCodeSplit {
  95. key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, upCode)
  96. global.GVA_REDIS.Set(ctx, key, task1.TaskId, time.Hour*6)
  97. }
  98. }
  99. return
  100. }
  101. func (s *UrgentTaskService) GetUrgentTaskById(id int) (task2 task.ByIdUrgentTaskConf, err error) {
  102. var task1 task.UrgentTaskConf
  103. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("id", id).First(&task1).Error
  104. task2.TaskId = task1.TaskId
  105. task2.Id = task1.Id
  106. task2.PcCode = strings.Split(task1.PcCode, ",")
  107. return task2, err
  108. }
  109. //查询空闲租机
  110. func (s *UrgentTaskService) GetUnusedPc() (unusedPcList []task.UnusedPc, err error) {
  111. //获取当日日期
  112. date := time.Now().Format("2006-01-02")
  113. // 放开状态为-1条件
  114. db := global.GVA_DB.Table("computer_status").Where("create_date = ?", date)
  115. pcCodes, err := s.GetTemporaryTaskId(date)
  116. if pcCodes != "" {
  117. db.Not("pc_code", strings.Split(pcCodes, ","))
  118. }
  119. err = db.Order("pc_code").Find(&unusedPcList).Error
  120. return unusedPcList, err
  121. }
  122. func (s *UrgentTaskService) GetTemporaryTaskId(date string) (pcCodes string, err error) {
  123. var urgentTaskConf []task.UrgentTaskConf
  124. err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("create_date = ?", date).Find(&urgentTaskConf).Error
  125. if len(urgentTaskConf) == 0 {
  126. return pcCodes, errors.New("没有任务数据")
  127. }
  128. for _, conf := range urgentTaskConf {
  129. pcCodes += conf.PcCode + ","
  130. }
  131. pcCodes = strings.Trim(pcCodes, ",")
  132. return
  133. }
  134. //获取紧急任务列表
  135. func (s *UrgentTaskService) GetUrgentTaskList(conf task.UrgentTaskConfRequest, info request.PageInfo, order string, desc bool) (confList []task.UrgentTaskConf, total int64, err error) {
  136. limit := info.PageSize
  137. offset := info.PageSize * (info.Page - 1)
  138. db := global.GVA_DB.Model(&task.UrgentTaskConf{})
  139. startDate := time.Now().Format("2006-01-02")
  140. endDate := time.Now().Format("2006-01-02")
  141. if len(conf.Date) == 2 {
  142. startDate = conf.Date[0]
  143. endDate = conf.Date[1]
  144. }
  145. //筛选日期
  146. db = db.Where("create_date >= ? and create_date <= ?", startDate, endDate)
  147. if conf.TaskId != 0 {
  148. db = db.Where("task_id = ?", conf.TaskId)
  149. }
  150. err = db.Count(&total).Error
  151. if err != nil {
  152. return confList, total, err
  153. } else {
  154. db = db.Limit(limit).Offset(offset)
  155. if order != "" {
  156. var OrderStr string
  157. // 设置有效排序key 防止sql注入
  158. // 感谢 Tom4t0 提交漏洞信息
  159. orderMap := make(map[string]bool, 4)
  160. orderMap["task_id"] = true
  161. orderMap["create_date"] = true
  162. orderMap["create_time"] = true
  163. orderMap["update_time"] = true
  164. if orderMap[order] {
  165. if desc {
  166. OrderStr = order + " desc"
  167. } else {
  168. OrderStr = order
  169. }
  170. } else { // didn't matched any order key in `orderMap`
  171. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  172. return confList, total, err
  173. }
  174. err = db.Order(OrderStr).Find(&confList).Error
  175. } else {
  176. err = db.Order("id desc").Find(&confList).Error
  177. }
  178. }
  179. //遍历更改日期格式
  180. for i, _ := range confList {
  181. confList[i].CreateDate = confList[i].CreateDate[:10]
  182. }
  183. return confList, total, err
  184. }