package handler import ( "net/http" "strings" "github.com/gin-gonic/gin" "myschools.me/heritage/heritage-api/model" "myschools.me/heritage/heritage-api/service" ) type DeviceCreateRequest struct { Name string `json:"name"` ModelID string `json:"modelId"` SerialNo string `json:"serialNo"` StatusCode string `json:"statusCode"` PointID string `json:"pointId"` InstalledAt string `json:"installedAt"` InstalledImage string `json:"installedImage"` Channel1ParamID string `json:"channel1ParamId"` Channel2ParamID string `json:"channel2ParamId"` Channel3ParamID string `json:"channel3ParamId"` CreatorUserID string `json:"creatorUserId"` Remark string `json:"remark"` } type DeviceUpdateRequest struct { Name *string `json:"name"` ModelID *string `json:"modelId"` SerialNo *string `json:"serialNo"` StatusCode *string `json:"statusCode"` PointID *string `json:"pointId"` InstalledAt *string `json:"installedAt"` InstalledImage *string `json:"installedImage"` Channel1ParamID *string `json:"channel1ParamId"` Channel2ParamID *string `json:"channel2ParamId"` Channel3ParamID *string `json:"channel3ParamId"` CreatorUserID *string `json:"creatorUserId"` Remark *string `json:"remark"` } func DeviceCreate(c *gin.Context) { var req DeviceCreateRequest if err := c.ShouldBindJSON(&req); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"}) return } t, err := parseDateOnly(&req.InstalledAt) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "安装日期格式错误"}) return } usr := currentUser(c) creatorID := strings.TrimSpace(req.CreatorUserID) if creatorID == "" && usr != nil { creatorID = usr.ID } d := &model.Device{ Name: req.Name, ModelID: req.ModelID, SerialNo: req.SerialNo, StatusCode: req.StatusCode, PointID: req.PointID, InstalledImage: req.InstalledImage, Channel1ParamID: req.Channel1ParamID, Channel2ParamID: req.Channel2ParamID, Channel3ParamID: req.Channel3ParamID, CreatorUserID: creatorID, Remark: req.Remark, } if t != nil { d.InstalledAt = t } out, err := service.DeviceCreateDevice(d) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"device": out}) } func DeviceUpdate(c *gin.Context) { id := c.Param("id") var req DeviceUpdateRequest if err := c.ShouldBindJSON(&req); err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"}) return } patch := &model.Device{} if req.Name != nil { patch.Name = *req.Name } if req.ModelID != nil { patch.ModelID = *req.ModelID } if req.SerialNo != nil { patch.SerialNo = *req.SerialNo } if req.StatusCode != nil { patch.StatusCode = *req.StatusCode } if req.PointID != nil { patch.PointID = *req.PointID } if req.InstalledAt != nil { t, err := parseDateOnly(req.InstalledAt) if err != nil { c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "安装日期格式错误"}) return } patch.InstalledAt = t } if req.InstalledImage != nil { patch.InstalledImage = *req.InstalledImage } if req.Channel1ParamID != nil { patch.Channel1ParamID = *req.Channel1ParamID } if req.Channel2ParamID != nil { patch.Channel2ParamID = *req.Channel2ParamID } if req.Channel3ParamID != nil { patch.Channel3ParamID = *req.Channel3ParamID } if req.CreatorUserID != nil { patch.CreatorUserID = *req.CreatorUserID } if req.Remark != nil { patch.Remark = *req.Remark } out, err := service.DeviceUpdateDevice(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{"device": out}) } func DeviceDelete(c *gin.Context) { id := c.Param("id") if err := service.DeviceDeleteDevice(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 DeviceGet(c *gin.Context) { id := c.Param("id") d, err := service.DeviceGetDevice(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{"device": d}) } func DeviceList(c *gin.Context) { page, size := pageAndSize(c) keyword := c.Query("keyword") var kw *string if keyword != "" { kw = &keyword } items, total, err := service.DeviceListDevices(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, }) }