Files
gb28181/internal/core/ipc/channel.go
T

157 lines
4.7 KiB
Go
Executable File

// Code generated by godddx, DO AVOID EDIT.
package ipc
import (
"context"
"log/slog"
"slices"
"strconv"
"strings"
"github.com/gowvp/owl/internal/core/bz"
"github.com/ixugo/goddd/pkg/orm"
"github.com/ixugo/goddd/pkg/reason"
"github.com/jinzhu/copier"
"gorm.io/gorm"
)
// ChannelStorer Instantiation interface
type ChannelStorer interface {
Find(context.Context, *[]*Channel, orm.Pager, ...orm.QueryOption) (int64, error)
Get(context.Context, *Channel, ...orm.QueryOption) error
Add(context.Context, *Channel) error
Edit(context.Context, *Channel, func(*Channel) error, ...orm.QueryOption) error
Del(context.Context, *Channel, ...orm.QueryOption) error
BatchEdit(context.Context, string, any, ...orm.QueryOption) error // 批量更新一个字段
Session(ctx context.Context, changeFns ...func(*gorm.DB) error) error
}
// FindChannel Paginated search
func (c *Core) FindChannel(ctx context.Context, in *FindChannelInput) ([]*Channel, int64, error) {
items := make([]*Channel, 0)
query := orm.NewQuery(1)
query.OrderBy("channel_id ASC")
switch true {
case in.DID != "":
query.Where("did=?", in.DID)
case in.DeviceID != "":
query.Where("device_id = ?", in.DeviceID)
}
if in.Key != "" {
if strings.HasPrefix(in.Key, bz.IDPrefixGBChannel) {
query.Where("id=?", in.Key)
} else {
query.Where("channel_id like ? OR name like ?", "%"+in.Key+"%", "%"+in.Key+"%")
}
}
if in.IsOnline == "true" || in.IsOnline == "false" {
isOnline, _ := strconv.ParseBool(in.IsOnline)
query.Where("is_online = ?", isOnline)
}
total, err := c.store.Channel().Find(ctx, &items, in, query.Encode()...)
if err != nil {
return nil, 0, reason.ErrDB.Withf(`Find err[%s]`, err.Error())
}
return items, total, nil
}
// GetChannel Query a single object
func (c *Core) GetChannel(ctx context.Context, id string) (*Channel, error) {
var out Channel
if err := c.store.Channel().Get(ctx, &out, orm.Where("id=?", id)); err != nil {
if orm.IsErrRecordNotFound(err) {
return nil, reason.ErrNotFound.Withf(`Get err[%s]`, err.Error())
}
return nil, reason.ErrDB.Withf(`Get err[%s]`, err.Error())
}
return &out, nil
}
// AddChannel Insert into database
func (c *Core) AddChannel(ctx context.Context, in *AddChannelInput) (*Channel, error) {
var out Channel
if err := copier.Copy(&out, in); err != nil {
slog.ErrorContext(ctx, "Copy", "err", err)
}
if err := c.store.Channel().Add(ctx, &out); err != nil {
return nil, reason.ErrDB.Withf(`Add err[%s]`, err.Error())
}
return &out, nil
}
// EditChannel Update object information
func (c *Core) EditChannel(ctx context.Context, in *EditChannelInput, id string) (*Channel, error) {
// TODO: 修改 onvif 的账号/密码 后需要重新连接设备
var out Channel
if err := c.store.Channel().Edit(ctx, &out, func(b *Channel) error {
if err := copier.Copy(b, in); err != nil {
slog.ErrorContext(ctx, "Copy", "err", err)
}
return nil
}, orm.Where("id=?", id)); err != nil {
return nil, reason.ErrDB.Withf(`Edit err[%s]`, err.Error())
}
return &out, nil
}
// DelChannel Delete object
func (c *Core) DelChannel(ctx context.Context, id string) (*Channel, error) {
var out Channel
if err := c.store.Channel().Del(ctx, &out, orm.Where("id=?", id)); err != nil {
return nil, reason.ErrDB.Withf(`Del err[%s]`, err.Error())
}
return &out, nil
}
func (c *Core) AddZone(ctx context.Context, in *AddZoneInput, channelID string) (*Zone, error) {
newZone := Zone{
Name: in.Name,
Coordinates: in.Coordinates,
Color: in.Color,
Labels: in.Labels,
}
var out Channel
if err := c.store.Channel().Edit(ctx, &out, func(b *Channel) error {
if slices.ContainsFunc(b.Ext.Zones, func(z Zone) bool {
return z.Name == in.Name
}) {
return reason.ErrBadRequest.SetMsg("存在同名区域")
}
b.Ext.Zones = append(b.Ext.Zones, newZone)
return nil
}, orm.Where("id=?", channelID)); err != nil {
if reason.IsCustomError(err) {
return nil, err
}
return nil, reason.ErrDB.Withf(`Edit err[%s]`, err.Error())
}
return &newZone, nil
}
func (c *Core) GetZones(ctx context.Context, channelID string) ([]Zone, error) {
var out Channel
if err := c.store.Channel().Get(ctx, &out, orm.Where("id=?", channelID)); err != nil {
return nil, reason.ErrDB.Withf(`Get err[%s]`, err.Error())
}
return out.Ext.Zones, nil
}
// SetAIEnabled 设置通道的 AI 检测开关状态,同时返回更新后的完整通道信息供调用方使用
func (c *Core) SetAIEnabled(ctx context.Context, channelID string, enabled bool) (*Channel, error) {
var out Channel
if err := c.store.Channel().Edit(ctx, &out, func(b *Channel) error {
b.Ext.EnabledAI = enabled
return nil
}, orm.Where("id=?", channelID)); err != nil {
return nil, reason.ErrDB.Withf(`Edit err[%s]`, err.Error())
}
return &out, nil
}