Files
hi-server/internal/model/promo_rule/promo_rule.go
T
2026-05-26 22:11:02 -07:00

74 lines
2.0 KiB
Go

package promo_rule
import (
"database/sql/driver"
"encoding/json"
"time"
"gorm.io/gorm"
)
type JSONRawMessage json.RawMessage
func (j *JSONRawMessage) Scan(value interface{}) error {
if value == nil {
*j = JSONRawMessage([]byte("{}"))
return nil
}
switch v := value.(type) {
case []byte:
*j = JSONRawMessage(v)
case string:
*j = JSONRawMessage([]byte(v))
default:
*j = JSONRawMessage([]byte("{}"))
}
return nil
}
func (j JSONRawMessage) Value() (driver.Value, error) {
if len(j) == 0 {
return "{}", nil
}
data, err := json.RawMessage(j).MarshalJSON()
if err != nil {
return nil, err
}
return string(data), nil
}
func (j JSONRawMessage) MarshalJSON() ([]byte, error) {
if len(j) == 0 {
return []byte("{}"), nil
}
return json.RawMessage(j).MarshalJSON()
}
func (j *JSONRawMessage) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
*j = JSONRawMessage([]byte("{}"))
return nil
}
*j = JSONRawMessage(data)
return nil
}
type PromoRule struct {
Id int64 `gorm:"type:bigint unsigned;primaryKey"`
Name string `gorm:"type:varchar(100);not null;default:'';comment:Rule Name"`
Type string `gorm:"type:varchar(32);not null;default:'';comment:Rule Type: new_user, inactive_user, campaign"`
Params JSONRawMessage `gorm:"type:json;not null;comment:Rule Params"`
Priority int64 `gorm:"type:int;not null;default:0;index:idx_enabled_priority,priority:2,sort:desc;comment:Priority"`
Enabled bool `gorm:"type:tinyint(1);not null;default:1;index:idx_enabled_priority,priority:1;comment:Enabled"`
StartTime *time.Time `gorm:"type:datetime;default:null;comment:Start Time"`
EndTime *time.Time `gorm:"type:datetime;default:null;comment:End Time"`
CreatedAt time.Time `gorm:"<-:create;comment:Create Time"`
UpdatedAt time.Time `gorm:"comment:Update Time"`
DeletedAt gorm.DeletedAt `gorm:"index;comment:Delete Time"`
}
func (PromoRule) TableName() string {
return "promo_rule"
}