189 lines
4.7 KiB
Go
189 lines
4.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"myschools.me/heritage/heritage-api/model"
|
|
"myschools.me/heritage/heritage-api/service"
|
|
)
|
|
|
|
type DataCreateRequest struct {
|
|
Value string `json:"value"`
|
|
IndicatorParamID string `json:"indicatorParamId"`
|
|
PointID string `json:"pointId"`
|
|
TaskID string `json:"taskId"`
|
|
CollectedAt string `json:"collectedAt"`
|
|
CreatorUserID string `json:"creatorUserId"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
|
|
type DataUpdateRequest struct {
|
|
Value *string `json:"value"`
|
|
IndicatorParamID *string `json:"indicatorParamId"`
|
|
PointID *string `json:"pointId"`
|
|
TaskID *string `json:"taskId"`
|
|
CollectedAt *string `json:"collectedAt"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
func DataCreate(c *gin.Context) {
|
|
var req DataCreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
t, err := dataParseTime(req.CollectedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "采集时间格式错误"})
|
|
return
|
|
}
|
|
usr := currentUser(c)
|
|
creatorID := req.CreatorUserID
|
|
if creatorID == "" && usr != nil {
|
|
creatorID = usr.ID
|
|
}
|
|
r := &model.DataRecord{
|
|
Value: req.Value,
|
|
IndicatorParamID: req.IndicatorParamID,
|
|
PointID: req.PointID,
|
|
TaskID: req.TaskID,
|
|
CreatorUserID: creatorID,
|
|
Remark: req.Remark,
|
|
}
|
|
if t != nil {
|
|
r.CollectedAt = *t
|
|
}
|
|
out, err := service.DataRecordCreate(r)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"dataRecord": out})
|
|
}
|
|
|
|
func DataUpdate(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req DataUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
patch := &model.DataRecord{}
|
|
if req.Value != nil {
|
|
patch.Value = *req.Value
|
|
}
|
|
if req.IndicatorParamID != nil {
|
|
patch.IndicatorParamID = *req.IndicatorParamID
|
|
}
|
|
if req.PointID != nil {
|
|
patch.PointID = *req.PointID
|
|
}
|
|
if req.TaskID != nil {
|
|
patch.TaskID = *req.TaskID
|
|
}
|
|
if req.CollectedAt != nil {
|
|
t, err := dataParseTime(*req.CollectedAt)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "采集时间格式错误"})
|
|
return
|
|
}
|
|
if t != nil {
|
|
patch.CollectedAt = *t
|
|
}
|
|
}
|
|
if req.Remark != nil {
|
|
patch.Remark = *req.Remark
|
|
}
|
|
out, err := service.DataRecordUpdate(id, patch)
|
|
if err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"dataRecord": out})
|
|
}
|
|
|
|
func DataDelete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if err := service.DataRecordDelete(id); err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "删除失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": "ok"})
|
|
}
|
|
|
|
func DataGet(c *gin.Context) {
|
|
id := c.Param("id")
|
|
r, err := service.DataGetDataRecord(id)
|
|
if err != nil {
|
|
if err == service.ErrNotFound {
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
|
|
return
|
|
}
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"dataRecord": r})
|
|
}
|
|
|
|
func DataList(c *gin.Context) {
|
|
page, size := pageAndSize(c)
|
|
pointID := c.Query("pointId")
|
|
taskID := c.Query("taskId")
|
|
startAtStr := c.Query("startAt")
|
|
endAtStr := c.Query("endAt")
|
|
|
|
var pid *string
|
|
if pointID != "" {
|
|
pid = &pointID
|
|
}
|
|
var tid *string
|
|
if taskID != "" {
|
|
tid = &taskID
|
|
}
|
|
|
|
startAt, err := dataParseTime(startAtStr)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "startAt格式错误"})
|
|
return
|
|
}
|
|
endAt, err := dataParseTime(endAtStr)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "endAt格式错误"})
|
|
return
|
|
}
|
|
|
|
items, total, err := service.DataListDataRecords(pid, tid, startAt, endAt, page, size)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"items": items,
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
})
|
|
}
|
|
|
|
func dataParseTime(s string) (*time.Time, error) {
|
|
s = strings.TrimSpace(s)
|
|
if s == "" {
|
|
return nil, nil
|
|
}
|
|
if t, err := time.ParseInLocation(time.RFC3339, s, time.Local); err == nil {
|
|
return &t, nil
|
|
}
|
|
return parseDateOnly(&s)
|
|
}
|