feat: 调度任务多语言

This commit is contained in:
TsMask
2023-11-20 18:54:15 +08:00
parent 506866e082
commit 084a7b3c93
3 changed files with 120 additions and 62 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"time"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/file"
"ems.agt/src/framework/utils/parse"
@@ -41,6 +42,18 @@ type SysJobController struct {
func (s *SysJobController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.sysJobService.SelectJobPage(querys)
rows := data["rows"].([]model.SysJob)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
converI18n := func(language string, arr *[]model.SysJob) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
@@ -48,14 +61,18 @@ func (s *SysJobController) List(c *gin.Context) {
//
// GET /:jobId
func (s *SysJobController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobId := c.Param("jobId")
if jobId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysJobService.SelectJobById(jobId)
if data.JobID == jobId {
// 处理多语言
data.JobName = i18n.TKey(language, data.JobName)
data.Remark = i18n.TKey(language, data.Remark)
c.JSON(200, result.OkData(data))
return
}
@@ -66,17 +83,18 @@ func (s *SysJobController) Info(c *gin.Context) {
//
// POST /
func (s *SysJobController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysJob
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.JobID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务新增【%s】失败Cron表达式不正确
msg := fmt.Sprintf("Scheduling task add [%s] fails with incorrect Cron expression", body.JobName)
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -84,7 +102,7 @@ func (s *SysJobController) Add(c *gin.Context) {
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务新增【%s】失败任务传入参数json字符串不正确
msg := fmt.Sprintf("Scheduling task add [%s] failed, task incoming parameter json string is incorrect", body.JobName)
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
@@ -99,7 +117,7 @@ func (s *SysJobController) Add(c *gin.Context) {
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, "")
if !uniqueJob {
// 调度任务新增【%s】失败同任务组内有相同任务名称
msg := fmt.Sprintf("Scheduling tasks with new [%s] failures, with the same task name in the same task group", body.JobName)
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -117,17 +135,18 @@ func (s *SysJobController) Add(c *gin.Context) {
//
// PUT /
func (s *SysJobController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysJob
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.JobID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务修改【%s】失败Cron表达式不正确
msg := fmt.Sprintf("Scheduling task modification [%s] fails with incorrect Cron expression", body.JobName)
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -135,7 +154,7 @@ func (s *SysJobController) Edit(c *gin.Context) {
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务修改【%s】失败任务传入参数json字符串不正确
msg := fmt.Sprintf("Scheduling task modification [%s] failed, task incoming parameter json string is not correct", body.JobName)
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
@@ -150,7 +169,7 @@ func (s *SysJobController) Edit(c *gin.Context) {
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, body.JobID)
if !uniqueJob {
// 调度任务修改【%s】失败同任务组内有相同任务名称
msg := fmt.Sprintf("Scheduling task modification [%s] failed with the same task name in the same task group", body.JobName)
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -168,9 +187,10 @@ func (s *SysJobController) Edit(c *gin.Context) {
//
// DELETE /:jobIds
func (s *SysJobController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobIds := c.Param("jobIds")
if jobIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
@@ -185,7 +205,7 @@ func (s *SysJobController) Remove(c *gin.Context) {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
msg := fmt.Sprintf("Deleted successfully: %d", rows)
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}
@@ -193,6 +213,7 @@ func (s *SysJobController) Remove(c *gin.Context) {
//
// PUT /changeStatus
func (s *SysJobController) Status(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 任务ID
JobId string `json:"jobId" binding:"required"`
@@ -201,7 +222,7 @@ func (s *SysJobController) Status(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -209,14 +230,14 @@ func (s *SysJobController) Status(c *gin.Context) {
job := s.sysJobService.SelectJobById(body.JobId)
if job.JobID != body.JobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg("No accessible scheduling task data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
// 与旧值相等不变更
if job.Status == body.Status {
// 变更状态与旧值相等!
c.JSON(200, result.ErrMsg("Change status equals old value!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.statusEq")))
return
}
@@ -235,9 +256,10 @@ func (s *SysJobController) Status(c *gin.Context) {
//
// PUT /run/:jobId
func (s *SysJobController) Run(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobId := c.Param("jobId")
if jobId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -245,7 +267,7 @@ func (s *SysJobController) Run(c *gin.Context) {
job := s.sysJobService.SelectJobById(jobId)
if job.JobID != jobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg("No accessible scheduling task data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
@@ -269,30 +291,38 @@ func (s *SysJobController) ResetQueueJob(c *gin.Context) {
//
// POST /export
func (s *SysJobController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
data := s.sysJobService.SelectJobPage(querys)
if data["total"].(int64) == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg("Export data record is empty"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
rows := data["rows"].([]model.SysJob)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysJob) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("job_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "JobID",
"B1": "JobName",
"C1": "JobGroupName",
"D1": "InvokeTarget",
"E1": "TargetParams",
"F1": "CronExpression",
"G1": "MisfirePolicy",
"H1": "Concurrent",
"I1": "Status",
"J1": "Remark",
"A1": i18n.TKey(language, "job.export.jobID"),
"B1": i18n.TKey(language, "job.export.jobName"),
"C1": i18n.TKey(language, "job.export.jobGroupName"),
"D1": i18n.TKey(language, "job.export.invokeTarget"),
"E1": i18n.TKey(language, "job.export.targetParams"),
"F1": i18n.TKey(language, "job.export.cronExpression"),
"G1": i18n.TKey(language, "job.export.status"),
"H1": i18n.TKey(language, "job.export.remark"),
}
// 读取任务组名字典数据
dictSysJobGroup := s.sysDictDataService.SelectDictDataByType("sys_job_group")
@@ -304,24 +334,15 @@ func (s *SysJobController) Export(c *gin.Context) {
sysJobGroup := ""
for _, v := range dictSysJobGroup {
if row.JobGroup == v.DictValue {
sysJobGroup = v.DictLabel
sysJobGroup = i18n.TKey(language, v.DictLabel)
break
}
}
misfirePolicy := "Abandon execution"
if row.MisfirePolicy == "1" {
misfirePolicy = "Execute immediately"
} else if row.MisfirePolicy == "2" {
misfirePolicy = "Execute once"
}
concurrent := "prohibit"
if row.Concurrent == "1" {
concurrent = "allow"
}
// 状态
statusValue := "fail"
statusValue := i18n.TKey(language, "dictData.fail")
if row.Status == "1" {
statusValue = "successes"
statusValue = i18n.TKey(language, "dictData.success")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.JobID,
@@ -330,10 +351,8 @@ func (s *SysJobController) Export(c *gin.Context) {
"D" + idx: row.InvokeTarget,
"E" + idx: row.TargetParams,
"F" + idx: row.CronExpression,
"G" + idx: misfirePolicy,
"H" + idx: concurrent,
"I" + idx: statusValue,
"J" + idx: row.Remark,
"G" + idx: statusValue,
"H" + idx: row.Remark,
})
}

View File

@@ -6,6 +6,7 @@ import (
"strings"
"time"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/date"
"ems.agt/src/framework/utils/file"
@@ -40,17 +41,42 @@ type SysJobLogController struct {
func (s *SysJobLogController) List(c *gin.Context) {
// 查询参数转换map
querys := ctx.QueryMap(c)
list := s.sysJobLogService.SelectJobLogPage(querys)
c.JSON(200, result.Ok(list))
data := s.sysJobLogService.SelectJobLogPage(querys)
language := ctx.AcceptLanguage(c)
// 反查多语言key
querys["jobName"] = i18n.ValueKey(language, querys["jobName"].(string))
dataI18n := s.sysJobLogService.SelectJobLogPage(querys)
totalI18n := parse.Number(dataI18n["total"])
if totalI18n != 0 {
rows := data["rows"].([]model.SysJobLog)
total := parse.Number(data["total"])
rowsI18n := dataI18n["rows"].([]model.SysJobLog)
data["rows"] = append(rows, rowsI18n...)
data["total"] = total + totalI18n
}
rows := data["rows"].([]model.SysJobLog)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysJobLog) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
// 调度任务日志信息
//
// GET /:jobLogId
func (s *SysJobLogController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobLogId := c.Param("jobLogId")
if jobLogId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysJobLogService.SelectJobLogById(jobLogId)
@@ -65,9 +91,10 @@ func (s *SysJobLogController) Info(c *gin.Context) {
//
// DELETE /:jobLogIds
func (s *SysJobLogController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobLogIds := c.Param("jobLogIds")
if jobLogIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -81,7 +108,7 @@ func (s *SysJobLogController) Remove(c *gin.Context) {
rows := s.sysJobLogService.DeleteJobLogByIds(uniqueIDs)
if rows > 0 {
// 删除成功:%d
msg := fmt.Sprintf("Deleted successfully: %d", rows)
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
return
}
@@ -113,18 +140,27 @@ func (s *SysJobLogController) Export(c *gin.Context) {
}
rows := data["rows"].([]model.SysJobLog)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
converI18n := func(language string, arr *[]model.SysJobLog) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("jobLog_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "JobLogID",
"B1": "JobName",
"C1": "JobGroupName",
"D1": "InvokeTarget",
"E1": "TargetParams",
"F1": "JobMsg",
"G1": "Status",
"H1": "Time",
"A1": i18n.TKey(language, "job.export.jobLogID"),
"B1": i18n.TKey(language, "job.export.jobName"),
"C1": i18n.TKey(language, "job.export.jobGroupName"),
"D1": i18n.TKey(language, "job.export.invokeTarget"),
"E1": i18n.TKey(language, "job.export.targetParams"),
"F1": i18n.TKey(language, "job.export.jobID"),
"G1": i18n.TKey(language, "job.export.jobLogStatus"),
"H1": i18n.TKey(language, "job.export.jobLogTime"),
}
// 读取任务组名字典数据
dictSysJobGroup := s.sysDictDataService.SelectDictDataByType("sys_job_group")
@@ -136,14 +172,14 @@ func (s *SysJobLogController) Export(c *gin.Context) {
sysJobGroup := ""
for _, v := range dictSysJobGroup {
if row.JobGroup == v.DictValue {
sysJobGroup = v.DictLabel
sysJobGroup = i18n.TKey(language, v.DictLabel)
break
}
}
// 状态
statusValue := "Fail"
statusValue := i18n.TKey(language, "dictData.fail")
if row.Status == "1" {
statusValue = "Success"
statusValue = i18n.TKey(language, "dictData.success")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.JobLogID,