device_all.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package model
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. "time"
  6. "smart-enterprise-management/consts"
  7. )
  8. type DeviceAll struct {
  9. ID int64 `gorm:"column:ID" json:"id"`
  10. SN string `gorm:"column:SN" json:"sn"`
  11. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  12. Longitude string `gorm:"column:Longitude" json:"longitude"`
  13. Latitude string `gorm:"column:Latitude" json:"latitude"`
  14. DeviceCode uint32 `gorm:"column:DeviceCode"`
  15. DeviceName string `gorm:"column:DeviceName"`
  16. SubId int64 `gorm:"column:SubId"`
  17. ProjectId int64 `gorm:"column:ProjectId"`
  18. ProviderId int64 `gorm:"column:ProviderId"`
  19. CreatedAt time.Time `gorm:"column:CreatedAt"`
  20. VerifyTime time.Time `gorm:"column:VerifyTime"`
  21. VerifyStatus int `gorm:"column:VerifyStatus"`
  22. }
  23. func (DeviceAll) TableName() string {
  24. return "db_smart_v2.DeviceAll"
  25. }
  26. func (p *DeviceAll) Insert(db *gorm.DB) error {
  27. return db.Table(p.TableName()).Create(p).Error
  28. }
  29. /*
  30. func (p *Dustdeviceinfo) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error {
  31. sql := fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d group by type", projectId)
  32. if deviceType > 0 {
  33. sql = fmt.Sprintf("select device_code as device_type, sum(status=0) as offline, sum(status=1) as online from t_device where project_id=%d and type = %d group by type", projectId, deviceType)
  34. }
  35. err := db.Raw(sql).Scan(result).Error
  36. return err
  37. }
  38. */
  39. func (p *DeviceAll) Del(db *gorm.DB, where map[string]interface{}) error {
  40. cond, val, err := whereBuild(where)
  41. if err != nil {
  42. return err
  43. }
  44. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  45. }
  46. func (p *DeviceAll) Find(db *gorm.DB, where map[string]interface{}) error {
  47. cond, val, err := whereBuild(where)
  48. if err != nil {
  49. return err
  50. }
  51. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  52. }
  53. func (p *DeviceAll) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  54. cond, val, err := whereBuild(where)
  55. if err != nil {
  56. return err
  57. }
  58. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  59. }
  60. func (p *DeviceAll) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  61. cond, val, err := whereBuild(where)
  62. if err != nil {
  63. return err
  64. }
  65. ps := []DeviceAll{}
  66. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  67. if err != nil {
  68. return err
  69. }
  70. if len(ps) > 0 {
  71. *p = ps[0]
  72. }
  73. return nil
  74. }
  75. func (p *DeviceAll) Save(db *gorm.DB) error {
  76. return db.Save(p).Error
  77. }
  78. func (p *DeviceAll) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  79. if len(where) > 0 {
  80. cond, val, err := whereBuild(where)
  81. if err != nil {
  82. return 0, err
  83. }
  84. ret := int64(0)
  85. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  86. return ret, err
  87. }
  88. ret := int64(0)
  89. err := db.Table(p.TableName()).Count(&ret).Error
  90. return ret, err
  91. }
  92. func (p *DeviceAll) List(db *gorm.DB, where map[string]interface{}, page int) (list []DeviceAll, err error) {
  93. if len(where) > 0 {
  94. cond, val, err := whereBuild(where)
  95. if err != nil {
  96. return list, err
  97. }
  98. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  99. return list, result.Error
  100. }
  101. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  102. return list, result.Error
  103. }
  104. type DeviceAllItem struct {
  105. Id int64
  106. Sn string
  107. TypeCode int32
  108. TypeName string
  109. ProjectName string
  110. ProjectId int64
  111. SafetyRecordNo string
  112. CreatedTime time.Time
  113. Status int32
  114. State int32
  115. ProjectApproveTime time.Time
  116. Key string
  117. ProviderName string
  118. SocialCode string
  119. Name string
  120. Ip string
  121. Port int
  122. MediaTransport string
  123. ChannelCount int
  124. }
  125. func deviceAllListSql(req DeviceAllListRequest)(string, string, []interface{}) {
  126. sql := fmt.Sprintf("select t1.DeviceCode as type_code, t1.ID as id, t1.DeviceName as name, t1.SN as sn, t1.VerifyTime, "+
  127. "t1.CreatedAt as created_time, t1.DeviceState 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 DeviceAll as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  128. countSql := fmt.Sprintf("select count(1) as count from DeviceAll as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ")
  129. args := []interface{}{}
  130. whereArray := []string{}
  131. if req.ProviderId > 0 {
  132. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  133. }
  134. if req.Cid > 0 {
  135. whereArray = append(whereArray, fmt.Sprintf("t2.Cid=%d", req.Cid))
  136. }
  137. if req.Filter != "" {
  138. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.DeviceName like '%%%s%%')", req.Filter, req.Filter))
  139. }
  140. if req.ProjectId > 0 {
  141. whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId))
  142. }
  143. if req.State >= 0 {
  144. whereArray = append(whereArray, fmt.Sprintf("t1.DeviceState=%d", req.State))
  145. }
  146. if req.CanDel {
  147. req.IsAll = true
  148. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  149. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  150. }
  151. if len(req.StatusFilter) > 0 {
  152. args = append(args, req.StatusFilter)
  153. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  154. }
  155. where := ""
  156. for _, v := range whereArray {
  157. if where == "" {
  158. where = fmt.Sprintf(" where %s", v)
  159. continue
  160. }
  161. where = fmt.Sprintf("%s and %s", where, v)
  162. }
  163. offset := (req.Page - 1) *int32(PageSize)
  164. if req.IsAll {
  165. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  166. countSql = fmt.Sprintf("%s %s", countSql, where)
  167. } else {
  168. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  169. countSql = fmt.Sprintf("%s %s", countSql, where)
  170. }
  171. return sql, countSql, args
  172. }
  173. type DeviceAllListRequest struct {
  174. ProviderId int64
  175. Filter string
  176. StatusFilter []int32
  177. IsAll bool
  178. CanDel bool
  179. Page int32
  180. ProjectId int64
  181. Cid int64
  182. State int32
  183. }
  184. func (p *DeviceAll) DeviceAllList(db *gorm.DB, req DeviceAllListRequest) ([]DeviceAllItem, int64, error) {
  185. type ResultCount struct {
  186. Count int64
  187. }
  188. array := []ResultCount{}
  189. ret := []DeviceAllItem{}
  190. var err error
  191. sql, countSql, args := deviceAllListSql(req)
  192. err = db.Raw(countSql, args...).Scan(&array).Error
  193. if err != nil {
  194. return nil, 0, err
  195. }
  196. if len(array) == 0 {
  197. return nil, 0, nil
  198. }
  199. if array[0].Count == 0 {
  200. return nil, 0, nil
  201. }
  202. err = db.Raw(sql, args...).Scan(&ret).Error
  203. if err != nil {
  204. return nil, array[0].Count, err
  205. }
  206. return ret, array[0].Count, nil
  207. }