1
0
Files
omc_api/src/modules/monitor/controller/sys_job.go
2023-11-21 14:42:37 +08:00

368 lines
9.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package controller
import (
"encoding/json"
"fmt"
"strconv"
"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"
"ems.agt/src/framework/vo/result"
"ems.agt/src/modules/monitor/model"
"ems.agt/src/modules/monitor/service"
systemService "ems.agt/src/modules/system/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// 实例化控制层 SysJobLogController 结构体
var NewSysJob = &SysJobController{
sysJobService: service.NewSysJobImpl,
sysDictDataService: systemService.NewSysDictDataImpl,
}
// 调度任务信息
//
// PATH /monitor/job
type SysJobController struct {
// 调度任务服务
sysJobService service.ISysJob
// 字典数据服务
sysDictDataService systemService.ISysDictData
}
// 调度任务列表
//
// GET /list
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))
}
// 调度任务信息
//
// 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, 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
}
c.JSON(200, result.Err(nil))
}
// 调度任务新增
//
// 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, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务新增【%s】失败Cron表达式不正确
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务新增【%s】失败任务传入参数json字符串不正确
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
}
if !json.Valid([]byte(body.TargetParams)) {
c.JSON(200, result.ErrMsg(msg))
return
}
}
// 检查属性值唯一
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, "")
if !uniqueJob {
// 调度任务新增【%s】失败同任务组内有相同任务名称
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
body.CreateBy = ctx.LoginUserToUserName(c)
insertId := s.sysJobService.InsertJob(body)
if insertId != "" {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 调度任务修改
//
// 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, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务修改【%s】失败Cron表达式不正确
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务修改【%s】失败任务传入参数json字符串不正确
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
}
if !json.Valid([]byte(body.TargetParams)) {
c.JSON(200, result.ErrMsg(msg))
return
}
}
// 检查属性值唯一
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, body.JobID)
if !uniqueJob {
// 调度任务修改【%s】失败同任务组内有相同任务名称
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
body.UpdateBy = ctx.LoginUserToUserName(c)
rows := s.sysJobService.UpdateJob(body)
if rows > 0 {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 调度任务删除
//
// 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, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
ids := strings.Split(jobIds, ",")
uniqueIDs := parse.RemoveDuplicates(ids)
if len(uniqueIDs) <= 0 {
c.JSON(200, result.Err(nil))
return
}
rows, err := s.sysJobService.DeleteJobByIds(uniqueIDs)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}
// 调度任务修改状态
//
// PUT /changeStatus
func (s *SysJobController) Status(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 任务ID
JobId string `json:"jobId" binding:"required"`
// 状态
Status string `json:"status" binding:"required"`
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否存在
job := s.sysJobService.SelectJobById(body.JobId)
if job.JobID != body.JobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
// 与旧值相等不变更
if job.Status == body.Status {
// 变更状态与旧值相等!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.statusEq")))
return
}
// 更新状态
job.Status = body.Status
job.UpdateBy = ctx.LoginUserToUserName(c)
ok := s.sysJobService.ChangeStatus(job)
if ok {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 调度任务立即执行一次
//
// 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, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否存在
job := s.sysJobService.SelectJobById(jobId)
if job.JobID != jobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
ok := s.sysJobService.RunQueueJob(job)
if ok {
c.JSON(200, result.Ok(nil))
return
}
c.JSON(200, result.Err(nil))
}
// 调度任务重置刷新队列
//
// PUT /resetQueueJob
func (s *SysJobController) ResetQueueJob(c *gin.Context) {
s.sysJobService.ResetQueueJob()
c.JSON(200, result.Ok(nil))
}
// 导出调度任务信息
//
// 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(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": 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")
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
// 任务组名
sysJobGroup := ""
for _, v := range dictSysJobGroup {
if row.JobGroup == v.DictValue {
sysJobGroup = i18n.TKey(language, v.DictLabel)
break
}
}
// 状态
statusValue := i18n.TKey(language, "dictData.fail")
if row.Status == "1" {
statusValue = i18n.TKey(language, "dictData.success")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.JobID,
"B" + idx: row.JobName,
"C" + idx: sysJobGroup,
"D" + idx: row.InvokeTarget,
"E" + idx: row.TargetParams,
"F" + idx: row.CronExpression,
"G" + idx: statusValue,
"H" + idx: row.Remark,
})
}
// 导出数据表格
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
c.FileAttachment(saveFilePath, fileName)
}