123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package repository
- import (
- "city_chips/internal/model"
- helper "city_chips/pkg/helper/dbutils"
- "context"
- )
- type IntelligentBuildingControlRepository interface {
- GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error)
- GetPoint(conds map[string]any, pageNum, pageSize int) (*[]model.Point, int64, error)
- GetPointType() ([]string, error)
- DeviceType() ([]string, error)
- DeviceCount() (int64, error)
- GetPopintList(conds map[string]any, pageNum, pageSize int) ([]string, int64, error)
- GetPointAll(conds map[string]any) (*[]model.Point, error)
- }
- func NewIntelligentBuildingControlRepository(
- repository *Repository,
- ) IntelligentBuildingControlRepository {
- return &intelligentBuildingControlRepository{
- Repository: repository,
- }
- }
- type intelligentBuildingControlRepository struct {
- *Repository
- }
- func (r *intelligentBuildingControlRepository) DeviceCount() (int64, error) {
- //TODO implement me
- var count int64
- err := r.db.Model(&model.Point{}).Count(&count).Error
- if err != nil {
- return 0, err
- }
- return count, nil
- }
- // GetPointType implements IntelligentBuildingControlRepository.
- func (r *intelligentBuildingControlRepository) DeviceType() ([]string, error) {
- var names []string
- err := r.db.Model(&model.Point{}).Select("device_type").Group("device_type").Find(&names).Error
- if err != nil {
- return nil, err
- }
- return names, nil
- }
- func (r *intelligentBuildingControlRepository) GetPointType() ([]string, error) {
- var names []string
- err := r.db.Model(&model.Point{}).Select("point_name").Group("point_name").Find(&names).Error
- if err != nil {
- return nil, err
- }
- return names, nil
- }
- func (r *intelligentBuildingControlRepository) GetIntelligentBuildingControl(ctx context.Context, id int64) (*model.IntelligentBuildingControl, error) {
- var intelligentBuildingControl model.IntelligentBuildingControl
- // 模拟从数据库获取数据
- return &intelligentBuildingControl, nil
- }
- // GetPoint 根据点位数据获取点位信息
- func (r *intelligentBuildingControlRepository) GetPoint(conds map[string]any, pageNum, pageSize int) (*[]model.Point, int64, error) {
- var points []model.Point
- points, total, err := helper.QueryByConditions[model.Point](r.db, conds, pageNum, pageSize)
- if err != nil {
- return &points, 0, err
- }
- return &points, total, nil
- }
- // func (r *intelligentBuildingControlRepository) GetPopintList() ([]string, error) {
- // var names []string
- // err := r.db.Model(&model.Point{}).Select("device_name").Group("device_name").Find(&names).Error
- // if err != nil {
- // return nil, err
- // }
- // return names, nil
- // }
- func (r *intelligentBuildingControlRepository) GetPopintList(conds map[string]any, pageNum, pageSize int) ([]string, int64, error) {
- var names []string
- var total int64
- db := r.db.Model(&model.Point{})
- // 应用查询条件
- for k, v := range conds {
- if v != nil {
- db = db.Where(k+" = ?", v)
- }
- }
- // 查询总数(去重后的)
- err := db.Select("device_name").Group("device_name").Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- // 分页查询
- err = db.Select("device_name").
- Group("device_name").
- Order("device_name ASC").
- Offset((pageNum - 1) * pageSize).
- Limit(pageSize).
- Find(&names).Error
- if err != nil {
- return nil, 0, err
- }
- return names, total, nil
- }
- // GetPointAll 获取所有点位表
- func (r *intelligentBuildingControlRepository) GetPointAll(conds map[string]any) (*[]model.Point, error) {
- var points []model.Point
- points, err := helper.QueryByCondition[model.Point](r.db, conds)
- if err != nil {
- return &points, err
- }
- return &points, nil
- }
|