business_conf.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "html"
  6. "strings"
  7. "time"
  8. )
  9. const (
  10. BusinessConfUseXf = "UseXf"
  11. BusinessConfXfAppid = "XfAppid"
  12. BusinessConfXfApiKey = "XfApiKey"
  13. BusinessConfXfApiSecret = "XfApiSecret"
  14. BusinessConfXfVcn = "XfVcn"
  15. BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
  16. )
  17. // BusinessConf 商户配置表
  18. type BusinessConf struct {
  19. Id int `orm:"column(id);pk"`
  20. ConfKey string `description:"配置Key"`
  21. ConfVal string `description:"配置值"`
  22. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  23. Necessary int `description:"是否必填:0-否;1-是"`
  24. Remark string `description:"备注"`
  25. CreateTime time.Time
  26. }
  27. func (m *BusinessConf) TableName() string {
  28. return "business_conf"
  29. }
  30. func (m *BusinessConf) PrimaryId() string {
  31. return "id"
  32. }
  33. func (m *BusinessConf) Create() (err error) {
  34. o := orm.NewOrmUsingDB("eta")
  35. id, err := o.Insert(m)
  36. if err != nil {
  37. return
  38. }
  39. m.Id = int(id)
  40. return
  41. }
  42. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  43. if len(items) == 0 {
  44. return
  45. }
  46. o := orm.NewOrmUsingDB("eta")
  47. _, err = o.InsertMulti(len(items), items)
  48. return
  49. }
  50. func (m *BusinessConf) Update(cols []string) (err error) {
  51. o := orm.NewOrmUsingDB("eta")
  52. _, err = o.Update(m, cols...)
  53. return
  54. }
  55. func (m *BusinessConf) Del() (err error) {
  56. o := orm.NewOrmUsingDB("eta")
  57. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  58. _, err = o.Raw(sql, m.Id).Exec()
  59. return
  60. }
  61. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  62. o := orm.NewOrmUsingDB("eta")
  63. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  64. err = o.Raw(sql, id).QueryRow(&item)
  65. return
  66. }
  67. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  68. o := orm.NewOrmUsingDB("eta")
  69. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  70. err = o.Raw(sql, pars).QueryRow(&item)
  71. return
  72. }
  73. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  74. o := orm.NewOrmUsingDB("eta")
  75. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  76. err = o.Raw(sql, pars).QueryRow(&count)
  77. return
  78. }
  79. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  80. o := orm.NewOrmUsingDB("eta")
  81. fields := strings.Join(fieldArr, ",")
  82. if len(fieldArr) == 0 {
  83. fields = `*`
  84. }
  85. order := `ORDER BY create_time DESC`
  86. if orderRule != "" {
  87. order = ` ORDER BY ` + orderRule
  88. }
  89. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  90. _, err = o.Raw(sql, pars).QueryRows(&items)
  91. return
  92. }
  93. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  94. o := orm.NewOrmUsingDB("eta")
  95. fields := strings.Join(fieldArr, ",")
  96. if len(fieldArr) == 0 {
  97. fields = `*`
  98. }
  99. order := `ORDER BY create_time DESC`
  100. if orderRule != "" {
  101. order = ` ORDER BY ` + orderRule
  102. }
  103. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  104. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  105. return
  106. }
  107. // GetBusinessConf 获取商家配置
  108. func GetBusinessConf() (list map[string]string, err error) {
  109. list = make(map[string]string)
  110. var items []*BusinessConf
  111. o := orm.NewOrmUsingDB("eta")
  112. sql := `SELECT * FROM business_conf`
  113. _, err = o.Raw(sql).QueryRows(&items)
  114. if err != nil {
  115. return
  116. }
  117. for _, v := range items {
  118. if v.ValType == 4 {
  119. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  120. continue
  121. }
  122. list[v.ConfKey] = v.ConfVal
  123. }
  124. return
  125. }
  126. // BusinessConfUpdate 更新配置
  127. type BusinessConfUpdate struct {
  128. ConfKey string
  129. ConfVal string
  130. }
  131. // UpdateBusinessConfMulti 批量修改配置
  132. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  133. o := orm.NewOrmUsingDB("eta")
  134. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  135. if err != nil {
  136. return
  137. }
  138. defer func() {
  139. _ = p.Close()
  140. }()
  141. for _, v := range items {
  142. _, err = p.Exec(v.ConfVal, v.ConfKey)
  143. if err != nil {
  144. return
  145. }
  146. }
  147. return
  148. }
  149. func (m *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) {
  150. o := orm.NewOrmUsingDB("eta")
  151. sql := fmt.Sprintf(`SELECT * FROM %s WHERE conf_key = ? LIMIT 1`, m.TableName())
  152. err = o.Raw(sql, key).QueryRow(&item)
  153. return
  154. }
  155. type EdbStopRefreshRule struct {
  156. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  157. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  158. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  159. }