business_conf.go 9.5 KB

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