business_conf.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package models
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "html"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. BusinessConfMap map[string]string
  14. )
  15. const (
  16. BusinessConfUseXf = "UseXf"
  17. BusinessConfXfAppid = "XfAppid"
  18. BusinessConfXfApiKey = "XfApiKey"
  19. BusinessConfXfApiSecret = "XfApiSecret"
  20. BusinessConfXfVcn = "XfVcn"
  21. BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
  22. BusinessConfIsReportApprove = "IsReportApprove"
  23. BusinessConfReportApproveType = "ReportApproveType"
  24. BusinessConfCompanyName = "CompanyName"
  25. BusinessConfCompanyWatermark = "CompanyWatermark"
  26. BusinessConfWatermarkChart = "WatermarkChart"
  27. BusinessConfLoginSmsTpId = "LoginSmsTpId"
  28. BusinessConfLoginSmsGjTpId = "LoginSmsGjTpId"
  29. BusinessConfSmsJhgnAppKey = "SmsJhgnAppKey"
  30. BusinessConfSmsJhgjAppKey = "SmsJhgjAppKey"
  31. BusinessConfLdapHost = "LdapHost"
  32. BusinessConfLdapBase = "LdapBase"
  33. BusinessConfLdapPort = "LdapPort"
  34. BusinessConfEmailClient = "EmailClient"
  35. BusinessConfEmailServerHost = "EmailServerHost"
  36. BusinessConfEmailServerPort = "EmailServerPort"
  37. BusinessConfEmailSender = "EmailSender"
  38. BusinessConfEmailSenderUserName = "EmailSenderUserName"
  39. BusinessConfEmailSenderPassword = "EmailSenderPassword"
  40. BusinessConfSmsClient = "SmsClient"
  41. BusinessConfNanHuaSmsAppKey = "NanHuaSmsAppKey"
  42. BusinessConfNanHuaSmsAppSecret = "NanHuaSmsAppSecret"
  43. BusinessConfNanHuaSmsApiHost = "NanHuaSmsApiHost"
  44. BusinessConfLoginSmsTplContent = "LoginSmsTplContent"
  45. BusinessConfLoginEmailTemplateSubject = "LoginEmailTemplateSubject"
  46. BusinessConfLoginEmailTemplateContent = "LoginEmailTemplateContent"
  47. BusinessConfLdapBindUserSuffix = "LdapBindUserSuffix"
  48. BusinessConfLdapUserFilter = "LdapUserFilter"
  49. BusinessConfTencentApiSecretId = "TencentApiSecretId" // 腾讯云API-密钥对
  50. BusinessConfTencentApiSecretKey = "TencentApiSecretKey" // 腾讯云API-密钥对
  51. BusinessConfTencentApiRecTaskCallbackUrl = "TencentApiRecTaskCallbackUrl" // 腾讯云API-语音识别回调地址
  52. BusinessConfSmsJhgjVariable = "SmsJhgjVariable" // 聚合国际短信变量
  53. BusinessConfEdbStopRefreshRule = "EdbStopRefreshRule" // 是否停止指标刷新规则
  54. BusinessConfReport2ImgUrl = "Report2ImgUrl" // 报告转长图地址(用于兼容内外网环境的)
  55. BusinessConfReportViewUrl = "ReportViewUrl" // 报告详情地址
  56. BusinessConfEsIndexNameExcel = "EsIndexNameExcel" // ES索引名称-表格
  57. BusinessConfEsIndexNameDataSource = "EsIndexNameDataSource" // ES索引名称-数据源
  58. LLMInitConfig = "llmInitConfig"
  59. KnowledgeBaseName = "KnowledgeBaseName" // 摘要库
  60. PrivateKnowledgeBaseName = "PrivateKnowledgeBaseName" // 私有摘要库
  61. KnowledgeArticleName = "KnowledgeArticleName" // 原文库
  62. BusinessConfEsWechatArticle = "EsIndexNameWechatArticle" // ES索引名称-微信文章
  63. BusinessConfEsWechatArticleAbstract = "EsIndexNameWechatArticleAbstract" // ES索引名称-微信文章摘要
  64. BusinessConfEsRagQuestion = "EsIndexNameRagQuestion" // ES索引名称-知识库问题
  65. BusinessConfEsRagEtaReportAbstract = "EsIndexNameRagEtaReportAbstract" // ES索引名称-eta报告摘要
  66. BusinessConfIsOpenChartExpired = "IsOpenChartExpired" // 是否开启图表有效期鉴权/报告禁止复制
  67. BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
  68. BusinessConfOssUrlReplace = "OssUrlReplace" // OSS地址替换-兼容内网客户用
  69. )
  70. const (
  71. BusinessConfReportApproveTypeEta = "eta"
  72. BusinessConfReportApproveTypeOther = "other"
  73. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  74. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  75. )
  76. // FromSceneMap 数据源名称与数据源ID的对应关系
  77. var FromSceneMap = map[int]string{
  78. 1: "SmartReportSheetSize",
  79. 2: "ReportSheetSize",
  80. 3: "EnReportSheetSize",
  81. 4: "CnPptSheetSize",
  82. 5: "EnPptSheetSize",
  83. }
  84. // BusinessConf 商户配置表
  85. type BusinessConf struct {
  86. Id int `gorm:"column:id;primaryKey;autoIncrement"` //`orm:"column(id);pk" gorm:"primaryKey" `
  87. ConfKey string `gorm:"column:conf_key"` //`description:"配置Key"`
  88. ConfVal string `gorm:"column:conf_val"` //`description:"配置值"`
  89. ValType int `gorm:"column:val_type"` //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  90. Necessary int `gorm:"column:necessary"` //`description:"是否必填:0-否;1-是"`
  91. Remark string `gorm:"column:remark"` // `description:"备注"`
  92. CreateTime time.Time `gorm:"column:create_time"`
  93. }
  94. func (m *BusinessConf) TableName() string {
  95. return "business_conf"
  96. }
  97. func (m *BusinessConf) PrimaryId() string {
  98. return "id"
  99. }
  100. func (m *BusinessConf) Create() (err error) {
  101. err = global.DEFAULT_DB.Create(m).Error
  102. return
  103. }
  104. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  105. if len(items) == 0 {
  106. return
  107. }
  108. err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error
  109. return
  110. }
  111. func (m *BusinessConf) Update(cols []string) (err error) {
  112. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  113. return
  114. }
  115. func (m *BusinessConf) Del() (err error) {
  116. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  117. err = global.DEFAULT_DB.Exec(sql, m.Id).Error
  118. return
  119. }
  120. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  121. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  122. err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error
  123. return
  124. }
  125. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  126. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  127. err = global.DEFAULT_DB.Raw(sql, pars...).First(&item).Error
  128. return
  129. }
  130. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  131. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  132. err = global.DEFAULT_DB.Raw(sql, pars...).Scan(&count).Error
  133. return
  134. }
  135. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  136. fields := strings.Join(fieldArr, ",")
  137. if len(fieldArr) == 0 {
  138. fields = `*`
  139. }
  140. order := `ORDER BY create_time DESC`
  141. if orderRule != "" {
  142. order = ` ORDER BY ` + orderRule
  143. }
  144. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  145. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  146. return
  147. }
  148. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  149. fields := strings.Join(fieldArr, ",")
  150. if len(fieldArr) == 0 {
  151. fields = `*`
  152. }
  153. order := `ORDER BY create_time DESC`
  154. if orderRule != "" {
  155. order = ` ORDER BY ` + orderRule
  156. }
  157. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  158. pars = append(pars, startSize, pageSize)
  159. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  160. return
  161. }
  162. // GetBusinessConf 获取商家配置
  163. func GetBusinessConf() (list map[string]string, err error) {
  164. list = make(map[string]string)
  165. var items []*BusinessConf
  166. sql := `SELECT * FROM business_conf`
  167. err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
  168. if err != nil {
  169. return
  170. }
  171. for _, v := range items {
  172. if v.ValType == 4 {
  173. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  174. continue
  175. }
  176. list[v.ConfKey] = v.ConfVal
  177. }
  178. return
  179. }
  180. // BusinessConfUpdate 更新配置
  181. type BusinessConfUpdate struct {
  182. ConfKey string
  183. ConfVal string
  184. }
  185. // UpdateBusinessConfMulti 批量修改配置
  186. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  187. tx := global.DEFAULT_DB.Begin()
  188. for _, v := range items {
  189. err = tx.Exec("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?", v.ConfVal, v.ConfKey).Error
  190. if err != nil {
  191. tx.Rollback()
  192. return
  193. }
  194. }
  195. tx.Commit()
  196. return
  197. }
  198. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  199. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  200. err = global.DEFAULT_DB.Raw(sql, key).First(&item).Error
  201. return
  202. }
  203. type BusinessConfSingleSaveReq struct {
  204. ConfKey string `description:"配置Key"`
  205. ConfVal string `description:"配置值"`
  206. }
  207. type EdbStopRefreshRule struct {
  208. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  209. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  210. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  211. }
  212. type BusinessConfSingleResp struct {
  213. ConfVal string
  214. }
  215. // InitUseMongoConf
  216. // @Description:
  217. // @author: Roc
  218. // @datetime 2024-07-01 13:49:09
  219. func InitUseMongoConf() {
  220. useMongo, e := GetBusinessConfByKey("UseMongo")
  221. if e != nil {
  222. return
  223. }
  224. if useMongo.ConfVal == `true` {
  225. utils.UseMongo = true
  226. }
  227. }
  228. type LLMConfig struct {
  229. LlmAddress string `json:"llm_server"`
  230. LlmModel string `json:"llm_model"`
  231. }
  232. func InitBusinessConf() {
  233. var e error
  234. BusinessConfMap, e = GetBusinessConf()
  235. if e != nil {
  236. return
  237. }
  238. // ES索引名称
  239. if BusinessConfMap[BusinessConfEsIndexNameExcel] != "" {
  240. utils.EsExcelIndexName = BusinessConfMap[BusinessConfEsIndexNameExcel]
  241. }
  242. if BusinessConfMap[BusinessConfEsIndexNameDataSource] != "" {
  243. utils.EsDataSourceIndexName = BusinessConfMap[BusinessConfEsIndexNameDataSource]
  244. }
  245. // ES索引名称
  246. if BusinessConfMap[BusinessConfEsWechatArticle] != "" {
  247. utils.EsWechatArticleName = BusinessConfMap[BusinessConfEsWechatArticle]
  248. }
  249. if BusinessConfMap[BusinessConfEsWechatArticleAbstract] != "" {
  250. utils.EsWechatArticleAbstractName = BusinessConfMap[BusinessConfEsWechatArticleAbstract]
  251. }
  252. if BusinessConfMap[BusinessConfEsRagQuestion] != "" {
  253. utils.EsRagQuestionName = BusinessConfMap[BusinessConfEsRagQuestion]
  254. }
  255. if BusinessConfMap[BusinessConfEsRagEtaReportAbstract] != "" {
  256. utils.EsRagEtaReportAbstractName = BusinessConfMap[BusinessConfEsRagEtaReportAbstract]
  257. }
  258. confStr := BusinessConfMap[LLMInitConfig]
  259. if confStr != "" {
  260. var config LLMConfig
  261. err := json.Unmarshal([]byte(confStr), &config)
  262. if err != nil {
  263. utils.FileLog.Error("LLM配置错误")
  264. }
  265. utils.LLM_MODEL = config.LlmModel
  266. utils.LLM_SERVER = config.LlmAddress
  267. }
  268. // 图表有效期的过期时间
  269. if BusinessConfMap[BusinessConfReportChartExpiredTime] != "" {
  270. reportChartExpiredTime, _ := strconv.Atoi(BusinessConfMap[BusinessConfReportChartExpiredTime])
  271. if reportChartExpiredTime <= 0 {
  272. reportChartExpiredTime = 30
  273. }
  274. utils.BusinessConfReportChartExpiredTime = time.Duration(reportChartExpiredTime) * time.Minute
  275. } else {
  276. utils.BusinessConfReportChartExpiredTime = 30 * time.Minute
  277. }
  278. }
  279. type OssUrlReplace struct {
  280. IsReplace bool `description:"是否替换"`
  281. OssUrlOrigin string `description:"被替换的资源地址"`
  282. OssUrlNew string `description:"新的资源地址"`
  283. }