business_conf.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package models
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "fmt"
  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. BusinessConfEdbStopRefreshRule = "EdbStopRefreshRule" // 是否停止指标刷新规则
  52. BusinessConfReport2ImgUrl = "Report2ImgUrl" // 报告转长图地址(用于兼容内外网环境的)
  53. BusinessConfReportViewUrl = "ReportViewUrl" // 报告详情地址
  54. BusinessConfEsIndexNameExcel = "EsIndexNameExcel" // ES索引名称-表格
  55. BusinessConfEsIndexNameDataSource = "EsIndexNameDataSource" // ES索引名称-数据源
  56. BusinessConfIsOpenChartExpired = "IsOpenChartExpired" // 图表是否鉴权
  57. LLMInitConfig="llmInitConfig"
  58. )
  59. const (
  60. BusinessConfReportApproveTypeEta = "eta"
  61. BusinessConfReportApproveTypeOther = "other"
  62. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  63. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  64. )
  65. // FromSceneMap 数据源名称与数据源ID的对应关系
  66. var FromSceneMap = map[int]string{
  67. 1: "SmartReportSheetSize",
  68. 2: "ReportSheetSize",
  69. 3: "EnReportSheetSize",
  70. 4: "CnPptSheetSize",
  71. 5: "EnPptSheetSize",
  72. }
  73. // BusinessConf 商户配置表
  74. type BusinessConf struct {
  75. Id int `gorm:"column:id;primaryKey;autoIncrement"` //`orm:"column(id);pk" gorm:"primaryKey" `
  76. ConfKey string `gorm:"column:conf_key"` //`description:"配置Key"`
  77. ConfVal string `gorm:"column:conf_val"` //`description:"配置值"`
  78. ValType int `gorm:"column:val_type"` //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  79. Necessary int `gorm:"column:necessary"` //`description:"是否必填:0-否;1-是"`
  80. Remark string `gorm:"column:remark"` // `description:"备注"`
  81. CreateTime time.Time `gorm:"column:create_time"`
  82. }
  83. func (m *BusinessConf) TableName() string {
  84. return "business_conf"
  85. }
  86. func (m *BusinessConf) PrimaryId() string {
  87. return "id"
  88. }
  89. func (m *BusinessConf) Create() (err error) {
  90. err = global.DEFAULT_DB.Create(m).Error
  91. return
  92. }
  93. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  94. if len(items) == 0 {
  95. return
  96. }
  97. err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error
  98. return
  99. }
  100. func (m *BusinessConf) Update(cols []string) (err error) {
  101. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  102. return
  103. }
  104. func (m *BusinessConf) Del() (err error) {
  105. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  106. err = global.DEFAULT_DB.Exec(sql, m.Id).Error
  107. return
  108. }
  109. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  110. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  111. err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error
  112. return
  113. }
  114. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  115. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  116. err = global.DEFAULT_DB.Raw(sql, pars...).First(&item).Error
  117. return
  118. }
  119. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  120. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  121. err = global.DEFAULT_DB.Raw(sql, pars...).Scan(&count).Error
  122. return
  123. }
  124. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  125. fields := strings.Join(fieldArr, ",")
  126. if len(fieldArr) == 0 {
  127. fields = `*`
  128. }
  129. order := `ORDER BY create_time DESC`
  130. if orderRule != "" {
  131. order = ` ORDER BY ` + orderRule
  132. }
  133. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  134. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  135. return
  136. }
  137. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  138. fields := strings.Join(fieldArr, ",")
  139. if len(fieldArr) == 0 {
  140. fields = `*`
  141. }
  142. order := `ORDER BY create_time DESC`
  143. if orderRule != "" {
  144. order = ` ORDER BY ` + orderRule
  145. }
  146. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  147. pars = append(pars, startSize, pageSize)
  148. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  149. return
  150. }
  151. // GetBusinessConf 获取商家配置
  152. func GetBusinessConf() (list map[string]string, err error) {
  153. list = make(map[string]string)
  154. var items []*BusinessConf
  155. sql := `SELECT * FROM business_conf`
  156. err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
  157. if err != nil {
  158. return
  159. }
  160. for _, v := range items {
  161. if v.ValType == 4 {
  162. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  163. continue
  164. }
  165. list[v.ConfKey] = v.ConfVal
  166. }
  167. return
  168. }
  169. // BusinessConfUpdate 更新配置
  170. type BusinessConfUpdate struct {
  171. ConfKey string
  172. ConfVal string
  173. }
  174. // UpdateBusinessConfMulti 批量修改配置
  175. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  176. tx := global.DEFAULT_DB.Begin()
  177. for _, v := range items {
  178. err = tx.Exec("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?", v.ConfVal, v.ConfKey).Error
  179. if err != nil {
  180. tx.Rollback()
  181. return
  182. }
  183. }
  184. tx.Commit()
  185. return
  186. }
  187. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  188. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  189. err = global.DEFAULT_DB.Raw(sql, key).First(&item).Error
  190. return
  191. }
  192. type BusinessConfSingleSaveReq struct {
  193. ConfKey string `description:"配置Key"`
  194. ConfVal string `description:"配置值"`
  195. }
  196. type EdbStopRefreshRule struct {
  197. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  198. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  199. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  200. }
  201. type BusinessConfSingleResp struct {
  202. ConfVal string
  203. }
  204. // InitUseMongoConf
  205. // @Description:
  206. // @author: Roc
  207. // @datetime 2024-07-01 13:49:09
  208. func InitUseMongoConf() {
  209. useMongo, e := GetBusinessConfByKey("UseMongo")
  210. if e != nil {
  211. return
  212. }
  213. if useMongo.ConfVal == `true` {
  214. utils.UseMongo = true
  215. }
  216. }
  217. func InitBusinessConf() {
  218. var e error
  219. BusinessConfMap, e = GetBusinessConf()
  220. if e != nil {
  221. return
  222. }
  223. // ES索引名称
  224. if BusinessConfMap[BusinessConfEsIndexNameExcel] != "" {
  225. utils.EsExcelIndexName = BusinessConfMap[BusinessConfEsIndexNameExcel]
  226. }
  227. if BusinessConfMap[BusinessConfEsIndexNameDataSource] != "" {
  228. utils.EsDataSourceIndexName = BusinessConfMap[BusinessConfEsIndexNameDataSource]
  229. }
  230. }