media_permission_mapping.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package models
  2. import (
  3. "eta/eta_mini_crm_ht/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type MediaPermissionMapping struct {
  8. Id int `orm:"pk" description:"id"` // 主键
  9. PermissionId int `description:"权限ID"` // 权限 ID
  10. MediaId int `description:"媒体ID"` // 媒体 ID
  11. MediaType MediaType `description:"媒体类型"` // 媒体类型
  12. Deleted int `description:"是否删除"`
  13. CreatedTime time.Time `description:"创建时间"`
  14. UpdatedTime time.Time `description:"更新时间"`
  15. }
  16. // TableName returns the table name for this model.
  17. func (mp *MediaPermissionMapping) TableName() string {
  18. return "media_permission_mappings"
  19. }
  20. func DeleteMappingsById(mediaId int) (err error) {
  21. o := orm.NewOrm()
  22. _, err = o.QueryTable(new(MediaPermissionMapping)).Filter("media_id", mediaId).Update(orm.Params{"deleted": 1})
  23. return
  24. }
  25. func GetMappingsByCondition(condition string, pars []interface{}) (items []int64, err error) {
  26. o := orm.NewOrm()
  27. sql := `SELECT distinct media_id FROM media_permission_mappings WHERE DELETED=0 `
  28. if condition != "" {
  29. sql += condition
  30. }
  31. _, err = o.Raw(sql, pars).QueryRows(&items)
  32. return
  33. }
  34. func GetPermissionIdsByMediaId(mediaType MediaType, mediaId int) (ids []int, err error) {
  35. o := orm.NewOrm()
  36. var sql string
  37. if mediaType == "" {
  38. sql = `SELECT distinct permission_id FROM media_permission_mappings WHERE media_id=? and DELETED=0 `
  39. _, err = o.Raw(sql, mediaId).QueryRows(&ids)
  40. } else {
  41. sql = `SELECT distinct permission_id FROM media_permission_mappings WHERE media_id=? and media_type=? and DELETED=0 `
  42. _, err = o.Raw(sql, mediaId, mediaType).QueryRows(&ids)
  43. }
  44. return
  45. }
  46. func GetMediaIdIdsByPermissionId(permissionIds []int) (ids []int, err error) {
  47. o := orm.NewOrm()
  48. condition := " permission_id in (" + utils.GetOrmReplaceHolder(len(permissionIds)) + ")"
  49. sql := `SELECT distinct media_id FROM media_permission_mappings WHERE` + condition + `and DELETED=0 `
  50. _, err = o.Raw(sql, permissionIds).QueryRows(&ids)
  51. return
  52. }