business_conf.go 8.7 KB

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