cutego/core/api/v1/cron_job_api.go

105 lines
2.3 KiB
Go
Raw Normal View History

2022-03-01 13:50:13 +08:00
package v1
import (
"cutego/core/api/v1/request"
2022-03-01 17:43:41 +08:00
"cutego/core/api/v1/response"
2022-03-01 13:50:13 +08:00
"cutego/core/entity"
"cutego/core/service"
2022-03-01 17:43:41 +08:00
"cutego/pkg/common"
"cutego/pkg/cronjob"
2022-03-01 13:50:13 +08:00
"cutego/pkg/resp"
2022-03-01 17:43:41 +08:00
"cutego/pkg/util"
2022-03-01 13:50:13 +08:00
"github.com/gin-gonic/gin"
)
type CronJobApi struct {
cronJobService service.CronJobService
}
// List 获取定时任务数据
func (a CronJobApi) List(c *gin.Context) {
query := request.CronJobQuery{}
if c.Bind(&query) != nil {
resp.ParamError(c)
return
}
find, i := a.cronJobService.FindPage(query)
2022-03-01 17:43:41 +08:00
var result []interface{}
for _, item := range find {
// 领域对象转换
result = append(result, util.DeepCopy(item, &response.CronJobResponse{}))
}
resp.OK(c, util.NewPage(result, i, query.PageSize))
}
// GetById 根据id获取任务详情
func (a CronJobApi) GetById(c *gin.Context) {
jobId := c.Param("jobId")
info := a.cronJobService.GetInfo(common.StringToInt64(jobId))
resp.OK(c, util.DeepCopy(info, &response.CronJobResponse{}))
2022-03-01 13:50:13 +08:00
}
// Edit 修改定时任务
func (a CronJobApi) Edit(c *gin.Context) {
2022-03-01 17:43:41 +08:00
record := entity.SysCronJob{}
if c.Bind(&record) != nil {
2022-03-01 13:50:13 +08:00
resp.ParamError(c)
return
}
2022-03-01 17:43:41 +08:00
if a.cronJobService.Edit(record) > 0 {
2022-03-01 13:50:13 +08:00
resp.OK(c)
} else {
resp.Error(c)
}
}
// Add 新增定时任务
func (a CronJobApi) Add(c *gin.Context) {
2022-03-01 17:43:41 +08:00
dto := entity.SysCronJob{}
if c.Bind(&dto) != nil {
2022-03-01 13:50:13 +08:00
resp.ParamError(c)
return
}
2022-03-01 17:43:41 +08:00
record := a.cronJobService.GetInfoByAlias(dto.FuncAlias)
if record.JobId > 0 {
resp.Error(c, "任务已存在!")
2022-03-01 13:50:13 +08:00
} else {
2022-03-01 17:43:41 +08:00
a.cronJobService.Save(dto)
cronjob.AppendCronFunc(dto.JobCron, dto.FuncAlias, dto.Status)
resp.OK(c)
2022-03-01 13:50:13 +08:00
}
}
2022-03-01 17:43:41 +08:00
// Remove 删除定时任务
2022-03-01 13:50:13 +08:00
func (a CronJobApi) Remove(c *gin.Context) {
2022-03-01 17:43:41 +08:00
jobId := common.StringToInt64(c.Param("jobId"))
funcAlias := c.Param("funcAlias")
if a.cronJobService.GetInfo(jobId).Status == "1" {
resp.Error(c, "任务运行中, 无法删除!")
return
2022-03-01 13:50:13 +08:00
}
2022-03-01 17:43:41 +08:00
cronjob.RemoveCronFunc(funcAlias)
a.cronJobService.Remove([]int64{jobId})
resp.OK(c)
}
2022-03-01 13:50:13 +08:00
2022-03-01 17:43:41 +08:00
// ChangeStatus 改变任务状态
func (a CronJobApi) ChangeStatus(c *gin.Context) {
record := entity.SysCronJob{}
if c.Bind(&record) != nil {
resp.ParamError(c)
return
}
if a.cronJobService.Edit(record) > 0 {
if record.Status == "1" {
cronjob.StartCronFunc(record.FuncAlias)
} else {
cronjob.StopCronFunc(record.FuncAlias)
}
}
resp.OK(c)
2022-03-01 13:50:13 +08:00
}