event.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package app
  2. import (
  3. "fmt"
  4. "github.com/sirupsen/logrus"
  5. "lc-fangdaosha/global"
  6. "lc-fangdaosha/model/app"
  7. "lc-fangdaosha/model/app/request"
  8. "lc-fangdaosha/model/app/response"
  9. )
  10. var picService = PictureService{}
  11. type EventService struct {
  12. }
  13. // List 获取事件列表
  14. func (EventService) List(req request.Event) (events []response.Event, err error) {
  15. fmt.Printf("参数:%+v\n", req)
  16. //查找用户所有摄像头的mac_address:
  17. var macs []string
  18. sub := global.Db.Select("c.mac_address").
  19. Table("gateway g JOIN camera c ON g.id = c.gid").
  20. Where("g.uid = ?", req.UId).
  21. Find(&macs)
  22. //查询事件
  23. db := global.Db.Select("e.id,e.event_type,e.event_code,e.start_time,e.handle_time,g.gateway_name").
  24. Table("event e JOIN camera c ON e.mac_address = c.mac_address JOIN gateway g ON c.gid = g.id")
  25. if req.EventType > 0 && req.EventType <= 4 { //todo 未生效
  26. db.Where("e.event_type = ?", eMap[req.EventType])
  27. }
  28. if req.IsHandled != nil && *req.IsHandled {
  29. db.Where("NOT ISNULL(e.handle_time)")
  30. }
  31. if req.CreatedAt != "" {
  32. db.Where("e.start_time > ?", req.CreatedAt)
  33. }
  34. if req.GatewayName != "" {
  35. db.Where("g.gateway_name LIKE ?", "%"+req.GatewayName+"%")
  36. }
  37. err = db.Where("e.mac_address IN (?)", sub).
  38. Offset((req.Page - 1) * req.PageSize).
  39. Limit(req.PageSize).
  40. Find(&events).Debug().Error
  41. if err != nil {
  42. logrus.Error(err)
  43. return nil, err
  44. }
  45. for i, v := range events {
  46. events[i].EventType = sMap[v.EventType]
  47. events[i].PictureUrl = "http:192.168.110.165:9000/picture/get?picode=" + v.EventCode
  48. }
  49. return events, err
  50. }
  51. // Create 创建事件
  52. func (EventService) Create(event app.Event) error {
  53. return global.Db.Omit("handle_time").Create(&event).Debug().Error
  54. }
  55. // Save 保存事件
  56. func (EventService) Save(event *app.Event, pic *app.Picture, picData *app.PictureData) {
  57. err := global.Db.Omit("handle_time").Create(event).Error
  58. if err != nil {
  59. logrus.Error("事件保存失败", err)
  60. return
  61. }
  62. pic.EventId = event.ID
  63. picService.SaveEventPicture(pic, picData)
  64. }
  65. // 事件类型
  66. var eMap = map[uint]string{
  67. 1: "shelteralarm",
  68. 2: "regionEntrance",
  69. 3: "fielddetection",
  70. 4: "linedetection",
  71. }
  72. var sMap = map[string]string{
  73. "shelteralarm": "遮盖报警",
  74. "regionEntrance": "进入区域",
  75. "fielddetection": "区域入侵",
  76. "linedetection": "越界侦测",
  77. }