初始
This commit is contained in:
225
internal/app/system/logic/ys7Devices/ys7devices.go
Normal file
225
internal/app/system/logic/ys7Devices/ys7devices.go
Normal file
@ -0,0 +1,225 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2024-03-01 12:49:32
|
||||
// 生成路径: internal/app/system/logic/ys7devices.go
|
||||
// 生成人:gfast
|
||||
// desc:荧石摄像头
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/common/coryCommon"
|
||||
"github.com/tiger1103/gfast/v3/api/v1/system"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/consts"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/dao"
|
||||
ct "github.com/tiger1103/gfast/v3/internal/app/system/logic/context"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/model/do"
|
||||
"github.com/tiger1103/gfast/v3/internal/app/system/service"
|
||||
"github.com/tiger1103/gfast/v3/library/liberr"
|
||||
"github.com/tiger1103/gfast/v3/third/ys7"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterYs7Devices(New())
|
||||
}
|
||||
|
||||
func New() *sYs7Devices {
|
||||
return &sYs7Devices{}
|
||||
}
|
||||
|
||||
type sYs7Devices struct{}
|
||||
|
||||
func (s *sYs7Devices) List(ctx context.Context, req *system.Ys7DevicesSearchReq) (listRes *system.Ys7DevicesSearchRes, err error) {
|
||||
listRes = new(system.Ys7DevicesSearchRes)
|
||||
|
||||
user := ct.New().Get(ctx)
|
||||
if user == nil {
|
||||
return nil, fmt.Errorf("用户未登录")
|
||||
}
|
||||
|
||||
// 是否添加了 Where 条件
|
||||
isWhereAdded := false
|
||||
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.Ys7Devices.Ctx(ctx).WithAll()
|
||||
if req.Id != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().Id+" = ?", req.Id)
|
||||
isWhereAdded = true
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.Ys7Devices.Columns().CreatedAt+" >=? AND "+dao.Ys7Devices.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.DeviceSerial != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().DeviceSerial+" = ?", req.DeviceSerial)
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.DeviceName != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().DeviceName+" like ?", "%"+req.DeviceName+"%")
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.DeviceType != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().DeviceType+" = ?", req.DeviceType)
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().Status+" = ?", gconv.Int(req.Status))
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.Defence != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().Defence+" = ?", gconv.Int64(req.Defence))
|
||||
isWhereAdded = true
|
||||
}
|
||||
if req.DeviceVersion != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().DeviceVersion+" = ?", req.DeviceVersion)
|
||||
isWhereAdded = true
|
||||
}
|
||||
|
||||
// 如果是前端页面请求,只能看见传入的项目ID 下的摄像头
|
||||
if req.IsFront {
|
||||
m = m.Where(dao.Ys7Devices.Columns().ProjectId, req.ProjectId)
|
||||
} else {
|
||||
// 非管理员只能看见自己的摄像头和未被绑定的摄像头
|
||||
if user.User.IsAdmin != 1 {
|
||||
m = m.Where(dao.Ys7Devices.Columns().ProjectId, req.ProjectId). // 查询指定项目下的摄像头
|
||||
WhereOrNull(dao.Ys7Devices.Columns().ProjectId) // 查询未被绑定的摄像头
|
||||
}
|
||||
|
||||
// 当前用户是管理员
|
||||
if user.User.IsAdmin == 1 {
|
||||
//// 如果加了其他 Where 筛选条件,或者 ProJectID 不为空的的情况下,则查询指定项目下的摄像头
|
||||
//// 用于查询指定项目下的摄像头
|
||||
//if isWhereAdded || req.ProjectId != "" {
|
||||
// m = m.Where(dao.Ys7Devices.Columns().ProjectId, req.ProjectId)
|
||||
//}
|
||||
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.Ys7Devices.Columns().ProjectId, req.ProjectId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有 Where 筛选,且不是前端页面请求, 则查询未被绑定的摄像头
|
||||
if !isWhereAdded && !req.IsFront {
|
||||
m = m.WhereOrNull(dao.Ys7Devices.Columns().ProjectId)
|
||||
}
|
||||
|
||||
listRes.Total, err = m.Count()
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "sort desc,id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.Ys7DevicesInfoRes
|
||||
err = m.Fields(system.Ys7DevicesSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
|
||||
listRes.List = make([]*model.Ys7DevicesListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.Ys7DevicesListRes{
|
||||
Id: v.Id,
|
||||
CreatedAt: v.CreatedAt,
|
||||
DeviceSerial: v.DeviceSerial,
|
||||
DeviceName: v.DeviceName,
|
||||
DeviceType: v.DeviceType,
|
||||
Status: v.Status,
|
||||
Defence: v.Defence,
|
||||
DeviceVersion: v.DeviceVersion,
|
||||
ProjectId: v.ProjectId,
|
||||
Detail: v.Detail,
|
||||
ReMark: v.ReMark,
|
||||
VideoEncrypted: v.VideoEncrypted,
|
||||
Sort: v.Sort,
|
||||
}
|
||||
|
||||
// 如果是小程序则经纬度转换
|
||||
if req.IsWechat {
|
||||
a := strings.Split(v.Position, ",")
|
||||
lng, lat := coryCommon.WGS84toGCJ02(gconv.Float64(a[0]), gconv.Float64(a[1]))
|
||||
listRes.List[k].Lag = lng
|
||||
listRes.List[k].Lat = lat
|
||||
} else {
|
||||
a := strings.Split(v.Position, ",")
|
||||
listRes.List[k].Lag = gconv.Float64(a[0])
|
||||
listRes.List[k].Lat = gconv.Float64(a[1])
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range listRes.List {
|
||||
v.SourceType = "camera"
|
||||
v.SourceId = v.DeviceSerial
|
||||
v.Token = ys7.Ys72Instance.GetAccessToken()
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sYs7Devices) GetById(ctx context.Context, id uint) (res *model.Ys7DevicesInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.Ys7Devices.Ctx(ctx).WithAll().Where(dao.Ys7Devices.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sYs7Devices) Add(ctx context.Context, req *system.Ys7DevicesAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.Ys7Devices.Ctx(ctx).Insert(do.Ys7Devices{
|
||||
DeviceSerial: req.DeviceSerial,
|
||||
DeviceName: req.DeviceName,
|
||||
DeviceType: req.DeviceType,
|
||||
Status: req.Status,
|
||||
Defence: req.Defence,
|
||||
DeviceVersion: req.DeviceVersion,
|
||||
ProjectId: req.ProjectId,
|
||||
ReMark: req.Remark,
|
||||
Sort: req.Sort,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sYs7Devices) Edit(ctx context.Context, req *system.Ys7DevicesEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.Ys7Devices.Ctx(ctx).WherePri(req.Id).Update(do.Ys7Devices{
|
||||
DeviceSerial: req.DeviceSerial,
|
||||
DeviceName: req.DeviceName,
|
||||
DeviceType: req.DeviceType,
|
||||
Status: req.Status,
|
||||
Defence: req.Defence,
|
||||
DeviceVersion: req.DeviceVersion,
|
||||
ProjectId: req.ProjectId,
|
||||
Detail: req.Detail,
|
||||
Position: req.Position,
|
||||
ReMark: req.ReMark,
|
||||
Sort: req.Sort,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sYs7Devices) Delete(ctx context.Context, ids []uint) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.Ys7Devices.Ctx(ctx).Delete(dao.Ys7Devices.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user