107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package biz
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"ifms/internal/mods/autocontrol/dal"
|
|
"ifms/internal/mods/autocontrol/schema"
|
|
"ifms/pkg/errors"
|
|
"ifms/pkg/util"
|
|
)
|
|
|
|
// 设备操作日志表
|
|
type DeviceControlLog struct {
|
|
Trans *util.Trans
|
|
DeviceControlLogDAL *dal.DeviceControlLog
|
|
}
|
|
|
|
// Query device control logs from the data access object based on the provided parameters and options.
|
|
func (a *DeviceControlLog) Query(ctx context.Context, params schema.DeviceControlLogQueryParam) (*schema.DeviceControlLogQueryResult, error) {
|
|
params.Pagination = true
|
|
|
|
result, err := a.DeviceControlLogDAL.Query(ctx, params, schema.DeviceControlLogQueryOptions{
|
|
QueryOptions: util.QueryOptions{
|
|
OrderFields: []util.OrderByParam{
|
|
{Field: "created_at", Direction: util.DESC},
|
|
},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// Get the specified device control log from the data access object.
|
|
func (a *DeviceControlLog) Get(ctx context.Context, id string) (*schema.DeviceControlLog, error) {
|
|
deviceControlLog, err := a.DeviceControlLogDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
} else if deviceControlLog == nil {
|
|
return nil, errors.NotFound("", "Device control log not found")
|
|
}
|
|
return deviceControlLog, nil
|
|
}
|
|
|
|
// Create a new device control log in the data access object.
|
|
func (a *DeviceControlLog) Create(ctx context.Context, formItem *schema.DeviceControlLogForm) (*schema.DeviceControlLog, error) {
|
|
deviceControlLog := &schema.DeviceControlLog{
|
|
CreatedAt: time.Now(),
|
|
}
|
|
|
|
if err := formItem.FillTo(deviceControlLog); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.DeviceControlLogDAL.Create(ctx, deviceControlLog); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return deviceControlLog, nil
|
|
}
|
|
|
|
// Update the specified device control log in the data access object.
|
|
func (a *DeviceControlLog) Update(ctx context.Context, id string, formItem *schema.DeviceControlLogForm) error {
|
|
deviceControlLog, err := a.DeviceControlLogDAL.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if deviceControlLog == nil {
|
|
return errors.NotFound("", "Device control log not found")
|
|
}
|
|
|
|
if err := formItem.FillTo(deviceControlLog); err != nil {
|
|
return err
|
|
}
|
|
deviceControlLog.UpdatedAt = time.Now()
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.DeviceControlLogDAL.Update(ctx, deviceControlLog); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|
|
|
|
// Delete the specified device control log from the data access object.
|
|
func (a *DeviceControlLog) Delete(ctx context.Context, id string) error {
|
|
exists, err := a.DeviceControlLogDAL.Exists(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
} else if !exists {
|
|
return errors.NotFound("", "Device control log not found")
|
|
}
|
|
|
|
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
|
if err := a.DeviceControlLogDAL.Delete(ctx, id); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|