intelligentLightingDao.go 2.7 KB

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