package dao import ( "iot_manager_service/app/record/edge_service" "time" ) type LightRecord struct { Id int `gorm:"column:id;primary_key;AUTO_INCREMENT; comment:编号" json:"id"` GroupId int `gorm:"column:group_id; comment:灯杆分组ID" json:"groupId"` GroupName string `gorm:"column:group_name; comment:灯杆分组名" json:"groupName"` LampPoleId int `gorm:"column:lamp_pole_id; comment:灯杆ID" json:"lampPoleId"` LampPoleName string `gorm:"column:lamp_pole_name; comment:灯杆名称" json:"lampPoleName"` LampPoleSn string `gorm:"column:lamp_pole_sn; comment:灯杆编码" json:"lampPoleSn"` LightControlId int `gorm:"column:light_control_id; comment:灯控ID" json:"lightControlId"` LightControlName string `gorm:"column:light_control_name; comment:灯控名称" json:"lightControlName"` LightControlSn string `gorm:"column:light_control_sn; comment:灯控编号" json:"lightControlSn"` Luminance int `gorm:"column:luminance; comment:亮度" json:"luminance"` LightingState int `gorm:"column:lighting_state;default:0; comment:亮灯状态:0-关闭,1-开启" json:"lightingState"` StartTime time.Time `gorm:"column:start_time; comment:亮灯开始时间; type:datetime" json:"startTime"` EndTime time.Time `gorm:"column:end_time; comment:亮灯结束时间; type:datetime" json:"endTime"` CountTime int `gorm:"column:count_time; comment:亮灯时长(分钟)" json:"countTime"` TenantId string `gorm:"column:tenant_id; comment:租户ID" json:"tenantId"` CreateTime time.Time `gorm:"column:create_time;NOT NULL; comment:创建日期;type:datetime" json:"createTime"` UpdateTime time.Time `gorm:"column:update_time;default:CURRENT_TIMESTAMP; comment:新时间 默认为当前时间;type:datetime" json:"updateTime"` IsDeleted int `gorm:"column:is_deleted;default:0; comment:是否删除:0-否,1-删除" json:"isDeleted"` } func (l LightRecord) TableName() string { return "device_lighting_record_his" } // GetMaxIdAndUpDateTime 得到最后请求记录 func (l LightRecord) GetMaxIdAndUpDateTime() (int64, string) { var lightRecord LightRecord err := Db.Debug().Order("id desc").First(&lightRecord).Error if err != nil { return 0, "2020-01-01 00:00:00" } return int64(lightRecord.Id), lightRecord.CreateTime.Format("2006-01-02 15:04:05") } // BatchCreate 批量插入 func (l LightRecord) BatchCreate(record []edge_service.RecordLightUpData) error { his, err := l.assembleRecordHis(record) if err != nil { return err } return Db.Debug().CreateInBatches(his, 1000).Error } // GetRecords 查记录 func (c LightRecord) GetRecords(offset int, limit int, start, end, searchValue string, groupId int) ([]LightRecord, int64, error) { var LightRecords []LightRecord db := Db.Debug().Model(&c) if groupId > 0 { db.Where("group_id=?", groupId) } if searchValue != "" { db = db.Where("light_control_sn like ? or lamp_pole_name like ?", "%"+searchValue+"%", "%"+searchValue+"%") } if start != "" { start = start + " 00:00:00" end = end + " 23:59:59" db = db.Where("start_time >=? and start_time <= ?", start, end) } db = db.Where("is_deleted = 0") err := db.Order("id desc").Offset(offset).Limit(limit).Find(&LightRecords).Error var count int64 db.Count(&count) return LightRecords, count, err } // 重写参数 func (l LightRecord) assembleRecordHis(records []edge_service.RecordLightUpData) ([]LightRecord, error) { var temLightRecords []LightRecord Db.Debug().Raw("SELECT a.id light_control_id, a.name light_control_name, a.sn light_control_sn, a.lamp_pole_id, a.lamp_pole_name, a.lamp_pole_sn, a.group_id, b.pole_group_name group_name, a.tenant_id FROM device_light_control a LEFT JOIN device_lamp_pole_group b ON a.group_id=b.id WHERE a.is_deleted=0"). Scan(&temLightRecords) m := make(map[string]LightRecord) for _, temLightRecord := range temLightRecords { m[temLightRecord.LightControlSn] = temLightRecord } var realLightRecords []LightRecord for _, record := range records { lightRecord := m[record.Code] if lightRecord.GroupId == 0 { continue } lightRecord.Id = record.ID lightRecord.Luminance = record.Brightness lightRecord.StartTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tstart, time.Local) lightRecord.EndTime, _ = time.ParseInLocation("2006-01-02 15:04:05", record.Tend, time.Local) lightRecord.CountTime = int(record.Duration) lightRecord.IsDeleted = 0 lightRecord.LightControlSn = record.Code lightRecord.CreateTime = time.Now() realLightRecords = append(realLightRecords, lightRecord) } return realLightRecords, nil } func (l LightRecord) Get() (LightRecord, error) { var lightRecord LightRecord err := Db.Debug().Model(&l).First(&lightRecord).Error return lightRecord, err }