index.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package actions
  2. import (
  3. dto2 "cold-delivery/common/dto"
  4. "cold-delivery/common/model"
  5. "errors"
  6. "gorm.io/gorm"
  7. "net/http"
  8. "github.com/gin-gonic/gin"
  9. log "gogs.baozhida.cn/zoie/OAuth-core/logger"
  10. "gogs.baozhida.cn/zoie/OAuth-core/pkg"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg/response"
  12. )
  13. // IndexAction 通用查询动作
  14. func IndexAction(m model.ActiveRecord, d dto2.Index, f func() interface{}) gin.HandlerFunc {
  15. return func(c *gin.Context) {
  16. db, err := pkg.GetOrm(c)
  17. if err != nil {
  18. log.Error(err)
  19. return
  20. }
  21. msgID := pkg.GenerateMsgIDFromContext(c)
  22. list := f()
  23. object := m.Generate()
  24. req := d.Generate()
  25. var count int64
  26. //查询列表
  27. err = req.Bind(c)
  28. if err != nil {
  29. response.Error(c, http.StatusUnprocessableEntity, err, "参数验证失败")
  30. return
  31. }
  32. //数据权限检查
  33. p := GetPermissionFromContext(c)
  34. err = db.WithContext(c).Model(object).
  35. Scopes(
  36. dto2.MakeCondition(req.GetNeedSearch()),
  37. dto2.Paginate(req.GetPageSize(), req.GetPageIndex()),
  38. Permission(object.TableName(), p),
  39. ).
  40. Find(list).Limit(-1).Offset(-1).
  41. Count(&count).Error
  42. if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
  43. log.Errorf("MsgID[%s] Index error: %s", msgID, err)
  44. response.Error(c, 500, err, "查询失败")
  45. return
  46. }
  47. response.PageOK(c, list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  48. c.Next()
  49. }
  50. }