dust_device.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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-enterprise-management/consts"
  8. "time"
  9. )
  10. type DustDeviceInfo 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. DeviceId string `gorm:"column:DeviceId" json:"deviceid"`
  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. DeviceState int64 `gorm:"column:DeviceState" json:"devicestate"`
  22. Unit string `gorm:"column:Unit" json:"unit"`
  23. Person string `gorm:"column:Person" json:"person"`
  24. Phone string `gorm:"column:phone" 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. Url string `gorm:"column:Url" json:"url"`
  29. VerifyTime time.Time `gorm:"column:VerifyTime"`
  30. VerifyStatus int `gorm:"column:VerifyStatus"`
  31. Key string `gorm:"column:Key"`
  32. ProjectId int64 `gorm:"column:ProjectId"`
  33. ProviderId int64 `gorm:"column:ProviderId"`
  34. CreatedAt time.Time `gorm:"column:CreatedAt"`
  35. }
  36. const (
  37. DeviceTypeVedio = 9
  38. DeviceTypeDust = 5
  39. DeviceTypeVehicle = 2
  40. DeviceTypeAttendance = 3
  41. )
  42. func (DustDeviceInfo) TableName() string {
  43. return "db_smart_v2.DustDeviceInfo"
  44. }
  45. func (p *DustDeviceInfo) Insert(db *gorm.DB) error {
  46. return db.Table(p.TableName()).Create(p).Error
  47. }
  48. func (p *DustDeviceInfo) Del(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...).Delete(p).Error
  54. }
  55. func (p *DustDeviceInfo) Find(db *gorm.DB, where 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...).First(p).Error
  61. }
  62. func (p *DustDeviceInfo) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  63. cond, val, err := whereBuild(where)
  64. if err != nil {
  65. return err
  66. }
  67. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  68. }
  69. func (p *DustDeviceInfo) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  70. cond, val, err := whereBuild(where)
  71. if err != nil {
  72. return err
  73. }
  74. ps := []DustDeviceInfo{}
  75. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  76. if err != nil {
  77. return err
  78. }
  79. if len(ps) > 0 {
  80. *p = ps[0]
  81. }
  82. return nil
  83. }
  84. func (p *DustDeviceInfo) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  85. if len(where) > 0 {
  86. cond, val, err := whereBuild(where)
  87. if err != nil {
  88. return 0, err
  89. }
  90. ret := int64(0)
  91. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  92. return ret, err
  93. }
  94. ret := int64(0)
  95. err := db.Table(p.TableName()).Count(&ret).Error
  96. return ret, err
  97. }
  98. func (p *DustDeviceInfo) List(db *gorm.DB, where map[string]interface{}, page int) (list []DustDeviceInfo, err error) {
  99. if len(where) > 0 {
  100. cond, val, err := whereBuild(where)
  101. if err != nil {
  102. return list, err
  103. }
  104. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  105. return list, result.Error
  106. }
  107. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  108. return list, result.Error
  109. }
  110. func (p *DustDeviceInfo) All(db *gorm.DB, where map[string]interface{}) (list []DustDeviceInfo, err error) {
  111. if len(where) > 0 {
  112. cond, val, err := whereBuild(where)
  113. if err != nil {
  114. return list, err
  115. }
  116. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  117. return list, result.Error
  118. }
  119. result := db.Table(p.TableName()).Find(&list)
  120. return list, result.Error
  121. }
  122. type DustDeviceItem struct {
  123. Id int64
  124. Sn string
  125. TypeCode int32
  126. TypeName string
  127. ProjectName string
  128. ProjectId int64
  129. SafetyRecordNo string
  130. CreatedTime time.Time
  131. Status int32
  132. State int32
  133. ProjectApproveTime time.Time
  134. Key string
  135. ProviderName string
  136. SocialCode string
  137. Name string
  138. Manufacturer string
  139. Url string
  140. Batch string
  141. Person string
  142. Phone string
  143. Unit string
  144. Lon string
  145. Lat string
  146. Model string
  147. }
  148. func dustDeviceListSql(req DustDeviceListRequest)(string, string, []interface{}) {
  149. sql := fmt.Sprintf("select t1.ID as id, t1.DeviceModel as model, t1.Batch as batch, t1.Person as person, t1.Phone as phone, t1.Manufacturer as manufacturer, t1.Longitude as lon, t1.Latitude as lat, t1.Url as url, t1.Key as 'key', t1.DeviceName as name, t1.SN as sn, t1.VerifyTime as project_approve_time, "+
  150. "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 DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID left join Provider as t3 on t1.ProviderId=t3.ID")
  151. countSql := fmt.Sprintf("select count(1) as count from DustDeviceInfo as t1 left join ProjectInfo as t2 on t1.ProjectId=t2.ID ")
  152. args := []interface{}{}
  153. whereArray := []string{}
  154. if req.ProviderId > 0 {
  155. whereArray = append(whereArray, fmt.Sprintf("t1.ProviderId=%d", req.ProviderId))
  156. }
  157. if req.Cid > 0 {
  158. whereArray = append(whereArray, fmt.Sprintf("t2.Cid=%d", req.Cid))
  159. }
  160. if req.Filter != "" {
  161. whereArray = append(whereArray, fmt.Sprintf("(t1.SN like '%%%s%%' or t1.DeviceName like '%%%s%%')", req.Filter, req.Filter))
  162. }
  163. if req.ProjectId > 0 {
  164. whereArray = append(whereArray, fmt.Sprintf("t1.ProjectId=%d", req.ProjectId))
  165. }
  166. if req.State >= 0 {
  167. whereArray = append(whereArray, fmt.Sprintf("t1.DeviceState=%d", req.State))
  168. }
  169. if req.CanDel {
  170. req.IsAll = true
  171. req.StatusFilter = []int32{consts.DeviceStatusAddAuditted}
  172. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from DeviceDelJob where DeviceId=t1.ID and (Status = 0 or Status = 1)) = 0"))
  173. }
  174. if len(req.StatusFilter) > 0 {
  175. args = append(args, req.StatusFilter)
  176. whereArray = append(whereArray, fmt.Sprintf("t1.VerifyStatus in(?)"))
  177. }
  178. where := ""
  179. for _, v := range whereArray {
  180. if where == "" {
  181. where = fmt.Sprintf(" where %s", v)
  182. continue
  183. }
  184. where = fmt.Sprintf("%s and %s", where, v)
  185. }
  186. offset := (req.Page - 1) *int32(PageSize)
  187. if req.IsAll {
  188. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc", sql, where)
  189. countSql = fmt.Sprintf("%s %s", countSql, where)
  190. } else {
  191. sql = fmt.Sprintf("%s %s order by t1.CreatedAt desc limit %d offset %d", sql, where, PageSize, offset)
  192. countSql = fmt.Sprintf("%s %s", countSql, where)
  193. }
  194. return sql, countSql, args
  195. }
  196. type DustDeviceListRequest struct {
  197. ProviderId int64
  198. Filter string
  199. StatusFilter []int32
  200. IsAll bool
  201. CanDel bool
  202. Page int32
  203. ProjectId int64
  204. Cid int64
  205. State int32
  206. }
  207. func (p *DustDeviceInfo) DustDeviceList(db *gorm.DB, req DustDeviceListRequest) ([]DustDeviceItem, int64, error) {
  208. type ResultCount struct {
  209. Count int64
  210. }
  211. array := []ResultCount{}
  212. ret := []DustDeviceItem{}
  213. var err error
  214. sql, countSql, args := dustDeviceListSql(req)
  215. err = db.Raw(countSql, args...).Scan(&array).Error
  216. if err != nil {
  217. return nil, 0, err
  218. }
  219. if len(array) == 0 {
  220. return nil, 0, nil
  221. }
  222. if array[0].Count == 0 {
  223. return nil, 0, nil
  224. }
  225. err = db.Raw(sql, args...).Scan(&ret).Error
  226. if err != nil {
  227. return nil, array[0].Count, err
  228. }
  229. return ret, array[0].Count, nil
  230. }