device_all.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package model
  2. import (
  3. "fmt"
  4. "github.com/jinzhu/gorm"
  5. "time"
  6. "smart-supplier-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.ID as id, t1.DeviceName as name, t1.SN as sn, t1.VerifyTime, t1.DeviceCode as type_code,"+
  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.Filter != "" {
  135. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t2.SafetyNo like '%%%s%%' or t2.Name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  136. }
  137. if req.CanDel {
  138. req.IsAll = true
  139. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  140. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  141. }
  142. if len(req.StatusFilter) > 0 {
  143. args = append(args, req.StatusFilter)
  144. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  145. }
  146. where := ""
  147. for _, v := range whereArray {
  148. if where == "" {
  149. where = fmt.Sprintf(" where %s", v)
  150. continue
  151. }
  152. where = fmt.Sprintf("%s and %s", where, v)
  153. }
  154. offset := (req.Page - 1) *int32(PageSize)
  155. if req.IsAll {
  156. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  157. countSql = fmt.Sprintf("%s %s", countSql, where)
  158. } else {
  159. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  160. countSql = fmt.Sprintf("%s %s", countSql, where)
  161. }
  162. return sql, countSql, args
  163. }
  164. type DeviceAllListRequest struct {
  165. ProviderId int64
  166. Filter string
  167. StatusFilter []int32
  168. IsAll bool
  169. CanDel bool
  170. Page int32
  171. ProjectId int64
  172. }
  173. func (p *DeviceAll) DeviceAllList(db *gorm.DB, req DeviceAllListRequest) ([]DeviceAllItem, int64, error) {
  174. type ResultCount struct {
  175. Count int64
  176. }
  177. array := []ResultCount{}
  178. ret := []DeviceAllItem{}
  179. var err error
  180. sql, countSql, args := deviceAllListSql(req)
  181. err = db.Raw(countSql, args...).Scan(&array).Error
  182. if err != nil {
  183. return nil, 0, err
  184. }
  185. if len(array) == 0 {
  186. return nil, 0, nil
  187. }
  188. if array[0].Count == 0 {
  189. return nil, 0, nil
  190. }
  191. err = db.Raw(sql, args...).Scan(&ret).Error
  192. if err != nil {
  193. return nil, array[0].Count, err
  194. }
  195. return ret, array[0].Count, nil
  196. }