role.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**********************************************
  2. ** @Des: This file ...
  3. ** @Author: haodaquan
  4. ** @Date: 2017-09-14 15:24:51
  5. ** @Last Modified by: haodaquan
  6. ** @Last Modified time: 2017-09-17 11:48:52
  7. ***********************************************/
  8. package backgroundm
  9. import (
  10. "github.com/astaxie/beego/orm"
  11. "wuyebaoxiuapi/models"
  12. )
  13. type Role struct {
  14. Id int
  15. RoleName string
  16. Detail string
  17. Status int
  18. CreateId int
  19. UpdateId int
  20. CreateTime int64
  21. UpdateTime int64
  22. }
  23. func (a *Role) TableName() string {
  24. return models.TableName("uc_role")
  25. }
  26. func RoleGetList(page, pageSize int, filters ...interface{}) ([]*Role, int64) {
  27. offset := (page - 1) * pageSize
  28. list := make([]*Role, 0)
  29. query := orm.NewOrm().QueryTable(models.TableName("uc_role"))
  30. if len(filters) > 0 {
  31. l := len(filters)
  32. for k := 0; k < l; k += 2 {
  33. query = query.Filter(filters[k].(string), filters[k+1])
  34. }
  35. }
  36. total, _ := query.Count()
  37. query.OrderBy("-id").Limit(pageSize, offset).All(&list)
  38. return list, total
  39. }
  40. func RoleAdd(role *Role) (int64, error) {
  41. id, err := orm.NewOrm().Insert(role)
  42. if err != nil {
  43. return 0, err
  44. }
  45. return id, nil
  46. }
  47. func init() {
  48. orm.RegisterModel(new(Role))
  49. }
  50. func RoleGetById(id int) (*Role, error) {
  51. r := new(Role)
  52. err := orm.NewOrm().QueryTable(models.TableName("uc_role")).Filter("id", id).One(r)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return r, nil
  57. }
  58. func (r *Role) Update(fields ...string) error {
  59. if _, err := orm.NewOrm().Update(r, fields...); err != nil {
  60. return err
  61. }
  62. return nil
  63. }