business_conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "strings"
  7. "time"
  8. )
  9. type BusinessConf struct {
  10. Id int `gorm:"column:id;primaryKey;autoIncrement"`
  11. ConfKey string `gorm:"column:conf_key;type:varchar(128);not null;default:'';uniqueIndex:idx_conf_key"`
  12. ConfVal string `gorm:"column:conf_val;type:text"`
  13. ValType int `gorm:"column:val_type;not null;default:0"`
  14. Necessary int `gorm:"column:necessary;not null;default:0"`
  15. Remark string `gorm:"column:remark;type:varchar(128);not null;default:''"`
  16. CreateTime *time.Time `gorm:"column:create_time"`
  17. }
  18. func (m *BusinessConf) TableName() string {
  19. return "business_conf"
  20. }
  21. func (m *BusinessConf) PrimaryId() string {
  22. return "id"
  23. }
  24. func (m *BusinessConf) Create() (err error) {
  25. err = global.DmSQL["master"].Create(m).Error
  26. if err != nil {
  27. return
  28. }
  29. return
  30. }
  31. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  32. if len(items) == 0 {
  33. return
  34. }
  35. err = global.DmSQL["master"].CreateInBatches(items, 500).Error
  36. return
  37. }
  38. func (m *BusinessConf) Update(cols []string) (err error) {
  39. err = global.DmSQL["master"].Model(m).Select(cols).Updates(m).Error
  40. return
  41. }
  42. func (m *BusinessConf) Del() (err error) {
  43. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  44. err = global.DmSQL["master"].Exec(sql, m.Id).Error
  45. return
  46. }
  47. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  48. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  49. err = global.DmSQL["master"].Raw(sql, id).First(&item).Error
  50. return
  51. }
  52. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  53. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  54. err = global.DmSQL["master"].Raw(sql, pars...).First(&item).Error
  55. return
  56. }
  57. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  58. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  59. err = global.DmSQL["master"].Raw(sql, pars...).Scan(&count).Error
  60. return
  61. }
  62. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  63. fields := strings.Join(fieldArr, ",")
  64. if len(fieldArr) == 0 {
  65. fields = `*`
  66. }
  67. order := `ORDER BY create_time DESC`
  68. if orderRule != "" {
  69. order = ` ORDER BY ` + orderRule
  70. }
  71. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  72. err = global.DmSQL["master"].Raw(sql, pars...).Scan(&items).Error
  73. return
  74. }
  75. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  76. fields := strings.Join(fieldArr, ",")
  77. if len(fieldArr) == 0 {
  78. fields = `*`
  79. }
  80. order := `ORDER BY create_time DESC`
  81. if orderRule != "" {
  82. order = ` ORDER BY ` + orderRule
  83. }
  84. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  85. pars = append(pars, startSize, pageSize)
  86. err = global.DmSQL["master"].Raw(sql, pars...).Scan(&items).Error
  87. return
  88. }
  89. type BusinessConfUpdate struct {
  90. ConfKey string
  91. ConfVal string
  92. }
  93. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  94. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  95. err = global.DmSQL["master"].Raw(sql, key).First(&item).Error
  96. return
  97. }
  98. func InitUseMongoConf() {
  99. useMongo, e := GetBusinessConfByKey("UseMongo")
  100. if e != nil {
  101. return
  102. }
  103. if useMongo.ConfVal == `true` {
  104. utils.UseMongo = true
  105. }
  106. }
  107. func InitSmmDataMethodConf() {
  108. utils.SmmDataMethod = "terminal"
  109. useMongo, e := GetBusinessConfByKey("SmmDataMethod")
  110. if e != nil {
  111. return
  112. }
  113. if useMongo.ConfVal == "api" {
  114. utils.SmmDataMethod = "api"
  115. }
  116. }