初始
This commit is contained in:
161
internal/app/system/logic/devicePreset/device_preset.go
Normal file
161
internal/app/system/logic/devicePreset/device_preset.go
Normal file
@ -0,0 +1,161 @@
|
||||
// ==========================================================================
|
||||
// GFast自动生成logic操作代码。
|
||||
// 生成日期:2024-03-01 12:50:08
|
||||
// 生成路径: internal/app/system/logic/device_preset.go
|
||||
// 生成人:gfast
|
||||
// desc:预置位
|
||||
// company:云南奇讯科技有限公司
|
||||
// ==========================================================================
|
||||
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"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/ys7"
|
||||
)
|
||||
|
||||
func init() {
|
||||
service.RegisterDevicePreset(New())
|
||||
}
|
||||
|
||||
func New() *sDevicePreset {
|
||||
return &sDevicePreset{}
|
||||
}
|
||||
|
||||
type sDevicePreset struct{}
|
||||
|
||||
func (s *sDevicePreset) List(ctx context.Context, req *system.DevicePresetSearchReq) (listRes *system.DevicePresetSearchRes, err error) {
|
||||
listRes = new(system.DevicePresetSearchRes)
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
m := dao.DevicePreset.Ctx(ctx).WithAll()
|
||||
if req.Id != "" {
|
||||
m = m.Where(dao.DevicePreset.Columns().Id+" = ?", req.Id)
|
||||
}
|
||||
if len(req.DateRange) != 0 {
|
||||
m = m.Where(dao.DevicePreset.Columns().CreatedAt+" >=? AND "+dao.DevicePreset.Columns().CreatedAt+" <=?", req.DateRange[0], req.DateRange[1])
|
||||
}
|
||||
if req.DeviceSerial != "" {
|
||||
m = m.Where(dao.DevicePreset.Columns().DeviceSerial+" = ?", req.DeviceSerial)
|
||||
}
|
||||
// if req.ChannelNo != "" {
|
||||
// m = m.Where(dao.DevicePreset.Columns().ChannelNo+" = ?", req.ChannelNo)
|
||||
// }
|
||||
|
||||
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.DevicePresetInfoRes
|
||||
err = m.Fields(system.DevicePresetSearchRes{}).Page(req.PageNum, req.PageSize).Order(order).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取数据失败")
|
||||
listRes.List = make([]*model.DevicePresetListRes, len(res))
|
||||
for k, v := range res {
|
||||
listRes.List[k] = &model.DevicePresetListRes{
|
||||
Id: v.Id,
|
||||
CreatedAt: v.CreatedAt,
|
||||
DeviceSerial: v.DeviceSerial,
|
||||
ChannelNo: v.ChannelNo,
|
||||
Index: v.Index,
|
||||
Name: v.Name,
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevicePreset) GetById(ctx context.Context, id uint) (res *model.DevicePresetInfoRes, err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
err = dao.DevicePreset.Ctx(ctx).WithAll().Where(dao.DevicePreset.Columns().Id, id).Scan(&res)
|
||||
liberr.ErrIsNil(ctx, err, "获取信息失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevicePreset) Add(ctx context.Context, req *system.DevicePresetAddReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
Device, err := ys7.GetDevice(req.DeviceSerial)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "设备不存在")
|
||||
}
|
||||
|
||||
resp, err := Device.AddPreset()
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
}
|
||||
|
||||
// 200 为成功
|
||||
if resp.Code != "200" {
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
}
|
||||
|
||||
_, err = dao.DevicePreset.Ctx(ctx).Insert(do.DevicePreset{
|
||||
DeviceSerial: req.DeviceSerial,
|
||||
ChannelNo: req.ChannelNo,
|
||||
Index: resp.Data.Index,
|
||||
Name: req.PresetName,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "添加失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevicePreset) Edit(ctx context.Context, req *system.DevicePresetEditReq) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
// 根据传入的 deviceSerial,Name,Index 修改预置点名
|
||||
dao.DevicePreset.Ctx(ctx).Where(dao.DevicePreset.Columns().DeviceSerial, req.DeviceSerial).
|
||||
Where(dao.DevicePreset.Columns().Id, req.Id).
|
||||
Update(do.DevicePreset{
|
||||
Name: req.NewName,
|
||||
})
|
||||
liberr.ErrIsNil(ctx, err, "修改失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sDevicePreset) Delete(ctx context.Context, DeviceSerial string, ids []int) (err error) {
|
||||
err = g.Try(ctx, func(ctx context.Context) {
|
||||
Device, err := ys7.GetDevice(DeviceSerial)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "设备不存在")
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
_, err := dao.DevicePreset.Ctx(ctx).Where(dao.DevicePreset.Columns().DeviceSerial, DeviceSerial).
|
||||
Where(dao.DevicePreset.Columns().Id, id).One()
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "预置位不存在")
|
||||
}
|
||||
|
||||
resp, err := Device.ClearPreset(id)
|
||||
if err != nil {
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
|
||||
if resp.Code != "200" {
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
}
|
||||
}
|
||||
|
||||
_, err = dao.DevicePreset.Ctx(ctx).Delete(dao.DevicePreset.Columns().Id+" in (?)", ids)
|
||||
liberr.ErrIsNil(ctx, err, "删除失败")
|
||||
})
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user