business_conf.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. const (
  11. BusinessConfUseXf = "UseXf"
  12. BusinessConfXfAppid = "XfAppid"
  13. BusinessConfXfApiKey = "XfApiKey"
  14. BusinessConfXfApiSecret = "XfApiSecret"
  15. BusinessConfXfVcn = "XfVcn"
  16. BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
  17. BusinessConfIsReportApprove = "IsReportApprove"
  18. BusinessConfReportApproveType = "ReportApproveType"
  19. BusinessConfCompanyName = "CompanyName"
  20. BusinessConfCompanyWatermark = "CompanyWatermark"
  21. BusinessConfWatermarkChart = "WatermarkChart"
  22. BusinessConfLoginSmsTpId = "LoginSmsTpId"
  23. BusinessConfLoginSmsGjTpId = "LoginSmsGjTpId"
  24. BusinessConfSmsJhgnAppKey = "SmsJhgnAppKey"
  25. BusinessConfSmsJhgjAppKey = "SmsJhgjAppKey"
  26. BusinessConfLdapHost = "LdapHost"
  27. BusinessConfLdapBase = "LdapBase"
  28. BusinessConfLdapPort = "LdapPort"
  29. BusinessConfEmailClient = "EmailClient"
  30. BusinessConfEmailServerHost = "EmailServerHost"
  31. BusinessConfEmailServerPort = "EmailServerPort"
  32. BusinessConfEmailSender = "EmailSender"
  33. BusinessConfEmailSenderUserName = "EmailSenderUserName"
  34. BusinessConfEmailSenderPassword = "EmailSenderPassword"
  35. BusinessConfSmsClient = "SmsClient"
  36. BusinessConfNanHuaSmsAppKey = "NanHuaSmsAppKey"
  37. BusinessConfNanHuaSmsAppSecret = "NanHuaSmsAppSecret"
  38. BusinessConfNanHuaSmsApiHost = "NanHuaSmsApiHost"
  39. BusinessConfLoginSmsTplContent = "LoginSmsTplContent"
  40. BusinessConfLoginEmailTemplateSubject = "LoginEmailTemplateSubject"
  41. BusinessConfLoginEmailTemplateContent = "LoginEmailTemplateContent"
  42. BusinessConfLdapBindUserSuffix = "LdapBindUserSuffix"
  43. BusinessConfLdapUserFilter = "LdapUserFilter"
  44. BusinessConfTencentApiSecretId = "TencentApiSecretId" // 腾讯云API-密钥对
  45. BusinessConfTencentApiSecretKey = "TencentApiSecretKey" // 腾讯云API-密钥对
  46. BusinessConfTencentApiRecTaskCallbackUrl = "TencentApiRecTaskCallbackUrl" // 腾讯云API-语音识别回调地址
  47. BusinessConfSmsJhgjVariable = "SmsJhgjVariable" // 聚合国际短信变量
  48. BusinessConfEdbStopRefreshRule = "EdbStopRefreshRule" // 是否停止指标刷新规则
  49. BusinessConfReport2ImgUrl = "Report2ImgUrl" // 报告转长图地址(用于兼容内外网环境的)
  50. BusinessConfReportViewUrl = "ReportViewUrl" // 报告详情地址
  51. )
  52. const (
  53. BusinessConfReportApproveTypeEta = "eta"
  54. BusinessConfReportApproveTypeOther = "other"
  55. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  56. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  57. )
  58. // FromSceneMap 数据源名称与数据源ID的对应关系
  59. var FromSceneMap = map[int]string{
  60. 1: "SmartReportSheetSize",
  61. 2: "ReportSheetSize",
  62. 3: "EnReportSheetSize",
  63. 4: "CnPptSheetSize",
  64. 5: "EnPptSheetSize",
  65. }
  66. // BusinessConf 商户配置表
  67. type BusinessConf struct {
  68. Id int `orm:"column(id);pk"`
  69. ConfKey string `description:"配置Key"`
  70. ConfVal string `description:"配置值"`
  71. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  72. Necessary int `description:"是否必填:0-否;1-是"`
  73. Remark string `description:"备注"`
  74. CreateTime time.Time
  75. }
  76. func (m *BusinessConf) TableName() string {
  77. return "business_conf"
  78. }
  79. func (m *BusinessConf) PrimaryId() string {
  80. return "id"
  81. }
  82. func (m *BusinessConf) Create() (err error) {
  83. o := orm.NewOrm()
  84. id, err := o.Insert(m)
  85. if err != nil {
  86. return
  87. }
  88. m.Id = int(id)
  89. return
  90. }
  91. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  92. if len(items) == 0 {
  93. return
  94. }
  95. o := orm.NewOrm()
  96. _, err = o.InsertMulti(len(items), items)
  97. return
  98. }
  99. func (m *BusinessConf) Update(cols []string) (err error) {
  100. o := orm.NewOrm()
  101. _, err = o.Update(m, cols...)
  102. return
  103. }
  104. func (m *BusinessConf) Del() (err error) {
  105. o := orm.NewOrm()
  106. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  107. _, err = o.Raw(sql, m.Id).Exec()
  108. return
  109. }
  110. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  111. o := orm.NewOrm()
  112. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  113. err = o.Raw(sql, id).QueryRow(&item)
  114. return
  115. }
  116. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  117. o := orm.NewOrm()
  118. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  119. err = o.Raw(sql, pars).QueryRow(&item)
  120. return
  121. }
  122. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  123. o := orm.NewOrm()
  124. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  125. err = o.Raw(sql, pars).QueryRow(&count)
  126. return
  127. }
  128. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  129. o := orm.NewOrm()
  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 = o.Raw(sql, pars).QueryRows(&items)
  140. return
  141. }
  142. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  143. o := orm.NewOrm()
  144. fields := strings.Join(fieldArr, ",")
  145. if len(fieldArr) == 0 {
  146. fields = `*`
  147. }
  148. order := `ORDER BY create_time DESC`
  149. if orderRule != "" {
  150. order = ` ORDER BY ` + orderRule
  151. }
  152. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  153. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  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. o := orm.NewOrm()
  161. sql := `SELECT * FROM business_conf`
  162. _, err = o.Raw(sql).QueryRows(&items)
  163. if err != nil {
  164. return
  165. }
  166. for _, v := range items {
  167. if v.ValType == 4 {
  168. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  169. continue
  170. }
  171. list[v.ConfKey] = v.ConfVal
  172. }
  173. return
  174. }
  175. // BusinessConfUpdate 更新配置
  176. type BusinessConfUpdate struct {
  177. ConfKey string
  178. ConfVal string
  179. }
  180. // UpdateBusinessConfMulti 批量修改配置
  181. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  182. o := orm.NewOrm()
  183. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  184. if err != nil {
  185. return
  186. }
  187. defer func() {
  188. _ = p.Close()
  189. }()
  190. for _, v := range items {
  191. _, err = p.Exec(v.ConfVal, v.ConfKey)
  192. if err != nil {
  193. return
  194. }
  195. }
  196. return
  197. }
  198. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  199. o := orm.NewOrm()
  200. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  201. err = o.Raw(sql, key).QueryRow(&item)
  202. return
  203. }
  204. type BusinessConfSingleSaveReq struct {
  205. ConfKey string `description:"配置Key"`
  206. ConfVal string `description:"配置值"`
  207. }
  208. type EdbStopRefreshRule struct {
  209. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  210. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  211. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  212. }
  213. type BusinessConfSingleResp struct {
  214. ConfVal string
  215. }
  216. // InitUseMongoConf
  217. // @Description:
  218. // @author: Roc
  219. // @datetime 2024-07-01 13:49:09
  220. func InitUseMongoConf() {
  221. useMongo, e := GetBusinessConfByKey("UseMongo")
  222. if e != nil {
  223. return
  224. }
  225. if useMongo.ConfVal == `true` {
  226. utils.UseMongo = true
  227. }
  228. }