business_conf.go 5.7 KB

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