2026-04-02 00:31:28 +08:00
|
|
|
package transport
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strconv"
|
2026-04-02 21:16:12 +08:00
|
|
|
"time"
|
2026-04-02 00:31:28 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/bootstrap"
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/shared/response"
|
|
|
|
|
"github.com/geo-platform/tenant-api/internal/tenant/app"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type ScheduleTaskHandler struct {
|
|
|
|
|
svc *app.ScheduleTaskService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewScheduleTaskHandler(a *bootstrap.App) *ScheduleTaskHandler {
|
2026-04-15 16:11:05 +08:00
|
|
|
return &ScheduleTaskHandler{svc: app.NewScheduleTaskService(a.DB, a.AuditLogs).WithCache(a.Cache)}
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) List(c *gin.Context) {
|
|
|
|
|
params := app.ScheduleTaskListParams{
|
|
|
|
|
Page: 1,
|
|
|
|
|
PageSize: 20,
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("page"); v != "" {
|
|
|
|
|
if p, err := strconv.Atoi(v); err == nil {
|
|
|
|
|
params.Page = p
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("page_size"); v != "" {
|
|
|
|
|
if ps, err := strconv.Atoi(v); err == nil {
|
|
|
|
|
params.PageSize = ps
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("prompt_rule_id"); v != "" {
|
|
|
|
|
if rid, err := strconv.ParseInt(v, 10, 64); err == nil {
|
|
|
|
|
params.PromptRuleID = &rid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("status"); v != "" {
|
|
|
|
|
params.Status = &v
|
|
|
|
|
}
|
2026-04-02 21:16:12 +08:00
|
|
|
if v := c.Query("keyword"); v != "" {
|
|
|
|
|
params.Keyword = &v
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("created_from"); v != "" {
|
|
|
|
|
parsed, err := time.Parse(time.RFC3339, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40014, "invalid_created_from", "created_from must be RFC3339"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
params.CreatedFrom = &parsed
|
|
|
|
|
}
|
|
|
|
|
if v := c.Query("created_to"); v != "" {
|
|
|
|
|
parsed, err := time.Parse(time.RFC3339, v)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40015, "invalid_created_to", "created_to must be RFC3339"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
params.CreatedTo = &parsed
|
|
|
|
|
}
|
2026-04-02 00:31:28 +08:00
|
|
|
|
|
|
|
|
data, err := h.svc.List(c.Request.Context(), params)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) Detail(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, err := h.svc.Detail(c.Request.Context(), id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) Create(c *gin.Context) {
|
|
|
|
|
var req app.ScheduleTaskRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
data, err := h.svc.Create(c.Request.Context(), req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.SuccessWithStatus(c, 201, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) Update(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var req app.ScheduleTaskRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := h.svc.Update(c.Request.Context(), id, req); err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) Delete(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *ScheduleTaskHandler) ToggleStatus(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_id", "schedule task id must be a number"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var req app.ScheduleTaskStatusRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.Error(c, response.ErrBadRequest(40001, "invalid_params", err.Error()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := h.svc.ToggleStatus(c.Request.Context(), id, req); err != nil {
|
|
|
|
|
response.Error(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
response.Success(c, nil)
|
|
|
|
|
}
|