2017-04-21 18:20:35 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
"github.com/lifei6671/godoc/conf"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// Option struct .
|
|
|
|
type Option struct {
|
2017-05-01 20:08:03 +08:00
|
|
|
OptionId int `orm:"column(option_id);pk;auto;unique;" json:"option_id"`
|
2017-04-21 18:20:35 +08:00
|
|
|
OptionTitle string `orm:"column(option_title);size(500)" json:"option_title"`
|
|
|
|
OptionName string `orm:"column(option_name);unique;size(80)" json:"option_name"`
|
|
|
|
OptionValue string `orm:"column(option_value);type(text);null" json:"option_value"`
|
|
|
|
Remark string `orm:"column(remark);type(text);null" json:"remark"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName 获取对应数据库表名.
|
|
|
|
func (m *Option) TableName() string {
|
|
|
|
return "options"
|
|
|
|
}
|
|
|
|
// TableEngine 获取数据使用的引擎.
|
|
|
|
func (m *Option) TableEngine() string {
|
|
|
|
return "INNODB"
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
func (m *Option)TableNameWithPrefix() string {
|
|
|
|
return conf.GetDatabasePrefix() + m.TableName()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
func NewOption() *Option {
|
|
|
|
return &Option{}
|
|
|
|
}
|
|
|
|
|
2017-05-03 17:09:01 +08:00
|
|
|
func (p *Option) Find(id int) (*Option,error) {
|
2017-04-21 18:20:35 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
p.OptionId = id
|
|
|
|
|
|
|
|
if err := o.Read(p);err != nil {
|
2017-05-03 17:09:01 +08:00
|
|
|
return p,err
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2017-05-03 17:09:01 +08:00
|
|
|
return p,nil
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-03 17:09:01 +08:00
|
|
|
func (p *Option) FindByKey(key string) (*Option,error) {
|
2017-04-21 18:20:35 +08:00
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
p.OptionName = key
|
|
|
|
if err := o.Read(p);err != nil {
|
2017-05-03 17:09:01 +08:00
|
|
|
return p,err
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
2017-05-03 17:09:01 +08:00
|
|
|
return p,nil
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetOptionValue(key, def string) string {
|
|
|
|
|
2017-05-03 17:39:27 +08:00
|
|
|
if option,err := NewOption().FindByKey(key); err == nil {
|
2017-04-21 18:20:35 +08:00
|
|
|
return option.OptionValue
|
|
|
|
}
|
|
|
|
return def
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Option) InsertOrUpdate() error {
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
2017-04-30 22:13:12 +08:00
|
|
|
|
2017-04-21 18:20:35 +08:00
|
|
|
var err error
|
2017-05-31 15:26:51 +08:00
|
|
|
|
|
|
|
|
|
|
|
if p.OptionId > 0 || o.QueryTable(p.TableNameWithPrefix()).Filter("option_name",p.OptionName).Exist() {
|
2017-04-30 22:13:12 +08:00
|
|
|
_,err = o.Update(p)
|
2017-04-21 18:20:35 +08:00
|
|
|
}else{
|
|
|
|
_,err = o.Insert(p)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Option) InsertMulti(option... Option ) (error){
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
_,err := o.InsertMulti(len(option),option)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
func (p *Option) All() ([]*Option,error) {
|
2017-04-21 18:20:35 +08:00
|
|
|
o := orm.NewOrm()
|
2017-04-26 18:17:38 +08:00
|
|
|
var options []*Option
|
2017-04-21 18:20:35 +08:00
|
|
|
|
2017-04-26 18:17:38 +08:00
|
|
|
_,err := o.QueryTable(p.TableNameWithPrefix()).All(&options)
|
2017-04-21 18:20:35 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return options,err
|
|
|
|
}
|
|
|
|
return options,nil
|
2017-05-31 15:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Option) Init() error {
|
|
|
|
|
|
|
|
o := orm.NewOrm()
|
|
|
|
|
|
|
|
if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLED_REGISTER").Exist() {
|
|
|
|
option := NewOption()
|
|
|
|
option.OptionValue = "false"
|
|
|
|
option.OptionName = "ENABLED_REGISTER"
|
|
|
|
option.OptionTitle = "是否启用注册"
|
|
|
|
if _,err := o.Insert(option);err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLE_DOCUMENT_HISTORY").Exist() {
|
|
|
|
option := NewOption()
|
|
|
|
option.OptionValue = "true"
|
|
|
|
option.OptionName = "ENABLE_DOCUMENT_HISTORY"
|
|
|
|
option.OptionTitle = "是否启用文档历史"
|
|
|
|
if _,err := o.Insert(option);err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLED_CAPTCHA").Exist() {
|
|
|
|
option := NewOption()
|
|
|
|
option.OptionValue = "true"
|
|
|
|
option.OptionName = "ENABLED_CAPTCHA"
|
|
|
|
option.OptionTitle = "是否启用验证码"
|
|
|
|
if _,err := o.Insert(option);err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLE_ANONYMOUS").Exist() {
|
|
|
|
option := NewOption()
|
|
|
|
option.OptionValue = "false"
|
|
|
|
option.OptionName = "ENABLE_ANONYMOUS"
|
|
|
|
option.OptionTitle = "启用匿名访问"
|
|
|
|
if _,err := o.Insert(option);err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","SITE_NAME").Exist() {
|
|
|
|
option := NewOption()
|
|
|
|
option.OptionValue = "MinDoc"
|
|
|
|
option.OptionName = "SITE_NAME"
|
|
|
|
option.OptionTitle = "站点名称"
|
|
|
|
if _,err := o.Insert(option);err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-04-21 18:20:35 +08:00
|
|
|
}
|