|
@@ -1,19 +1,23 @@
|
|
|
package task
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "context"
|
|
|
"errors"
|
|
"errors"
|
|
|
|
|
+ "fmt"
|
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm"
|
|
|
"log-server/global"
|
|
"log-server/global"
|
|
|
"log-server/model/common/request"
|
|
"log-server/model/common/request"
|
|
|
"log-server/model/task"
|
|
"log-server/model/task"
|
|
|
|
|
+ "log-server/service/cache"
|
|
|
|
|
+ "strings"
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type UrgentTaskService struct {
|
|
type UrgentTaskService struct {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (s *UrgentTaskService) CreateUrgentTask(task1 task.UrgentTaskConf) (err error) {
|
|
|
|
|
|
|
+func (s *UrgentTaskService) CreateUrgentTask(task1 task.UrgentTaskConf) (err error) {
|
|
|
//根据平均期望,将故障较大的放前面可以减少查询次数。那么需要先查是否有task_id以及create_date相等的
|
|
//根据平均期望,将故障较大的放前面可以减少查询次数。那么需要先查是否有task_id以及create_date相等的
|
|
|
var urgentEntity task.UrgentTaskConf
|
|
var urgentEntity task.UrgentTaskConf
|
|
|
var centralEntity task.CentralControlConf
|
|
var centralEntity task.CentralControlConf
|
|
@@ -30,7 +34,17 @@ func (s *UrgentTaskService) CreateUrgentTask(task1 task.UrgentTaskConf) (err err
|
|
|
return errors.New("请先添加此任务中控配置")
|
|
return errors.New("请先添加此任务中控配置")
|
|
|
}
|
|
}
|
|
|
//创建紧急任务
|
|
//创建紧急任务
|
|
|
- return global.GVA_DB.Model(&task.UrgentTaskConf{}).Omit("create_time", "update_time").Create(&task1).Error
|
|
|
|
|
|
|
+ err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Omit("create_time", "update_time").Create(&task1).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ codeSplit := strings.Split(task1.PcCode, ",")
|
|
|
|
|
+ ctx := context.Background()
|
|
|
|
|
+ for _, code := range codeSplit {
|
|
|
|
|
+ key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, code)
|
|
|
|
|
+ global.GVA_REDIS.Set(ctx, key, task1.TaskId, time.Hour*6)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (s *UrgentTaskService) DeleteUrgentTask(task1 task.UrgentTaskConf) (err error) {
|
|
func (s *UrgentTaskService) DeleteUrgentTask(task1 task.UrgentTaskConf) (err error) {
|
|
@@ -40,11 +54,22 @@ func (s *UrgentTaskService) DeleteUrgentTask(task1 task.UrgentTaskConf) (err err
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
return errors.New("此任务不存在")
|
|
return errors.New("此任务不存在")
|
|
|
}
|
|
}
|
|
|
- return global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete(&entity).Error
|
|
|
|
|
|
|
+ err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete(&entity).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ codeSplit := strings.Split(entity.PcCode, ",")
|
|
|
|
|
+ date := entity.CreateDate[:10]
|
|
|
|
|
+ ctx := context.Background()
|
|
|
|
|
+ for _, code := range codeSplit {
|
|
|
|
|
+ key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, code)
|
|
|
|
|
+ global.GVA_REDIS.Del(ctx, key)
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//批量删除
|
|
//批量删除
|
|
|
-func (s *UrgentTaskService) DeleteUrgentTaskByIds(ids []int) (err error){
|
|
|
|
|
|
|
+func (s *UrgentTaskService) DeleteUrgentTaskByIds(ids []int) (err error) {
|
|
|
err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete("id in ?", ids).Error
|
|
err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Delete("id in ?", ids).Error
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
@@ -53,10 +78,28 @@ func (s *UrgentTaskService) UpdateUrgentTask(task1 task.UrgentTaskConf) (err err
|
|
|
//查找此task_id是否存在,如果存在不更新,否则更新
|
|
//查找此task_id是否存在,如果存在不更新,否则更新
|
|
|
var entity task.UrgentTaskConf
|
|
var entity task.UrgentTaskConf
|
|
|
err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("task_id = ? and id != ? and create_date = ?", task1.TaskId, task1.Id, task1.CreateDate).First(&entity).Error
|
|
err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("task_id = ? and id != ? and create_date = ?", task1.TaskId, task1.Id, task1.CreateDate).First(&entity).Error
|
|
|
- if !errors.Is(err , gorm.ErrRecordNotFound) {
|
|
|
|
|
|
|
+ if !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
return errors.New("已存在此task_id的任务,更新失败")
|
|
return errors.New("已存在此task_id的任务,更新失败")
|
|
|
}
|
|
}
|
|
|
- return global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("id", task1.Id).Updates(&task1).Error
|
|
|
|
|
|
|
+ err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("id", task1.Id).Updates(&task1).Error
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return err
|
|
|
|
|
+ }
|
|
|
|
|
+ date := entity.CreateDate[:10]
|
|
|
|
|
+ if entity.PcCode != task1.PcCode {
|
|
|
|
|
+ codeSplit := strings.Split(entity.PcCode, ",")
|
|
|
|
|
+ ctx := context.Background()
|
|
|
|
|
+ for _, code := range codeSplit {
|
|
|
|
|
+ key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, code)
|
|
|
|
|
+ global.GVA_REDIS.Del(ctx, key)
|
|
|
|
|
+ }
|
|
|
|
|
+ upCodeSplit := strings.Split(task1.PcCode, ",")
|
|
|
|
|
+ for _, upCode := range upCodeSplit {
|
|
|
|
|
+ key := fmt.Sprintf(cache.TemporaryTaskPcCode, date, upCode)
|
|
|
|
|
+ global.GVA_REDIS.Set(ctx, key, task1.TaskId, time.Hour*6)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (s *UrgentTaskService) GetUrgentTaskById(id int) (task1 task.UrgentTaskConf, err error) {
|
|
func (s *UrgentTaskService) GetUrgentTaskById(id int) (task1 task.UrgentTaskConf, err error) {
|
|
@@ -65,15 +108,33 @@ func (s *UrgentTaskService) GetUrgentTaskById(id int) (task1 task.UrgentTaskConf
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//查询空闲租机
|
|
//查询空闲租机
|
|
|
-func (s *UrgentTaskService) GetUnusedPc() (unusedPcList []task.UnusedPc, err error) {
|
|
|
|
|
|
|
+func (s *UrgentTaskService) GetUnusedPc() (unusedPcList []task.UnusedPc, err error) {
|
|
|
//获取当日日期
|
|
//获取当日日期
|
|
|
date := time.Now().Format("2006-01-02")
|
|
date := time.Now().Format("2006-01-02")
|
|
|
- err = global.GVA_DB.Table("computer_status").Where("status = ? and create_date = ?", -1, date).Order("pc_code").Find(&unusedPcList).Error
|
|
|
|
|
|
|
+ db := global.GVA_DB.Table("computer_status").Where("status = ? and create_date = ?", -1, date)
|
|
|
|
|
+ pcCodes, err := s.GetTemporaryTaskId(date)
|
|
|
|
|
+ if pcCodes != "" {
|
|
|
|
|
+ db.Not("pc_code", strings.Split(pcCodes, ","))
|
|
|
|
|
+ }
|
|
|
|
|
+ err = db.Order("pc_code").Find(&unusedPcList).Error
|
|
|
return unusedPcList, err
|
|
return unusedPcList, err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+func (s *UrgentTaskService) GetTemporaryTaskId(date string) (pcCodes string, err error) {
|
|
|
|
|
+ var urgentTaskConf []task.UrgentTaskConf
|
|
|
|
|
+ err = global.GVA_DB.Model(&task.UrgentTaskConf{}).Where("create_date = ?", date).Find(&urgentTaskConf).Error
|
|
|
|
|
+ if len(urgentTaskConf) == 0 {
|
|
|
|
|
+ return pcCodes, errors.New("没有任务数据")
|
|
|
|
|
+ }
|
|
|
|
|
+ for _, conf := range urgentTaskConf {
|
|
|
|
|
+ pcCodes += conf.PcCode + ","
|
|
|
|
|
+ }
|
|
|
|
|
+ pcCodes = strings.Trim(pcCodes, ",")
|
|
|
|
|
+ return
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
//获取紧急任务列表
|
|
//获取紧急任务列表
|
|
|
-func (s *UrgentTaskService) GetUrgentTaskList(conf task.UrgentTaskConfRequest, info request.PageInfo, order string, desc bool) (confList []task.UrgentTaskConf, total int64, err error){
|
|
|
|
|
|
|
+func (s *UrgentTaskService) GetUrgentTaskList(conf task.UrgentTaskConfRequest, info request.PageInfo, order string, desc bool) (confList []task.UrgentTaskConf, total int64, err error) {
|
|
|
limit := info.PageSize
|
|
limit := info.PageSize
|
|
|
offset := info.PageSize * (info.Page - 1)
|
|
offset := info.PageSize * (info.Page - 1)
|
|
|
db := global.GVA_DB.Model(&task.UrgentTaskConf{})
|
|
db := global.GVA_DB.Model(&task.UrgentTaskConf{})
|
|
@@ -122,4 +183,4 @@ func (s *UrgentTaskService) GetUrgentTaskList(conf task.UrgentTaskConfRequest, i
|
|
|
confList[i].CreateDate = confList[i].CreateDate[:10]
|
|
confList[i].CreateDate = confList[i].CreateDate[:10]
|
|
|
}
|
|
}
|
|
|
return confList, total, err
|
|
return confList, total, err
|
|
|
-}
|
|
|
|
|
|
|
+}
|