event.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package app
  2. import (
  3. "github.com/sirupsen/logrus"
  4. "lcfns/global"
  5. "lcfns/model/app"
  6. "lcfns/model/app/request"
  7. "lcfns/model/app/response"
  8. )
  9. var picService = PictureService{}
  10. type EventService struct {
  11. }
  12. func (EventService) List(req request.Event) (events []response.Event, err error) {
  13. //SELECT id,event_type,event_code,start_time,handle_time
  14. //FROM `event` WHERE mac_address IN (SELECT c.mac_address FROM gateway g JOIN camera c ON g.id = c.gid WHERE g.uid = 1);
  15. //查找用户所有摄像头的mac_address:
  16. //SELECT c.mac_address FROM gateway g JOIN camera c ON g.id = c.gid WHERE g.uid = ?
  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.69:8889/picture/get?picode=" + v.EventCode
  48. }
  49. return events, err
  50. }
  51. func (EventService) Create(event app.Event) error {
  52. return global.Db.Omit("handle_time").Create(&event).Debug().Error
  53. }
  54. func (EventService) Save(event *app.Event, pic *app.Picture, picData *app.PictureData) {
  55. err := global.Db.Omit("handle_time").Create(event).Error
  56. if err != nil {
  57. logrus.Error("事件保存失败", err)
  58. return
  59. }
  60. pic.EventId = event.ID
  61. picService.SaveEventPicture(pic, picData)
  62. }
  63. // 事件类型
  64. var eMap = map[uint]string{
  65. 1: "shelteralarm",
  66. 2: "regionEntrance",
  67. 3: "fielddetection",
  68. 4: "linedetection",
  69. }
  70. var sMap = map[string]string{
  71. "shelteralarm": "遮盖报警",
  72. "regionEntrance": "进入区域",
  73. "fielddetection": "区域入侵",
  74. "linedetection": "越界侦测",
  75. }