permission.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_mini_crm_ht/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. "time"
  7. )
  8. // MetaInfo 表示 meta_infos 表的模型
  9. // Permission 表示权限表的结构
  10. type Permission struct {
  11. Id int `description:"column:id;primary_key;autoIncrement"`
  12. PermissionId int `description:"column:permission_id"`
  13. Name string `description:"column:name"`
  14. ParentId int `description:"column:parent_id"`
  15. RiskLevel string `description:"column:risk_level"`
  16. Sort int `description:"column:sort"`
  17. CreateTime time.Time `description:"column:create_time"`
  18. UpdateTime time.Time `description:"column:update_time"`
  19. }
  20. func (p *Permission) TableName() string {
  21. return "permissions"
  22. }
  23. func (p *Permission) Insert() (insert int64, err error) {
  24. o := orm.NewOrm()
  25. insert, err = o.Insert(p)
  26. return
  27. }
  28. func GetPermissionList() (items []*Permission, err error) {
  29. o := orm.NewOrm()
  30. sql := `SELECT * FROM permissions ORDER BY sort ASC`
  31. _, err = o.Raw(sql).QueryRows(&items)
  32. return
  33. }
  34. func GetPermissionByPermissionId(permissionId int) (items *Permission, err error) {
  35. o := orm.NewOrm()
  36. sql := `SELECT * FROM permissions where permission_id =?`
  37. err = o.Raw(sql, permissionId).QueryRow(&items)
  38. return
  39. }
  40. func (p *Permission) SetPermissionRiskLevel() (err error) {
  41. o := orm.NewOrm()
  42. _, err = o.Update(p, "RiskLevel")
  43. return
  44. }
  45. func GetPermissionNames(ids []int) (permissionNameList []string, err error) {
  46. o := orm.NewOrm()
  47. if len(ids) == 0 {
  48. err = errors.New("品种id列表为空")
  49. return
  50. }
  51. condition := " permission_id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  52. sql := `SELECT name FROM permissions where ` + condition + ` order by permission_id asc`
  53. _, err = o.Raw(sql, ids).QueryRows(&permissionNameList)
  54. if err != nil {
  55. return
  56. }
  57. return
  58. }
  59. func GetPermissionNamesWithRiskLevel(ids []int) (permissionNameList []string, err error) {
  60. o := orm.NewOrm()
  61. var sql string
  62. if len(ids) == 0 {
  63. sql = `SELECT distinct name FROM permissions where risk_level !='' order by permission_id asc`
  64. } else {
  65. condition := " permission_id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  66. sql = `SELECT distinct name FROM permissions where risk_level !='' and ` + condition + ` order by permission_id asc`
  67. }
  68. _, err = o.Raw(sql, ids).QueryRows(&permissionNameList)
  69. if err != nil {
  70. return
  71. }
  72. return
  73. }
  74. func GetPermissionsWithRiskLevel(ids []int) (permissionList []Permission, err error) {
  75. o := orm.NewOrm()
  76. var sql string
  77. if len(ids) == 0 {
  78. sql = `SELECT * FROM permissions where risk_level !='' order by permission_id asc`
  79. } else {
  80. condition := " permission_id in (" + utils.GetOrmReplaceHolder(len(ids)) + ")"
  81. sql = `SELECT * FROM permissions where risk_level !='' and ` + condition + ` order by permission_id asc`
  82. }
  83. _, err = o.Raw(sql, ids).QueryRows(&permissionList)
  84. if err != nil {
  85. return
  86. }
  87. return
  88. }
  89. func GetClassifyIdByPermissionIds(permissionIds []int) (classifyIds []int, err error) {
  90. o := orm.NewOrm()
  91. if len(permissionIds) == 0 {
  92. return
  93. }
  94. sql := `SELECT distinct classify_id FROM permission_classify_mapping where permission_id in (` + utils.GetOrmReplaceHolder(len(permissionIds)) + `) order by permission_id asc`
  95. _, err = o.Raw(sql, permissionIds).QueryRows(&classifyIds)
  96. return
  97. }
  98. func FilterPermissionIdsWithRiskLevel(ids []int) (permissionIds []int, err error) {
  99. o := orm.NewOrm()
  100. var sql string
  101. if len(ids) == 0 {
  102. sql = `SELECT distinct permission_id FROM permissions where risk_level!='' order by permission_id asc`
  103. } else {
  104. sql = `SELECT distinct permission_id FROM permissions where risk_level!='' and permission_id in (` + utils.GetOrmReplaceHolder(len(ids)) + `) order by permission_id asc`
  105. }
  106. _, err = o.Raw(sql, ids).QueryRows(&permissionIds)
  107. return
  108. }
  109. func GetPermissionByName(name string) (permission Permission, err error) {
  110. o := orm.NewOrm()
  111. sql := `SELECT * FROM permissions where name = ?`
  112. err = o.Raw(sql, name).QueryRow(&permission)
  113. return
  114. }