intelligentbuildingcontrol.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package repository
  2. import (
  3. "city_chips/internal/model"
  4. helper "city_chips/pkg/helper/dbutils"
  5. "context"
  6. )
  7. type IntelligentBuildingControlRepository interface {
  8. GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error)
  9. GetPoint(conds map[string]any, pageNum, pageSize int) (*[]model.Point, int64, error)
  10. GetPointType() ([]string, error)
  11. DeviceType() ([]string, error)
  12. DeviceCount() (int64, error)
  13. GetPopintList(conds map[string]any, pageNum, pageSize int) ([]string, int64, error)
  14. GetPointAll(conds map[string]any) (*[]model.Point, error)
  15. }
  16. func NewIntelligentBuildingControlRepository(
  17. repository *Repository,
  18. ) IntelligentBuildingControlRepository {
  19. return &intelligentBuildingControlRepository{
  20. Repository: repository,
  21. }
  22. }
  23. type intelligentBuildingControlRepository struct {
  24. *Repository
  25. }
  26. func (r *intelligentBuildingControlRepository) DeviceCount() (int64, error) {
  27. //TODO implement me
  28. var count int64
  29. err := r.db.Model(&model.Point{}).Count(&count).Error
  30. if err != nil {
  31. return 0, err
  32. }
  33. return count, nil
  34. }
  35. // GetPointType implements IntelligentBuildingControlRepository.
  36. func (r *intelligentBuildingControlRepository) DeviceType() ([]string, error) {
  37. var names []string
  38. err := r.db.Model(&model.Point{}).Select("device_type").Group("device_type").Find(&names).Error
  39. if err != nil {
  40. return nil, err
  41. }
  42. return names, nil
  43. }
  44. func (r *intelligentBuildingControlRepository) GetPointType() ([]string, error) {
  45. var names []string
  46. err := r.db.Model(&model.Point{}).Select("point_name").Group("point_name").Find(&names).Error
  47. if err != nil {
  48. return nil, err
  49. }
  50. return names, nil
  51. }
  52. func (r *intelligentBuildingControlRepository) GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error) {
  53. var intelligentBuildingControl model.IntelligentBuildingControl
  54. // 模拟从数据库获取数据
  55. return &intelligentBuildingControl, nil
  56. }
  57. // GetPoint 根据点位数据获取点位信息
  58. func (r *intelligentBuildingControlRepository) GetPoint(conds map[string]any, pageNum, pageSize int) (*[]model.Point, int64, error) {
  59. var points []model.Point
  60. points, total, err := helper.QueryByConditions[model.Point](r.db, conds, pageNum, pageSize)
  61. if err != nil {
  62. return &points, 0, err
  63. }
  64. return &points, total, nil
  65. }
  66. // func (r *intelligentBuildingControlRepository) GetPopintList() ([]string, error) {
  67. // var names []string
  68. // err := r.db.Model(&model.Point{}).Select("device_name").Group("device_name").Find(&names).Error
  69. // if err != nil {
  70. // return nil, err
  71. // }
  72. // return names, nil
  73. // }
  74. func (r *intelligentBuildingControlRepository) GetPopintList(conds map[string]any, pageNum, pageSize int) ([]string, int64, error) {
  75. var names []string
  76. var total int64
  77. db := r.db.Model(&model.Point{})
  78. // 应用查询条件
  79. for k, v := range conds {
  80. if v != nil {
  81. db = db.Where(k+" = ?", v)
  82. }
  83. }
  84. // 查询总数(去重后的)
  85. err := db.Select("device_name").Group("device_name").Count(&total).Error
  86. if err != nil {
  87. return nil, 0, err
  88. }
  89. // 分页查询
  90. err = db.Select("device_name").
  91. Group("device_name").
  92. Order("device_name ASC").
  93. Offset((pageNum - 1) * pageSize).
  94. Limit(pageSize).
  95. Find(&names).Error
  96. if err != nil {
  97. return nil, 0, err
  98. }
  99. return names, total, nil
  100. }
  101. // GetPointAll 获取所有点位表
  102. func (r *intelligentBuildingControlRepository) GetPointAll(conds map[string]any) (*[]model.Point, error) {
  103. var points []model.Point
  104. points, err := helper.QueryByCondition[model.Point](r.db, conds)
  105. if err != nil {
  106. return &points, err
  107. }
  108. return &points, nil
  109. }