slag_car.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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-supplier-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.Filter != "" {
  122. 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))
  123. }
  124. if req.CanDel {
  125. req.IsAll = true
  126. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  127. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  128. }
  129. if len(req.StatusFilter) > 0 {
  130. args = append(args, req.StatusFilter)
  131. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  132. }
  133. where := ""
  134. for _, v := range whereArray {
  135. if where == "" {
  136. where = fmt.Sprintf(" where %s", v)
  137. continue
  138. }
  139. where = fmt.Sprintf("%s and %s", where, v)
  140. }
  141. offset := (req.Page - 1) *int32(PageSize)
  142. if req.IsAll {
  143. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  144. countSql = fmt.Sprintf("%s %s", countSql, where)
  145. } else {
  146. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  147. countSql = fmt.Sprintf("%s %s", countSql, where)
  148. }
  149. return sql, countSql, args
  150. }
  151. type SlagDeviceListRequest struct {
  152. ProviderId int64
  153. Filter string
  154. StatusFilter []int32
  155. IsAll bool
  156. CanDel bool
  157. Page int32
  158. ProjectId int64
  159. }
  160. func (p *SlagcarDevice) SlagDeviceList(db *gorm.DB, req SlagDeviceListRequest) ([]SlagDeviceItem, int64, error) {
  161. type ResultCount struct {
  162. Count int64
  163. }
  164. array := []ResultCount{}
  165. ret := []SlagDeviceItem{}
  166. var err error
  167. sql, countSql, args := slagDeviceListSql(req)
  168. err = db.Raw(countSql, args...).Scan(&array).Error
  169. if err != nil {
  170. return nil, 0, err
  171. }
  172. if len(array) == 0 {
  173. return nil, 0, nil
  174. }
  175. if array[0].Count == 0 {
  176. return nil, 0, nil
  177. }
  178. err = db.Raw(sql, args...).Scan(&ret).Error
  179. if err != nil {
  180. return nil, array[0].Count, err
  181. }
  182. return ret, array[0].Count, nil
  183. }