business_conf.go 8.1 KB

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