business_conf.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package models
  2. import (
  3. "eta/eta_api/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  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. )
  58. const (
  59. BusinessConfReportApproveTypeEta = "eta"
  60. BusinessConfReportApproveTypeOther = "other"
  61. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  62. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  63. )
  64. // FromSceneMap 数据源名称与数据源ID的对应关系
  65. var FromSceneMap = map[int]string{
  66. 1: "SmartReportSheetSize",
  67. 2: "ReportSheetSize",
  68. 3: "EnReportSheetSize",
  69. 4: "CnPptSheetSize",
  70. 5: "EnPptSheetSize",
  71. }
  72. // BusinessConf 商户配置表
  73. type BusinessConf struct {
  74. Id int `orm:"column(id);pk"`
  75. ConfKey string `description:"配置Key"`
  76. ConfVal string `description:"配置值"`
  77. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  78. Necessary int `description:"是否必填:0-否;1-是"`
  79. Remark string `description:"备注"`
  80. CreateTime time.Time
  81. }
  82. func (m *BusinessConf) TableName() string {
  83. return "business_conf"
  84. }
  85. func (m *BusinessConf) PrimaryId() string {
  86. return "id"
  87. }
  88. func (m *BusinessConf) Create() (err error) {
  89. o := orm.NewOrm()
  90. id, err := o.Insert(m)
  91. if err != nil {
  92. return
  93. }
  94. m.Id = int(id)
  95. return
  96. }
  97. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  98. if len(items) == 0 {
  99. return
  100. }
  101. o := orm.NewOrm()
  102. _, err = o.InsertMulti(len(items), items)
  103. return
  104. }
  105. func (m *BusinessConf) Update(cols []string) (err error) {
  106. o := orm.NewOrm()
  107. _, err = o.Update(m, cols...)
  108. return
  109. }
  110. func (m *BusinessConf) Del() (err error) {
  111. o := orm.NewOrm()
  112. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  113. _, err = o.Raw(sql, m.Id).Exec()
  114. return
  115. }
  116. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  117. o := orm.NewOrm()
  118. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  119. err = o.Raw(sql, id).QueryRow(&item)
  120. return
  121. }
  122. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  123. o := orm.NewOrm()
  124. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  125. err = o.Raw(sql, pars).QueryRow(&item)
  126. return
  127. }
  128. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  129. o := orm.NewOrm()
  130. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  131. err = o.Raw(sql, pars).QueryRow(&count)
  132. return
  133. }
  134. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  135. o := orm.NewOrm()
  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 = o.Raw(sql, pars).QueryRows(&items)
  146. return
  147. }
  148. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  149. o := orm.NewOrm()
  150. fields := strings.Join(fieldArr, ",")
  151. if len(fieldArr) == 0 {
  152. fields = `*`
  153. }
  154. order := `ORDER BY create_time DESC`
  155. if orderRule != "" {
  156. order = ` ORDER BY ` + orderRule
  157. }
  158. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  159. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  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. o := orm.NewOrm()
  167. sql := `SELECT * FROM business_conf`
  168. _, err = o.Raw(sql).QueryRows(&items)
  169. if err != nil {
  170. return
  171. }
  172. for _, v := range items {
  173. if v.ValType == 4 {
  174. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  175. continue
  176. }
  177. list[v.ConfKey] = v.ConfVal
  178. }
  179. return
  180. }
  181. // BusinessConfUpdate 更新配置
  182. type BusinessConfUpdate struct {
  183. ConfKey string
  184. ConfVal string
  185. }
  186. // UpdateBusinessConfMulti 批量修改配置
  187. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  188. o := orm.NewOrm()
  189. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  190. if err != nil {
  191. return
  192. }
  193. defer func() {
  194. _ = p.Close()
  195. }()
  196. for _, v := range items {
  197. _, err = p.Exec(v.ConfVal, v.ConfKey)
  198. if err != nil {
  199. return
  200. }
  201. }
  202. return
  203. }
  204. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  205. o := orm.NewOrm()
  206. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  207. err = o.Raw(sql, key).QueryRow(&item)
  208. return
  209. }
  210. type BusinessConfSingleSaveReq struct {
  211. ConfKey string `description:"配置Key"`
  212. ConfVal string `description:"配置值"`
  213. }
  214. type EdbStopRefreshRule struct {
  215. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  216. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  217. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  218. }
  219. type BusinessConfSingleResp struct {
  220. ConfVal string
  221. }
  222. // InitUseMongoConf
  223. // @Description:
  224. // @author: Roc
  225. // @datetime 2024-07-01 13:49:09
  226. func InitUseMongoConf() {
  227. useMongo, e := GetBusinessConfByKey("UseMongo")
  228. if e != nil {
  229. return
  230. }
  231. if useMongo.ConfVal == `true` {
  232. utils.UseMongo = true
  233. }
  234. }
  235. func InitBusinessConf() {
  236. var e error
  237. BusinessConfMap, e = GetBusinessConf()
  238. if e != nil {
  239. return
  240. }
  241. // ES索引名称
  242. if BusinessConfMap[BusinessConfEsIndexNameExcel] != "" {
  243. utils.EsExcelIndexName = BusinessConfMap[BusinessConfEsIndexNameExcel]
  244. }
  245. if BusinessConfMap[BusinessConfEsIndexNameDataSource] != "" {
  246. utils.EsDataSourceIndexName = BusinessConfMap[BusinessConfEsIndexNameDataSource]
  247. }
  248. }