business_conf.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. LLMInitConfig = "llmInitConfig"
  58. KnowledgeBaseName = "KnowledgeBaseName" // 摘要库
  59. KnowledgeArticleName = "KnowledgeArticleName" // 原文库
  60. BusinessConfEsWechatArticle = "EsIndexNameWechatArticle" // ES索引名称-微信文章
  61. BusinessConfEsWechatArticleAbstract = "EsIndexNameWechatArticleAbstract" // ES索引名称-微信文章摘要
  62. )
  63. const (
  64. BusinessConfReportApproveTypeEta = "eta"
  65. BusinessConfReportApproveTypeOther = "other"
  66. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  67. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  68. )
  69. // FromSceneMap 数据源名称与数据源ID的对应关系
  70. var FromSceneMap = map[int]string{
  71. 1: "SmartReportSheetSize",
  72. 2: "ReportSheetSize",
  73. 3: "EnReportSheetSize",
  74. 4: "CnPptSheetSize",
  75. 5: "EnPptSheetSize",
  76. }
  77. // BusinessConf 商户配置表
  78. type BusinessConf struct {
  79. Id int `gorm:"column:id;primaryKey;autoIncrement"` //`orm:"column(id);pk" gorm:"primaryKey" `
  80. ConfKey string `gorm:"column:conf_key"` //`description:"配置Key"`
  81. ConfVal string `gorm:"column:conf_val"` //`description:"配置值"`
  82. ValType int `gorm:"column:val_type"` //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  83. Necessary int `gorm:"column:necessary"` //`description:"是否必填:0-否;1-是"`
  84. Remark string `gorm:"column:remark"` // `description:"备注"`
  85. CreateTime time.Time `gorm:"column:create_time"`
  86. }
  87. func (m *BusinessConf) TableName() string {
  88. return "business_conf"
  89. }
  90. func (m *BusinessConf) PrimaryId() string {
  91. return "id"
  92. }
  93. func (m *BusinessConf) Create() (err error) {
  94. err = global.DEFAULT_DB.Create(m).Error
  95. return
  96. }
  97. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  98. if len(items) == 0 {
  99. return
  100. }
  101. err = global.DEFAULT_DB.CreateInBatches(items, utils.MultiAddNum).Error
  102. return
  103. }
  104. func (m *BusinessConf) Update(cols []string) (err error) {
  105. err = global.DEFAULT_DB.Select(cols).Updates(m).Error
  106. return
  107. }
  108. func (m *BusinessConf) Del() (err error) {
  109. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  110. err = global.DEFAULT_DB.Exec(sql, m.Id).Error
  111. return
  112. }
  113. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  114. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  115. err = global.DEFAULT_DB.Raw(sql, id).First(&item).Error
  116. return
  117. }
  118. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  119. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  120. err = global.DEFAULT_DB.Raw(sql, pars...).First(&item).Error
  121. return
  122. }
  123. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  124. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  125. err = global.DEFAULT_DB.Raw(sql, pars...).Scan(&count).Error
  126. return
  127. }
  128. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (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`, fields, m.TableName(), condition, order)
  138. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  139. return
  140. }
  141. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  142. fields := strings.Join(fieldArr, ",")
  143. if len(fieldArr) == 0 {
  144. fields = `*`
  145. }
  146. order := `ORDER BY create_time DESC`
  147. if orderRule != "" {
  148. order = ` ORDER BY ` + orderRule
  149. }
  150. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  151. pars = append(pars, startSize, pageSize)
  152. err = global.DEFAULT_DB.Raw(sql, pars...).Find(&items).Error
  153. return
  154. }
  155. // GetBusinessConf 获取商家配置
  156. func GetBusinessConf() (list map[string]string, err error) {
  157. list = make(map[string]string)
  158. var items []*BusinessConf
  159. sql := `SELECT * FROM business_conf`
  160. err = global.DEFAULT_DB.Raw(sql).Find(&items).Error
  161. if err != nil {
  162. return
  163. }
  164. for _, v := range items {
  165. if v.ValType == 4 {
  166. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  167. continue
  168. }
  169. list[v.ConfKey] = v.ConfVal
  170. }
  171. return
  172. }
  173. // BusinessConfUpdate 更新配置
  174. type BusinessConfUpdate struct {
  175. ConfKey string
  176. ConfVal string
  177. }
  178. // UpdateBusinessConfMulti 批量修改配置
  179. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  180. tx := global.DEFAULT_DB.Begin()
  181. for _, v := range items {
  182. err = tx.Exec("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?", v.ConfVal, v.ConfKey).Error
  183. if err != nil {
  184. tx.Rollback()
  185. return
  186. }
  187. }
  188. tx.Commit()
  189. return
  190. }
  191. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  192. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  193. err = global.DEFAULT_DB.Raw(sql, key).First(&item).Error
  194. return
  195. }
  196. type BusinessConfSingleSaveReq struct {
  197. ConfKey string `description:"配置Key"`
  198. ConfVal string `description:"配置值"`
  199. }
  200. type EdbStopRefreshRule struct {
  201. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  202. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  203. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  204. }
  205. type BusinessConfSingleResp struct {
  206. ConfVal string
  207. }
  208. // InitUseMongoConf
  209. // @Description:
  210. // @author: Roc
  211. // @datetime 2024-07-01 13:49:09
  212. func InitUseMongoConf() {
  213. useMongo, e := GetBusinessConfByKey("UseMongo")
  214. if e != nil {
  215. return
  216. }
  217. if useMongo.ConfVal == `true` {
  218. utils.UseMongo = true
  219. }
  220. }
  221. type LLMConfig struct {
  222. LlmAddress string `json:"llm_server"`
  223. LlmModel string `json:"llm_model"`
  224. }
  225. func InitBusinessConf() {
  226. var e error
  227. BusinessConfMap, e = GetBusinessConf()
  228. if e != nil {
  229. return
  230. }
  231. // ES索引名称
  232. if BusinessConfMap[BusinessConfEsIndexNameExcel] != "" {
  233. utils.EsExcelIndexName = BusinessConfMap[BusinessConfEsIndexNameExcel]
  234. }
  235. if BusinessConfMap[BusinessConfEsIndexNameDataSource] != "" {
  236. utils.EsDataSourceIndexName = BusinessConfMap[BusinessConfEsIndexNameDataSource]
  237. }
  238. // ES索引名称
  239. if BusinessConfMap[BusinessConfEsWechatArticle] != "" {
  240. utils.EsWechatArticleName = BusinessConfMap[BusinessConfEsWechatArticle]
  241. }
  242. if BusinessConfMap[BusinessConfEsWechatArticleAbstract] != "" {
  243. utils.EsWechatArticleAbstractName = BusinessConfMap[BusinessConfEsWechatArticleAbstract]
  244. }
  245. confStr := BusinessConfMap[LLMInitConfig]
  246. if confStr != "" {
  247. var config LLMConfig
  248. err := json.Unmarshal([]byte(confStr), &config)
  249. if err != nil {
  250. utils.FileLog.Error("LLM配置错误")
  251. }
  252. utils.LLM_MODEL = config.LlmModel
  253. utils.LLM_SERVER = config.LlmAddress
  254. }
  255. }