device.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "time"
  6. "fmt"
  7. "github.com/jinzhu/gorm"
  8. )
  9. type TDevice struct {
  10. Id int64 `gorm:"column:id" json:"id" form:"id"`
  11. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  12. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  13. Name string `gorm:"column:name" json:"name" form:"name"`
  14. Sn string `gorm:"column:sn" json:"sn" form:"sn"`
  15. Addr string `gorm:"column:addr" json:"addr" form:"addr"`
  16. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  17. Lon float64 `gorm:"column:lon" json:"lon" form:"lon"`
  18. Lat float64 `gorm:"column:lat" json:"lat" form:"lat"`
  19. XCoord float64 `gorm:"column:x_coord" json:"x_coord" form:"x_coord"`
  20. YCoord float64 `gorm:"column:y_coord" json:"y_coord" form:"y_coord"`
  21. Status int64 `gorm:"column:status" json:"status" form:"status"`
  22. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  23. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  24. VerifyStatus int64 `gorm:"column:verify_status" json:"verify_status" form:"verify_status"`
  25. Key string `gorm:"column:key" json:"key" form:"key"`
  26. JobId int64 `gorm:"column:job_id" json:"job_id" form:"job_id"`
  27. ProjectApproveTime time.Time `json:"project_approve_time"`
  28. DelApplyTime time.Time `json:"del_apply_time"`
  29. }
  30. func (TDevice) TableName() string {
  31. return "t_device"
  32. }
  33. func (p *TDevice) Insert(db *gorm.DB) error {
  34. return db.Create(p).Error
  35. }
  36. func (p *TDevice) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error {
  37. 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)
  38. if deviceType > 0 {
  39. 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)
  40. }
  41. err := db.Raw(sql).Scan(result).Error
  42. return err
  43. }
  44. func (p *TDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  45. cond, val, err := whereBuild(where)
  46. if err != nil {
  47. return err
  48. }
  49. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  50. }
  51. func (p *TDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  52. cond, val, err := whereBuild(where)
  53. if err != nil {
  54. return err
  55. }
  56. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  57. }
  58. func (p *TDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  59. cond, val, err := whereBuild(where)
  60. if err != nil {
  61. return err
  62. }
  63. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  64. }
  65. func (p *TDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  66. cond, val, err := whereBuild(where)
  67. if err != nil {
  68. return err
  69. }
  70. ps := []TDevice{}
  71. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  72. if err != nil {
  73. return err
  74. }
  75. if len(ps) > 0 {
  76. *p = ps[0]
  77. }
  78. return nil
  79. }
  80. func (p *TDevice) Save(db *gorm.DB) error {
  81. return db.Save(p).Error
  82. }
  83. func (p *TDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  84. if len(where) > 0 {
  85. cond, val, err := whereBuild(where)
  86. if err != nil {
  87. return 0, err
  88. }
  89. ret := int64(0)
  90. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  91. return ret, err
  92. }
  93. ret := int64(0)
  94. err := db.Table(p.TableName()).Count(&ret).Error
  95. return ret, err
  96. }
  97. func (p *TDevice) List(db *gorm.DB, where map[string]interface{}, page int) (list []TDevice, err error) {
  98. if len(where) > 0 {
  99. cond, val, err := whereBuild(where)
  100. if err != nil {
  101. return list, err
  102. }
  103. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  104. return list, result.Error
  105. }
  106. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  107. return list, result.Error
  108. }
  109. func (p *TDevice) All(db *gorm.DB) (list []TDevice, err error) {
  110. result := db.Table(p.TableName()).Find(&list)
  111. return list, result.Error
  112. }
  113. type DeviceItem struct {
  114. Id int64
  115. Sn string
  116. TypeCode int32
  117. TypeName string
  118. ProjectName string
  119. ProjectId int64
  120. SafetyRecordNo string
  121. CreatedTime time.Time
  122. Status int32
  123. State int32
  124. ProjectApproveTime time.Time
  125. DelApplyTime time.Time
  126. DelReason string
  127. Key string
  128. }
  129. func listWithProjectSql(providerId int64, filter string, statusFilter []int32, projectId int64, page int32, typeCode int32)(string, string, []interface{}) {
  130. sql := fmt.Sprintf("select t1.id as id, t1.key, t1.sn as sn, t1.device_code as type_code, t1.project_id as project_id, t1.project_approve_time, t1.del_apply_time,"+
  131. "t1.created_at as created_time, t1.status as state, t1.verify_status as status, t2.safety_record_no, t2.name as project_name from t_device as t1 left join t_project as t2 on t1.project_id=t2.id ")
  132. countSql := fmt.Sprintf("select count(1) as count from t_device as t1 left join t_project as t2 on t1.project_id=t2.id ")
  133. args := []interface{}{}
  134. whereArray := []string{}
  135. if projectId > 0 {
  136. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", providerId))
  137. }
  138. if typeCode > 0 {
  139. whereArray = append(whereArray, fmt.Sprintf("t1.device_code = %d", typeCode))
  140. }
  141. if filter != "" {
  142. whereArray = append(whereArray, fmt.Sprintf("(t1.sn like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", filter, filter, filter))
  143. }
  144. if len(statusFilter) > 0 {
  145. args = append(args, statusFilter)
  146. whereArray = append(whereArray, fmt.Sprintf("t1.verify_status in(?)"))
  147. }
  148. if projectId > 0 {
  149. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", projectId))
  150. }
  151. where := ""
  152. for _, v := range whereArray {
  153. if where == "" {
  154. where = fmt.Sprintf(" where %s", v)
  155. continue
  156. }
  157. where = fmt.Sprintf("%s and %s", where, v)
  158. }
  159. offset := (page - 1) *int32(page)
  160. if providerId > 0 {
  161. sql = fmt.Sprintf("%s %s", sql, where)
  162. countSql = fmt.Sprintf("%s %s", countSql, where)
  163. } else {
  164. sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset)
  165. countSql = fmt.Sprintf("%s %s", countSql, where)
  166. }
  167. return sql, countSql, args
  168. }
  169. func (p *TDevice) ListWithProject(db *gorm.DB, providerId int64, filter string, statusFilter []int32, projectId int64, page int32, typeCode int32) ([]DeviceItem, int64, error) {
  170. type ResultCount struct {
  171. Count int64
  172. }
  173. array := []ResultCount{}
  174. ret := []DeviceItem{}
  175. var err error
  176. sql, countSql, args := listWithProjectSql(providerId, filter, statusFilter, projectId, page, typeCode)
  177. err = db.Raw(countSql, args...).Scan(&array).Error
  178. if err != nil {
  179. return nil, 0, err
  180. }
  181. if len(array) == 0 {
  182. return nil, 0, nil
  183. }
  184. if array[0].Count == 0 {
  185. return nil, 0, nil
  186. }
  187. err = db.Raw(sql, args...).Scan(&ret).Error
  188. if err != nil {
  189. return nil, array[0].Count, err
  190. }
  191. return ret, array[0].Count, nil
  192. }