188 lines
4.9 KiB
Go
188 lines
4.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"myschools.me/heritage/heritage-api/model"
|
|
"myschools.me/heritage/heritage-api/service"
|
|
)
|
|
|
|
type PointCreateRequest struct {
|
|
Name string `json:"name"`
|
|
X float64 `json:"x"`
|
|
Y float64 `json:"y"`
|
|
Z float64 `json:"z"`
|
|
LocationObjectID string `json:"locationObjectId"`
|
|
StatusCode string `json:"statusCode"`
|
|
Image string `json:"image"`
|
|
ObjectID string `json:"objectId"`
|
|
IndicatorID string `json:"indicatorId"`
|
|
CreatorPersonID string `json:"creatorPersonId"`
|
|
SensorID string `json:"sensorId"`
|
|
CreatedTaskID string `json:"createdTaskId"`
|
|
Remark string `json:"remark"`
|
|
}
|
|
|
|
type PointUpdateRequest struct {
|
|
Name *string `json:"name"`
|
|
X *float64 `json:"x"`
|
|
Y *float64 `json:"y"`
|
|
Z *float64 `json:"z"`
|
|
LocationObjectID *string `json:"locationObjectId"`
|
|
StatusCode *string `json:"statusCode"`
|
|
Image *string `json:"image"`
|
|
ObjectID *string `json:"objectId"`
|
|
IndicatorID *string `json:"indicatorId"`
|
|
CreatorPersonID *string `json:"creatorPersonId"`
|
|
SensorID *string `json:"sensorId"`
|
|
CreatedTaskID *string `json:"createdTaskId"`
|
|
Remark *string `json:"remark"`
|
|
}
|
|
|
|
func PointCreate(c *gin.Context) {
|
|
var req PointCreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
p := &model.Point{
|
|
Name: req.Name,
|
|
X: req.X,
|
|
Y: req.Y,
|
|
Z: req.Z,
|
|
LocationObjectID: req.LocationObjectID,
|
|
StatusCode: req.StatusCode,
|
|
Image: req.Image,
|
|
ObjectID: req.ObjectID,
|
|
IndicatorID: req.IndicatorID,
|
|
CreatorPersonID: req.CreatorPersonID,
|
|
SensorID: req.SensorID,
|
|
CreatedTaskID: req.CreatedTaskID,
|
|
Remark: req.Remark,
|
|
}
|
|
out, err := service.PointCreatePoint(p)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"point": out})
|
|
}
|
|
|
|
func PointUpdate(c *gin.Context) {
|
|
id := c.Param("id")
|
|
var req PointUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
patch := &model.Point{}
|
|
if req.Name != nil {
|
|
patch.Name = *req.Name
|
|
}
|
|
if req.X != nil {
|
|
patch.X = *req.X
|
|
}
|
|
if req.Y != nil {
|
|
patch.Y = *req.Y
|
|
}
|
|
if req.Z != nil {
|
|
patch.Z = *req.Z
|
|
}
|
|
if req.LocationObjectID != nil {
|
|
patch.LocationObjectID = *req.LocationObjectID
|
|
}
|
|
if req.StatusCode != nil {
|
|
patch.StatusCode = *req.StatusCode
|
|
}
|
|
if req.Image != nil {
|
|
patch.Image = *req.Image
|
|
}
|
|
if req.ObjectID != nil {
|
|
patch.ObjectID = *req.ObjectID
|
|
}
|
|
if req.IndicatorID != nil {
|
|
patch.IndicatorID = *req.IndicatorID
|
|
}
|
|
if req.CreatorPersonID != nil {
|
|
patch.CreatorPersonID = *req.CreatorPersonID
|
|
}
|
|
if req.SensorID != nil {
|
|
patch.SensorID = *req.SensorID
|
|
}
|
|
if req.CreatedTaskID != nil {
|
|
patch.CreatedTaskID = *req.CreatedTaskID
|
|
}
|
|
if req.Remark != nil {
|
|
patch.Remark = *req.Remark
|
|
}
|
|
|
|
out, err := service.PointUpdatePoint(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{"point": out})
|
|
}
|
|
|
|
func PointDelete(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if err := service.PointDeletePoint(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 PointGet(c *gin.Context) {
|
|
id := c.Param("id")
|
|
p, err := service.PointGetPoint(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{"point": p})
|
|
}
|
|
|
|
func PointList(c *gin.Context) {
|
|
page, size := pageAndSize(c)
|
|
objectID := c.Query("objectId")
|
|
indicatorID := c.Query("indicatorId")
|
|
keyword := c.Query("keyword")
|
|
var oid *string
|
|
if objectID != "" {
|
|
oid = &objectID
|
|
}
|
|
var iid *string
|
|
if indicatorID != "" {
|
|
iid = &indicatorID
|
|
}
|
|
var kw *string
|
|
if keyword != "" {
|
|
kw = &keyword
|
|
}
|
|
items, total, err := service.PointListPoints(oid, iid, kw, 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,
|
|
})
|
|
}
|