business_conf.go 4.8 KB

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