business_conf.go 7.9 KB

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