request.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package request
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/baiy/Cadmin-server-go/models"
  6. "github.com/baiy/Cadmin-server-go/models/requestRelate"
  7. "github.com/doug-martin/goqu/v9"
  8. )
  9. type Model struct {
  10. models.Model
  11. Type string `json:"type"`
  12. Name string `json:"name"`
  13. Action string `json:"action"`
  14. Call string `json:"call"`
  15. }
  16. func (m Model) AuthIds() []int {
  17. return requestRelate.AuthIds([]int{m.Id})
  18. }
  19. func Add(name, action, type_, call string) error {
  20. exist, _ := GetByAction(action)
  21. if exist.Id > 0 {
  22. return errors.New(fmt.Sprintf("[%s] 请求已经存在", action))
  23. }
  24. _, err := models.Db.Insert("admin_request").Rows(
  25. goqu.Record{"name": name, "action": action, "type": type_, "call": call},
  26. ).Executor().Exec()
  27. return err
  28. }
  29. func Updata(id int, name, action, type_, call string) error {
  30. exist, _ := GetByAction(action)
  31. if exist.Id > 0 && exist.Id != id {
  32. return errors.New(fmt.Sprintf("[%s] 请求已经存在", action))
  33. }
  34. _, err := models.Db.Update("admin_request").Where(goqu.Ex{"id": id}).Set(
  35. goqu.Record{"name": name, "action": action, "type": type_, "call": call},
  36. ).Executor().Exec()
  37. return err
  38. }
  39. func Remove(id int) error {
  40. _, err := models.Db.Delete("admin_request").Where(goqu.Ex{
  41. "id": id,
  42. }).Executor().Exec()
  43. if err == nil {
  44. _ = requestRelate.Remove(id, 0)
  45. }
  46. return err
  47. }
  48. func GetByAction(action string) (model *Model, err error) {
  49. model = new(Model)
  50. found, err := models.Db.From("admin_request").Where(goqu.Ex{
  51. "action": action,
  52. }).ScanStruct(model)
  53. if err == nil {
  54. if !found {
  55. err = errors.New("请求不存在")
  56. }
  57. }
  58. return
  59. }
  60. func GetLists(ids []int) ([]*Model, error) {
  61. model := make([]*Model, 0)
  62. if len(ids) == 0 {
  63. return model, nil
  64. }
  65. if len(ids) == 0 {
  66. return model, nil
  67. }
  68. err := models.Db.From("admin_request").Where(goqu.Ex{
  69. "id": ids,
  70. }).ScanStructs(&model)
  71. return model, err
  72. }