218 lines
7.2 KiB
Go
218 lines
7.2 KiB
Go
// review_handler.go Day 43:消息审核端点(待审列表/通过/拒绝)+D19 定时任务管理
|
|
// (列表/取消/改期)。审核动作 audit:write 门禁;列表 message:read。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/jwt"
|
|
"go-sms/internal/pkg/response"
|
|
auditsvc "go-sms/internal/service/audit"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// registerReviewRoutes 审核中心+定时任务端点(rv 可 nil——未装配时跳过)。
|
|
func registerReviewRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker, rv *auditsvc.ReviewService) {
|
|
h := &reviewHandler{rv: rv}
|
|
gate := func(obj, act string) gin.HandlerFunc {
|
|
return middleware.RequirePermission(perm, obj, act)
|
|
}
|
|
// Day 43:审核中心
|
|
protected.GET("/audits/messages", gate("audit", "read"), h.list)
|
|
protected.GET("/audits/messages/counts", gate("audit", "read"), h.counts)
|
|
protected.POST("/audits/messages/batch-review", gate("audit", "write"), h.batchReview)
|
|
protected.POST("/audits/messages/:id/approve", gate("audit", "write"), h.approve)
|
|
protected.POST("/audits/messages/:id/reject", gate("audit", "write"), h.reject)
|
|
// D19:定时任务管理
|
|
protected.GET("/send/tasks", gate("message", "read"), h.tasks)
|
|
protected.GET("/send/tasks/:id/details", gate("message", "read"), h.details) // Day 46/D22
|
|
protected.POST("/send/tasks/:id/cancel", gate("message", "write"), h.cancel)
|
|
protected.PUT("/send/tasks/:id/reschedule", gate("message", "write"), h.reschedule)
|
|
}
|
|
|
|
type reviewHandler struct {
|
|
rv *auditsvc.ReviewService
|
|
}
|
|
|
|
// list GET /audits/messages:待审任务分页(audit_status 过滤,默认 0 待审;-1 全部)。
|
|
func (h *reviewHandler) list(c *gin.Context) {
|
|
q := auditsvc.TaskQuery{
|
|
Page: intQuery(c, "page", 1), PageSize: intQuery(c, "page_size", 20),
|
|
AuditStatus: int8QueryDefault(c, "audit_status", 0), // 默认待审
|
|
Status: int8QueryDefault(c, "status", -1),
|
|
}
|
|
if v, err := strconv.ParseInt(c.Query("account_id"), 10, 64); err == nil && v > 0 {
|
|
q.AccountID = v
|
|
}
|
|
rows, total, err := h.rv.ListTasks(c.Request.Context(), q)
|
|
if err != nil {
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "list": rows})
|
|
}
|
|
|
|
// counts GET /audits/messages/counts:审核态计数(D21 Tab 徽标)。
|
|
func (h *reviewHandler) counts(c *gin.Context) {
|
|
total, byStatus, err := h.rv.CountByStatus(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "by_status": byStatus})
|
|
}
|
|
|
|
// batchReviewBody 批量审核请求体(D21)。
|
|
type batchReviewBody struct {
|
|
IDs []int64 `json:"ids" binding:"required"`
|
|
Pass bool `json:"pass"`
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
// batchReview POST /audits/messages/batch-review:批量通过/拒绝(已审跳过幂等)。
|
|
func (h *reviewHandler) batchReview(c *gin.Context) {
|
|
var body batchReviewBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
ok, err := h.rv.BatchReview(c.Request.Context(), body.IDs, body.Pass,
|
|
currentAuditor(c), body.Reason)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"ok": ok, "total": len(body.IDs)})
|
|
}
|
|
|
|
// auditActionBody 审核/拒绝请求体。
|
|
type auditActionBody struct {
|
|
Reason string `json:"reason"`
|
|
}
|
|
|
|
// approve POST /audits/messages/:id/approve(当前登录人为审核人)。
|
|
func (h *reviewHandler) approve(c *gin.Context) {
|
|
var body auditActionBody
|
|
_ = c.ShouldBindJSON(&body) // reason 可空
|
|
task, rec, err := h.rv.Approve(c.Request.Context(), pathID(c), currentAuditor(c), body.Reason)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"task": task, "record": rec})
|
|
}
|
|
|
|
// reject POST /audits/messages/:id/reject(reason 必填)。
|
|
func (h *reviewHandler) reject(c *gin.Context) {
|
|
var body auditActionBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
task, rec, err := h.rv.Reject(c.Request.Context(), pathID(c), currentAuditor(c), body.Reason)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"task": task, "record": rec})
|
|
}
|
|
|
|
// tasks GET /send/tasks:任务分页(状态/审核态/类型/账号过滤;D19 定时列表)。
|
|
func (h *reviewHandler) tasks(c *gin.Context) {
|
|
q := auditsvc.TaskQuery{
|
|
Page: intQuery(c, "page", 1), PageSize: intQuery(c, "page_size", 20),
|
|
AuditStatus: int8QueryDefault(c, "audit_status", -1),
|
|
Status: int8QueryDefault(c, "status", -1),
|
|
SendType: int8QueryDefault(c, "send_type", 0),
|
|
}
|
|
if v, err := strconv.ParseInt(c.Query("account_id"), 10, 64); err == nil && v > 0 {
|
|
q.AccountID = v
|
|
}
|
|
rows, total, err := h.rv.ListTasks(c.Request.Context(), q)
|
|
if err != nil {
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "list": rows})
|
|
}
|
|
|
|
// details GET /send/tasks/:id/details:任务明细分页(Day 46/D22 结果反馈页;
|
|
// status/error_code 过滤;含 ext_code/call_data 逐号核对)。
|
|
func (h *reviewHandler) details(c *gin.Context) {
|
|
q := auditsvc.DetailQuery{
|
|
Page: intQuery(c, "page", 1),
|
|
PageSize: intQuery(c, "page_size", 50),
|
|
Status: int8QueryDefault(c, "status", -1),
|
|
ErrorCode: c.Query("error_code"),
|
|
}
|
|
rows, total, err := h.rv.TaskDetails(c.Request.Context(), pathID(c), q)
|
|
if err != nil {
|
|
response.Error(c, errors.InternalError)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "list": rows})
|
|
}
|
|
|
|
// cancel POST /send/tasks/:id/cancel:取消待发任务(幂等拒绝:非待发 ISC:038)。
|
|
func (h *reviewHandler) cancel(c *gin.Context) {
|
|
task, err := h.rv.Cancel(c.Request.Context(), pathID(c))
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, task)
|
|
}
|
|
|
|
// rescheduleBody 改期请求体。
|
|
type rescheduleBody struct {
|
|
SendAt string `json:"send_at" binding:"required"` // YYYY-MM-DD HH:mm(与提交口径一致)
|
|
}
|
|
|
|
// reschedule PUT /send/tasks/:id/reschedule:定时任务改期(仅定时+待发)。
|
|
func (h *reviewHandler) reschedule(c *gin.Context) {
|
|
var body rescheduleBody
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
at, err := time.ParseInLocation("2006-01-02 15:04", body.SendAt, time.Local)
|
|
if err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "send_at 须为 YYYY-MM-DD HH:mm 格式")
|
|
return
|
|
}
|
|
task, err := h.rv.Reschedule(c.Request.Context(), pathID(c), at)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, task)
|
|
}
|
|
|
|
// pathID 取 :id 路径参数(非法→0;服务层 NotFound)。
|
|
func pathID(c *gin.Context) int64 {
|
|
v, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
return v
|
|
}
|
|
|
|
// currentAuditor 当前操作人(JWT claims.Username;缺省 system)。
|
|
func currentAuditor(c *gin.Context) string {
|
|
if v, ok := c.Get(middleware.ContextKeyClaims); ok {
|
|
if claims, ok := v.(*jwt.Claims); ok && claims.Username != "" {
|
|
return claims.Username
|
|
}
|
|
}
|
|
return "system"
|
|
}
|
|
|
|
// int8QueryDefault int8 查询参数(缺省/非法→def)。
|
|
func int8QueryDefault(c *gin.Context, key string, def int8) int8 {
|
|
if v, err := strconv.Atoi(c.Query(key)); err == nil {
|
|
return int8(v)
|
|
}
|
|
return def
|
|
}
|