business_conf.go 10 KB

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