175 lines
4.2 KiB
Go
175 lines
4.2 KiB
Go
package task
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (handler Handler) AdminList(c *gin.Context) {
|
|
page, err := positiveQuery(c.Query("page"), 1)
|
|
if err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "page 无效"))
|
|
return
|
|
}
|
|
pageSize, err := positiveQuery(c.Query("pageSize"), 20)
|
|
if err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "pageSize 无效"))
|
|
return
|
|
}
|
|
deviceID, err := optionalUint(c.Query("deviceId"))
|
|
if err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "deviceId 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.AdminList(c.Request.Context(), AdminListRequest{
|
|
Page: page, PageSize: pageSize, Status: strings.TrimSpace(c.Query("status")),
|
|
GoodsID: strings.TrimSpace(c.Query("goodsId")), DeviceID: deviceID,
|
|
})
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func (handler Handler) AdminCreate(c *gin.Context) {
|
|
var request CreateRequest
|
|
if err := decodeStrict(c, &request); err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "请求 JSON 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.Create(c.Request.Context(), request)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func (handler Handler) AdminBatchCreate(c *gin.Context) {
|
|
var request BatchCreateRequest
|
|
if err := decodeStrict(c, &request); err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "请求 JSON 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.BatchCreate(c.Request.Context(), request)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func (handler Handler) AdminDetail(c *gin.Context) {
|
|
id, err := taskID(c)
|
|
if err != nil || id == 0 {
|
|
writeError(c, serviceError("INVALID_REQUEST", "taskId 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.Detail(c.Request.Context(), id)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func (handler Handler) AdminReset(c *gin.Context) {
|
|
id, err := taskID(c)
|
|
if err != nil || id == 0 {
|
|
writeError(c, serviceError("INVALID_REQUEST", "taskId 无效"))
|
|
return
|
|
}
|
|
var request ActionRequest
|
|
if err := decodeStrict(c, &request); err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "请求 JSON 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.Reset(c.Request.Context(), id, request)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func (handler Handler) AdminDelete(c *gin.Context) {
|
|
id, err := taskID(c)
|
|
if err != nil || id == 0 {
|
|
writeError(c, serviceError("INVALID_REQUEST", "taskId 无效"))
|
|
return
|
|
}
|
|
var request ActionRequest
|
|
if err := decodeStrict(c, &request); err != nil {
|
|
writeError(c, serviceError("INVALID_REQUEST", "请求 JSON 无效"))
|
|
return
|
|
}
|
|
service, _, ok := handler.service(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
response, err := service.DeleteFailed(c.Request.Context(), id, request)
|
|
if err != nil {
|
|
writeError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 200, "data": response})
|
|
}
|
|
|
|
func decodeStrict(c *gin.Context, destination any) error {
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<20)
|
|
decoder := json.NewDecoder(c.Request.Body)
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(destination); err != nil {
|
|
return err
|
|
}
|
|
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
|
return errors.New("one object required")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func positiveQuery(raw string, fallback int) (int, error) {
|
|
if strings.TrimSpace(raw) == "" {
|
|
return fallback, nil
|
|
}
|
|
value, err := strconv.Atoi(raw)
|
|
if err != nil || value < 1 {
|
|
return 0, errors.New("invalid positive integer")
|
|
}
|
|
return value, nil
|
|
}
|
|
|
|
func optionalUint(raw string) (uint64, error) {
|
|
if strings.TrimSpace(raw) == "" {
|
|
return 0, nil
|
|
}
|
|
return strconv.ParseUint(raw, 10, 64)
|
|
}
|