ifms_go/internal/mods/autocontrol/dal/device_run_log.dal.go

92 lines
2.7 KiB
Go

package dal
import (
"context"
"ifms/internal/mods/autocontrol/schema"
"ifms/pkg/errors"
"ifms/pkg/util"
"gorm.io/gorm"
)
// Get device run log storage instance
func GetDeviceRunLogDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
return util.GetDB(ctx, defDB).Model(new(schema.DeviceRunLog))
}
// 设备运行日志表
type DeviceRunLog struct {
DB *gorm.DB
}
// Query device run logs from the database based on the provided parameters and options.
func (a *DeviceRunLog) Query(ctx context.Context, params schema.DeviceRunLogQueryParam, opts ...schema.DeviceRunLogQueryOptions) (*schema.DeviceRunLogQueryResult, error) {
var opt schema.DeviceRunLogQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
db := GetDeviceRunLogDB(ctx, a.DB)
if v := params.Type; v != nil {
db = db.Where("type = ?", v)
}
if v := params.DeviceName; v != "" {
db = db.Where("device_name LIKE ?", "%"+v+"%")
}
var list schema.DeviceRunLogs
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
if err != nil {
return nil, errors.WithStack(err)
}
queryResult := &schema.DeviceRunLogQueryResult{
PageResult: pageResult,
Data: list,
}
return queryResult, nil
}
// Get the specified device run log from the database.
func (a *DeviceRunLog) Get(ctx context.Context, id string, opts ...schema.DeviceRunLogQueryOptions) (*schema.DeviceRunLog, error) {
var opt schema.DeviceRunLogQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
item := new(schema.DeviceRunLog)
ok, err := util.FindOne(ctx, GetDeviceRunLogDB(ctx, a.DB).Where("id=?", id), opt.QueryOptions, item)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return item, nil
}
// Exists checks if the specified device run log exists in the database.
func (a *DeviceRunLog) Exists(ctx context.Context, id string) (bool, error) {
ok, err := util.Exists(ctx, GetDeviceRunLogDB(ctx, a.DB).Where("id=?", id))
return ok, errors.WithStack(err)
}
// Create a new device run log.
func (a *DeviceRunLog) Create(ctx context.Context, item *schema.DeviceRunLog) error {
result := GetDeviceRunLogDB(ctx, a.DB).Create(item)
return errors.WithStack(result.Error)
}
// Update the specified device run log in the database.
func (a *DeviceRunLog) Update(ctx context.Context, item *schema.DeviceRunLog) error {
result := GetDeviceRunLogDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
return errors.WithStack(result.Error)
}
// Delete the specified device run log from the database.
func (a *DeviceRunLog) Delete(ctx context.Context, id string) error {
result := GetDeviceRunLogDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.DeviceRunLog))
return errors.WithStack(result.Error)
}