2022-09-27 17:59:05 +08:00
|
|
|
package entity
|
|
|
|
|
2023-05-25 20:13:18 +08:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-08-29 16:47:38 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2023-05-25 20:13:18 +08:00
|
|
|
|
|
|
|
"github.com/answerdev/answer/pkg/converter"
|
|
|
|
)
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
// Config config
|
|
|
|
type Config struct {
|
2022-10-19 20:03:41 +08:00
|
|
|
ID int `xorm:"not null pk autoincr INT(11) id"`
|
2022-11-25 10:27:13 +08:00
|
|
|
Key string `xorm:"unique VARCHAR(128) key"`
|
2022-10-19 20:03:41 +08:00
|
|
|
Value string `xorm:"TEXT value"`
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// TableName config table name
|
2023-05-25 20:13:18 +08:00
|
|
|
func (c *Config) TableName() string {
|
2022-09-27 17:59:05 +08:00
|
|
|
return "config"
|
|
|
|
}
|
2023-05-25 20:13:18 +08:00
|
|
|
|
|
|
|
func (c *Config) BuildByJSON(data []byte) {
|
|
|
|
cf := &Config{}
|
|
|
|
_ = json.Unmarshal(data, cf)
|
|
|
|
c.ID = cf.ID
|
|
|
|
c.Key = cf.Key
|
|
|
|
c.Value = cf.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) JsonString() string {
|
|
|
|
data, _ := json.Marshal(c)
|
|
|
|
return string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIntValue get int value
|
|
|
|
func (c *Config) GetIntValue() int {
|
2023-08-29 16:47:38 +08:00
|
|
|
if len(c.Value) == 0 {
|
|
|
|
log.Warnf("config value is empty, key: %s, value: %s", c.Key, c.Value)
|
|
|
|
}
|
2023-05-25 20:13:18 +08:00
|
|
|
return converter.StringToInt(c.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetArrayStringValue get array string value
|
|
|
|
func (c *Config) GetArrayStringValue() []string {
|
|
|
|
var arr []string
|
|
|
|
_ = json.Unmarshal([]byte(c.Value), &arr)
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByteValue get byte value
|
|
|
|
func (c *Config) GetByteValue() []byte {
|
|
|
|
return []byte(c.Value)
|
|
|
|
}
|