package service import ( "encoding/json" "fmt" "github.com/pkg/errors" "github.com/spf13/cast" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/resp" "go.uber.org/zap" "strconv" "time" ) type TaskHistorySvc struct { User *entity.SystemUser } // TaskHistoryList 任务历史列表 func (t *TaskHistorySvc) TaskHistoryList(req request.TaskHistoryReq) (total int64, taskHistoryListRes []response.TaskHistoryListRes, err error) { var ( //dueDate int start time.Time end time.Time ) db, err := client.GetDbClient() if err != nil { err = resp.DbConnectError.WithError(err) return } finder := db.Table("task_manage").Alias("t1"). Join("RIGHT", "(SELECT task_id,exec_start_time,exec_end_time,create_user,state FROM task_history"+ " WHERE ID IN (SELECT MAX(ID) FROM task_history GROUP BY task_id) ORDER BY ID DESC) t2", "t1.id = t2.task_id"). Where("t1.is_delete = 0") if req.Search != "" { finder.Where("t1.task_name LIKE ?", "%"+req.Search+"%") } //查询任务历史 total, err = finder.Select("t2.state,t2.task_id,t1.task_name,t2.exec_start_time,t2.exec_end_time,t2.create_user"). OrderBy("t2.exec_start_time desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskHistoryListRes) if err != nil { err = resp.DbSelectError.WithError(err) return } for k, v := range taskHistoryListRes { //获取执行耗时 start, err = time.Parse("2006-01-02 15:04:05", v.ExecStartTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } end, err = time.Parse("2006-01-02 15:04:05", v.ExecEndTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } execTime := int(end.Sub(start).Seconds()) if execTime > 0 { taskHistoryListRes[k].ExecTime = execTime } } return } // TaskInfoList 任务历史详情列表 func (t *TaskHistorySvc) TaskInfoList(req request.TaskInfoListReq) (total int64, taskInfoListRes []response.TaskInfoListRes, err error) { var ( //dueDate int start time.Time end time.Time ) db, err := client.GetDbClient() if err != nil { err = resp.DbConnectError.WithError(err) return } finder := db.Table("task_history").Where("task_id = ?", req.TaskId) if req.Search != "" { finder.Where("exec_desc LIKE ?", "%"+req.Search+"%") } //查询任务历史 total, err = finder.OrderBy("id desc").Limit(req.PageSize, (req.Page-1)*req.PageSize).FindAndCount(&taskInfoListRes) if err != nil { err = resp.DbSelectError.WithError(err) return } for k, v := range taskInfoListRes { //获取执行耗时 start, err = time.Parse("2006-01-02 15:04:05", v.ExecStartTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } end, err = time.Parse("2006-01-02 15:04:05", v.ExecEndTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } execTime := int(end.Sub(start).Seconds()) if execTime > 0 { taskInfoListRes[k].ExecTime = execTime } } return } // TaskExecLog 任务执行日志 func (t *TaskHistorySvc) TaskExecLog(id int) (taskExecLogRes response.TaskExecLogRes, err error) { var ( //dueDate int start time.Time end time.Time ) db, err := client.GetDbClient() if err != nil { conf.Logger.Error("db err", zap.Error(err)) err = resp.DbConnectError.WithError(err) return } redis, err := client.GetRedisClient() if err != nil { conf.Logger.Error("redis err", zap.Error(err)) err = resp.RedisConnectError.ErrorDetail(err) return } //判断key值是否存在 t1, err := redis.HExists(conf.TaskExecLog, cast.ToString(id)) if err != nil { conf.Logger.Error("redis TaskExecLog HExists err", zap.Error(err)) err = resp.RedisExecError.ErrorDetail(err) } if t1 { //获取key value, err1 := redis.HGet(conf.TaskExecLog, cast.ToString(id)) if err1 != nil { conf.Logger.Error("redis TaskExecLog HGet err", zap.Error(err1)) err = resp.RedisExecError.ErrorDetail(err1) return } //解析key err = json.Unmarshal([]byte(value), &taskExecLogRes) if err != nil { conf.Logger.Error("redis value Unmarshal err", zap.Error(err)) err = resp.FAIL.WithError(err) return } } else { //查询 _, err = db.Table("task_history").Alias("th").Where("id = ?", id). Select("th.id,th.task_id,th.exec_desc,(select tm.task_name from task_manage tm where th.task_id = tm.id and tm.is_delete = 0) as task_name," + "th.exec_start_time,th.exec_end_time,th.state,th.exec_log").Get(&taskExecLogRes) if err != nil { err = resp.DbSelectError.WithError(err) return } taskExecLog, errMarshal := json.Marshal(taskExecLogRes) if errMarshal != nil { conf.Logger.Error("Marshal taskExecLog err", zap.Error(errMarshal)) err = resp.FAIL.WithError(errMarshal) return } // 写缓存 err = redis.HSet(conf.TaskExecLog, strconv.Itoa(id), fmt.Sprintf("%s", taskExecLog)) if err != nil { conf.Logger.Error("Failed to execute history cache err", zap.Error(err)) err = resp.FAIL.WithError(err) return } } if taskExecLogRes.State == 0 { //判断key值是否存在 b1, err1 := redis.HExists(conf.AutoExecHistory, cast.ToString(id)) if err1 != nil { conf.Logger.Error("redis HExists err", zap.Error(err1)) err = resp.RedisExecError.ErrorDetail(err1) } if !b1 { return } //获取key value, err2 := redis.HGet(conf.AutoExecHistory, cast.ToString(id)) if err2 != nil { conf.Logger.Error("redis HGet err", zap.Error(err2)) err = resp.RedisExecError.ErrorDetail(err2) return } //解析key var execCache ExecCache err = json.Unmarshal([]byte(value), &execCache) if err != nil { conf.Logger.Error("redis value Unmarshal err", zap.Error(err)) err = resp.FAIL.WithError(err) return } taskExecLogRes.ExecLog = execCache.ExecLog taskExecLogRes.ExecEndTime = execCache.ExecEndTime } //获取执行耗时 start, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecStartTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } end, err = time.Parse("2006-01-02 15:04:05", taskExecLogRes.ExecEndTime.Format("2006-01-02 15:04:05")) if err != nil { err = resp.FAIL.WithError(errors.Wrap(err, "时间转换错误!")) return } execTime := int(end.Sub(start).Seconds()) if execTime > 0 { taskExecLogRes.ExecTime = execTime } return } // AddExecHistory 新增执行历史 func AddExecHistory(req request.AddExecHistory) (id int, err error) { db, err := client.GetDbClient() if err != nil { err = resp.DbConnectError.WithError(err) return } count, err := db.Table("task_history").Where("task_id = ?", req.TaskId).Count(&entity.TaskHistory{}) if err != nil { err = resp.DbSelectError.WithError(err) return } //新增 taskHistory := entity.TaskHistory{ TaskId: req.TaskId, ExecStartTime: time.Now(), CreateUser: req.CreateUser, ExecDesc: "执行说明" + fmt.Sprintf("%v", count+1), State: 0, } _, err = db.Table("task_history").Insert(&taskHistory) if err != nil { err = resp.DbInsertError.WithError(err) return } return taskHistory.Id, nil } // UpdateExecHistory 修改执行历史 func UpdateExecHistory(req request.UpdateExecHistory) (err error) { db, err := client.GetDbClient() if err != nil { err = resp.DbConnectError.WithError(err) return } _, err = db.Table("task_history").Where("id = ?", req.TaskHistoryId).Cols("state,exec_end_time,exec_log"). Update(&entity.TaskHistory{ State: req.State, ExecEndTime: time.Now(), ExecLog: req.ExecLog, }) if err != nil { err = resp.DbUpdateError.WithError(err) return } return }