business_conf.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package models
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "html"
  7. "strings"
  8. "time"
  9. )
  10. var (
  11. BusinessConfMap map[string]string
  12. )
  13. const (
  14. BusinessConfUseXf = "UseXf"
  15. BusinessConfXfAppid = "XfAppid"
  16. BusinessConfXfApiKey = "XfApiKey"
  17. BusinessConfXfApiSecret = "XfApiSecret"
  18. BusinessConfXfVcn = "XfVcn"
  19. BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
  20. BusinessConfIsReportApprove = "IsReportApprove"
  21. BusinessConfReportApproveType = "ReportApproveType"
  22. BusinessConfCompanyName = "CompanyName"
  23. BusinessConfCompanyWatermark = "CompanyWatermark"
  24. BusinessConfWatermarkChart = "WatermarkChart"
  25. BusinessConfLoginSmsTpId = "LoginSmsTpId"
  26. BusinessConfLoginSmsGjTpId = "LoginSmsGjTpId"
  27. BusinessConfSmsJhgnAppKey = "SmsJhgnAppKey"
  28. BusinessConfSmsJhgjAppKey = "SmsJhgjAppKey"
  29. BusinessConfLdapHost = "LdapHost"
  30. BusinessConfLdapBase = "LdapBase"
  31. BusinessConfLdapPort = "LdapPort"
  32. BusinessConfEmailClient = "EmailClient"
  33. BusinessConfEmailServerHost = "EmailServerHost"
  34. BusinessConfEmailServerPort = "EmailServerPort"
  35. BusinessConfEmailSender = "EmailSender"
  36. BusinessConfEmailSenderUserName = "EmailSenderUserName"
  37. BusinessConfEmailSenderPassword = "EmailSenderPassword"
  38. BusinessConfSmsClient = "SmsClient"
  39. BusinessConfNanHuaSmsAppKey = "NanHuaSmsAppKey"
  40. BusinessConfNanHuaSmsAppSecret = "NanHuaSmsAppSecret"
  41. BusinessConfNanHuaSmsApiHost = "NanHuaSmsApiHost"
  42. BusinessConfLoginSmsTplContent = "LoginSmsTplContent"
  43. BusinessConfLoginEmailTemplateSubject = "LoginEmailTemplateSubject"
  44. BusinessConfLoginEmailTemplateContent = "LoginEmailTemplateContent"
  45. BusinessConfLdapBindUserSuffix = "LdapBindUserSuffix"
  46. BusinessConfLdapUserFilter = "LdapUserFilter"
  47. BusinessConfTencentApiSecretId = "TencentApiSecretId" // 腾讯云API-密钥对
  48. BusinessConfTencentApiSecretKey = "TencentApiSecretKey" // 腾讯云API-密钥对
  49. BusinessConfTencentApiRecTaskCallbackUrl = "TencentApiRecTaskCallbackUrl" // 腾讯云API-语音识别回调地址
  50. BusinessConfSmsJhgjVariable = "SmsJhgjVariable" // 聚合国际短信变量
  51. BusinessConfEsIndexNameExcel = "EsIndexNameExcel" // ES索引名称-表格
  52. BusinessConfEsIndexNameDataSource = "EsIndexNameDataSource" // ES索引名称-数据源
  53. )
  54. const (
  55. BusinessConfReportApproveTypeEta = "eta"
  56. BusinessConfReportApproveTypeOther = "other"
  57. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  58. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  59. )
  60. // FromSceneMap 数据源名称与数据源ID的对应关系
  61. var FromSceneMap = map[int]string{
  62. 1: "SmartReportSheetSize",
  63. 2: "ReportSheetSize",
  64. 3: "EnReportSheetSize",
  65. 4: "CnPptSheetSize",
  66. 5: "EnPptSheetSize",
  67. }
  68. // BusinessConf 商户配置表
  69. type BusinessConf struct {
  70. Id int `orm:"column(id);pk"`
  71. ConfKey string `description:"配置Key"`
  72. ConfVal string `description:"配置值"`
  73. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  74. Necessary int `description:"是否必填:0-否;1-是"`
  75. Remark string `description:"备注"`
  76. CreateTime time.Time
  77. }
  78. func (m *BusinessConf) TableName() string {
  79. return "business_conf"
  80. }
  81. func (m *BusinessConf) PrimaryId() string {
  82. return "id"
  83. }
  84. func (m *BusinessConf) Create() (err error) {
  85. o := orm.NewOrm()
  86. id, err := o.Insert(m)
  87. if err != nil {
  88. return
  89. }
  90. m.Id = int(id)
  91. return
  92. }
  93. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  94. if len(items) == 0 {
  95. return
  96. }
  97. o := orm.NewOrm()
  98. _, err = o.InsertMulti(len(items), items)
  99. return
  100. }
  101. func (m *BusinessConf) Update(cols []string) (err error) {
  102. o := orm.NewOrm()
  103. _, err = o.Update(m, cols...)
  104. return
  105. }
  106. func (m *BusinessConf) Del() (err error) {
  107. o := orm.NewOrm()
  108. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  109. _, err = o.Raw(sql, m.Id).Exec()
  110. return
  111. }
  112. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  113. o := orm.NewOrm()
  114. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  115. err = o.Raw(sql, id).QueryRow(&item)
  116. return
  117. }
  118. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  119. o := orm.NewOrm()
  120. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  121. err = o.Raw(sql, pars).QueryRow(&item)
  122. return
  123. }
  124. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  125. o := orm.NewOrm()
  126. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  127. err = o.Raw(sql, pars).QueryRow(&count)
  128. return
  129. }
  130. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  131. o := orm.NewOrm()
  132. fields := strings.Join(fieldArr, ",")
  133. if len(fieldArr) == 0 {
  134. fields = `*`
  135. }
  136. order := `ORDER BY create_time DESC`
  137. if orderRule != "" {
  138. order = ` ORDER BY ` + orderRule
  139. }
  140. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  141. _, err = o.Raw(sql, pars).QueryRows(&items)
  142. return
  143. }
  144. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  145. o := orm.NewOrm()
  146. fields := strings.Join(fieldArr, ",")
  147. if len(fieldArr) == 0 {
  148. fields = `*`
  149. }
  150. order := `ORDER BY create_time DESC`
  151. if orderRule != "" {
  152. order = ` ORDER BY ` + orderRule
  153. }
  154. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  155. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  156. return
  157. }
  158. // GetBusinessConf 获取商家配置
  159. func GetBusinessConf() (list map[string]string, err error) {
  160. list = make(map[string]string)
  161. var items []*BusinessConf
  162. o := orm.NewOrm()
  163. sql := `SELECT * FROM business_conf`
  164. _, err = o.Raw(sql).QueryRows(&items)
  165. if err != nil {
  166. return
  167. }
  168. for _, v := range items {
  169. if v.ValType == 4 {
  170. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  171. continue
  172. }
  173. list[v.ConfKey] = v.ConfVal
  174. }
  175. return
  176. }
  177. // BusinessConfUpdate 更新配置
  178. type BusinessConfUpdate struct {
  179. ConfKey string
  180. ConfVal string
  181. }
  182. // UpdateBusinessConfMulti 批量修改配置
  183. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  184. o := orm.NewOrm()
  185. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  186. if err != nil {
  187. return
  188. }
  189. defer func() {
  190. _ = p.Close()
  191. }()
  192. for _, v := range items {
  193. _, err = p.Exec(v.ConfVal, v.ConfKey)
  194. if err != nil {
  195. return
  196. }
  197. }
  198. return
  199. }
  200. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  201. o := orm.NewOrm()
  202. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  203. err = o.Raw(sql, key).QueryRow(&item)
  204. return
  205. }
  206. // InitUseMongoConf
  207. // @Description:
  208. // @author: Roc
  209. // @datetime 2024-07-01 13:49:09
  210. func InitUseMongoConf() {
  211. useMongo, e := GetBusinessConfByKey("UseMongo")
  212. if e != nil {
  213. return
  214. }
  215. if useMongo.ConfVal == `true` {
  216. utils.UseMongo = true
  217. }
  218. }
  219. func InitBusinessConf() {
  220. var e error
  221. BusinessConfMap, e = GetBusinessConf()
  222. if e != nil {
  223. return
  224. }
  225. // ES索引名称
  226. if BusinessConfMap[BusinessConfEsIndexNameExcel] != "" {
  227. utils.EsExcelIndexName = BusinessConfMap[BusinessConfEsIndexNameExcel]
  228. }
  229. if BusinessConfMap[BusinessConfEsIndexNameDataSource] != "" {
  230. utils.EsDataSourceIndexName = BusinessConfMap[BusinessConfEsIndexNameDataSource]
  231. }
  232. }