tdirectorymodel.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package model
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/zeromicro/go-zero/core/stores/sqlx"
  6. )
  7. var _ TDirectoryModel = (*customTDirectoryModel)(nil)
  8. type (
  9. // TDirectoryModel is an interface to be customized, add more methods here,
  10. // and implement the added methods in customTDirectoryModel.
  11. TDirectoryModel interface {
  12. tDirectoryModel
  13. FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, int64, error)
  14. FindDirsByServiceId(ctx context.Context, serviceId int64) ([]string, error)
  15. DeleteByServiceId(ctx context.Context, serviceId int64) error
  16. WithSession(session sqlx.Session) TDirectoryModel
  17. }
  18. customTDirectoryModel struct {
  19. *defaultTDirectoryModel
  20. }
  21. )
  22. // NewTDirectoryModel returns a model for the database table.
  23. func NewTDirectoryModel(conn sqlx.SqlConn) TDirectoryModel {
  24. return &customTDirectoryModel{
  25. defaultTDirectoryModel: newTDirectoryModel(conn),
  26. }
  27. }
  28. func (m *customTDirectoryModel) FindPage(ctx context.Context, page, pageSize int64) ([]TDirectory, int64, error) {
  29. var total int64
  30. if err := m.conn.QueryRowCtx(ctx, &total, fmt.Sprintf("select count(*) from %s", m.table)); err != nil {
  31. return nil, 0, err
  32. }
  33. if total == 0 {
  34. return nil, 0, nil
  35. }
  36. offset := (page - 1) * pageSize
  37. query := fmt.Sprintf("select %s from %s order by `id` limit ? offset ?", tDirectoryRows, m.table)
  38. var resp []TDirectory
  39. if err := m.conn.QueryRowsCtx(ctx, &resp, query, pageSize, offset); err != nil {
  40. return nil, 0, err
  41. }
  42. return resp, total, nil
  43. }
  44. // FindDirsByServiceId 查询某个服务下全部保护目录路径(剔除空值),
  45. // 删除服务前的联动卸载用它拿到要卸载的挂载点清单。
  46. func (m *customTDirectoryModel) FindDirsByServiceId(ctx context.Context, serviceId int64) ([]string, error) {
  47. // 查全列扫描进 struct 切片(与 FindPage 同款成熟模式),再抽取 directory 字符串
  48. query := fmt.Sprintf("select %s from %s where `service_id` = ?", tDirectoryRows, m.table)
  49. var resp []TDirectory
  50. if err := m.conn.QueryRowsCtx(ctx, &resp, query, serviceId); err != nil {
  51. return nil, err
  52. }
  53. dirs := make([]string, 0, len(resp))
  54. for _, d := range resp {
  55. if d.Directory.Valid && d.Directory.String != "" {
  56. dirs = append(dirs, d.Directory.String)
  57. }
  58. }
  59. return dirs, nil
  60. }
  61. // DeleteByServiceId 删除某个服务下的全部保护目录,配合服务删除做级联清理。
  62. func (m *customTDirectoryModel) DeleteByServiceId(ctx context.Context, serviceId int64) error {
  63. query := fmt.Sprintf("delete from %s where `service_id` = ?", m.table)
  64. _, err := m.conn.ExecCtx(ctx, query, serviceId)
  65. return err
  66. }
  67. func (m *customTDirectoryModel) WithSession(session sqlx.Session) TDirectoryModel {
  68. return NewTDirectoryModel(sqlx.NewSqlConnFromSession(session))
  69. }