business_conf.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package models
  2. import (
  3. "eta_gn/eta_task/global"
  4. "eta_gn/eta_task/utils"
  5. "fmt"
  6. "html"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. BusinessConfUseXf = "UseXf"
  12. BusinessConfXfAppid = "XfAppid"
  13. BusinessConfXfApiKey = "XfApiKey"
  14. BusinessConfXfApiSecret = "XfApiSecret"
  15. BusinessConfXfVcn = "XfVcn"
  16. BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
  17. )
  18. type BusinessConf struct {
  19. Id int `gorm:"column:id;primaryKey"` // `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. err = global.DmSQL["eta"].Create(m).Error
  35. return
  36. }
  37. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  38. if len(items) == 0 {
  39. return
  40. }
  41. err = global.DmSQL["eta"].CreateInBatches(items, utils.MultiAddNum).Error
  42. return
  43. }
  44. func (m *BusinessConf) Update(cols []string) (err error) {
  45. err = global.DmSQL["eta"].Select(cols).Updates(m).Error
  46. return
  47. }
  48. func (m *BusinessConf) Del() (err error) {
  49. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  50. err = global.DmSQL["eta"].Exec(sql, m.Id).Error
  51. return
  52. }
  53. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  54. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  55. err = global.DmSQL["eta"].Raw(sql, id).Find(&item).Error
  56. return
  57. }
  58. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  59. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  60. err = global.DmSQL["eta"].Raw(sql, pars...).First(&item).Error
  61. return
  62. }
  63. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  64. sql := fmt.Sprintf(`SELECT COUNT(1) AS count FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  65. err = global.DmSQL["eta"].Raw(sql, pars...).Scan(&count).Error
  66. return
  67. }
  68. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  69. fields := strings.Join(fieldArr, ",")
  70. if len(fieldArr) == 0 {
  71. fields = `*`
  72. }
  73. order := `ORDER BY create_time DESC`
  74. if orderRule != "" {
  75. order = ` ORDER BY ` + orderRule
  76. }
  77. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  78. err = global.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error
  79. return
  80. }
  81. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  82. fields := strings.Join(fieldArr, ",")
  83. if len(fieldArr) == 0 {
  84. fields = `*`
  85. }
  86. order := `ORDER BY create_time DESC`
  87. if orderRule != "" {
  88. order = ` ORDER BY ` + orderRule
  89. }
  90. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  91. pars = append(pars, startSize, pageSize)
  92. err = global.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error
  93. return
  94. }
  95. func GetBusinessConf() (list map[string]string, err error) {
  96. list = make(map[string]string)
  97. var items []*BusinessConf
  98. sql := `SELECT * FROM business_conf`
  99. err = global.DmSQL["eta"].Raw(sql).Find(&items).Error
  100. if err != nil {
  101. return
  102. }
  103. for _, v := range items {
  104. if v.ValType == 4 {
  105. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  106. continue
  107. }
  108. list[v.ConfKey] = v.ConfVal
  109. }
  110. return
  111. }
  112. type BusinessConfUpdate struct {
  113. ConfKey string
  114. ConfVal string
  115. }
  116. func (m *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) {
  117. sql := fmt.Sprintf(`SELECT * FROM %s WHERE conf_key = ? LIMIT 1`, m.TableName())
  118. err = global.DmSQL["eta"].Raw(sql, key).First(&item).Error
  119. return
  120. }
  121. type EdbStopRefreshRule struct {
  122. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  123. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  124. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  125. }