business_conf.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/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. )
  49. const (
  50. BusinessConfReportApproveTypeEta = "eta"
  51. BusinessConfReportApproveTypeOther = "other"
  52. BusinessConfClientFlagNanHua = "nhqh" // 南华标记
  53. BusinessConfEmailClientSmtp = "smtp" // 普通邮箱标记
  54. )
  55. // FromSceneMap 数据源名称与数据源ID的对应关系
  56. var FromSceneMap = map[int]string{
  57. 1: "SmartReportSheetSize",
  58. 2: "ReportSheetSize",
  59. 3: "EnReportSheetSize",
  60. 4: "CnPptSheetSize",
  61. 5: "EnPptSheetSize",
  62. }
  63. // BusinessConf 商户配置表
  64. type BusinessConf struct {
  65. Id int `gorm:"column:id;primaryKey;autoIncrement"`
  66. ConfKey string `gorm:"column:conf_key;type:varchar(128);not null;default:'';uniqueIndex:idx_conf_key"`
  67. ConfVal string `gorm:"column:conf_val;type:text"`
  68. ValType int `gorm:"column:val_type;not null;default:0"`
  69. Necessary int `gorm:"column:necessary;not null;default:0"`
  70. Remark string `gorm:"column:remark;type:varchar(128);not null;default:''"`
  71. CreateTime *time.Time `gorm:"column:create_time"`
  72. }
  73. func (m *BusinessConf) TableName() string {
  74. return "business_conf"
  75. }
  76. func (m *BusinessConf) PrimaryId() string {
  77. return "id"
  78. }
  79. func (m *BusinessConf) Create() (err error) {
  80. err = global.DmSQL["master"].Create(m).Error
  81. if err != nil {
  82. return
  83. }
  84. return
  85. }
  86. func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
  87. if len(items) == 0 {
  88. return
  89. }
  90. err = global.DmSQL["master"].CreateInBatches(items, 500).Error
  91. return
  92. }
  93. func (m *BusinessConf) Update(cols []string) (err error) {
  94. err = global.DmSQL["master"].Model(m).Select(cols).Updates(m).Error
  95. return
  96. }
  97. func (m *BusinessConf) Del() (err error) {
  98. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  99. err = global.DmSQL["master"].Exec(sql, m.Id).Error
  100. return
  101. }
  102. func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
  103. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  104. err = global.DmSQL["master"].Raw(sql, id).First(&item).Error
  105. return
  106. }
  107. func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
  108. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  109. err = global.DmSQL["master"].Raw(sql, pars).First(&item).Error
  110. return
  111. }
  112. func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  113. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  114. err = global.DmSQL["master"].Raw(sql, pars).Scan(&count).Error
  115. return
  116. }
  117. func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
  118. fields := strings.Join(fieldArr, ",")
  119. if len(fieldArr) == 0 {
  120. fields = `*`
  121. }
  122. order := `ORDER BY create_time DESC`
  123. if orderRule != "" {
  124. order = ` ORDER BY ` + orderRule
  125. }
  126. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  127. err = global.DmSQL["master"].Raw(sql, pars).Scan(&items).Error
  128. return
  129. }
  130. func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
  131. fields := strings.Join(fieldArr, ",")
  132. if len(fieldArr) == 0 {
  133. fields = `*`
  134. }
  135. order := `ORDER BY create_time DESC`
  136. if orderRule != "" {
  137. order = ` ORDER BY ` + orderRule
  138. }
  139. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  140. err = global.DmSQL["master"].Raw(sql, pars, startSize, pageSize).Scan(&items).Error
  141. return
  142. }
  143. // GetBusinessConf 获取商家配置
  144. func GetBusinessConf() (list map[string]string, err error) {
  145. list = make(map[string]string)
  146. var items []*BusinessConf
  147. sql := `SELECT * FROM business_conf`
  148. err = global.DmSQL["master"].Raw(sql).Scan(&items).Error
  149. if err != nil {
  150. return
  151. }
  152. for _, v := range items {
  153. if v.ValType == 4 {
  154. list[v.ConfKey] = html.UnescapeString(v.ConfVal)
  155. continue
  156. }
  157. list[v.ConfKey] = v.ConfVal
  158. }
  159. return
  160. }
  161. // BusinessConfUpdate 更新配置
  162. type BusinessConfUpdate struct {
  163. ConfKey string
  164. ConfVal string
  165. }
  166. // UpdateBusinessConfMulti 批量修改配置
  167. func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
  168. sql := "UPDATE business_conf SET conf_val = ? WHERE conf_key = ?"
  169. for _, v := range items {
  170. err = global.DmSQL["master"].Exec(sql, v.ConfVal, v.ConfKey).Error
  171. if err != nil {
  172. return
  173. }
  174. }
  175. return
  176. }
  177. func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
  178. sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
  179. err = global.DmSQL["master"].Raw(sql, key).First(&item).Error
  180. return
  181. }
  182. // InitUseMongoConf
  183. // @Description:
  184. // @author: Roc
  185. // @datetime 2024-07-01 13:49:09
  186. func InitUseMongoConf() {
  187. useMongo, e := GetBusinessConfByKey("UseMongo")
  188. if e != nil {
  189. return
  190. }
  191. if useMongo.ConfVal == `true` {
  192. utils.UseMongo = true
  193. }
  194. }
  195. func InitSmmDataMethodConf() {
  196. utils.SmmDataMethod = "terminal"
  197. useMongo, e := GetBusinessConfByKey("SmmDataMethod")
  198. if e != nil {
  199. return
  200. }
  201. if useMongo.ConfVal == "api" {
  202. utils.SmmDataMethod = "api"
  203. }
  204. }