business_conf.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. BusinessOpenOaMsg = "OpenOaMsg" // 是否开启OA消息推送
  18. BusinessOaReportHost = "OaReportHost" // OA推送跳转报告Host
  19. )
  20. // BusinessConf 商户配置表
  21. type BusinessConf struct {
  22. Id int `gorm:"column:id;primaryKey"` // `orm:"column(id);pk"`
  23. ConfKey string `description:"配置Key"`
  24. ConfVal string `description:"配置值"`
  25. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  26. Necessary int `description:"是否必填:0-否;1-是"`
  27. Remark string `description:"备注"`
  28. CreateTime time.Time
  29. }
  30. func (m *BusinessConf) TableName() string {
  31. return "business_conf"
  32. }
  33. func (m *BusinessConf) PrimaryId() string {
  34. return "id"
  35. }
  36. func (m *BusinessConf) Create() (err error) {
  37. //o := orm.NewOrmUsingDB("eta")
  38. //id, err := o.Insert(m)
  39. //if err != nil {
  40. // return
  41. //}
  42. //m.Id = int(id)
  43. err = global.DmSQL["eta"].Create(m).Error
  44. return
  45. }
  46. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  47. if len(items) == 0 {
  48. return
  49. }
  50. //o := orm.NewOrmUsingDB("eta")
  51. //_, err = o.InsertMulti(len(items), items)
  52. err = global.DmSQL["eta"].CreateInBatches(items, utils.MultiAddNum).Error
  53. return
  54. }
  55. func (m *BusinessConf) Update(cols []string) (err error) {
  56. //o := orm.NewOrmUsingDB("eta")
  57. //_, err = o.Update(m, cols...)
  58. err = global.DmSQL["eta"].Select(cols).Updates(m).Error
  59. return
  60. }
  61. func (m *BusinessConf) Del() (err error) {
  62. //o := orm.NewOrmUsingDB("eta")
  63. //sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  64. //_, err = o.Raw(sql, m.Id).Exec()
  65. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  66. err = global.DmSQL["eta"].Exec(sql, m.Id).Error
  67. return
  68. }
  69. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  70. //o := orm.NewOrmUsingDB("eta")
  71. //sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  72. //err = o.Raw(sql, id).QueryRow(&item)
  73. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  74. err = global.DmSQL["eta"].Raw(sql, id).Find(&item).Error
  75. return
  76. }
  77. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  78. //o := orm.NewOrmUsingDB("eta")
  79. //sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  80. //err = o.Raw(sql, pars).QueryRow(&item)
  81. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  82. err = global.DmSQL["eta"].Raw(sql, pars...).First(&item).Error
  83. return
  84. }
  85. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  86. //o := orm.NewOrmUsingDB("eta")
  87. //sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  88. //err = o.Raw(sql, pars).QueryRow(&count)
  89. sql := fmt.Sprintf(`SELECT COUNT(1) AS count FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  90. err = global.DmSQL["eta"].Raw(sql, pars...).Scan(&count).Error
  91. return
  92. }
  93. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (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`, fields, m.TableName(), condition, order)
  104. //_, err = o.Raw(sql, pars).QueryRows(&items)
  105. fields := strings.Join(fieldArr, ",")
  106. if len(fieldArr) == 0 {
  107. fields = `*`
  108. }
  109. order := `ORDER BY create_time DESC`
  110. if orderRule != "" {
  111. order = ` ORDER BY ` + orderRule
  112. }
  113. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  114. err = global.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error
  115. return
  116. }
  117. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  118. //o := orm.NewOrmUsingDB("eta")
  119. //fields := strings.Join(fieldArr, ",")
  120. //if len(fieldArr) == 0 {
  121. // fields = `*`
  122. //}
  123. //order := `ORDER BY create_time DESC`
  124. //if orderRule != "" {
  125. // order = ` ORDER BY ` + orderRule
  126. //}
  127. //sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  128. //_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  129. fields := strings.Join(fieldArr, ",")
  130. if len(fieldArr) == 0 {
  131. fields = `*`
  132. }
  133. order := `ORDER BY create_time DESC`
  134. if orderRule != "" {
  135. order = ` ORDER BY ` + orderRule
  136. }
  137. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  138. pars = append(pars, startSize, pageSize)
  139. err = global.DmSQL["eta"].Raw(sql, pars...).Find(&items).Error
  140. return
  141. }
  142. // GetBusinessConf 获取商家配置
  143. func GetBusinessConf() (list map[string]string, err error) {
  144. list = make(map[string]string)
  145. var items []*BusinessConf
  146. //o := orm.NewOrmUsingDB("eta")
  147. //sql := `SELECT * FROM business_conf`
  148. //_, err = o.Raw(sql).QueryRows(&items)
  149. //if err != nil {
  150. // return
  151. //}
  152. sql := `SELECT * FROM business_conf`
  153. err = global.DmSQL["eta"].Raw(sql).Find(&items).Error
  154. if err != nil {
  155. return
  156. }
  157. for _, v := range items {
  158. if v.ValType == 4 {
  159. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  160. continue
  161. }
  162. list[v.ConfKey] = v.ConfVal
  163. }
  164. return
  165. }
  166. // BusinessConfUpdate 更新配置
  167. type BusinessConfUpdate struct {
  168. ConfKey string
  169. ConfVal string
  170. }
  171. //// UpdateBusinessConfMulti 批量修改配置
  172. //func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  173. // o := orm.NewOrmUsingDB("eta")
  174. // p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  175. // if err != nil {
  176. // return
  177. // }
  178. // defer func() {
  179. // _ = p.Close()
  180. // }()
  181. // for _, v := range items {
  182. // _, err = p.Exec(v.ConfVal, v.ConfKey)
  183. // if err != nil {
  184. // return
  185. // }
  186. // }
  187. // return
  188. //}
  189. func (m *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) {
  190. //o := orm.NewOrmUsingDB("eta")
  191. //sql := fmt.Sprintf(`SELECT * FROM %s WHERE conf_key = ? LIMIT 1`, m.TableName())
  192. //err = o.Raw(sql, key).QueryRow(&item)
  193. sql := fmt.Sprintf(`SELECT * FROM %s WHERE conf_key = ? LIMIT 1`, m.TableName())
  194. err = global.DmSQL["eta"].Raw(sql, key).First(&item).Error
  195. return
  196. }
  197. type EdbStopRefreshRule struct {
  198. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  199. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  200. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  201. }
  202. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  203. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  204. err = global.DmSQL["eta"].Raw(sql, key).First(&item).Error
  205. return
  206. }