device.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package model
  2. import (
  3. "fmt"
  4. "sparrow/pkg/models"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. // Device devices data
  9. type Device struct {
  10. db *gorm.DB
  11. }
  12. // Init device
  13. // TODO:增加产品ID查询,目前的查询都没有指定产品ID的查询
  14. func (a *Device) Init(db *gorm.DB) *Device {
  15. a.db = db
  16. return a
  17. }
  18. // GetDeviceCount 获取vendorid下的设备激活总数
  19. func (a *Device) GetDeviceCount(vendorid string) (count int, err error) {
  20. device := &models.Device{
  21. VendorID: vendorid,
  22. }
  23. err = a.db.Model(device).Where(device).Count(&count).Error
  24. return
  25. }
  26. // GetActiveNumberOfDate 获取某一天的设备激活数量
  27. func (a *Device) GetActiveNumberOfDate(vendorid string, datetime string) (count int, err error) {
  28. device := &models.Device{
  29. VendorID: vendorid,
  30. }
  31. err = a.db.Model(device).
  32. Where("vendor_id = ? and DATE_FORMAT(created_at, '%Y-%m-%d') = ?",
  33. vendorid, datetime).
  34. Count(&count).
  35. Error
  36. return
  37. }
  38. // GetLivelyCountOfDate 获取某一天的设备活跃数据
  39. func (a *Device) GetLivelyCountOfDate(vendorid string, datetime string) (count int, err error) {
  40. device := &models.Device{
  41. VendorID: vendorid,
  42. }
  43. err = a.db.Model(device).
  44. Where("vendor_id = ? and DATE_FORMAT(updated_at, '%Y-%m-%d') = ?",
  45. vendorid, datetime).Count(&count).Error
  46. return
  47. }
  48. // // GetActiveOf7Days 设备近7日的激活数据
  49. // func (a *Device) GetActiveOf7Days(vendorid int) (datas []models.DeviceChartData, err error) {
  50. // err = a.db.Model(&models.Device{}).
  51. // Select("DATE_FORMAT(created_at,'%Y-%m-%d') as dt, count(id) as count").
  52. // Where("DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(created_at) and vendor_id = ?",
  53. // vendorid).
  54. // Group("dt").Order("dt").Scan(&datas).Error
  55. // if err != nil {
  56. // return
  57. // }
  58. // return
  59. // }
  60. // // GetActiveOf14Days 设备近14日的激活数据
  61. // func (a *Device) GetActiveOf14Days(vendorid int) (datas []models.DeviceChartData, err error) {
  62. // err = a.db.Model(&models.Device{}).
  63. // Select("DATE_FORMAT(created_at,'%Y-%m-%d') as dt, count(id) as count").
  64. // Where("DATE_SUB(CURDATE(), INTERVAL 14 DAY) <= date(created_at) and vendor_id = ?",
  65. // vendorid).
  66. // Group("dt").Order("dt").Scan(&datas).Error
  67. // if err != nil {
  68. // return
  69. // }
  70. // return
  71. // }
  72. // // GetActiveOf30Days 设备近30日的激活数据
  73. // func (a *Device) GetActiveOf30Days(vendorid int) (datas []models.DeviceChartData, err error) {
  74. // err = a.db.Model(&models.Device{}).
  75. // Select("DATE_FORMAT(created_at,'%Y-%m-%d') as dt, count(id) as count").
  76. // Where("DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(created_at) and vendor_id = ?",
  77. // vendorid).
  78. // Group("dt").Order("dt").Scan(&datas).Error
  79. // if err != nil {
  80. // return
  81. // }
  82. // return
  83. // }
  84. // GetActiveOfNumDays 查询几日内的激活趋势数据
  85. // days参数为7则查询7日的数据
  86. func (a *Device) GetActiveOfNumDays(vendorid string, days int) ([]map[string]interface{}, error) {
  87. i := 1
  88. datas := make([]map[string]interface{}, 0)
  89. for i <= days {
  90. day := i - days
  91. i = i + 1
  92. datetime := time.Now().AddDate(0, 0, day).Format("2006-01-02")
  93. count, err := a.GetActiveNumberOfDate(vendorid, datetime)
  94. if err != nil {
  95. return nil, err
  96. }
  97. data := make(map[string]interface{})
  98. data[datetime] = count
  99. datas = append(datas, data)
  100. }
  101. return datas, nil
  102. }
  103. // GetLivelyOfNumDays 获取设备N日内活跃趋势数据
  104. func (a *Device) GetLivelyOfNumDays(vendorid string, days int) ([]map[string]interface{}, error) {
  105. i := 1
  106. datas := make([]map[string]interface{}, 0)
  107. for i <= days {
  108. day := i - days
  109. i = i + 1
  110. datetime := time.Now().AddDate(0, 0, day).Format("2006-01-02")
  111. count, err := a.GetLivelyCountOfDate(vendorid, datetime)
  112. if err != nil {
  113. return nil, err
  114. }
  115. data := make(map[string]interface{})
  116. data[datetime] = count
  117. datas = append(datas, data)
  118. }
  119. return datas, nil
  120. }
  121. // GetDevices 获取厂商已经激活的设备列表
  122. func (a *Device) GetDevices(vendorid, proid string, pi, ps int, deviceid string) (datas []models.Device, total int, err error) {
  123. tx := a.db.Where("vendor_id = ?", vendorid)
  124. if proid != "" {
  125. tx = tx.Where("product_id = ?", proid)
  126. }
  127. if deviceid != "" {
  128. tx = tx.Where("device_identifier like ?", "%"+deviceid+"%")
  129. }
  130. tx = tx.Order("id DESC")
  131. err = tx.Limit(ps).Offset((pi - 1) * ps).Find(&datas).Error
  132. tx.Model(&models.Device{}).Count(&total)
  133. return
  134. }
  135. // GetDevicesByVenderId 获取用户设备
  136. func (a *Device) GetDevicesByVenderId(vendorid string) (datas []models.Device, err error) {
  137. a.db.Where("vendor_id = ?", vendorid).Find(&datas)
  138. return
  139. }
  140. // Query 获取厂商已经激活的设备列表
  141. func (a *Device) Query(vendorId, productId string, deviceIDs []string) (datas []models.Device, err error) {
  142. tx := a.db.Where("vendor_id = ?", vendorId)
  143. if productId != "" {
  144. tx = tx.Where("product_id = ?", productId)
  145. }
  146. if len(deviceIDs) > 0 {
  147. tx = tx.Where("record_id in (?)", deviceIDs)
  148. }
  149. tx = tx.Order("id DESC")
  150. err = tx.Find(&datas).Error
  151. return
  152. }
  153. // Get 获取数据内容
  154. func (a *Device) Get(vendorId string, recordId string) (data models.Device, err error) {
  155. cache := getCache()
  156. key := fmt.Sprintf("OtaId:%s", recordId)
  157. if v, ok := cache.Get(key); ok {
  158. _d := v.(*models.Device)
  159. data = *_d
  160. } else {
  161. err = a.db.Where("vendor_id = ? and record_id = ?", vendorId, recordId).First(&data).Error
  162. if err == nil {
  163. cache.Set(key, &data)
  164. }
  165. }
  166. return
  167. }
  168. // Update update
  169. func (a *Device) Update(Device *models.Device) (data models.Device, err error) {
  170. cache := getCache()
  171. key := fmt.Sprintf("Device:%d", Device.ID)
  172. if _, ok := cache.Get(key); ok {
  173. cache.Delete(key)
  174. }
  175. err = a.db.Model(&data).Update(Device).Error
  176. return
  177. }