role_auth.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**********************************************
  2. ** @Des: This file ...
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-15 11:44:13
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 11:49:13
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "bytes"
  11. "strconv"
  12. "strings"
  13. "github.com/astaxie/beego/orm"
  14. "wuyebaoxiuapi/models"
  15. )
  16. type RoleAuth struct {
  17. AuthId int `orm:"pk"`
  18. RoleId int64
  19. }
  20. func (ra *RoleAuth) TableName() string {
  21. return models.TableName("uc_role_auth")
  22. }
  23. func RoleAuthAdd(ra *RoleAuth) (int64, error) {
  24. return orm.NewOrm().Insert(ra)
  25. }
  26. func RoleAuthGetById(id int) ([]*RoleAuth, error) {
  27. list := make([]*RoleAuth, 0)
  28. query := orm.NewOrm().QueryTable(models.TableName("uc_role_auth"))
  29. _, err := query.Filter("role_id", id).All(&list, "AuthId")
  30. if err != nil {
  31. return nil, err
  32. }
  33. return list, nil
  34. }
  35. func RoleAuthDelete(id int) (int64, error) {
  36. query := orm.NewOrm().QueryTable(models.TableName("uc_role_auth"))
  37. return query.Filter("role_id", id).Delete()
  38. }
  39. //获取多个
  40. func RoleAuthGetByIds(RoleIds string) (Authids string, err error) {
  41. list := make([]*RoleAuth, 0)
  42. query := orm.NewOrm().QueryTable(models.TableName("uc_role_auth"))
  43. ids := strings.Split(RoleIds, ",")
  44. _, err = query.Filter("role_id__in", ids).All(&list, "AuthId")
  45. if err != nil {
  46. return "", err
  47. }
  48. b := bytes.Buffer{}
  49. for _, v := range list {
  50. if v.AuthId != 0 && v.AuthId != 1 {
  51. b.WriteString(strconv.Itoa(v.AuthId))
  52. b.WriteString(",")
  53. }
  54. }
  55. Authids = strings.TrimRight(b.String(), ",")
  56. return Authids, nil
  57. }
  58. func init() {
  59. orm.RegisterModel(new(RoleAuth))
  60. }
  61. func RoleAuthMultiAdd(ras []*RoleAuth) (n int, err error) {
  62. query := orm.NewOrm().QueryTable(models.TableName("uc_role_auth"))
  63. i, _ := query.PrepareInsert()
  64. for _, ra := range ras {
  65. _, err := i.Insert(ra)
  66. if err == nil {
  67. n = n + 1
  68. }
  69. }
  70. i.Close() // 别忘记关闭 statement
  71. return n, err
  72. }