device.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. "github.com/tidwall/gjson"
  8. "time"
  9. )
  10. type TDevice struct {
  11. Id int64 `gorm:"column:id" json:"id" form:"id"`
  12. ProjectId int64 `gorm:"column:project_id" json:"project_id" form:"project_id"`
  13. ProviderId int64 `gorm:"column:provider_id" json:"provider_id" form:"provider_id"`
  14. Name string `gorm:"column:name" json:"name" form:"name"`
  15. Sn string `gorm:"column:sn" json:"sn" form:"sn"`
  16. Addr string `gorm:"column:addr" json:"addr" form:"addr"`
  17. DeviceCode int32 `gorm:"column:device_code" json:"device_code" form:"device_code"`
  18. Lon float64 `gorm:"column:lon" json:"lon" form:"lon"`
  19. Lat float64 `gorm:"column:lat" json:"lat" form:"lat"`
  20. XCoord float64 `gorm:"column:x_coord" json:"x_coord" form:"x_coord"`
  21. YCoord float64 `gorm:"column:y_coord" json:"y_coord" form:"y_coord"`
  22. Status int64 `gorm:"column:status" json:"status" form:"status"`
  23. CreatedAt time.Time `gorm:"column:created_at" json:"created_at" form:"created_at"`
  24. UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at" form:"updated_at"`
  25. VerifyStatus int64 `gorm:"column:verify_status" json:"verify_status" form:"verify_status"`
  26. Key string `gorm:"column:key" json:"key" form:"key"`
  27. ProjectApproveTime time.Time `json:"project_approve_time"`
  28. }
  29. const (
  30. DeviceTypeVedio = 9
  31. DeviceTypeDust = 5
  32. )
  33. func (TDevice) TableName() string {
  34. return "t_device"
  35. }
  36. func (p *TDevice) Insert(db *gorm.DB) error {
  37. return db.Create(p).Error
  38. }
  39. func (p *TDevice) Statistic(db *gorm.DB, projectId int64, deviceType int32, result interface{}) error {
  40. 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)
  41. if deviceType > 0 {
  42. 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)
  43. }
  44. err := db.Raw(sql).Scan(result).Error
  45. return err
  46. }
  47. func (p *TDevice) Del(db *gorm.DB, where map[string]interface{}) error {
  48. cond, val, err := whereBuild(where)
  49. if err != nil {
  50. return err
  51. }
  52. return db.Table(p.TableName()).Where(cond, val...).Delete(p).Error
  53. }
  54. func (p *TDevice) Find(db *gorm.DB, where map[string]interface{}) error {
  55. cond, val, err := whereBuild(where)
  56. if err != nil {
  57. return err
  58. }
  59. return db.Table(p.TableName()).Where(cond, val...).First(p).Error
  60. }
  61. func (p *TDevice) Update(db *gorm.DB, where map[string]interface{}, values map[string]interface{}) error {
  62. cond, val, err := whereBuild(where)
  63. if err != nil {
  64. return err
  65. }
  66. return db.Table(p.TableName()).Where(cond, val...).Updates(values).Error
  67. }
  68. func (p *TDevice) FindSort(db *gorm.DB, where map[string]interface{}, sort string) error {
  69. cond, val, err := whereBuild(where)
  70. if err != nil {
  71. return err
  72. }
  73. ps := []TDevice{}
  74. err = db.Table(p.TableName()).Where(cond, val...).Order(sort).Limit(1).Find(&ps).Error
  75. if err != nil {
  76. return err
  77. }
  78. if len(ps) > 0 {
  79. *p = ps[0]
  80. }
  81. return nil
  82. }
  83. func (p *TDevice) Save(db *gorm.DB) error {
  84. return db.Save(p).Error
  85. }
  86. func (p *TDevice) Count(db *gorm.DB, where map[string]interface{}) (int64, error) {
  87. if len(where) > 0 {
  88. cond, val, err := whereBuild(where)
  89. if err != nil {
  90. return 0, err
  91. }
  92. ret := int64(0)
  93. err = db.Table(p.TableName()).Where(cond, val...).Count(&ret).Error
  94. return ret, err
  95. }
  96. ret := int64(0)
  97. err := db.Table(p.TableName()).Count(&ret).Error
  98. return ret, err
  99. }
  100. func (p *TDevice) List(db *gorm.DB, where map[string]interface{}, page int) (list []TDevice, err error) {
  101. if len(where) > 0 {
  102. cond, val, err := whereBuild(where)
  103. if err != nil {
  104. return list, err
  105. }
  106. result := db.Table(p.TableName()).Where(cond, val...).Limit(PageSize).Offset(page).Find(&list)
  107. return list, result.Error
  108. }
  109. result := db.Table(p.TableName()).Limit(10).Offset(page).Find(&list)
  110. return list, result.Error
  111. }
  112. func (p *TDevice) All(db *gorm.DB, where map[string]interface{}) (list []TDevice, err error) {
  113. if len(where) > 0 {
  114. cond, val, err := whereBuild(where)
  115. if err != nil {
  116. return list, err
  117. }
  118. result := db.Table(p.TableName()).Where(cond, val...).Find(&list)
  119. return list, result.Error
  120. }
  121. result := db.Table(p.TableName()).Find(&list)
  122. return list, result.Error
  123. }
  124. type DeviceItem struct {
  125. Id int64
  126. Sn string
  127. TypeCode int32
  128. TypeName string
  129. ProjectName string
  130. ProjectId int64
  131. SafetyRecordNo string
  132. CreatedTime time.Time
  133. Status int32
  134. State int32
  135. ProjectApproveTime time.Time
  136. Key string
  137. ProviderName string
  138. SocialCode string
  139. Name string
  140. Ip string
  141. Port int
  142. MediaTransport string
  143. ChannelCount int
  144. }
  145. func listRawSqlForVedio(req ListRawRequest)(string, string, []interface{}) {
  146. sql := fmt.Sprintf("select t1.id as id, t1.key, t1.name as name, t1.sn as sn, t1.device_code as type_code, t1.project_id as project_id, t1.project_approve_time, t4.MediaTransport as media_transport, t4.IP as ip, t4.Port as port, t4.channel_count, "+
  147. "t1.created_at as created_time, t4.Alive as state, t1.verify_status as status, t2.safety_record_no, t2.name as project_name, t3.name as provider_name, t3.social_code from t_device as t1 left join t_project as t2 on t1.project_id=t2.id and t2.cid=%d left join t_provider as t3 on t1.provider_id=t3.id left join VSSDevTbl as t4 on t4.DevPubID=t1.sn", req.Cid)
  148. 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 and t2.cid=%d left join t_provider as t3 on t1.provider_id=t3.id left join VSSDevTbl as t4 on t4.DevPubID=t1.sn", req.Cid)
  149. args := []interface{}{}
  150. whereArray := []string{}
  151. if req.ProviderId > 0 {
  152. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  153. }
  154. if req.Cid > 0 {
  155. whereArray = append(whereArray, fmt.Sprintf("t2.cid=%d", req.Cid))
  156. }
  157. whereArray = append(whereArray, fmt.Sprintf("t1.device_code = %d", DeviceTypeVedio))
  158. if req.Filter != "" {
  159. whereArray = append(whereArray, fmt.Sprintf("(t1.sn like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  160. }
  161. if req.ProjectId > 0 {
  162. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  163. }
  164. if req.CanDel {
  165. req.IsAll = true
  166. req.StatusFilter = []int32{1}
  167. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from t_project_job where device_id=t1.id and type = 1 and (status = 0 or status = 1)) = 0"))
  168. }
  169. if len(req.StatusFilter) > 0 {
  170. args = append(args, req.StatusFilter)
  171. whereArray = append(whereArray, fmt.Sprintf("t1.verify_status in(?)"))
  172. }
  173. where := ""
  174. for _, v := range whereArray {
  175. if where == "" {
  176. where = fmt.Sprintf(" where %s", v)
  177. continue
  178. }
  179. where = fmt.Sprintf("%s and %s", where, v)
  180. }
  181. offset := (req.Page - 1) *int32(PageSize)
  182. if req.IsAll {
  183. sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where)
  184. countSql = fmt.Sprintf("%s %s", countSql, where)
  185. } else {
  186. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  187. countSql = fmt.Sprintf("%s %s", countSql, where)
  188. }
  189. return sql, countSql, args
  190. }
  191. func listRawSql(req ListRawRequest)(string, string, []interface{}) {
  192. if req.IsVedio {
  193. return listRawSqlForVedio(req)
  194. }
  195. sql := fmt.Sprintf("select t1.id as id, t1.key, t1.name, t1.sn as sn, t1.device_code as type_code, t1.project_id as project_id, t1.project_approve_time, "+
  196. "t1.created_at as created_time, t1.status as state, t1.verify_status as status, t2.safety_record_no, t2.name as project_name, t3.name as provider_name, t3.social_code from t_device as t1 left join t_project as t2 on t1.project_id=t2.id and t2.cid=%d left join t_provider as t3 on t1.provider_id=t3.id", req.Cid)
  197. 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 and t2.cid=%d left join t_provider as t3 on t1.provider_id=t3.id", req.Cid)
  198. args := []interface{}{}
  199. whereArray := []string{}
  200. if req.ProviderId > 0 {
  201. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  202. }
  203. if req.Cid > 0 {
  204. whereArray = append(whereArray, fmt.Sprintf("t2.cid=%d", req.Cid))
  205. }
  206. if !req.CanDel {
  207. whereArray = append(whereArray, fmt.Sprintf("t1.device_code <> %d", DeviceTypeVedio))
  208. }
  209. if req.TypeCode > 0 {
  210. whereArray = append(whereArray, fmt.Sprintf("t1.device_code = %d", req.TypeCode))
  211. }
  212. if req.Filter != "" {
  213. whereArray = append(whereArray, fmt.Sprintf("(t1.sn like '%%%s%%' or t2.safety_record_no like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  214. }
  215. if req.ProjectId > 0 {
  216. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  217. }
  218. if req.CanDel {
  219. req.IsAll = true
  220. req.StatusFilter = []int32{1}
  221. whereArray = append(whereArray, fmt.Sprintf("(select count(1) from t_project_job where device_id=t1.id and type = 1 and (status = 0 or status = 1)) = 0"))
  222. }
  223. if len(req.StatusFilter) > 0 {
  224. args = append(args, req.StatusFilter)
  225. whereArray = append(whereArray, fmt.Sprintf("t1.verify_status in(?)"))
  226. }
  227. where := ""
  228. for _, v := range whereArray {
  229. if where == "" {
  230. where = fmt.Sprintf(" where %s", v)
  231. continue
  232. }
  233. where = fmt.Sprintf("%s and %s", where, v)
  234. }
  235. offset := (req.Page - 1) *int32(PageSize)
  236. if req.IsAll {
  237. sql = fmt.Sprintf("%s %s order by t1.created_at desc", sql, where)
  238. countSql = fmt.Sprintf("%s %s", countSql, where)
  239. } else {
  240. sql = fmt.Sprintf("%s %s order by t1.created_at desc limit %d offset %d", sql, where, PageSize, offset)
  241. countSql = fmt.Sprintf("%s %s", countSql, where)
  242. }
  243. return sql, countSql, args
  244. }
  245. type ListRawRequest struct {
  246. ProviderId int64
  247. Filter string
  248. StatusFilter []int32
  249. IsAll bool
  250. CanDel bool
  251. Page int32
  252. TypeCode int32
  253. ProjectId int64
  254. IsVedio bool
  255. Cid int64
  256. }
  257. func (p *TDevice) ListRaw(db *gorm.DB, req ListRawRequest) ([]DeviceItem, int64, error) {
  258. type ResultCount struct {
  259. Count int64
  260. }
  261. array := []ResultCount{}
  262. ret := []DeviceItem{}
  263. var err error
  264. sql, countSql, args := listRawSql(req)
  265. err = db.Raw(countSql, args...).Scan(&array).Error
  266. if err != nil {
  267. return nil, 0, err
  268. }
  269. if len(array) == 0 {
  270. return nil, 0, nil
  271. }
  272. if array[0].Count == 0 {
  273. return nil, 0, nil
  274. }
  275. err = db.Raw(sql, args...).Scan(&ret).Error
  276. if err != nil {
  277. return nil, array[0].Count, err
  278. }
  279. return ret, array[0].Count, nil
  280. }
  281. type DeviceDelJobItem struct {
  282. Id int64
  283. Sn string
  284. TypeCode int32
  285. TypeName string
  286. ProjectName string
  287. ProviderName string
  288. SocialCode string
  289. SafetyRecordNo string
  290. CreatedTime time.Time
  291. Status int32
  292. ApproveTime time.Time
  293. Origin string
  294. Reason string
  295. Feedback string
  296. }
  297. func listDelJobSql(req ListRawRequest)(string, string, []interface{}) {
  298. sql := "select t1.origin, t1.reason, t1.feedback, t1.id, t1.status, t1.updated_at as approve_time, t1.created_at as created_time, t2.name as provider_name, t2.social_code, t3.safety_record_no, t3.name as project_name " +
  299. "from t_project_job as t1 left join t_provider as t2 on t1.provider_id=t2.id left join t_project as t3 on t3.id=t1.project_id "
  300. countSql := "select count(1) as count " +
  301. "from t_project_job as t1 left join t_provider as t2 on t1.provider_id=t2.id left join t_project as t3 on t3.id=t1.project_id "
  302. args := []interface{}{}
  303. whereArray := []string{"t1.type = 1"}
  304. if req.ProviderId > 0 {
  305. whereArray = append(whereArray, fmt.Sprintf("t1.provider_id=%d", req.ProviderId))
  306. }
  307. if req.Filter != "" {
  308. whereArray = append(whereArray, fmt.Sprintf("(t1.content like '%%%s%%' or t2.social_code like '%%%s%%' or t2.name like '%%%s%%')", req.Filter, req.Filter, req.Filter))
  309. }
  310. if len(req.StatusFilter) > 0 {
  311. args = append(args, req.StatusFilter)
  312. whereArray = append(whereArray, fmt.Sprintf("t1.status in(?)"))
  313. }
  314. if req.ProjectId > 0 {
  315. whereArray = append(whereArray, fmt.Sprintf("t1.project_id = %d", req.ProjectId))
  316. }
  317. where := ""
  318. for _, v := range whereArray {
  319. if where == "" {
  320. where = fmt.Sprintf(" where %s", v)
  321. continue
  322. }
  323. where = fmt.Sprintf("%s and %s", where, v)
  324. }
  325. offset := (req.Page - 1) *int32(PageSize)
  326. if req.IsAll {
  327. sql = fmt.Sprintf("%s %s", sql, where)
  328. countSql = fmt.Sprintf("%s %s", countSql, where)
  329. } else {
  330. sql = fmt.Sprintf("%s %s limit %d offset %d", sql, where, PageSize, offset)
  331. countSql = fmt.Sprintf("%s %s", countSql, where)
  332. }
  333. return sql, countSql, args
  334. }
  335. func (p *TDevice) ListDelJob(db *gorm.DB, req ListRawRequest) ([]DeviceDelJobItem, int64, error) {
  336. type ResultCount struct {
  337. Count int64
  338. }
  339. array := []ResultCount{}
  340. ret := []DeviceDelJobItem{}
  341. var err error
  342. sql, countSql, args := listDelJobSql(req)
  343. err = db.Raw(countSql, args...).Scan(&array).Error
  344. if err != nil {
  345. return nil, 0, err
  346. }
  347. if len(array) == 0 {
  348. return nil, 0, nil
  349. }
  350. if array[0].Count == 0 {
  351. return nil, 0, nil
  352. }
  353. err = db.Raw(sql, args...).Scan(&ret).Error
  354. if err != nil {
  355. return nil, array[0].Count, err
  356. }
  357. for i, v := range ret {
  358. ret[i].Sn = gjson.GetBytes([]byte(v.Origin), "sn").String()
  359. ret[i].TypeCode = int32(gjson.GetBytes([]byte(v.Origin), "device_code").Int())
  360. }
  361. return ret, array[0].Count, nil
  362. }