options.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package models
  2. import (
  3. "github.com/astaxie/beego/orm"
  4. "github.com/lifei6671/mindoc/conf"
  5. )
  6. // Option struct .
  7. type Option struct {
  8. OptionId int `orm:"column(option_id);pk;auto;unique;" json:"option_id"`
  9. OptionTitle string `orm:"column(option_title);size(500)" json:"option_title"`
  10. OptionName string `orm:"column(option_name);unique;size(80)" json:"option_name"`
  11. OptionValue string `orm:"column(option_value);type(text);null" json:"option_value"`
  12. Remark string `orm:"column(remark);type(text);null" json:"remark"`
  13. }
  14. // TableName 获取对应数据库表名.
  15. func (m *Option) TableName() string {
  16. return "options"
  17. }
  18. // TableEngine 获取数据使用的引擎.
  19. func (m *Option) TableEngine() string {
  20. return "INNODB"
  21. }
  22. func (m *Option)TableNameWithPrefix() string {
  23. return conf.GetDatabasePrefix() + m.TableName()
  24. }
  25. func NewOption() *Option {
  26. return &Option{}
  27. }
  28. func (p *Option) Find(id int) (*Option,error) {
  29. o := orm.NewOrm()
  30. p.OptionId = id
  31. if err := o.Read(p);err != nil {
  32. return p,err
  33. }
  34. return p,nil
  35. }
  36. func (p *Option) FindByKey(key string) (*Option,error) {
  37. o := orm.NewOrm()
  38. p.OptionName = key
  39. if err := o.Read(p);err != nil {
  40. return p,err
  41. }
  42. return p,nil
  43. }
  44. func GetOptionValue(key, def string) string {
  45. if option,err := NewOption().FindByKey(key); err == nil {
  46. return option.OptionValue
  47. }
  48. return def
  49. }
  50. func (p *Option) InsertOrUpdate() error {
  51. o := orm.NewOrm()
  52. var err error
  53. if p.OptionId > 0 || o.QueryTable(p.TableNameWithPrefix()).Filter("option_name",p.OptionName).Exist() {
  54. _,err = o.Update(p)
  55. }else{
  56. _,err = o.Insert(p)
  57. }
  58. return err
  59. }
  60. func (p *Option) InsertMulti(option... Option ) (error){
  61. o := orm.NewOrm()
  62. _,err := o.InsertMulti(len(option),option)
  63. return err
  64. }
  65. func (p *Option) All() ([]*Option,error) {
  66. o := orm.NewOrm()
  67. var options []*Option
  68. _,err := o.QueryTable(p.TableNameWithPrefix()).All(&options)
  69. if err != nil {
  70. return options,err
  71. }
  72. return options,nil
  73. }
  74. func (m *Option) Init() error {
  75. o := orm.NewOrm()
  76. if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLED_REGISTER").Exist() {
  77. option := NewOption()
  78. option.OptionValue = "false"
  79. option.OptionName = "ENABLED_REGISTER"
  80. option.OptionTitle = "是否启用注册"
  81. if _,err := o.Insert(option);err != nil {
  82. return err
  83. }
  84. }
  85. if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLE_DOCUMENT_HISTORY").Exist() {
  86. option := NewOption()
  87. option.OptionValue = "true"
  88. option.OptionName = "ENABLE_DOCUMENT_HISTORY"
  89. option.OptionTitle = "是否启用文档历史"
  90. if _,err := o.Insert(option);err != nil {
  91. return err
  92. }
  93. }
  94. if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLED_CAPTCHA").Exist() {
  95. option := NewOption()
  96. option.OptionValue = "true"
  97. option.OptionName = "ENABLED_CAPTCHA"
  98. option.OptionTitle = "是否启用验证码"
  99. if _,err := o.Insert(option);err != nil {
  100. return err
  101. }
  102. }
  103. if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","ENABLE_ANONYMOUS").Exist() {
  104. option := NewOption()
  105. option.OptionValue = "false"
  106. option.OptionName = "ENABLE_ANONYMOUS"
  107. option.OptionTitle = "启用匿名访问"
  108. if _,err := o.Insert(option);err != nil {
  109. return err
  110. }
  111. }
  112. if !o.QueryTable(m.TableNameWithPrefix()).Filter("option_name","SITE_NAME").Exist() {
  113. option := NewOption()
  114. option.OptionValue = "MinDoc"
  115. option.OptionName = "SITE_NAME"
  116. option.OptionTitle = "站点名称"
  117. if _,err := o.Insert(option);err != nil {
  118. return err
  119. }
  120. }
  121. return nil
  122. }