business_conf.go 9.5 KB

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