112 lines
3.2 KiB
Go
112 lines
3.2 KiB
Go
package cb_message
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type CBEventStatus int
|
|
|
|
// CBEventStatus CB事件状态枚举
|
|
const (
|
|
CBEventStatusNull CBEventStatus = iota // 未知状态
|
|
CBEventStatusActive
|
|
CBEventStatusInactive
|
|
)
|
|
|
|
func (status CBEventStatus) Enum() string {
|
|
switch status {
|
|
case CBEventStatusNull:
|
|
return "NULL"
|
|
case CBEventStatusActive:
|
|
return "ACTIVE"
|
|
case CBEventStatusInactive:
|
|
return "INACTIVE"
|
|
default:
|
|
return "UNKNOWN"
|
|
}
|
|
}
|
|
|
|
func (status CBEventStatus) String() string {
|
|
return fmt.Sprintf("%d", status)
|
|
}
|
|
|
|
// ParseCBEventStatus 将字符串转换为 枚举类型
|
|
func ParseCBEventStatus(s string) CBEventStatus {
|
|
if i, err := strconv.Atoi(s); err == nil {
|
|
return CBEventStatus(i)
|
|
}
|
|
// 如果转换失败,则按名称匹配(忽略大小写)
|
|
switch strings.ToUpper(s) {
|
|
case "NULL":
|
|
return CBEventStatusNull
|
|
case "ACTIVE":
|
|
return CBEventStatusActive
|
|
case "INACTIVE":
|
|
return CBEventStatusInactive
|
|
case "":
|
|
// 如果字符串为空,则返回未知状态
|
|
return CBEventStatusNull
|
|
default:
|
|
// 默认返回未知状态
|
|
return CBEventStatusNull
|
|
}
|
|
}
|
|
|
|
// CBMessageQuery 查询条件结构体
|
|
type CBMessageQuery struct {
|
|
NeType string `form:"neType"` // 网元类型
|
|
NeId string `form:"neId"` // 网元ID
|
|
EventName string `form:"eventName"` // 事件名称
|
|
Status string `form:"status"` // 消息状态
|
|
StartTime string `form:"startTime"` // 创建时间范围-起始
|
|
EndTime string `form:"endTime"` // 创建时间范围-结束
|
|
PageNum int `form:"pageNum" binding:"required"`
|
|
PageSize int `form:"pageSize" binding:"required"`
|
|
}
|
|
|
|
// @Description CBMessage CB消息
|
|
type CBMessage struct {
|
|
Id int64 `json:"id" gorm:"column:id"` // CB消息ID
|
|
NeType string `json:"neType" gorm:"column:ne_type"` // 网元类型
|
|
NeId string `json:"neId" gorm:"column:ne_id"` // 网元ID
|
|
MessageJson json.RawMessage `json:"messageJson" gorm:"column:message_json"` // 消息内容JSON
|
|
Status CBEventStatus `json:"status" gorm:"column:status"` // 消息状态
|
|
CreatedAt int64 `json:"createdAt" gorm:"column:created_at"` // 创建时间
|
|
UpdatedAt *int64 `json:"updatedAt" gorm:"column:updated_at;autoUpdateTime:false"` // 更新时间
|
|
}
|
|
|
|
// TableName 表名称
|
|
func (*CBMessage) TableName() string {
|
|
return "cb_message"
|
|
}
|
|
|
|
// Scan 实现 sql.Scanner 接口,支持从数据库字符串转为 CBEventStatus
|
|
func (s *CBEventStatus) Scan(value interface{}) error {
|
|
switch v := value.(type) {
|
|
case string:
|
|
*s = ParseCBEventStatus(v)
|
|
return nil
|
|
case []byte:
|
|
*s = ParseCBEventStatus(string(v))
|
|
return nil
|
|
case int64:
|
|
*s = CBEventStatus(v)
|
|
return nil
|
|
case int:
|
|
*s = CBEventStatus(v)
|
|
return nil
|
|
default:
|
|
return errors.New("unsupported Scan type for CBEventStatus")
|
|
}
|
|
}
|
|
|
|
// Value 实现 driver.Valuer 接口,支持将 CBEventStatus 存为字符串
|
|
func (s CBEventStatus) Value() (driver.Value, error) {
|
|
return s.Enum(), nil
|
|
}
|