business_conf.go 4.7 KB

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