campus_repo.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // 校区仓储(T6):t_campus 的**作用域**数据访问。
  2. //
  3. // 与 identity 平台级仓储的关键差异(架构规格 §3.5):
  4. // - **继承 scope.BaseRepo**:t_campus 隶属机构(institution_id 列),是"租户内的校区"维度资源,
  5. // 每次查询必须经 Scoping 强制过滤——多租户防线的唯一收口点在 BaseRepo.Scoping,
  6. // 本包禁止裸查(r.DB.Where(...) 不经过 Scoping = 跨校区/跨机构越权,风险登记册项)。
  7. // - scoping 行为(由 scope 包保证):
  8. // Sc 为 nil → panic(fail-fast,正常请求链路 Auth 中间件保证作用域已注入);
  9. // SuperAdmin=true → 不加机构/校区过滤(平台超管跨机构视角);
  10. // 普通用户 → 强制 institution_id = Sc.InstitutionID;CampusID>0 时再强制 campus_id
  11. // (老师角色锁定本校;教务/财务 campus=0 时可看本机构全部校区)。
  12. //
  13. // 依赖方向铁律:org repository → infra/scope + org/model + infra/errors(全下层/同域),
  14. // 不依赖 handler/service。
  15. package repository
  16. import (
  17. "context"
  18. stderrors "errors"
  19. "gorm.io/gorm"
  20. "yuansiwei/unified-backend/internal/infra/errors"
  21. appscope "yuansiwei/unified-backend/internal/infra/scope"
  22. "yuansiwei/unified-backend/internal/org/model"
  23. )
  24. // CampusRepo 校区仓储(作用域强制过滤)。
  25. type CampusRepo struct {
  26. appscope.BaseRepo
  27. }
  28. // NewCampusRepo 构造校区仓储(Handler 在拿到 Scope 后调用;db 必填、sc 可为 nil——
  29. // nil 进入 Scoping 会 panic,属预期 fail-fast,见 scope.BaseRepo 注释)。
  30. func NewCampusRepo(db *gorm.DB, sc *appscope.Scope) *CampusRepo {
  31. return &CampusRepo{BaseRepo: appscope.NewBaseRepo(db, sc)}
  32. }
  33. // CampusFilter 校区列表筛选条件(keyword 模糊匹配校区名 / 状态精确匹配;均可空)。
  34. type CampusFilter struct {
  35. Keyword string
  36. Status string
  37. }
  38. // PagedList 分页取校区列表(强制作用域过滤:普通用户仅见本机构;CampusID>0 时仅见本校区)。
  39. //
  40. // 入参:
  41. // - ctx context.Context:请求上下文(GORM WithContext 传递取消/超时)。
  42. // - f CampusFilter:筛选条件(Keyword/Status 为空则不过滤该维度)。
  43. // - page int:页码(从 1 开始;<1 按 1 处理)。
  44. // - pageSize int:每页条数(0/负数按 10 兜底——与 response.ParsePage 同语义)。
  45. //
  46. // 出参:
  47. // - []model.Campus:当前页记录(无记录时返回非 nil 空切片,JSON 序列化为 [] 而非 null)。
  48. // - int64:符合筛选条件的总记录数(分页器"共 N 条")。
  49. // - error:*AppError——ErrDBError(5002) 数据库异常;无作用域由 Scoping panic 兜底(全局 Recover)。
  50. func (r *CampusRepo) PagedList(ctx context.Context, f CampusFilter, page, pageSize int) ([]model.Campus, int64, error) {
  51. if page < 1 {
  52. page = 1
  53. }
  54. if pageSize < 1 {
  55. pageSize = 10
  56. }
  57. q := r.Scoping(r.WithContext(ctx).Model(&model.Campus{}))
  58. if f.Keyword != "" {
  59. q = q.Where("campus_name LIKE ?", "%"+f.Keyword+"%")
  60. }
  61. if f.Status != "" {
  62. q = q.Where("status = ?", f.Status)
  63. }
  64. var total int64
  65. if err := q.Count(&total).Error; err != nil { // GORM 纪律:Count 收 *int64
  66. return nil, 0, errors.WithCause(errors.ErrDBError, err)
  67. }
  68. list := make([]model.Campus, 0, pageSize)
  69. if err := q.Order("campus_id ASC").
  70. Offset((page - 1) * pageSize).
  71. Limit(pageSize).
  72. Find(&list).Error; err != nil {
  73. return nil, 0, errors.WithCause(errors.ErrDBError, err)
  74. }
  75. return list, total, nil
  76. }
  77. // Get 按校区 ID 取单个校区(强制作用域过滤:非超管取别家/别校校区 → 查不到 → ErrNotFound)。
  78. //
  79. // 入参:ctx 请求上下文;campusID 校区 ID(>0)。
  80. // 出参:*model.Campus(命中);error——ErrNotFound(3001) 不存在或作用域外 / ErrDBError(5002)。
  81. func (r *CampusRepo) Get(ctx context.Context, campusID int64) (*model.Campus, error) {
  82. var cp model.Campus
  83. err := r.Scoping(r.WithContext(ctx)).
  84. Where("campus_id = ?", campusID).
  85. First(&cp).Error
  86. if stderrors.Is(err, gorm.ErrRecordNotFound) {
  87. return nil, errors.ErrNotFound
  88. }
  89. if err != nil {
  90. return nil, errors.WithCause(errors.ErrDBError, err)
  91. }
  92. return &cp, nil
  93. }