slag_car.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2019 github.com. All rights reserved.
  2. // Use of this source code is governed by github.com.
  3. package model
  4. import (
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "smart-enterprise-management/consts"
  8. "time"
  9. )
  10. type SlagcarDevice struct {
  11. ID int64 `gorm:"column:ID;PRIMARY_KEY" json:"id"`
  12. Code string `gorm:"column:Code" json:"code"`
  13. Name string `gorm:"column:name" json:"name"`
  14. DeviceNum string `gorm:"column:deviceNum" json:"devicenum"`
  15. DeviceModel string `gorm:"column:deviceModel" json:"devicemodel"`
  16. DeviceName string `gorm:"column:deviceName" json:"devicename"`
  17. Manufacturer string `gorm:"column:manufacturer" json:"manufacturer"`
  18. Batch string `gorm:"column:batch" json:"batch"`
  19. SN string `gorm:"column:SN" json:"sn"`
  20. SIM string `gorm:"column:SIM" json:"sim"`
  21. State int64 `gorm:"column:state" json:"state"`
  22. Unit string `gorm:"column:unit" json:"unit"`
  23. Person string `gorm:"column:contactName" json:"person"`
  24. Phone string `gorm:"column:contactPerson" json:"phone"`
  25. Remark string `gorm:"column:remark" json:"remark"`
  26. Longitude string `gorm:"column:longitude" json:"longitude"`
  27. Latitude string `gorm:"column:latitude" json:"latitude"`
  28. VerifyTime time.Time `gorm:"column:VerifyTime"`
  29. VerifyStatus int `gorm:"column:VerifyStatus"`
  30. Key string `gorm:"column:Key"`
  31. ProjectId int64 `gorm:"column:ProjectId"`
  32. ProviderId int64 `gorm:"column:ProviderId"`
  33. CreatedAt time.Time `gorm:"column:CreatedAt"`
  34. }
  35. func (SlagcarDevice) TableName() string {
  36. return "db_smart_v2.SlagcarDevice"
  37. }
  38. func (p *SlagcarDevice) Insert(db *gorm.DB) error {
  39. return db.Table(p.TableName()).Create(p).Error
  40. }
  41. func (p *SlagcarDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  42. cond, val, err := whereBuild(where)
  43. if err != nil {
  44. return err
  45. }
  46. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  47. }
  48. func (p *SlagcarDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  49. cond, val, err := whereBuild(where)
  50. if err != nil {
  51. return err
  52. }
  53. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  54. }
  55. func (p *SlagcarDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  56. cond, val, err := whereBuild(where)
  57. if err != nil {
  58. return err
  59. }
  60. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  61. }
  62. func (p *SlagcarDevice) Save(db *gorm.DB) error {
  63. return db.Save(p).Error
  64. }
  65. func (p *SlagcarDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  66. if len(where) > 0 {
  67. cond, val, err := whereBuild(where)
  68. if err != nil {
  69. return 0, err
  70. }
  71. ret := int64(0)
  72. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  73. return ret, err
  74. }
  75. ret := int64(0)
  76. err := db.Table(p.TableName()).Count(&ret).Error
  77. return ret, err
  78. }
  79. func (p *SlagcarDevice) List(db *gorm.DB, where map[string]interface{}, page int) (list []SlagcarDevice, err error) {
  80. if len(where) > 0 {
  81. cond, val, err := whereBuild(where)
  82. if err != nil {
  83. return list, err
  84. }
  85. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  86. return list, result.Error
  87. }
  88. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  89. return list, result.Error
  90. }
  91. type SlagDeviceItem struct {
  92. Id int64
  93. Sn string
  94. TypeCode int32
  95. TypeName string
  96. ProjectName string
  97. ProjectId int64
  98. SafetyRecordNo string
  99. CreatedTime time.Time
  100. Status int32
  101. State int32
  102. ProjectApproveTime time.Time
  103. Key string
  104. ProviderName string
  105. SocialCode string
  106. Name string
  107. Ip string
  108. Port int
  109. MediaTransport string
  110. ChannelCount int
  111. }
  112. func slagDeviceListSql(req SlagDeviceListRequest)(string, string, []interface{}) {
  113. sql := fmt.Sprintf("select t1.ID as id, t1.Key as 'key', t1.deviceName as name, t1.SN as sn, t1.VerifyTime, "+
  114. "t1.CreatedAt as created_time, t1.state as state, t1.VerifyStatus as status, t2.SafetyNo as safety_record_no, t2.Name as project_name, t3.Name as provider_name, t3.SocialCode as social_code from DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  115. countSql := fmt.Sprintf("select count(1) as count from DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectID=t2.ID")
  116. args := []interface{}{}
  117. whereArray := []string{}
  118. if req.ProviderId > 0 {
  119. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  120. }
  121. if req.Cid > 0 {
  122. whereArray = append(whereArray, fmt.Sprintf("t2.Cid=%d", req.Cid))
  123. }
  124. if req.ProjectId > 0 {
  125. whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId))
  126. }
  127. if req.State >= 0 {
  128. whereArray = append(whereArray, fmt.Sprintf("t1.state=%d", req.State))
  129. }
  130. if req.Filter != "" {
  131. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.deviceName like '%%%s%%')",req.Filter, req.Filter))
  132. }
  133. if req.CanDel {
  134. req.IsAll = true
  135. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  136. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  137. }
  138. if len(req.StatusFilter) > 0 {
  139. args = append(args, req.StatusFilter)
  140. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  141. }
  142. where := ""
  143. for _, v := range whereArray {
  144. if where == "" {
  145. where = fmt.Sprintf(" where %s", v)
  146. continue
  147. }
  148. where = fmt.Sprintf("%s and %s", where, v)
  149. }
  150. offset := (req.Page - 1) *int32(PageSize)
  151. if req.IsAll {
  152. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  153. countSql = fmt.Sprintf("%s %s", countSql, where)
  154. } else {
  155. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  156. countSql = fmt.Sprintf("%s %s", countSql, where)
  157. }
  158. return sql, countSql, args
  159. }
  160. type SlagDeviceListRequest struct {
  161. ProviderId int64
  162. Filter string
  163. StatusFilter []int32
  164. IsAll bool
  165. CanDel bool
  166. Page int32
  167. ProjectId int64
  168. Cid int64
  169. State int32
  170. }
  171. func (p *SlagcarDevice) SlagDeviceList(db *gorm.DB, req SlagDeviceListRequest) ([]SlagDeviceItem, int64, error) {
  172. type ResultCount struct {
  173. Count int64
  174. }
  175. array := []ResultCount{}
  176. ret := []SlagDeviceItem{}
  177. var err error
  178. sql, countSql, args := slagDeviceListSql(req)
  179. err = db.Raw(countSql, args...).Scan(&array).Error
  180. if err != nil {
  181. return nil, 0, err
  182. }
  183. if len(array) == 0 {
  184. return nil, 0, nil
  185. }
  186. if array[0].Count == 0 {
  187. return nil, 0, nil
  188. }
  189. err = db.Raw(sql, args...).Scan(&ret).Error
  190. if err != nil {
  191. return nil, array[0].Count, err
  192. }
  193. return ret, array[0].Count, nil
  194. }