初始
This commit is contained in:
252
internal/app/system/logic/manageDevice/manage_device.go
Normal file
252
internal/app/system/logic/manageDevice/manage_device.go
Normal file
@ -0,0 +1,252 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-11-14 10:53:21
|
||||
// 生成路径: internal/app/system/logic/manage_device.go
|
||||
// 生成人:gfast
|
||||
// desc:设备信息
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"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"
|
||||
"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/plane/dj"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var key = "airportLocation"
|
||||
|
||||
func init() {
|
||||
service.RegisterManageDevice(New())
|
||||
}
|
||||
|
||||
func New() *sManageDevice {
|
||||
return &sManageDevice{}
|
||||
}
|
||||
|
||||
type sManageDevice struct{}
|
||||
|
||||
func (s *sManageDevice) List(ctx context.Context, req *system.ManageDeviceSearchReq) (listRes *system.ManageDeviceSearchRes, err error) {
|
||||
listRes = new(system.ManageDeviceSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.ManageDevice.Ctx(ctx).WithAll()
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.Domain != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().Domain+" = ?", gconv.Int64(req.Domain))
|
||||
}
|
||||
if req.DeviceSn != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().DeviceSn+" like ?", "%"+req.DeviceSn+"%")
|
||||
}
|
||||
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 := "id desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.ManageDeviceInfoRes
|
||||
err = m.Fields(system.ManageDeviceSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.ManageDeviceListRes, len(res))
|
||||
for k, v := range res {
|
||||
get, _ := g.Redis().Get(ctx, key+"-"+v.MqClientId)
|
||||
listRes.List[k] = &model.ManageDeviceListRes{
|
||||
Id: v.Id,
|
||||
DeviceSn: v.DeviceSn,
|
||||
DeviceName: v.DeviceName,
|
||||
MqClientId: v.MqClientId,
|
||||
UserId: v.UserId,
|
||||
Nickname: v.Nickname,
|
||||
WorkspaceId: v.WorkspaceId,
|
||||
DeviceType: v.DeviceType,
|
||||
SubType: v.SubType,
|
||||
Domain: v.Domain,
|
||||
FirmwareVersion: v.FirmwareVersion,
|
||||
CompatibleStatus: v.CompatibleStatus,
|
||||
Version: v.Version,
|
||||
DeviceIndex: v.DeviceIndex,
|
||||
//ChildSn: v.ChildSn,
|
||||
CreatedAt: v.CreatedAt,
|
||||
BoundTime: v.BoundTime,
|
||||
BoundStatus: v.BoundStatus,
|
||||
LoginTime: v.LoginTime,
|
||||
DeviceDesc: v.DeviceDesc,
|
||||
UrlNormal: v.UrlNormal,
|
||||
UrlSelect: v.UrlSelect,
|
||||
ProjectId: v.ProjectId,
|
||||
LngAndLatAndHeight: get.String(),
|
||||
ManageDeviceVideoEntity: v.ManageDeviceVideoEntity,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageDevice) NoPageList(ctx context.Context, req *system.NoPageListSearchReq) (listRes *system.NoPageListSearchRes, err error) {
|
||||
listRes = new(system.NoPageListSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.ManageDevice.Ctx(ctx).WithAll()
|
||||
if req.ProjectId != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.Domain != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().Domain+" = ?", gconv.Int64(req.Domain))
|
||||
}
|
||||
if req.DeviceSn != "" {
|
||||
m = m.Where(dao.ManageDevice.Columns().DeviceSn+" like ?", "%"+req.DeviceSn+"%")
|
||||
}
|
||||
order := "id desc"
|
||||
var res []*model.ManageDeviceInfoRes
|
||||
err = m.Fields(system.ManageDeviceSearchRes{}).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.ManageDeviceListRes, len(res))
|
||||
for k, v := range res {
|
||||
get, _ := g.Redis().Get(ctx, key+"-"+v.MqClientId)
|
||||
listRes.List[k] = &model.ManageDeviceListRes{
|
||||
Id: v.Id,
|
||||
DeviceSn: v.DeviceSn,
|
||||
DeviceName: v.DeviceName,
|
||||
MqClientId: v.MqClientId,
|
||||
UserId: v.UserId,
|
||||
Nickname: v.Nickname,
|
||||
WorkspaceId: v.WorkspaceId,
|
||||
DeviceType: v.DeviceType,
|
||||
SubType: v.SubType,
|
||||
Domain: v.Domain,
|
||||
FirmwareVersion: v.FirmwareVersion,
|
||||
CompatibleStatus: v.CompatibleStatus,
|
||||
Version: v.Version,
|
||||
DeviceIndex: v.DeviceIndex,
|
||||
//ChildSn: v.ChildSn,
|
||||
CreatedAt: v.CreatedAt,
|
||||
BoundTime: v.BoundTime,
|
||||
BoundStatus: v.BoundStatus,
|
||||
LoginTime: v.LoginTime,
|
||||
DeviceDesc: v.DeviceDesc,
|
||||
UrlNormal: v.UrlNormal,
|
||||
UrlSelect: v.UrlSelect,
|
||||
ProjectId: v.ProjectId,
|
||||
LngAndLatAndHeight: get.String(),
|
||||
ManageDeviceVideoEntity: v.ManageDeviceVideoEntity,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageDevice) GetById(ctx context.Context, id uint64) (res *model.ManageDeviceInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.ManageDevice.Ctx(ctx).WithAll().Where(dao.ManageDevice.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageDevice) Add(ctx context.Context, req *system.ManageDeviceAddReq) (err error) {
|
||||
err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var mdi *model.ManageDeviceDictionaryInfo
|
||||
err = g.DB().Model("manage_device_dictionary").Ctx(ctx).WherePri(req.DictionaryId).Scan(&mdi)
|
||||
_, err = dao.ManageDevice.Ctx(ctx).OmitEmpty().Insert(do.ManageDevice{
|
||||
ProjectId: req.ProjectId,
|
||||
DeviceSn: req.DeviceSn,
|
||||
DictionaryId: req.DictionaryId,
|
||||
MqClientId: req.MqClientId,
|
||||
DeviceName: mdi.DeviceName,
|
||||
DeviceType: mdi.DeviceType,
|
||||
SubType: mdi.SubType,
|
||||
Domain: mdi.Domain,
|
||||
Nickname: req.Nickname,
|
||||
WorkspaceId: req.WorkspaceId,
|
||||
//FirmwareVersion: req.FirmwareVersion,
|
||||
//CompatibleStatus: req.CompatibleStatus,
|
||||
//ChildSn: req.ChildSn,
|
||||
BoundStatus: 1,
|
||||
BoundTime: gtime.Now(),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "数据添加失败!")
|
||||
//当前sn下面的调试模式状态全为关,如果查询到原本有就不用操作
|
||||
var m = g.DB().Model("manage_button_state").Safe()
|
||||
count, err2 := m.Where("mq_client_id", req.MqClientId).Count()
|
||||
liberr.ErrIsNil(ctx, err2, "初始化调试模式按钮失败!")
|
||||
if count == 0 {
|
||||
_, err2 := g.DB().Model("manage_button_state").Insert(g.Map{"mq_client_id": req.MqClientId})
|
||||
liberr.ErrIsNil(ctx, err2, "初始化调试模式按钮失败!")
|
||||
return
|
||||
}
|
||||
})
|
||||
return err
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageDevice) Edit(ctx context.Context, req *system.ManageDeviceEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
var mdi *model.ManageDeviceDictionaryInfo
|
||||
err = g.DB().Model("manage_device_dictionary").Ctx(ctx).WherePri(req.DictionaryId).Scan(&mdi)
|
||||
_, err = dao.ManageDevice.Ctx(ctx).OmitEmpty().WherePri(req.Id).Update(do.ManageDevice{
|
||||
DeviceSn: req.DeviceSn,
|
||||
DictionaryId: req.DictionaryId,
|
||||
DeviceName: mdi.DeviceName,
|
||||
MqClientId: req.MqClientId,
|
||||
DeviceType: mdi.DeviceType,
|
||||
SubType: mdi.SubType,
|
||||
Domain: mdi.Domain,
|
||||
Nickname: req.Nickname,
|
||||
WorkspaceId: req.WorkspaceId,
|
||||
//FirmwareVersion: req.FirmwareVersion,
|
||||
//CompatibleStatus: req.CompatibleStatus,
|
||||
//ChildSn: req.ChildSn,
|
||||
BoundStatus: 1,
|
||||
BoundTime: gtime.Now(),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sManageDevice) Delete(ctx context.Context, ids []uint64) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.ManageDevice.Ctx(ctx).Delete(dao.ManageDevice.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// LongitudeAndLatitudeEdit 给指定机场添加经度纬度坐标
|
||||
func LongitudeAndLatitudeEdit(gatewaySn string, point dj.Point) (err error) {
|
||||
ctx := gctx.New()
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
lon := strconv.FormatFloat(point.Longitude, 'f', -1, 64)
|
||||
lat := strconv.FormatFloat(point.Latitude, 'f', -1, 64)
|
||||
height := strconv.FormatFloat(point.Height, 'f', -1, 64)
|
||||
var pt = lon + "," + lat + "," + height
|
||||
_, err2 := g.Redis().Set(ctx, key+"-"+gatewaySn, pt)
|
||||
if err2 != nil {
|
||||
return
|
||||
}
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user