feat: project_type.
This commit is contained in:
+42
-3
@@ -36,7 +36,7 @@ func NewGatewayAdminController() *GatewayAdminController {
|
||||
}
|
||||
|
||||
// List 获取网关列表
|
||||
// GET /api/gateways?keyword=&isSold=&projectType=
|
||||
// GET /api/gateways?keyword=&isSold=&projectType=®ionId=
|
||||
func (gc *GatewayAdminController) List(c *gin.Context) {
|
||||
var items []models.Gateway
|
||||
query := gc.DB.Model(&models.Gateway{}).Order("region_id ASC, created_at DESC")
|
||||
@@ -57,6 +57,14 @@ func (gc *GatewayAdminController) List(c *gin.Context) {
|
||||
if projectType := c.Query("projectType"); projectType != "" {
|
||||
query = query.Where("project_type = ?", projectType)
|
||||
}
|
||||
if regionIDStr := strings.TrimSpace(c.Query("regionId")); regionIDStr != "" {
|
||||
regionID, err := strconv.ParseUint(regionIDStr, 10, 32)
|
||||
if err != nil || regionID == 0 {
|
||||
writeError(c, http.StatusBadRequest, "regionId参数无效")
|
||||
return
|
||||
}
|
||||
query = query.Where("region_id = ?", uint32(regionID))
|
||||
}
|
||||
|
||||
if err := query.Find(&items).Error; err != nil {
|
||||
writeError(c, http.StatusInternalServerError, "查询网关列表失败")
|
||||
@@ -78,6 +86,11 @@ func (gc *GatewayAdminController) Create(c *gin.Context) {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
projectType, err := gc.findGatewayProjectType(payload.ProjectType)
|
||||
if err != nil {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 处理售出逻辑:如果已售出但未提供时间,默认为当前时间 (2026-04-29)
|
||||
var soldAt *time.Time
|
||||
@@ -96,7 +109,7 @@ func (gc *GatewayAdminController) Create(c *gin.Context) {
|
||||
Name: strings.TrimSpace(payload.Name),
|
||||
RegionID: payload.RegionID,
|
||||
Location: strings.TrimSpace(payload.Location),
|
||||
ProjectType: strings.TrimSpace(payload.ProjectType),
|
||||
ProjectType: projectType.Code,
|
||||
IsSold: payload.IsSold,
|
||||
SoldAt: soldAt,
|
||||
}
|
||||
@@ -131,12 +144,17 @@ func (gc *GatewayAdminController) Update(c *gin.Context) {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
projectType, err := gc.findGatewayProjectType(payload.ProjectType)
|
||||
if err != nil {
|
||||
writeError(c, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
record.Name = strings.TrimSpace(payload.Name)
|
||||
record.RegionID = payload.RegionID
|
||||
record.Location = strings.TrimSpace(payload.Location)
|
||||
record.ProjectType = strings.TrimSpace(payload.ProjectType)
|
||||
record.ProjectType = projectType.Code
|
||||
record.IsSold = payload.IsSold
|
||||
|
||||
// 核心逻辑:处理售出时间
|
||||
@@ -196,6 +214,24 @@ func (gc *GatewayAdminController) findByID(id string) (models.Gateway, error) {
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func (gc *GatewayAdminController) findGatewayProjectType(projectTypeCode string) (models.ProjectType, error) {
|
||||
var projectType models.ProjectType
|
||||
code := strings.TrimSpace(strings.ToLower(projectTypeCode))
|
||||
if code == "" {
|
||||
return projectType, errors.New("projectType is required")
|
||||
}
|
||||
if err := gc.DB.Where("code = ? AND is_active = ?", code, true).First(&projectType).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return projectType, errors.New("projectType is invalid")
|
||||
}
|
||||
return projectType, errors.New("failed to query project type")
|
||||
}
|
||||
if !projectType.SupportsGateway {
|
||||
return projectType, errors.New("current project type does not support gateways")
|
||||
}
|
||||
return projectType, nil
|
||||
}
|
||||
|
||||
// 辅助方法:验证输入数据
|
||||
func validateGatewayPayload(payload gatewayPayload) error {
|
||||
mac := strings.TrimSpace(payload.MAC)
|
||||
@@ -208,6 +244,9 @@ func validateGatewayPayload(payload gatewayPayload) error {
|
||||
if strings.TrimSpace(payload.Name) == "" {
|
||||
return errors.New("网关名称是必填项")
|
||||
}
|
||||
if strings.TrimSpace(payload.ProjectType) == "" {
|
||||
return errors.New("项目类型是必填项")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user