business_conf.go 4.1 KB

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