intelligentLightingDao.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package dao
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type LightrelationVo struct {
  7. ID int `json:"id"`
  8. LightID int `json:"lightId"`
  9. RelationType int `json:"relationType"`
  10. Rid int `json:"rid"`
  11. CreateTime string `json:"createTime"`
  12. CreateUser string `json:"createUser"`
  13. UpdateTime string `json:"updateTime"`
  14. UpdateUser string `json:"updateUser"`
  15. IsDeleted int `json:"isDeleted"`
  16. TenantID string `json:"tenantId"`
  17. IsAutomaticRenewal int `json:"isAutomaticRenewal"`
  18. EffectiveDate string `json:"effectiveDate"`
  19. EndTime string `json:"endTime"`
  20. DeviceSn string `json:"deviceSn"`
  21. Location string `json:"location"`
  22. LightControlState string `json:"lightControlState"`
  23. IsShowOpts bool `json:"isShowOpts"`
  24. PublicID int `json:"publicId"`
  25. CombinationStrList []interface{} `gorm:"-" json:"combinationStrList"`
  26. RunState string `json:"runState"`
  27. Children *[]LightrelationVo `gorm:"-" json:"children"`
  28. GroupID int `json:"groupId"`
  29. LampPoleSn string `json:"lampPoleSn"`
  30. LightName string `json:"lightName"`
  31. ControlType int `json:"controlType"`
  32. InstallTime string `json:"installTime"`
  33. LampPoleName string `json:"lampPoleName"`
  34. IsAllYear int `json:"isAllYear"`
  35. PublicName string `json:"publicName"`
  36. StartTime string `json:"startTime"`
  37. LightSn string `json:"lightSn"`
  38. HandSwitch int `json:"handSwitch"`
  39. CombinationStr string `json:"combinationStr"`
  40. LampLng float64 `json:"lampLng"`
  41. LampLat float64 `json:"lampLat"`
  42. }
  43. type IntelligentLightGroupRelation struct {
  44. IntelligentLight
  45. LightStrategy
  46. LightControl
  47. TimeCondition
  48. PublicName string `gorm:"_" json:"publicName"`
  49. }
  50. type IntelligentLight struct {
  51. ID int `gorm:"primary key" json:"id"` //编号
  52. LightID int `gorm:"type:int" json:"lightID"` //照明策略id
  53. RelationType int `gorm:"type:int" json:"relationType"` //关联类型1=灯控,2=灯杆分组
  54. Rid int `gorm:"type:int" json:"rid"` //关联外键ID 根据type类型关联至其表
  55. TenantId string `gorm:"type:varchar(12)" json:"tenantId"` //租户ID
  56. CreateTime time.Time `gorm:"type:datetime" json:"createTime"` //新增时间
  57. CreateUser int `gorm:"type:int" json:"createUser"` //新增记录操作用户ID
  58. UpdateTime time.Time `gorm:"type:datetime" json:"updateTime"` //修改时间
  59. UpdateUser int `gorm:"type:int" json:"updateUser"` //修改用户
  60. IsDeleted int `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
  61. }
  62. func (IntelligentLight) TableName() string {
  63. return "strategy_intelligent_light"
  64. }
  65. // CreateDefaultStrategyByLight 创建默认
  66. func (c *IntelligentLight) CreateDefaultStrategyByLight(rid int, relationType int, lightId int) error {
  67. var intelligentLights IntelligentLight
  68. intelligentLights.LightID = lightId
  69. intelligentLights.RelationType = relationType
  70. intelligentLights.Rid = rid
  71. intelligentLights.TenantId = c.TenantId
  72. intelligentLights.CreateTime = time.Now()
  73. intelligentLights.UpdateTime = time.Now()
  74. err := Db.Model(&c).Save(&intelligentLights).Error
  75. return err
  76. }
  77. // 批量获取
  78. func (c *IntelligentLight) BatchGet(ids []int) ([]IntelligentLight, error) {
  79. var intelligentLights []IntelligentLight
  80. err := Db.Model(&c).Where("light_id in ? and is_deleted = 0", ids).Find(&intelligentLights).Error
  81. return intelligentLights, err
  82. }
  83. func (c *IntelligentLight) GetByRidAndType() ([]IntelligentLight, error) {
  84. var intelligentLights []IntelligentLight
  85. db := Db.Model(&c).Where("relation_type = ? and is_deleted = 0 and tenant_id = ?",
  86. c.RelationType, c.TenantId)
  87. if c.Rid > 0 {
  88. db = db.Where("rid = ?", c.Rid)
  89. }
  90. err := db.Find(&intelligentLights).Error
  91. return intelligentLights, err
  92. }
  93. func (c *IntelligentLight) GetByGroup2(searchValue string, current, size,
  94. groupId int) ([]LightrelationVo, error) {
  95. var groupRelations []LightrelationVo
  96. where := "lig.tenant_id = ?"
  97. if groupId != 0 {
  98. where += fmt.Sprintf(" AND lig.rid in(%v)", groupId)
  99. }
  100. sql := `SELECT lig.*,gro.id public_id,gro.pole_group_name public_name,b.light_name,b.light_sn,b.start_time,b.end_time,b.is_all_year,b.is_automatic_renewal
  101. FROM strategy_intelligent_light lig
  102. LEFT JOIN device_lamp_pole_group gro ON lig.rid=gro.id
  103. LEFT JOIN strategy_light b ON b.id=lig.light_id
  104. WHERE lig.is_deleted=0 AND lig.relation_type=2 AND gro.is_deleted=0
  105. and ` + where + `
  106. ORDER BY lig.create_time desc`
  107. tx := Db.Raw(sql, c.TenantId).Scan(&groupRelations)
  108. return groupRelations, tx.Error
  109. }
  110. func (c *IntelligentLight) GetByGroup(searchValue string, current, size,
  111. groupId int) ([]LightrelationVo, int64, error) {
  112. var groupRelations []LightrelationVo
  113. where := " AND con.tenant_id = ?"
  114. if groupId != 0 {
  115. where += fmt.Sprintf(" AND con.group_id in(%v)", groupId)
  116. }
  117. if searchValue != "" {
  118. where += fmt.Sprintf(" and (con.sn like '%%v%' or con.name like '%$v%')", searchValue)
  119. }
  120. offset := (current - 1) * size
  121. limit := size
  122. sql := `SELECT
  123. lig.*,
  124. con.control_type,
  125. con.id public_id,
  126. con.NAME public_name,
  127. con.sn device_sn,
  128. con.lamp_pole_name,
  129. con.lamp_pole_sn,
  130. con.group_id,
  131. con.lamp_lat,
  132. con.lamp_lng,
  133. con.id as rid,
  134. b.light_name,
  135. b.light_sn,
  136. b.start_time,
  137. b.end_time,
  138. is_all_year,
  139. b.is_automatic_renewal,
  140. con.lamp_pole_location,
  141. con.install_time
  142. FROM
  143. device_light_control con
  144. left JOIN strategy_intelligent_light lig ON con.id = lig.rid
  145. LEFT JOIN strategy_light b ON b.id = lig.light_id
  146. WHERE
  147. con.is_deleted = 0
  148. ` + where + `
  149. ORDER BY
  150. con.name asc `
  151. var count int64
  152. Db.Raw(sql, c.TenantId).Count(&count)
  153. sql += `limit ?,?`
  154. tx := Db.Raw(sql, c.TenantId, offset, limit).Scan(&groupRelations)
  155. return groupRelations, count, tx.Error
  156. }
  157. func (c IntelligentLight) Update() error {
  158. return Db.Model(&c).Where(" id = ? and is_deleted = 0", c.ID).Updates(&c).Error
  159. }
  160. func (c IntelligentLight) Get() (IntelligentLight, error) {
  161. var intelligentLight IntelligentLight
  162. err := Db.Model(&c).Where("is_deleted = 0 and rid = ? and relation_type = ?", c.Rid, c.RelationType).First(&intelligentLight).Error
  163. return intelligentLight, err
  164. }