business_conf.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. BusinessConfLoginSmsTpId = "LoginSmsTpId"
  22. BusinessConfLoginSmsGjTpId = "LoginSmsGjTpId"
  23. BusinessConfSmsJhgnAppKey = "SmsJhgnAppKey"
  24. BusinessConfSmsJhgjAppKey = "SmsJhgjAppKey"
  25. BusinessConfLdapHost = "LdapHost"
  26. BusinessConfLdapBase = "LdapBase"
  27. BusinessConfLdapPort = "LdapPort"
  28. BusinessConfEmailClient = "EmailClient"
  29. BusinessConfEmailServerHost = "EmailServerHost"
  30. BusinessConfEmailServerPort = "EmailServerPort"
  31. BusinessConfEmailSender = "EmailSender"
  32. BusinessConfEmailSenderUserName = "EmailSenderUserName"
  33. BusinessConfEmailSenderPassword = "EmailSenderPassword"
  34. BusinessConfSmsClient = "SmsClient"
  35. BusinessConfNanHuaSmsAppKey = "NanHuaSmsAppKey"
  36. BusinessConfNanHuaSmsAppSecret = "NanHuaSmsAppSecret"
  37. BusinessConfNanHuaSmsApiHost = "NanHuaSmsApiHost"
  38. BusinessConfLoginSmsTplContent = "LoginSmsTplContent"
  39. BusinessConfLoginEmailTemplateSubject = "LoginEmailTemplateSubject"
  40. BusinessConfLoginEmailTemplateContent = "LoginEmailTemplateContent"
  41. BusinessConfLdapBindUserSuffix = "LdapBindUserSuffix"
  42. BusinessConfLdapUserFilter = "LdapUserFilter"
  43. BusinessConfTencentApiSecretId = "TencentApiSecretId" // 腾讯云API-密钥对
  44. BusinessConfTencentApiSecretKey = "TencentApiSecretKey" // 腾讯云API-密钥对
  45. BusinessConfTencentApiRecTaskCallbackUrl = "TencentApiRecTaskCallbackUrl" // 腾讯云API-语音识别回调地址
  46. BusinessConfSmsJhgjVariable = "SmsJhgjVariable" // 聚合国际短信变量
  47. )
  48. const (
  49. BusinessConfReportApproveTypeEta = "eta"
  50. BusinessConfReportApproveTypeOther = "other"
  51. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  52. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  53. )
  54. // FromSceneMap 数据源名称与数据源ID的对应关系
  55. var FromSceneMap = map[int]string{
  56. 1: "SmartReportSheetSize",
  57. 2: "ReportSheetSize",
  58. 3: "EnReportSheetSize",
  59. 4: "CnPptSheetSize",
  60. 5: "EnPptSheetSize",
  61. }
  62. // BusinessConf 商户配置表
  63. type BusinessConf struct {
  64. Id int `orm:"column(id);pk"`
  65. ConfKey string `description:"配置Key"`
  66. ConfVal string `description:"配置值"`
  67. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  68. Necessary int `description:"是否必填:0-否;1-是"`
  69. Remark string `description:"备注"`
  70. CreateTime time.Time
  71. }
  72. func (m *BusinessConf) TableName() string {
  73. return "business_conf"
  74. }
  75. func (m *BusinessConf) PrimaryId() string {
  76. return "id"
  77. }
  78. func (m *BusinessConf) Create() (err error) {
  79. o := orm.NewOrm()
  80. id, err := o.Insert(m)
  81. if err != nil {
  82. return
  83. }
  84. m.Id = int(id)
  85. return
  86. }
  87. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  88. if len(items) == 0 {
  89. return
  90. }
  91. o := orm.NewOrm()
  92. _, err = o.InsertMulti(len(items), items)
  93. return
  94. }
  95. func (m *BusinessConf) Update(cols []string) (err error) {
  96. o := orm.NewOrm()
  97. _, err = o.Update(m, cols...)
  98. return
  99. }
  100. func (m *BusinessConf) Del() (err error) {
  101. o := orm.NewOrm()
  102. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  103. _, err = o.Raw(sql, m.Id).Exec()
  104. return
  105. }
  106. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  107. o := orm.NewOrm()
  108. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  109. err = o.Raw(sql, id).QueryRow(&item)
  110. return
  111. }
  112. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  113. o := orm.NewOrm()
  114. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  115. err = o.Raw(sql, pars).QueryRow(&item)
  116. return
  117. }
  118. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  119. o := orm.NewOrm()
  120. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  121. err = o.Raw(sql, pars).QueryRow(&count)
  122. return
  123. }
  124. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  125. o := orm.NewOrm()
  126. fields := strings.Join(fieldArr, ",")
  127. if len(fieldArr) == 0 {
  128. fields = `*`
  129. }
  130. order := `ORDER BY create_time DESC`
  131. if orderRule != "" {
  132. order = ` ORDER BY ` + orderRule
  133. }
  134. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  135. _, err = o.Raw(sql, pars).QueryRows(&items)
  136. return
  137. }
  138. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  139. o := orm.NewOrm()
  140. fields := strings.Join(fieldArr, ",")
  141. if len(fieldArr) == 0 {
  142. fields = `*`
  143. }
  144. order := `ORDER BY create_time DESC`
  145. if orderRule != "" {
  146. order = ` ORDER BY ` + orderRule
  147. }
  148. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  149. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  150. return
  151. }
  152. // GetBusinessConf 获取商家配置
  153. func GetBusinessConf() (list map[string]string, err error) {
  154. list = make(map[string]string)
  155. var items []*BusinessConf
  156. o := orm.NewOrm()
  157. sql := `SELECT * FROM business_conf`
  158. _, err = o.Raw(sql).QueryRows(&items)
  159. if err != nil {
  160. return
  161. }
  162. for _, v := range items {
  163. if v.ValType == 4 {
  164. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  165. continue
  166. }
  167. list[v.ConfKey] = v.ConfVal
  168. }
  169. return
  170. }
  171. // BusinessConfUpdate 更新配置
  172. type BusinessConfUpdate struct {
  173. ConfKey string
  174. ConfVal string
  175. }
  176. // UpdateBusinessConfMulti 批量修改配置
  177. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  178. o := orm.NewOrm()
  179. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  180. if err != nil {
  181. return
  182. }
  183. defer func() {
  184. _ = p.Close()
  185. }()
  186. for _, v := range items {
  187. _, err = p.Exec(v.ConfVal, v.ConfKey)
  188. if err != nil {
  189. return
  190. }
  191. }
  192. return
  193. }
  194. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  195. o := orm.NewOrm()
  196. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  197. err = o.Raw(sql, key).QueryRow(&item)
  198. return
  199. }