12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package app
- import (
- "fmt"
- "github.com/sirupsen/logrus"
- "lc-fangdaosha/global"
- "lc-fangdaosha/model/app"
- "lc-fangdaosha/model/app/request"
- "lc-fangdaosha/model/app/response"
- )
- var picService = PictureService{}
- type EventService struct {
- }
- // List 获取事件列表
- func (EventService) List(req request.Event) (events []response.Event, err error) {
- fmt.Printf("参数:%+v\n", req)
- //查找用户所有摄像头的mac_address:
- 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.165:9000/picture/get?picode=" + v.EventCode
- }
- return events, err
- }
- // Create 创建事件
- func (EventService) Create(event app.Event) error {
- return global.Db.Omit("handle_time").Create(&event).Debug().Error
- }
- // Save 保存事件
- 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": "越界侦测",
- }
|