feat: 合并Gin_Vue
This commit is contained in:
29
src/modules/monitor/repository/sys_job.go
Normal file
29
src/modules/monitor/repository/sys_job.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// ISysJob 调度任务表 数据层接口
|
||||
type ISysJob interface {
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
SelectJobPage(query map[string]any) map[string]any
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
SelectJobList(sysJob model.SysJob) []model.SysJob
|
||||
|
||||
// SelectJobByIds 通过调度ID查询调度任务信息
|
||||
SelectJobByIds(jobIds []string) []model.SysJob
|
||||
|
||||
// CheckUniqueJob 校验调度任务是否唯一
|
||||
CheckUniqueJob(sysJob model.SysJob) string
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
InsertJob(sysJob model.SysJob) string
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
UpdateJob(sysJob model.SysJob) int64
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
DeleteJobByIds(jobIds []string) int64
|
||||
}
|
||||
344
src/modules/monitor/repository/sys_job.impl.go
Normal file
344
src/modules/monitor/repository/sys_job.impl.go
Normal file
@@ -0,0 +1,344 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysJobImpl 结构体
|
||||
var NewSysJobImpl = &SysJobImpl{
|
||||
selectSql: `select job_id, job_name, job_group, invoke_target, target_params, cron_expression,
|
||||
misfire_policy, concurrent, status, create_by, create_time, remark from sys_job`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"job_id": "JobID",
|
||||
"job_name": "JobName",
|
||||
"job_group": "JobGroup",
|
||||
"invoke_target": "InvokeTarget",
|
||||
"target_params": "TargetParams",
|
||||
"cron_expression": "CronExpression",
|
||||
"misfire_policy": "MisfirePolicy",
|
||||
"concurrent": "Concurrent",
|
||||
"status": "Status",
|
||||
"create_by": "CreateBy",
|
||||
"create_time": "CreateTime",
|
||||
"update_by": "UpdateBy",
|
||||
"update_time": "UpdateTime",
|
||||
"remark": "Remark",
|
||||
},
|
||||
}
|
||||
|
||||
// SysJobImpl 调度任务表 数据层处理
|
||||
type SysJobImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysJobImpl) convertResultRows(rows []map[string]any) []model.SysJob {
|
||||
arr := make([]model.SysJob, 0)
|
||||
for _, row := range rows {
|
||||
sysJob := model.SysJob{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysJob, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysJob)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// SelectJobPage 分页查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["jobName"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["jobGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["invokeTarget"]; ok && v != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysJob{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_job"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// SelectJobList 查询调度任务集合
|
||||
func (r *SysJobImpl) SelectJobList(sysJob model.SysJob) []model.SysJob {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJob.JobName != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, sysJob.JobName)
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJob.JobGroup)
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, sysJob.InvokeTarget)
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysJob.Status)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJob{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// SelectJobByIds 通过调度ID查询调度任务信息
|
||||
func (r *SysJobImpl) SelectJobByIds(jobIds []string) []model.SysJob {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobIds))
|
||||
querySql := r.selectSql + " where job_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobIds)
|
||||
results, err := datasource.RawDB("", querySql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJob{}
|
||||
}
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// CheckUniqueJob 校验调度任务是否唯一
|
||||
func (r *SysJobImpl) CheckUniqueJob(sysJob model.SysJob) string {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJob.JobName != "" {
|
||||
conditions = append(conditions, "job_name = ?")
|
||||
params = append(params, sysJob.JobName)
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJob.JobGroup)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := "select job_id as 'str' from sys_job " + whereSql + " limit 1"
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err %v", err)
|
||||
return ""
|
||||
}
|
||||
if len(results) > 0 {
|
||||
return fmt.Sprintf("%v", results[0]["str"])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// InsertJob 新增调度任务信息
|
||||
func (r *SysJobImpl) InsertJob(sysJob model.SysJob) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysJob.JobID != "" {
|
||||
params["job_id"] = sysJob.JobID
|
||||
}
|
||||
if sysJob.JobName != "" {
|
||||
params["job_name"] = sysJob.JobName
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
params["job_group"] = sysJob.JobGroup
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJob.InvokeTarget
|
||||
}
|
||||
if sysJob.TargetParams != "" {
|
||||
params["target_params"] = sysJob.TargetParams
|
||||
}
|
||||
if sysJob.CronExpression != "" {
|
||||
params["cron_expression"] = sysJob.CronExpression
|
||||
}
|
||||
if sysJob.MisfirePolicy != "" {
|
||||
params["misfire_policy"] = sysJob.MisfirePolicy
|
||||
}
|
||||
if sysJob.Concurrent != "" {
|
||||
params["concurrent"] = sysJob.Concurrent
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
if sysJob.CreateBy != "" {
|
||||
params["create_by"] = sysJob.CreateBy
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_job (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// UpdateJob 修改调度任务信息
|
||||
func (r *SysJobImpl) UpdateJob(sysJob model.SysJob) int64 {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
if sysJob.JobName != "" {
|
||||
params["job_name"] = sysJob.JobName
|
||||
}
|
||||
if sysJob.JobGroup != "" {
|
||||
params["job_group"] = sysJob.JobGroup
|
||||
}
|
||||
if sysJob.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJob.InvokeTarget
|
||||
}
|
||||
if sysJob.TargetParams != "" {
|
||||
params["target_params"] = sysJob.TargetParams
|
||||
}
|
||||
if sysJob.CronExpression != "" {
|
||||
params["cron_expression"] = sysJob.CronExpression
|
||||
}
|
||||
if sysJob.MisfirePolicy != "" {
|
||||
params["misfire_policy"] = sysJob.MisfirePolicy
|
||||
}
|
||||
if sysJob.Concurrent != "" {
|
||||
params["concurrent"] = sysJob.Concurrent
|
||||
}
|
||||
if sysJob.Status != "" {
|
||||
params["status"] = sysJob.Status
|
||||
}
|
||||
if sysJob.Remark != "" {
|
||||
params["remark"] = sysJob.Remark
|
||||
}
|
||||
if sysJob.UpdateBy != "" {
|
||||
params["update_by"] = sysJob.UpdateBy
|
||||
params["update_time"] = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, values := repo.KeyValueByUpdate(params)
|
||||
sql := "update sys_job set " + strings.Join(keys, ",") + " where job_id = ?"
|
||||
|
||||
// 执行更新
|
||||
values = append(values, sysJob.JobID)
|
||||
rows, err := datasource.ExecDB("", sql, values)
|
||||
if err != nil {
|
||||
logger.Errorf("update row : %v", err.Error())
|
||||
return 0
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// DeleteJobByIds 批量删除调度任务信息
|
||||
func (r *SysJobImpl) DeleteJobByIds(jobIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobIds))
|
||||
sql := "delete from sys_job where job_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
26
src/modules/monitor/repository/sys_job_log.go
Normal file
26
src/modules/monitor/repository/sys_job_log.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 调度任务日志表 数据层接口
|
||||
type ISysJobLog interface {
|
||||
// 分页查询调度任务日志集合
|
||||
SelectJobLogPage(query map[string]any) map[string]any
|
||||
|
||||
// 查询调度任务日志集合
|
||||
SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog
|
||||
|
||||
// 通过调度ID查询调度任务日志信息
|
||||
SelectJobLogById(jobLogId string) model.SysJobLog
|
||||
|
||||
// 新增调度任务日志信息
|
||||
InsertJobLog(sysJobLog model.SysJobLog) string
|
||||
|
||||
// 批量删除调度任务日志信息
|
||||
DeleteJobLogByIds(jobLogId []string) int64
|
||||
|
||||
// 清空调度任务日志
|
||||
CleanJobLog() error
|
||||
}
|
||||
272
src/modules/monitor/repository/sys_job_log.impl.go
Normal file
272
src/modules/monitor/repository/sys_job_log.impl.go
Normal file
@@ -0,0 +1,272 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"ems.agt/src/framework/datasource"
|
||||
"ems.agt/src/framework/logger"
|
||||
"ems.agt/src/framework/utils/date"
|
||||
"ems.agt/src/framework/utils/parse"
|
||||
"ems.agt/src/framework/utils/repo"
|
||||
"ems.agt/src/modules/monitor/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 SysJobLogImpl 结构体
|
||||
var NewSysJobLogImpl = &SysJobLogImpl{
|
||||
selectSql: `select job_log_id, job_name, job_group, invoke_target,
|
||||
target_params, job_msg, status, create_time, cost_time from sys_job_log`,
|
||||
|
||||
resultMap: map[string]string{
|
||||
"job_log_id": "JobLogID",
|
||||
"job_name": "JobName",
|
||||
"job_group": "JobGroup",
|
||||
"invoke_target": "InvokeTarget",
|
||||
"target_params": "TargetParams",
|
||||
"job_msg": "JobMsg",
|
||||
"status": "Status",
|
||||
"create_time": "CreateTime",
|
||||
"cost_time": "CostTime",
|
||||
},
|
||||
}
|
||||
|
||||
// SysJobLogImpl 调度任务日志表 数据层处理
|
||||
type SysJobLogImpl struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
|
||||
// convertResultRows 将结果记录转实体结果组
|
||||
func (r *SysJobLogImpl) convertResultRows(rows []map[string]any) []model.SysJobLog {
|
||||
arr := make([]model.SysJobLog, 0)
|
||||
for _, row := range rows {
|
||||
sysJobLog := model.SysJobLog{}
|
||||
for key, value := range row {
|
||||
if keyMapper, ok := r.resultMap[key]; ok {
|
||||
repo.SetFieldValue(&sysJobLog, keyMapper, value)
|
||||
}
|
||||
}
|
||||
arr = append(arr, sysJobLog)
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// 分页查询调度任务日志集合
|
||||
func (r *SysJobLogImpl) SelectJobLogPage(query map[string]any) map[string]any {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if v, ok := query["jobName"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["jobGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["status"]; ok && v != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, v)
|
||||
}
|
||||
if v, ok := query["invokeTarget"]; ok && v != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, v)
|
||||
}
|
||||
beginTime, ok := query["beginTime"]
|
||||
if !ok {
|
||||
beginTime, ok = query["params[beginTime]"]
|
||||
}
|
||||
if ok && beginTime != "" {
|
||||
conditions = append(conditions, "create_time >= ?")
|
||||
beginDate := date.ParseStrToDate(beginTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, beginDate.UnixMilli())
|
||||
}
|
||||
endTime, ok := query["endTime"]
|
||||
if !ok {
|
||||
endTime, ok = query["params[endTime]"]
|
||||
}
|
||||
if ok && endTime != "" {
|
||||
conditions = append(conditions, "create_time <= ?")
|
||||
endDate := date.ParseStrToDate(endTime.(string), date.YYYY_MM_DD)
|
||||
params = append(params, endDate.UnixMilli())
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询结果
|
||||
result := map[string]any{
|
||||
"total": 0,
|
||||
"rows": []model.SysJobLog{},
|
||||
}
|
||||
|
||||
// 查询数量 长度为0直接返回
|
||||
totalSql := "select count(1) as 'total' from sys_job_log"
|
||||
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
}
|
||||
total := parse.Number(totalRows[0]["total"])
|
||||
if total == 0 {
|
||||
return result
|
||||
} else {
|
||||
result["total"] = total
|
||||
}
|
||||
|
||||
// 分页
|
||||
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
pageSql := " order by job_log_id desc limit ?,? "
|
||||
params = append(params, pageNum*pageSize)
|
||||
params = append(params, pageSize)
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + pageSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return result
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
result["rows"] = r.convertResultRows(results)
|
||||
return result
|
||||
}
|
||||
|
||||
// 查询调度任务日志集合
|
||||
func (r *SysJobLogImpl) SelectJobLogList(sysJobLog model.SysJobLog) []model.SysJobLog {
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if sysJobLog.JobName != "" {
|
||||
conditions = append(conditions, "job_name like concat(?, '%')")
|
||||
params = append(params, sysJobLog.JobName)
|
||||
}
|
||||
if sysJobLog.JobGroup != "" {
|
||||
conditions = append(conditions, "job_group = ?")
|
||||
params = append(params, sysJobLog.JobGroup)
|
||||
}
|
||||
if sysJobLog.Status != "" {
|
||||
conditions = append(conditions, "status = ?")
|
||||
params = append(params, sysJobLog.Status)
|
||||
}
|
||||
if sysJobLog.InvokeTarget != "" {
|
||||
conditions = append(conditions, "invoke_target like concat(?, '%')")
|
||||
params = append(params, sysJobLog.InvokeTarget)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return []model.SysJobLog{}
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
}
|
||||
|
||||
// 通过调度ID查询调度任务日志信息
|
||||
func (r *SysJobLogImpl) SelectJobLogById(jobLogId string) model.SysJobLog {
|
||||
querySql := r.selectSql + " where job_log_id = ?"
|
||||
results, err := datasource.RawDB("", querySql, []any{jobLogId})
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
return model.SysJobLog{}
|
||||
}
|
||||
// 转换实体
|
||||
rows := r.convertResultRows(results)
|
||||
if len(rows) > 0 {
|
||||
return rows[0]
|
||||
}
|
||||
return model.SysJobLog{}
|
||||
}
|
||||
|
||||
// 新增调度任务日志信息
|
||||
func (r *SysJobLogImpl) InsertJobLog(sysJobLog model.SysJobLog) string {
|
||||
// 参数拼接
|
||||
params := make(map[string]any)
|
||||
params["create_time"] = time.Now().UnixMilli()
|
||||
if sysJobLog.JobLogID != "" {
|
||||
params["job_log_id"] = sysJobLog.JobLogID
|
||||
}
|
||||
if sysJobLog.JobName != "" {
|
||||
params["job_name"] = sysJobLog.JobName
|
||||
}
|
||||
if sysJobLog.JobGroup != "" {
|
||||
params["job_group"] = sysJobLog.JobGroup
|
||||
}
|
||||
if sysJobLog.InvokeTarget != "" {
|
||||
params["invoke_target"] = sysJobLog.InvokeTarget
|
||||
}
|
||||
if sysJobLog.TargetParams != "" {
|
||||
params["target_params"] = sysJobLog.TargetParams
|
||||
}
|
||||
if sysJobLog.JobMsg != "" {
|
||||
params["job_msg"] = sysJobLog.JobMsg
|
||||
}
|
||||
if sysJobLog.Status != "" {
|
||||
params["status"] = sysJobLog.Status
|
||||
}
|
||||
if sysJobLog.CostTime > 0 {
|
||||
params["cost_time"] = sysJobLog.CostTime
|
||||
}
|
||||
|
||||
// 构建执行语句
|
||||
keys, placeholder, values := repo.KeyPlaceholderValueByInsert(params)
|
||||
sql := "insert into sys_job_log (" + strings.Join(keys, ",") + ")values(" + placeholder + ")"
|
||||
|
||||
db := datasource.DefaultDB()
|
||||
// 开启事务
|
||||
tx := db.Begin()
|
||||
// 执行插入
|
||||
err := tx.Exec(sql, values...).Error
|
||||
if err != nil {
|
||||
logger.Errorf("insert row : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 获取生成的自增 ID
|
||||
var insertedID string
|
||||
err = tx.Raw("select last_insert_id()").Row().Scan(&insertedID)
|
||||
if err != nil {
|
||||
logger.Errorf("insert last id : %v", err.Error())
|
||||
tx.Rollback()
|
||||
return ""
|
||||
}
|
||||
// 提交事务
|
||||
tx.Commit()
|
||||
return insertedID
|
||||
}
|
||||
|
||||
// 批量删除调度任务日志信息
|
||||
func (r *SysJobLogImpl) DeleteJobLogByIds(jobLogIds []string) int64 {
|
||||
placeholder := repo.KeyPlaceholderByQuery(len(jobLogIds))
|
||||
sql := "delete from sys_job_log where job_log_id in (" + placeholder + ")"
|
||||
parameters := repo.ConvertIdsSlice(jobLogIds)
|
||||
results, err := datasource.ExecDB("", sql, parameters)
|
||||
if err != nil {
|
||||
logger.Errorf("delete err => %v", err)
|
||||
return 0
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// 清空调度任务日志
|
||||
func (r *SysJobLogImpl) CleanJobLog() error {
|
||||
sql := "truncate table sys_job_log"
|
||||
_, err := datasource.ExecDB("", sql, []any{})
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user