208 lines
7.0 KiB
Go
208 lines
7.0 KiB
Go
package video_hat
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"github.com/gogf/gf/v2/frame/g"
|
||
"time"
|
||
)
|
||
|
||
type VideoDeviceHat struct {
|
||
DevNum string `json:"dev_num"` // 设备编号
|
||
DevName string `json:"dev_name"` // 设备名
|
||
Status int `json:"status"` // 安全帽状态(0:离线,1:在线,2:监控中,3:通话中,4:隐私模式)
|
||
ProjectID int64 `json:"project_id"` // 项目id
|
||
UserID string `json:"user_id"` // 用户id
|
||
BatteryLevel string `json:"battery_level"` // 电量
|
||
IsLowBattery bool `json:"is_low_battery"` // 是否处于低电量,true代表低电量,false则不是低电量
|
||
CreateTime time.Time `json:"create_time"` // 创建时间
|
||
UpdatedAt time.Time `json:"updated_at"` // 更新时间
|
||
Longitude string `json:"longitude"` // 最新的经度
|
||
Latitude string `json:"latitude"` // 最新的维度
|
||
SipId int `json:"sip_id"` // SIPID
|
||
Uid int `json:"uid"` // 安全帽厂商返回的用户ID
|
||
PushStatus string `json:"push_status"` // 推流状态
|
||
Nickname string `json:"nick_name"` // 用户名称
|
||
UserName string `json:"user_name"` // 真实名称
|
||
HeadIcon string `json:"head_icon"` // 头像
|
||
Phone string `json:"phone"` // 电话
|
||
}
|
||
|
||
// 视频安全帽设备列表
|
||
type DeviceListReq struct {
|
||
g.Meta `path:"/video/device/list" method:"post" tags:"视频安全帽相关" summary:"视频安全帽设备列表"`
|
||
ProjectId int64 `json:"projectId" dc:"项目ID"`
|
||
DevNum string `json:"devNum" dc:"设备编号模糊查询"`
|
||
DevName string `json:"devName" dc:"设备名称模糊查询"`
|
||
Status *int `json:"status" dc:"安全帽状态等值查询"` // 使用指针允许状态为nil,表示不过滤状态
|
||
Page int64 `json:"page" dc:"请求的页码" v:"required"`
|
||
PageSize int64 `json:"pageSize" dc:"每页显示的条目数" v:"required"`
|
||
}
|
||
|
||
// 视频安全帽设备列表响应结构体
|
||
type DeviceListRes struct {
|
||
Total int64 `json:"total"` // 总条目数
|
||
VideoDeviceHats []VideoDeviceHat `json:"videoDeviceHats"`
|
||
}
|
||
|
||
func (v VideoHat) DeviceList(ctx context.Context, req *DeviceListReq) (res *DeviceListRes, err error) {
|
||
res = new(DeviceListRes)
|
||
offset := (req.Page - 1) * req.PageSize
|
||
var devices []VideoDeviceHat
|
||
|
||
// 准备查询模型,设置连表查询
|
||
model := g.Model("device_video_hat AS dvh").LeftJoin("bus_construction_user AS user", "user.id=dvh.user_id")
|
||
|
||
// 根据项目ID过滤
|
||
if req.ProjectId != 0 {
|
||
model = model.Where("dvh.project_id = ?", req.ProjectId)
|
||
}
|
||
|
||
// 设备编号模糊查询
|
||
if req.DevNum != "" {
|
||
model = model.Where("dvh.dev_num LIKE ?", "%"+req.DevNum+"%")
|
||
}
|
||
|
||
// 设备名称模糊查询
|
||
if req.DevName != "" {
|
||
model = model.Where("dvh.dev_name LIKE ?", "%"+req.DevName+"%")
|
||
}
|
||
|
||
// 安全帽状态等值查询
|
||
if req.Status != nil {
|
||
model = model.Where("dvh.status = ?", *req.Status)
|
||
}
|
||
|
||
// 应用查询条件但不选取字段,用于计数
|
||
count, err := model.Count()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
// 选取具体字段,包括连表查询字段
|
||
model = model.Fields("dvh.*, user.nick_name,user.user_name,user.head_icon,user.phone")
|
||
|
||
// 获取当前页的设备列表
|
||
err = model.Offset(int(offset)).Limit(int(req.PageSize)).Scan(&devices)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res.Total = int64(count)
|
||
res.VideoDeviceHats = devices
|
||
return res, nil
|
||
}
|
||
|
||
// 为设备绑定项目ID
|
||
type BindProjectIdReq struct {
|
||
g.Meta `path:"/video/project/bind" method:"post" tags:"视频安全帽相关" summary:"为设备绑定项目"`
|
||
ProjectId int64 `json:"projectId" dc:"项目ID"`
|
||
DevNum string `json:"devNum" dc:"设备编号"`
|
||
}
|
||
|
||
type BindProjectIdRes struct {
|
||
}
|
||
|
||
func (v VideoHat) BindProjectId(ctx context.Context, req *BindProjectIdReq) (res *BindProjectIdRes, err error) {
|
||
res = new(BindProjectIdRes)
|
||
param := g.Map{
|
||
"project_id": req.ProjectId,
|
||
}
|
||
g.Model("device_video_hat").Data(param).Where("dev_num = ?", req.DevNum).Update()
|
||
return res, nil
|
||
}
|
||
|
||
// 为设备解绑项目ID
|
||
type UnBindProjectIdReq struct {
|
||
g.Meta `path:"/video/project/unbind" method:"post" tags:"视频安全帽相关" summary:"为设备解绑项目"`
|
||
DevNum string `json:"devNum" dc:"设备编号"`
|
||
}
|
||
|
||
// 视频安全帽设备列表响应结构体
|
||
type UnBindProjectIdRes struct {
|
||
}
|
||
|
||
func (v VideoHat) UnBindProjectId(ctx context.Context, req *UnBindProjectIdReq) (res *UnBindProjectIdRes, err error) {
|
||
res = new(UnBindProjectIdRes)
|
||
param := g.Map{
|
||
"project_id": nil,
|
||
}
|
||
g.Model("device_video_hat").Data(param).Where("dev_num = ?", req.DevNum).Update()
|
||
return res, nil
|
||
}
|
||
|
||
// 为设备绑定用户ID
|
||
type BindUserIdReq struct {
|
||
g.Meta `path:"/video/user/bind" method:"post" tags:"视频安全帽相关" summary:"为设备绑定用户"`
|
||
UserId int64 `json:"userId" dc:"用户ID"`
|
||
DevNum string `json:"devNum" dc:"设备编号"`
|
||
}
|
||
|
||
type BindUserIdRes struct {
|
||
}
|
||
|
||
func (v VideoHat) BindUserId(ctx context.Context, req *BindUserIdReq) (res *BindUserIdRes, err error) {
|
||
param := g.Map{
|
||
"user_id": req.UserId,
|
||
}
|
||
g.Model("device_video_hat").Data(param).Where("dev_num = ?", req.DevNum).Update()
|
||
return res, nil
|
||
}
|
||
|
||
// 为设备绑定用户ID
|
||
type UnBindUserIdReq struct {
|
||
g.Meta `path:"/video/user/unbind" method:"post" tags:"视频安全帽相关" summary:"为设备解绑用户"`
|
||
DevNum string `json:"devNum" dc:"设备编号"`
|
||
}
|
||
|
||
type UnBindUserIdRes struct {
|
||
}
|
||
|
||
func (v VideoHat) UnBindUserId(ctx context.Context, req *UnBindUserIdReq) (res *UnBindUserIdRes, err error) {
|
||
param := g.Map{
|
||
"user_id": nil,
|
||
}
|
||
g.Model("device_video_hat").Data(param).Where("dev_num = ?", req.DevNum).Update()
|
||
return res, nil
|
||
}
|
||
|
||
// 查询某个安全帽有轨迹的时间
|
||
type DeviceListDateTimeReq struct {
|
||
g.Meta `path:"/video/device/dateList" method:"post" tags:"视频安全帽相关" summary:"视频安全帽轨迹时间列表"`
|
||
DevNum string `json:"devNum" dc:"设备编号"`
|
||
StartTime string `json:"startTime" dc:"开始时间,格式为 YYYY-MM"`
|
||
}
|
||
|
||
type DeviceDataTime struct {
|
||
DataTime string `json:"dataTime"`
|
||
}
|
||
|
||
// 视频安全帽设备列表响应结构体
|
||
type DeviceListDateTimeRes struct {
|
||
DateTimeList []DeviceDataTime `json:"dateTimeList" dc:"设备含有轨迹的数据列表"`
|
||
}
|
||
|
||
func (v VideoHat) DeviceListDateTime(ctx context.Context, req *DeviceListDateTimeReq) (res *DeviceListDateTimeRes, err error) {
|
||
res = new(DeviceListDateTimeRes)
|
||
|
||
// 解析开始时间
|
||
startTime, err := time.Parse("2006-01", req.StartTime)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("invalid start time format: %v", err)
|
||
}
|
||
|
||
// 计算该月的第一天和最后一天
|
||
firstDayOfMonth := startTime
|
||
lastDayOfMonth := firstDayOfMonth.AddDate(0, 1, -1) // 增加一个月,减去一天
|
||
|
||
// 查询设备的数据时间在指定月份内
|
||
g.Model("device_data_time").
|
||
Fields("data_time AS dataTime").
|
||
Where("dev_num = ?", req.DevNum).
|
||
Where("data_time >= ?", firstDayOfMonth.Format("2006-01-02")).
|
||
Where("data_time <= ?", lastDayOfMonth.Format("2006-01-02")).
|
||
Scan(&res.DateTimeList)
|
||
|
||
return res, nil
|
||
}
|