180 lines
5.9 KiB
Go
180 lines
5.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"silk-server-go/internal/middleware"
|
|
"silk-server-go/internal/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// RegisterDeviceMaintenanceRoutes 注册设备校准/故障/维护/固件记录路由。
|
|
func RegisterDeviceMaintenanceRoutes(rg *gin.RouterGroup, db *gorm.DB) {
|
|
read := middleware.RequirePermission(db, "device:read")
|
|
write := middleware.RequirePermission(db, "device:write")
|
|
rg.GET("/device-maintenance", read, listDeviceMaintenance(db))
|
|
rg.POST("/device-maintenance", write, createDeviceMaintenance(db))
|
|
rg.PATCH("/device-maintenance/:id", write, updateDeviceMaintenance(db))
|
|
rg.DELETE("/device-maintenance/:id", write, deleteDeviceMaintenance(db))
|
|
}
|
|
|
|
func listDeviceMaintenance(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
q := db.Model(&model.DeviceMaintenanceRecord{}).
|
|
Joins("JOIN devices d ON d.id = device_maintenance_records.device_id")
|
|
q = applyRoomScope(q, c, "d.room_id")
|
|
if deviceID := c.Query("deviceId"); deviceID != "" {
|
|
q = q.Where("device_maintenance_records.device_id = ?", deviceID)
|
|
}
|
|
if kind := c.Query("kind"); kind != "" {
|
|
q = q.Where("device_maintenance_records.kind = ?", kind)
|
|
}
|
|
var list []model.DeviceMaintenanceRecord
|
|
q.Order("device_maintenance_records.created_at DESC").Limit(200).Find(&list)
|
|
c.JSON(http.StatusOK, list)
|
|
}
|
|
}
|
|
|
|
func createDeviceMaintenance(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
var body struct {
|
|
DeviceID string `json:"deviceId"`
|
|
Kind string `json:"kind"`
|
|
Title string `json:"title"`
|
|
ScheduledAt *time.Time `json:"scheduledAt"`
|
|
PerformedAt *time.Time `json:"performedAt"`
|
|
PerformerID *string `json:"performerId"`
|
|
Result *string `json:"result"`
|
|
FirmwareFrom *string `json:"firmwareFrom"`
|
|
FirmwareTo *string `json:"firmwareTo"`
|
|
CostAmount *float64 `json:"costAmount"`
|
|
CostUnit *string `json:"costUnit"`
|
|
Note *string `json:"note"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if !isUUID(body.DeviceID) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "deviceId 不是合法的 UUID"})
|
|
return
|
|
}
|
|
if !requireObjectAccess(c, db, "device", body.DeviceID) {
|
|
return
|
|
}
|
|
record := model.DeviceMaintenanceRecord{
|
|
DeviceID: body.DeviceID, Kind: body.Kind, Title: body.Title,
|
|
ScheduledAt: body.ScheduledAt, PerformedAt: body.PerformedAt,
|
|
PerformerID: body.PerformerID, Result: body.Result,
|
|
FirmwareFrom: body.FirmwareFrom, FirmwareTo: body.FirmwareTo,
|
|
CostAmount: body.CostAmount, CostUnit: body.CostUnit, Note: body.Note,
|
|
CreatedBy: currentUserID(c),
|
|
}
|
|
if err := model.ValidateDeviceMaintenanceRecord(record); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if err := db.Create(&record).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "创建设备维护记录失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, record)
|
|
}
|
|
}
|
|
|
|
func updateDeviceMaintenance(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if !requireObjectAccess(c, db, "device_maintenance_record", id) {
|
|
return
|
|
}
|
|
var record model.DeviceMaintenanceRecord
|
|
if db.Where("id = ?", id).First(&record).Error != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "device maintenance record not found"})
|
|
return
|
|
}
|
|
var body struct {
|
|
Kind *string `json:"kind"`
|
|
Title *string `json:"title"`
|
|
ScheduledAt *time.Time `json:"scheduledAt"`
|
|
PerformedAt *time.Time `json:"performedAt"`
|
|
PerformerID *string `json:"performerId"`
|
|
Result *string `json:"result"`
|
|
FirmwareFrom *string `json:"firmwareFrom"`
|
|
FirmwareTo *string `json:"firmwareTo"`
|
|
CostAmount *float64 `json:"costAmount"`
|
|
CostUnit *string `json:"costUnit"`
|
|
Note *string `json:"note"`
|
|
}
|
|
if err := c.ShouldBindJSON(&body); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
updates := map[string]interface{}{}
|
|
if body.Kind != nil {
|
|
if !model.ValidMaintenanceKind(*body.Kind) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "kind 仅支持 calibration/fault/maintenance/firmware"})
|
|
return
|
|
}
|
|
updates["kind"] = *body.Kind
|
|
}
|
|
if body.Title != nil {
|
|
updates["title"] = *body.Title
|
|
}
|
|
if body.ScheduledAt != nil {
|
|
updates["scheduled_at"] = *body.ScheduledAt
|
|
}
|
|
if body.PerformedAt != nil {
|
|
updates["performed_at"] = *body.PerformedAt
|
|
}
|
|
if body.PerformerID != nil {
|
|
updates["performer_id"] = *body.PerformerID
|
|
}
|
|
if body.Result != nil {
|
|
updates["result"] = *body.Result
|
|
}
|
|
if body.FirmwareFrom != nil {
|
|
updates["firmware_from"] = *body.FirmwareFrom
|
|
}
|
|
if body.FirmwareTo != nil {
|
|
updates["firmware_to"] = *body.FirmwareTo
|
|
}
|
|
if body.CostAmount != nil {
|
|
updates["cost_amount"] = *body.CostAmount
|
|
}
|
|
if body.CostUnit != nil {
|
|
updates["cost_unit"] = *body.CostUnit
|
|
}
|
|
if body.Note != nil {
|
|
updates["note"] = *body.Note
|
|
}
|
|
if len(updates) > 0 {
|
|
if err := db.Model(&model.DeviceMaintenanceRecord{}).Where("id = ?", id).Updates(updates).Error; err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "更新设备维护记录失败"})
|
|
return
|
|
}
|
|
}
|
|
db.Where("id = ?", id).First(&record)
|
|
c.JSON(http.StatusOK, record)
|
|
}
|
|
}
|
|
|
|
func deleteDeviceMaintenance(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if !requireObjectAccess(c, db, "device_maintenance_record", id) {
|
|
return
|
|
}
|
|
var record model.DeviceMaintenanceRecord
|
|
if db.Where("id = ?", id).First(&record).Error != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "device maintenance record not found"})
|
|
return
|
|
}
|
|
db.Where("id = ?", id).Delete(&model.DeviceMaintenanceRecord{})
|
|
c.JSON(http.StatusOK, gin.H{"id": id})
|
|
}
|
|
}
|