business_conf.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package models
  2. import (
  3. "eta/eta_mobile/utils"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "html"
  7. "strconv"
  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. BusinessConfEsIndexNameExcel = "EsIndexNameExcel" // ES索引名称-表格
  53. BusinessConfEsIndexNameDataSource = "EsIndexNameDataSource" // ES索引名称-数据源
  54. BusinessConfIsOpenChartExpired = "IsOpenChartExpired" // 图表是否鉴权
  55. BusinessConfEdbStopRefreshRule = "EdbStopRefreshRule" // 是否停止指标刷新规则
  56. BusinessConfReport2ImgUrl = "Report2ImgUrl" // 报告转长图地址(用于兼容内外网环境的)
  57. BusinessConfReportViewUrl = "ReportViewUrl" // 报告详情地址 // 报告详情地址
  58. BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
  59. )
  60. const (
  61. BusinessConfReportApproveTypeEta = "eta"
  62. BusinessConfReportApproveTypeOther = "other"
  63. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  64. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  65. )
  66. // FromSceneMap 数据源名称与数据源ID的对应关系
  67. var FromSceneMap = map[int]string{
  68. 1: "SmartReportSheetSize",
  69. 2: "ReportSheetSize",
  70. 3: "EnReportSheetSize",
  71. 4: "CnPptSheetSize",
  72. 5: "EnPptSheetSize",
  73. }
  74. // BusinessConf 商户配置表
  75. type BusinessConf struct {
  76. Id int `orm:"column(id);pk"`
  77. ConfKey string `description:"配置Key"`
  78. ConfVal string `description:"配置值"`
  79. ValType int `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  80. Necessary int `description:"是否必填:0-否;1-是"`
  81. Remark string `description:"备注"`
  82. CreateTime time.Time
  83. }
  84. func (m *BusinessConf) TableName() string {
  85. return "business_conf"
  86. }
  87. func (m *BusinessConf) PrimaryId() string {
  88. return "id"
  89. }
  90. func (m *BusinessConf) Create() (err error) {
  91. o := orm.NewOrm()
  92. id, err := o.Insert(m)
  93. if err != nil {
  94. return
  95. }
  96. m.Id = int(id)
  97. return
  98. }
  99. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  100. if len(items) == 0 {
  101. return
  102. }
  103. o := orm.NewOrm()
  104. _, err = o.InsertMulti(len(items), items)
  105. return
  106. }
  107. func (m *BusinessConf) Update(cols []string) (err error) {
  108. o := orm.NewOrm()
  109. _, err = o.Update(m, cols...)
  110. return
  111. }
  112. func (m *BusinessConf) Del() (err error) {
  113. o := orm.NewOrm()
  114. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  115. _, err = o.Raw(sql, m.Id).Exec()
  116. return
  117. }
  118. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  119. o := orm.NewOrm()
  120. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  121. err = o.Raw(sql, id).QueryRow(&item)
  122. return
  123. }
  124. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  125. o := orm.NewOrm()
  126. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  127. err = o.Raw(sql, pars).QueryRow(&item)
  128. return
  129. }
  130. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  131. o := orm.NewOrm()
  132. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  133. err = o.Raw(sql, pars).QueryRow(&count)
  134. return
  135. }
  136. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  137. o := orm.NewOrm()
  138. fields := strings.Join(fieldArr, ",")
  139. if len(fieldArr) == 0 {
  140. fields = `*`
  141. }
  142. order := `ORDER BY create_time DESC`
  143. if orderRule != "" {
  144. order = ` ORDER BY ` + orderRule
  145. }
  146. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  147. _, err = o.Raw(sql, pars).QueryRows(&items)
  148. return
  149. }
  150. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  151. o := orm.NewOrm()
  152. fields := strings.Join(fieldArr, ",")
  153. if len(fieldArr) == 0 {
  154. fields = `*`
  155. }
  156. order := `ORDER BY create_time DESC`
  157. if orderRule != "" {
  158. order = ` ORDER BY ` + orderRule
  159. }
  160. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  161. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  162. return
  163. }
  164. // GetBusinessConf 获取商家配置
  165. func GetBusinessConf() (list map[string]string, err error) {
  166. list = make(map[string]string)
  167. var items []*BusinessConf
  168. o := orm.NewOrm()
  169. sql := `SELECT * FROM business_conf`
  170. _, err = o.Raw(sql).QueryRows(&items)
  171. if err != nil {
  172. return
  173. }
  174. for _, v := range items {
  175. if v.ValType == 4 {
  176. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  177. continue
  178. }
  179. list[v.ConfKey] = v.ConfVal
  180. }
  181. return
  182. }
  183. // BusinessConfUpdate 更新配置
  184. type BusinessConfUpdate struct {
  185. ConfKey string
  186. ConfVal string
  187. }
  188. // UpdateBusinessConfMulti 批量修改配置
  189. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  190. o := orm.NewOrm()
  191. p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
  192. if err != nil {
  193. return
  194. }
  195. defer func() {
  196. _ = p.Close()
  197. }()
  198. for _, v := range items {
  199. _, err = p.Exec(v.ConfVal, v.ConfKey)
  200. if err != nil {
  201. return
  202. }
  203. }
  204. return
  205. }
  206. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  207. o := orm.NewOrm()
  208. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  209. err = o.Raw(sql, key).QueryRow(&item)
  210. return
  211. }
  212. // InitUseMongoConf
  213. // @Description:
  214. // @author: Roc
  215. // @datetime 2024-07-01 13:49:09
  216. func InitUseMongoConf() {
  217. useMongo, e := GetBusinessConfByKey("UseMongo")
  218. if e != nil {
  219. return
  220. }
  221. if useMongo.ConfVal == `true` {
  222. utils.UseMongo = true
  223. }
  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. // 图表有效期的过期时间
  239. if BusinessConfMap[BusinessConfReportChartExpiredTime] != "" {
  240. reportChartExpiredTime, _ := strconv.Atoi(BusinessConfMap[BusinessConfReportChartExpiredTime])
  241. if reportChartExpiredTime <= 0 {
  242. reportChartExpiredTime = 30
  243. }
  244. utils.BusinessConfReportChartExpiredTime = time.Duration(reportChartExpiredTime) * time.Minute
  245. } else {
  246. utils.BusinessConfReportChartExpiredTime = 30 * time.Minute
  247. }
  248. }