menuRelate.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package menuRelate
  2. import (
  3. "errors"
  4. "github.com/baiy/Cadmin-server-go/models"
  5. "github.com/doug-martin/goqu/v9"
  6. )
  7. type Model struct {
  8. models.Model
  9. AdminMenuId int `json:"admin_menu_id"`
  10. AdminAuthId int `json:"admin_auth_id"`
  11. }
  12. func MenuIds(authIds []int) []int {
  13. ids := make([]int, 0)
  14. _ = models.Db.From("admin_menu_relate").Select("admin_menu_id").Where(goqu.Ex{
  15. "admin_auth_id": authIds,
  16. }).ScanVals(&ids)
  17. return ids
  18. }
  19. func Add(menuId, authId int) error {
  20. _, err := models.Db.Insert("admin_menu_relate").Rows(
  21. goqu.Record{"admin_menu_id": menuId, "admin_auth_id": authId},
  22. ).Executor().Exec()
  23. return err
  24. }
  25. func AddMultiple(menuIds []int, authId int) error {
  26. rows := make([]goqu.Record, len(menuIds))
  27. for key, menuId := range menuIds {
  28. rows[key] = goqu.Record{
  29. "admin_menu_id": menuId,
  30. "admin_auth_id": authId,
  31. }
  32. }
  33. _, err := models.Db.Insert("admin_menu_relate").Rows(rows).Executor().Exec()
  34. return err
  35. }
  36. func RemoveMultiple(menuIds []int, authId int) error {
  37. _, err := models.Db.Delete("admin_menu_relate").Where(goqu.Ex{
  38. "admin_auth_id": authId,
  39. "admin_menu_id": menuIds,
  40. }).Executor().Exec()
  41. return err
  42. }
  43. func Remove(menuId, authId int) error {
  44. if menuId == 0 && authId == 0 {
  45. return errors.New("参数错误")
  46. }
  47. where := make(goqu.Ex)
  48. if menuId != 0 {
  49. where["admin_menu_id"] = menuId
  50. }
  51. if authId != 0 {
  52. where["admin_auth_id"] = authId
  53. }
  54. _, err := models.Db.Delete("admin_menu_relate").Where(where).Executor().Exec()
  55. return err
  56. }