permission.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package config
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm"
  5. "gorm.io/gorm/clause"
  6. "time"
  7. )
  8. const (
  9. Columns = "permission_id,name,parent_id,risk_level,sort"
  10. )
  11. // Permission 定义权限表的结构
  12. type Permission struct {
  13. Id int `gorm:"primaryKey;autoIncrement"`
  14. PermissionId int `gorm:"type:int"`
  15. Name string `gorm:"type:varchar(255)"`
  16. ParentId int `gorm:"default:null"`
  17. RiskLevel string `gorm:"type:varchar(10)"`
  18. Sort int `gorm:"default:null"`
  19. CreateTime time.Time `gorm:"default:null"`
  20. UpdateTime time.Time `gorm:"default:null;onUpdate:CURRENT_TIMESTAMP"`
  21. }
  22. // BeforeCreate 钩子用于在创建记录前设置 CreateTime
  23. func (p *Permission) BeforeCreate(tx *gorm.DB) (err error) {
  24. p.CreateTime = time.Now()
  25. return
  26. }
  27. func BatchInsertOrUpdate(list []Permission) (err error) {
  28. db := models.Main()
  29. OnConflictFunc := clause.OnConflict{
  30. Columns: []clause.Column{{Name: "permission_id"}},
  31. DoUpdates: clause.AssignmentColumns([]string{"name", "parent_id", "sort"}),
  32. }
  33. // 执行批量插入或更新操作
  34. err = db.Clauses(OnConflictFunc).Create(&list).Error
  35. return
  36. }
  37. func GetPermissionByName(name string) (permission Permission, err error) {
  38. db := models.Main()
  39. err = db.Model(&Permission{}).Select(Columns).Where("name = ?", name).First(&permission).Error
  40. return
  41. }
  42. func GetPermissionList() (list []Permission, err error) {
  43. db := models.Main()
  44. err = db.Model(&Permission{}).Select(Columns).Order("parent_id asc").Order("sort asc").Find(&list).Error
  45. return
  46. }
  47. func GetPermissionListByIds(permissionIds []int) (list []Permission, err error) {
  48. db := models.Main()
  49. //一级分类不能挂风险等级
  50. err = db.Model(&Permission{}).Select(Columns).Where("permission_id in ? and (risk_level !='' and risk_level is NOT NULL and parent_id !=0 )", permissionIds).Find(&list).Error
  51. return
  52. }
  53. func GetPermissionListWithRisk() (list []Permission, err error) {
  54. db := models.Main()
  55. //一级分类不能挂风险等级
  56. err = db.Model(&Permission{}).Select(Columns).Where("risk_level is NOT NULL and risk_level != '' and parent_id !=0").Find(&list).Error
  57. return
  58. }
  59. func GetPermissionNamesByPermissionIds(ids []int) (permissionNames []string, err error) {
  60. db := models.Main()
  61. err = db.Model(&Permission{}).Select("name").Where("permission_id in ?", ids).Scan(&permissionNames).Error
  62. return
  63. }
  64. func GetPermissionsByPermissionIds(ids []int) (permissions []Permission, err error) {
  65. db := models.Main()
  66. err = db.Model(&Permission{}).Select(Columns).Where("permission_id in ?", ids).Find(&permissions).Error
  67. return
  68. }
  69. func GetFirstPermissionsByIds(ids []int) (permissionList []Permission, err error) {
  70. db := models.Main()
  71. err = db.Model(&Permission{}).Select("permission_id, name").Where("permission_id in (select parent_id from permissions WHERE permission_id in ?)", ids).Find(&permissionList).Error
  72. //sql := "select permission_id, name from permission where chart_permission_id in (select parent_id from chart_permission WHERE chart_permission_id in ?)"
  73. //err = doSql(sql, &chartPermissionList, ids)
  74. return
  75. }
  76. func GetPermissionIdByName(name string) (id int, err error) {
  77. db := models.Main()
  78. //sql := "select chart_permission_id from chart_permission where permission_name =? "
  79. err = db.Model(&Permission{}).Select("permission_id").Where("name = ?", name).Scan(&id).Error
  80. return
  81. }
  82. func GetSecondPermissionsByClassifyID(classifyId int) (permissionList []Permission, err error) {
  83. //sql := "select chart_permission_id, permission_name from chart_permission WHERE chart_permission_id in( select DISTINCT cpskwp.chart_permission_id from chart_permission_search_key_word_mapping cpskwp where cpskwp.classify_id=? order by cpskwp.chart_permission_id)"
  84. //err = doSql(sql, &chartPermissionList, classifyID)
  85. db := models.Main()
  86. err = db.Model(&Permission{}).Select("permission_id, name,risk_level").Where("permission_id in ( select DISTINCT pcm.permission_id from permission_classify_mapping pcm where pcm.classify_id=? order by pcm.permission_id)", classifyId).Find(&permissionList).Error
  87. return
  88. }
  89. func GetFirstPermissionsByClassifyID(classifyId int) (permissionList []Permission, err error) {
  90. db := models.Main()
  91. //sql := "select chart_permission_id, permission_name,parent_id from chart_permission WHERE chart_permission_id in( select DISTINCT cp.parent_id from chart_permission_search_key_word_mapping cpskwp left JOIN chart_permission cp on cp.chart_permission_id =cpskwp.chart_permission_id where cpskwp.classify_id=? order by cp.parent_id)"
  92. err = db.Model(&Permission{}).Select("permission_id, name,parent_id").Where("permission_id in ( select DISTINCT ps.parent_id from permission_classify_mapping pcm left JOIN permissions ps on ps.permission_id =pcm.permission_id where pcm.classify_id=? order by ps.parent_id)", classifyId).Find(&permissionList).Error
  93. return
  94. }
  95. func PermissionsByPermissionId(permissionId int) (permission Permission, err error) {
  96. db := models.Main()
  97. //sql := "select chart_permission_id, permission_name,parent_id from chart_permission WHERE chart_permission_id in( select DISTINCT cp.parent_id from chart_permission_search_key_word_mapping cpskwp left JOIN chart_permission cp on cp.chart_permission_id =cpskwp.chart_permission_id where cpskwp.classify_id=? order by cp.parent_id)"
  98. err = db.Model(&Permission{}).Select(Columns).Where("permission_id =? ", permissionId).First(&permission).Error
  99. return
  100. }
  101. type PermissionWeight struct {
  102. PermissionId int
  103. Weight int
  104. }