intelligentLightingDao.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package dao
  2. import (
  3. "time"
  4. )
  5. type IntelligentLightGroupRelation struct {
  6. IntelligentLight
  7. LightStrategy
  8. LampPoleGroup
  9. }
  10. type IntelligentLight struct {
  11. ID int `gorm:"primary key" json:"id"` //编号
  12. LightID int `gorm:"type:int" json:"lightID"` //照明策略id
  13. RelationType int `gorm:"type:int" json:"relationType"` //关联类型1=灯控,2=灯杆分组
  14. Rid int `gorm:"type:int" json:"rid"` //关联外键ID 根据type类型关联至其表
  15. CreateTime time.Time `gorm:"type:timestamp" json:"createTime"` //新增时间
  16. CreateUser string `gorm:"type:varchar(60)" json:"createUser"` //新增记录操作用户ID
  17. UpdateTime time.Time `gorm:"type:timestamp" json:"updateTime"` //修改时间
  18. UpdateUser string `gorm:"type:varchar(60)" json:"updateUser"` //修改用户
  19. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  20. TenantID string `gorm:"type:varchar(12)" json:"tenantID"` //租户id
  21. }
  22. func (IntelligentLight) TableName() string {
  23. return "t_strategy_intelligent_light"
  24. }
  25. func (c *IntelligentLight) BatchGet(ids []int) ([]IntelligentLight, error) {
  26. var intelligentLights []IntelligentLight
  27. err := Db.Debug().Model(&c).Where("light_id in ? and is_deleted = 0", ids).Find(&intelligentLights).Error
  28. return intelligentLights, err
  29. }
  30. func (c *IntelligentLight) GetByRidAndType() ([]IntelligentLight, error) {
  31. var intelligentLights []IntelligentLight
  32. db := Db.Debug().Model(&c).Where("relation_type = ? and is_deleted = 0 and tenant_id = ?",
  33. c.RelationType, c.TenantID)
  34. if c.Rid > 0 {
  35. db = db.Where("rid = ?", c.Rid)
  36. }
  37. err := db.Find(&intelligentLights).Error
  38. return intelligentLights, err
  39. }
  40. func (c *IntelligentLight) GetByType() ([]IntelligentLightGroupRelation, error) {
  41. var groupRelations []IntelligentLightGroupRelation
  42. selectStr := "lig.*,gro.id,gro.pole_group_name,b.light_name,b.light_sn,b.start_time,b.end_time,b.is_all_year,b.is_automatic_renewal"
  43. whereStr := "lig.is_deleted = 0 and relation_type = 2 and lig.tenant_id = ? and gro.is_deleted = 0"
  44. whereValue := []interface{}{c.TenantID}
  45. if c.Rid > 0 {
  46. whereStr += " and lig.rid = ?"
  47. whereValue = append(whereValue, c.Rid)
  48. }
  49. err := Db.Debug().Table("t_strategy_intelligent_light lig").Select(selectStr).
  50. Joins("left join t_dev_lamp_pole_group gro ON lig.rid = gro.id").
  51. Joins("left join t_strategy_light b ON b.id = lig.light_id").
  52. Where(whereStr, whereValue...).Order("lig.create_time desc").Scan(&groupRelations).Error
  53. return groupRelations, err
  54. }