config.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package models
  2. import (
  3. "errors"
  4. _ "time"
  5. orm "device-manage/common/global"
  6. "device-manage/tools"
  7. )
  8. type SysConfig struct {
  9. ConfigId int `json:"configId" gorm:"primary_key;auto_increment;"` //编码
  10. ConfigName string `json:"configName" gorm:"size:128;"` //参数名称
  11. ConfigKey string `json:"configKey" gorm:"size:128;"` //参数键名
  12. ConfigValue string `json:"configValue" gorm:"size:255;"` //参数键值
  13. ConfigType string `json:"configType" gorm:"size:64;"` //是否系统内置
  14. Remark string `json:"remark" gorm:"size:128;"` //备注
  15. CreateBy string `json:"createBy" gorm:"size:128;"`
  16. UpdateBy string `json:"updateBy" gorm:"size:128;"`
  17. BaseModel
  18. DataScope string `json:"dataScope" gorm:"-"`
  19. Params string `json:"params" gorm:"-"`
  20. }
  21. func (SysConfig) TableName() string {
  22. return "sys_config"
  23. }
  24. // Config 创建
  25. func (e *SysConfig) Create() (SysConfig, error) {
  26. var doc SysConfig
  27. var i int64
  28. orm.Eloquent.Table(e.TableName()).Where("config_name=? or config_key = ?", e.ConfigName, e.ConfigKey).Count(&i)
  29. if i > 0 {
  30. return doc, errors.New("参数名称或者参数键名已经存在!")
  31. }
  32. result := orm.Eloquent.Table(e.TableName()).Create(&e)
  33. if result.Error != nil {
  34. err := result.Error
  35. return doc, err
  36. }
  37. doc = *e
  38. return doc, nil
  39. }
  40. // 获取 Config
  41. func (e *SysConfig) Get() (SysConfig, error) {
  42. var doc SysConfig
  43. table := orm.Eloquent.Table(e.TableName())
  44. if e.ConfigId != 0 {
  45. table = table.Where("config_id = ?", e.ConfigId)
  46. }
  47. if e.ConfigKey != "" {
  48. table = table.Where("config_key = ?", e.ConfigKey)
  49. }
  50. if err := table.First(&doc).Error; err != nil {
  51. return doc, err
  52. }
  53. return doc, nil
  54. }
  55. func (e *SysConfig) GetPage(pageSize int, pageIndex int) ([]SysConfig, int, error) {
  56. var doc []SysConfig
  57. table := orm.Eloquent.Table(e.TableName())
  58. if e.ConfigName != "" {
  59. table = table.Where("config_name = ?", e.ConfigName)
  60. }
  61. if e.ConfigKey != "" {
  62. table = table.Where("config_key = ?", e.ConfigKey)
  63. }
  64. if e.ConfigType != "" {
  65. table = table.Where("config_type = ?", e.ConfigType)
  66. }
  67. // 数据权限控制
  68. dataPermission := new(DataPermission)
  69. dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
  70. table, err := dataPermission.GetDataScope("sys_config", table)
  71. if err != nil {
  72. return nil, 0, err
  73. }
  74. var count int64
  75. if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  76. return nil, 0, err
  77. }
  78. //table.Where("`deleted_at` IS NULL").Count(&count)
  79. return doc, int(count), nil
  80. }
  81. func (e *SysConfig) Update(id int) (update SysConfig, err error) {
  82. if err = orm.Eloquent.Table(e.TableName()).Where("config_id = ?", id).First(&update).Error; err != nil {
  83. return
  84. }
  85. if e.ConfigName != "" && e.ConfigName != update.ConfigName {
  86. return update, errors.New("参数名称不允许修改!")
  87. }
  88. if e.ConfigKey != "" && e.ConfigKey != update.ConfigKey {
  89. return update, errors.New("参数键名不允许修改!")
  90. }
  91. //参数1:是要修改的数据
  92. //参数2:是修改的数据
  93. if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
  94. return
  95. }
  96. return
  97. }
  98. func (e *SysConfig) Delete() (success bool, err error) {
  99. if err = orm.Eloquent.Table(e.TableName()).Where("config_id = ?", e.ConfigId).Delete(&SysConfig{}).Error; err != nil {
  100. success = false
  101. return
  102. }
  103. success = true
  104. return
  105. }
  106. func (e *SysConfig) BatchDelete(id []int) (Result bool, err error) {
  107. if err = orm.Eloquent.Table(e.TableName()).Where("config_id in (?)", id).Delete(&SysConfig{}).Error; err != nil {
  108. return
  109. }
  110. Result = true
  111. return
  112. }