初始
This commit is contained in:
232
internal/app/system/logic/device/device.go
Normal file
232
internal/app/system/logic/device/device.go
Normal file
@ -0,0 +1,232 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2023-11-24 09:57:07
|
||||
// 生成路径: internal/app/system/logic/device.go
|
||||
// 生成人:gfast
|
||||
// desc:安全帽设备(java)
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"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"
|
||||
tool "github.com/tiger1103/gfast/v3/utility/coryUtils"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterDevice(New())
|
||||
}
|
||||
|
||||
func New() *sDevice {
|
||||
return &sDevice{}
|
||||
}
|
||||
|
||||
type sDevice struct{}
|
||||
|
||||
func (s *sDevice) ScheduleTimeFunc(ctx context.Context, req *system.ScheduleTimeReq) (res *system.ScheduleTimeRes, err error) {
|
||||
res = new(system.ScheduleTimeRes)
|
||||
var arrStr []string
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
array, err := g.DB().Model("location").Ctx(ctx).
|
||||
Fields("DISTINCT DATE_FORMAT( time, '%Y-%m-%d' ) as te").
|
||||
Where("dev_num", req.DevNum).
|
||||
WhereBetween(
|
||||
"time",
|
||||
req.DateRange[0],
|
||||
req.DateRange[1],
|
||||
).
|
||||
Array()
|
||||
for i := range array {
|
||||
arrStr = append(arrStr, array[i].String())
|
||||
}
|
||||
res.List = arrStr
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) AllList(ctx context.Context, req *system.DeviceSearchAllReq) (res *system.DeviceSearchAllRes, err error) {
|
||||
res = new(system.DeviceSearchAllRes)
|
||||
g.Try(ctx, func(ctx context.Context) {
|
||||
var dir []*model.DeviceListRes
|
||||
err := dao.Device.Ctx(ctx).As("a").
|
||||
LeftJoin("bus_construction_user", "b", "a.dev_num = b.dev_num").
|
||||
Fields("a.*,b.openid,b.nick_name,b.user_name").
|
||||
Where("b.project_id", req.ProjectId).
|
||||
WhereNotNull("b.head_icon").Scan(&dir)
|
||||
res.List = dir
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) HelmetListFunc(ctx context.Context, req *system.HelmetListReq) (listRes *system.HelmetListRes, err error) {
|
||||
listRes = new(system.HelmetListRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.Device.Ctx(ctx).As("a").
|
||||
Fields("a.dev_num,a.dev_name")
|
||||
if req.ProjectId > 0 {
|
||||
m = m.Where("a."+dao.Device.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.DevNum != "" {
|
||||
m = m.Where("a."+dao.Device.Columns().DevNum+" like ?", "%"+req.DevNum+"%")
|
||||
}
|
||||
if req.DevName != "" {
|
||||
m = m.Where("a."+dao.Device.Columns().DevName+" like ?", "%"+req.DevName+"%")
|
||||
}
|
||||
array, err := m.Array()
|
||||
listRes.Total = len(array)
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "a.dev_num desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.DeviceInfoRes
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.DeviceListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.DeviceListRes{
|
||||
DevNum: v.DevNum,
|
||||
DevName: v.DevName,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) List(ctx context.Context, req *system.DeviceSearchReq) (listRes *system.DeviceSearchRes, err error) {
|
||||
listRes = new(system.DeviceSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.Device.Ctx(ctx).As("a").
|
||||
LeftJoin("bus_construction_user", "b", "a.dev_num = b.dev_num").
|
||||
Fields("a.*,b.openid,b.head_icon,b.nick_name,b.user_name,b.phone")
|
||||
if req.ProjectId > 0 {
|
||||
m = m.Where("a."+dao.Device.Columns().ProjectId+" = ?", req.ProjectId)
|
||||
}
|
||||
if req.Status != "" {
|
||||
m = m.Where("a."+dao.Device.Columns().Status+" = ?", gconv.Int(req.Status))
|
||||
}
|
||||
if req.DevNum != "" {
|
||||
m = m.Where("a."+dao.Device.Columns().DevNum+" like ?", "%"+req.DevNum+"%")
|
||||
}
|
||||
if req.DevName != "" {
|
||||
m = m.Where("a."+dao.Device.Columns().DevName+" like ?", "%"+req.DevName+"%")
|
||||
}
|
||||
array, err := m.Array()
|
||||
listRes.Total = len(array)
|
||||
liberr.ErrIsNil(ctx, err, "获取总行数失败")
|
||||
if req.PageNum == 0 {
|
||||
req.PageNum = 1
|
||||
}
|
||||
listRes.CurrentPage = req.PageNum
|
||||
if req.PageSize == 0 {
|
||||
req.PageSize = consts.PageSize
|
||||
}
|
||||
order := "a.dev_num desc"
|
||||
if req.OrderBy != "" {
|
||||
order = req.OrderBy
|
||||
}
|
||||
var res []*model.DeviceInfoRes
|
||||
err = m.Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.DeviceListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.DeviceListRes{
|
||||
DevNum: v.DevNum,
|
||||
DevName: v.DevName,
|
||||
Status: v.Status,
|
||||
CreateTime: v.CreateTime,
|
||||
UpdateTime: v.UpdateTime,
|
||||
ProjectId: v.ProjectId,
|
||||
HeadIcon: v.HeadIcon,
|
||||
NickName: v.NickName,
|
||||
UserName: v.UserName,
|
||||
Phone: v.Phone,
|
||||
Openid: v.Openid,
|
||||
BatteryLevel: v.BatteryLevel,
|
||||
IsLowBattery: v.IsLowBattery,
|
||||
BatteryOn: v.BatteryOn,
|
||||
BatteryOff: v.BatteryOff,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) GetByDevNum(ctx context.Context, req *system.DeviceGetReq) (res *system.DeviceGetRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
|
||||
err = dao.Device.Ctx(ctx).As("a").
|
||||
LeftJoin("bus_construction_user", "b", "a.dev_num = b.dev_num").
|
||||
Fields("a.*,b.openid,b.head_icon,b.nick_name,b.user_name,b.phone").
|
||||
Where("a.dev_num", req.DevNum).Scan(&res)
|
||||
//err = dao.Device.Ctx(ctx).WithAll().Where(dao.Device.Columns().DevNum, req.DevNum).Scan(&res)
|
||||
//插叙当前用户的足迹,按照日期时间范围筛选
|
||||
var latAndLon []*system.ActionPathLatAndLonActionPathLatAndLon
|
||||
m := g.DB().Model("location").Ctx(ctx).Where("dev_num", req.DevNum)
|
||||
if len(req.DateRange) > 0 {
|
||||
m = m.Where("DATE_FORMAT(time,'%Y-%m-%d') >=? AND DATE_FORMAT(time,'%Y-%m-%d') <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
err = m.OrderAsc("time").Scan(&latAndLon)
|
||||
res.LatAndLonList = latAndLon
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) Add(ctx context.Context, req *system.DeviceAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err = dao.Device.Ctx(ctx).Insert(do.Device{
|
||||
DevNum: req.DevNum,
|
||||
DevName: req.DevName,
|
||||
Status: req.Status,
|
||||
ProjectId: req.ProjectId,
|
||||
CreateTime: gtime.NewFromStr(tool.New().GetFormattedDateTime(time.Now())),
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) RemoveRelationFunc(ctx context.Context, req *system.RemoveRelationReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
_, err := dao.BusConstructionUser.Ctx(ctx).Where("openid", req.Openid).Update(g.Map{"dev_num": ""})
|
||||
liberr.ErrIsNil(ctx, err, "移除失败!")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevice) Delete(ctx context.Context, devNums []string) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
for i := range devNums {
|
||||
_, err = dao.BusConstructionUser.Ctx(ctx).Where("dev_num", devNums[i]).Update(g.Map{"dev_num": ""})
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
return
|
||||
}
|
||||
}
|
||||
_, err = dao.Device.Ctx(ctx).Delete(dao.Device.Columns().DevNum+" in (?)", devNums)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user