package app import ( "github.com/sirupsen/logrus" "lcfns/global" "lcfns/model/app" "lcfns/model/app/request" "lcfns/model/app/response" ) var picService = PictureService{} type EventService struct { } func (EventService) List(req request.Event) (events []response.Event, err error) { //SELECT id,event_type,event_code,start_time,handle_time //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); //查找用户所有摄像头的mac_address: //SELECT c.mac_address FROM gateway g JOIN camera c ON g.id = c.gid WHERE g.uid = ? var macs []string sub := global.Db.Select("c.mac_address"). Table("gateway g JOIN camera c ON g.id = c.gid"). Where("g.uid = ?", req.UId). Find(&macs) //查询事件 db := global.Db.Select("e.id,e.event_type,e.event_code,e.start_time,e.handle_time,g.gateway_name"). Table("event e JOIN camera c ON e.mac_address = c.mac_address JOIN gateway g ON c.gid = g.id") if req.EventType > 0 && req.EventType <= 4 { //todo 未生效 db.Where("e.event_type = ?", eMap[req.EventType]) } if req.IsHandled != nil && *req.IsHandled { db.Where("NOT ISNULL(e.handle_time)") } if req.CreatedAt != "" { db.Where("e.start_time > ?", req.CreatedAt) } if req.GatewayName != "" { db.Where("g.gateway_name LIKE ?", "%"+req.GatewayName+"%") } err = db.Where("e.mac_address IN (?)", sub). Offset((req.Page - 1) * req.PageSize). Limit(req.PageSize). Find(&events).Debug().Error if err != nil { logrus.Error(err) return nil, err } for i, v := range events { events[i].EventType = sMap[v.EventType] events[i].PictureUrl = "http:192.168.110.69:8889/picture/get?picode=" + v.EventCode } return events, err } func (EventService) Create(event app.Event) error { return global.Db.Omit("handle_time").Create(&event).Debug().Error } func (EventService) Save(event *app.Event, pic *app.Picture, picData *app.PictureData) { err := global.Db.Omit("handle_time").Create(event).Error if err != nil { logrus.Error("事件保存失败", err) return } pic.EventId = event.ID picService.SaveEventPicture(pic, picData) } // 事件类型 var eMap = map[uint]string{ 1: "shelteralarm", 2: "regionEntrance", 3: "fielddetection", 4: "linedetection", } var sMap = map[string]string{ "shelteralarm": "遮盖报警", "regionEntrance": "进入区域", "fielddetection": "区域入侵", "linedetection": "越界侦测", }