business_conf.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package models
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/utils"
  5. "fmt"
  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. BusinessConfIsBIApprove = "IsBIApprove"
  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. BusinessConfEdbStopRefreshRule = "EdbStopRefreshRule" // 是否停止指标刷新规则
  53. BusinessConfOuterReportApiUrl = "OuterReportApiUrl" // 智力共享-报告API地址
  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 `gorm:"column:id;primaryKey"` //`orm:"column(id);pk" gorm:"primaryKey" `
  72. ConfKey string `gorm:"column:conf_key"` //`description:"配置Key"`
  73. ConfVal string `gorm:"column:conf_val"` //`description:"配置值"`
  74. ValType int `gorm:"column:val_type"` //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
  75. Necessary int `gorm:"column:necessary"` //`description:"是否必填:0-否;1-是"`
  76. Remark string `gorm:"column:remark"` // `description:"备注"`
  77. CreateTime time.Time `gorm:"column:create_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. err = global.DEFAULT_DmSQL.Create(m).Error
  87. return
  88. }
  89. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  90. if len(items) == 0 {
  91. return
  92. }
  93. err = global.DEFAULT_DmSQL.CreateInBatches(items, utils.MultiAddNum).Error
  94. return
  95. }
  96. func (m *BusinessConf) Update(cols []string) (err error) {
  97. err = global.DEFAULT_DmSQL.Select(cols).Updates(m).Error
  98. return
  99. }
  100. func (m *BusinessConf) Del() (err error) {
  101. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  102. err = global.DEFAULT_DmSQL.Exec(sql, m.Id).Error
  103. return
  104. }
  105. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  106. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  107. err = global.DEFAULT_DmSQL.Raw(sql, id).Find(&item).Error
  108. return
  109. }
  110. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  111. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  112. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  113. return
  114. }
  115. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  116. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  117. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Scan(&count).Error
  118. return
  119. }
  120. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  121. fields := strings.Join(fieldArr, ",")
  122. if len(fieldArr) == 0 {
  123. fields = `*`
  124. }
  125. order := `ORDER BY create_time DESC`
  126. if orderRule != "" {
  127. order = ` ORDER BY ` + orderRule
  128. }
  129. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  130. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  131. return
  132. }
  133. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  134. fields := strings.Join(fieldArr, ",")
  135. if len(fieldArr) == 0 {
  136. fields = `*`
  137. }
  138. order := `ORDER BY create_time DESC`
  139. if orderRule != "" {
  140. order = ` ORDER BY ` + orderRule
  141. }
  142. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  143. pars = append(pars, startSize)
  144. pars = append(pars, pageSize)
  145. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&items).Error
  146. return
  147. }
  148. // GetBusinessConf 获取商家配置
  149. func GetBusinessConf() (list map[string]string, err error) {
  150. list = make(map[string]string)
  151. var items []*BusinessConf
  152. sql := `SELECT * FROM business_conf`
  153. err = global.DEFAULT_DmSQL.Raw(sql).Find(&items).Error
  154. if err != nil {
  155. return
  156. }
  157. for _, v := range items {
  158. if v.ValType == 4 {
  159. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  160. continue
  161. }
  162. list[v.ConfKey] = v.ConfVal
  163. }
  164. return
  165. }
  166. // BusinessConfUpdate 更新配置
  167. type BusinessConfUpdate struct {
  168. ConfKey string
  169. ConfVal string
  170. }
  171. // UpdateBusinessConfMulti 批量修改配置
  172. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  173. tx := global.DEFAULT_DmSQL.Begin()
  174. for _, v := range items {
  175. err = tx.Exec("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?", v.ConfVal, v.ConfKey).Error
  176. if err != nil {
  177. tx.Rollback()
  178. return
  179. }
  180. }
  181. tx.Commit()
  182. return
  183. }
  184. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  185. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  186. err = global.DEFAULT_DmSQL.Raw(sql, key).First(&item).Error
  187. return
  188. }
  189. type BusinessConfSingleSaveReq struct {
  190. ConfKey string `description:"配置Key"`
  191. ConfVal string `description:"配置值"`
  192. }
  193. type EdbStopRefreshRule struct {
  194. IsOpen int `description:"是否开启自动禁用1,开启,0未开启"`
  195. BaseIndexStopDays int `description:"数据源间隔天数未加入指标库则停用"`
  196. EdbStopDays int `description:"指标库间隔天数未引用则停用"`
  197. }
  198. type BusinessConfSingleResp struct {
  199. ConfVal string
  200. }
  201. // InitUseMongoConf
  202. // @Description:
  203. // @author: Roc
  204. // @datetime 2024-07-01 13:49:09
  205. func InitUseMongoConf() {
  206. useMongo, e := GetBusinessConfByKey("UseMongo")
  207. if e != nil {
  208. return
  209. }
  210. if useMongo.ConfVal == `true` {
  211. utils.UseMongo = true
  212. }
  213. }
  214. func InitBusinessConf() {
  215. var e error
  216. BusinessConfMap, e = GetBusinessConf()
  217. if e != nil {
  218. return
  219. }
  220. }