315 lines
9.9 KiB
Go
315 lines
9.9 KiB
Go
package dal
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
|
||
"ifms/internal/consts"
|
||
"ifms/internal/mods/autocontrol/schema"
|
||
"ifms/pkg/errors"
|
||
"ifms/pkg/gormx"
|
||
"ifms/pkg/util"
|
||
|
||
"github.com/spf13/cast"
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// Get device storage instance
|
||
func GetDeviceDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
||
return util.GetDB(ctx, defDB).Model(new(schema.Device))
|
||
}
|
||
|
||
// 设备管理
|
||
type Device struct {
|
||
DB *gorm.DB
|
||
}
|
||
|
||
// Query devices from the database based on the provided parameters and options.
|
||
func (a *Device) Query(ctx context.Context, params schema.DeviceQueryParam, opts ...schema.DeviceQueryOptions) (*schema.DeviceQueryResult, error) {
|
||
var opt schema.DeviceQueryOptions
|
||
if len(opts) > 0 {
|
||
opt = opts[0]
|
||
}
|
||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||
db := GetDeviceDB(ctx, a.DB)
|
||
db = db.Preload("DeviceFishPonds").Preload("DeviceConfigAeration").Preload("DeviceConfigFeeding").Preload("DeviceConfigWaterCirculation")
|
||
if params.DeviceName != "" {
|
||
db = db.Where("name like ?", "%"+params.DeviceName+"%")
|
||
}
|
||
if params.Type != nil && *params.Type != 0 {
|
||
db = db.Where("type=?", *params.Type)
|
||
}
|
||
var list schema.Devices
|
||
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
||
if err != nil {
|
||
return nil, errors.WithStack(err)
|
||
}
|
||
// 上面预加载了DeviceConfigAeration,DeviceConfigFeeding,DeviceConfigWaterCirculation,则设置参数默认值不为null
|
||
for k, v := range list {
|
||
if v.DeviceConfigAeration == nil {
|
||
v.DeviceConfigAeration = &schema.DeviceConfigAeration{}
|
||
}
|
||
if v.DeviceConfigFeeding == nil {
|
||
v.DeviceConfigFeeding = &schema.DeviceConfigFeeding{}
|
||
}
|
||
if v.DeviceConfigWaterCirculation == nil {
|
||
v.DeviceConfigWaterCirculation = &schema.DeviceConfigWaterCirculation{}
|
||
}
|
||
list[k] = v
|
||
}
|
||
queryResult := &schema.DeviceQueryResult{
|
||
PageResult: pageResult,
|
||
Data: list,
|
||
}
|
||
return queryResult, nil
|
||
}
|
||
|
||
// Get the specified device from the database.
|
||
func (a *Device) Get(ctx context.Context, id string, opts ...schema.DeviceQueryOptions) (*schema.Device, error) {
|
||
var opt schema.DeviceQueryOptions
|
||
if len(opts) > 0 {
|
||
opt = opts[0]
|
||
}
|
||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||
db := GetDeviceDB(ctx, a.DB)
|
||
db = db.Preload("DeviceFishPonds").Preload("DeviceConfigAeration").Preload("DeviceConfigFeeding").Preload("DeviceConfigWaterCirculation")
|
||
item := new(schema.Device)
|
||
ok, err := util.FindOne(ctx, db.Where("id=?", id), opt.QueryOptions, item)
|
||
if err != nil {
|
||
return nil, errors.WithStack(err)
|
||
} else if !ok {
|
||
return nil, nil
|
||
}
|
||
return item, nil
|
||
}
|
||
|
||
func (a *Device) GetByDeviceNo(ctx context.Context, deviceNo string, opts ...schema.DeviceQueryOptions) (*schema.Device, error) {
|
||
var opt schema.DeviceQueryOptions
|
||
if len(opts) > 0 {
|
||
opt = opts[0]
|
||
}
|
||
|
||
item := new(schema.Device)
|
||
ok, err := util.FindOne(ctx, GetDeviceDB(ctx, a.DB).Where("device_no=?", deviceNo), 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 exists in the database.
|
||
func (a *Device) Exists(ctx context.Context, id string) (bool, error) {
|
||
ok, err := util.Exists(ctx, GetDeviceDB(ctx, a.DB).Where("id=?", id))
|
||
return ok, errors.WithStack(err)
|
||
}
|
||
|
||
// Create a new device.
|
||
func (a *Device) Create(ctx context.Context, item *schema.Device) error {
|
||
result := GetDeviceDB(ctx, a.DB).Create(item)
|
||
return errors.WithStack(result.Error)
|
||
}
|
||
|
||
// Update the specified device in the database.
|
||
func (a *Device) Update(ctx context.Context, item *schema.Device) error {
|
||
result := GetDeviceDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
||
return errors.WithStack(result.Error)
|
||
}
|
||
|
||
// Delete the specified device from the database.
|
||
func (a *Device) Delete(ctx context.Context, id string) error {
|
||
result := GetDeviceDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.Device))
|
||
return errors.WithStack(result.Error)
|
||
}
|
||
|
||
func (a *Device) Statistics(ctx context.Context) ([]schema.DeviceStatistics, error) {
|
||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||
|
||
// 统计不同的设备类型数量以及在线数量(status=true)
|
||
var statistics []schema.DeviceStatistics
|
||
// 类型为1,2,3
|
||
types := []int{schema.DeviceTypeAeration, schema.DeviceTypeFeeding, schema.DeviceTypeWaterCirculation}
|
||
for _, v := range types {
|
||
// 统计不同设备类型总数
|
||
total := int64(0)
|
||
GetDeviceDB(ctx, a.DB).Where("type=?", v).Count(&total)
|
||
// 统计不同设备类型在线数量
|
||
online := int64(0)
|
||
GetDeviceDB(ctx, a.DB).Where("type=?", v).Where("status=?", true).Count(&online)
|
||
statistics = append(statistics, schema.DeviceStatistics{
|
||
Type: v,
|
||
Total: int(total),
|
||
Online: int(online),
|
||
Offline: int(total - online),
|
||
})
|
||
if total > 0 {
|
||
statistics[len(statistics)-1].Rate = int(online * 100 / total)
|
||
}
|
||
}
|
||
return statistics, nil
|
||
}
|
||
|
||
func (a *Device) SaveAerationConfig(ctx context.Context, item *schema.DeviceConfigAeration) (string, error) {
|
||
|
||
operation := ""
|
||
// 查询数据是否存在 存在对比差异,不存在直接记录操作值
|
||
record := new(schema.DeviceConfigAeration)
|
||
GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigAeration)).Where("device_id=?", item.DeviceID).First(record)
|
||
level := ""
|
||
aeration := ""
|
||
status := ""
|
||
if record.ID > 0 {
|
||
if record.Level == 1 {
|
||
level = "高"
|
||
} else if record.Level == 2 {
|
||
level = "中"
|
||
} else if record.Level == 3 {
|
||
level = "低"
|
||
}
|
||
aeration = cast.ToString(record.Aeration)
|
||
if record.Status {
|
||
status = "开"
|
||
} else {
|
||
status = "关"
|
||
}
|
||
}
|
||
|
||
newLevel := ""
|
||
if item.Level == 1 {
|
||
newLevel = "高"
|
||
} else if item.Level == 2 {
|
||
newLevel = "中"
|
||
} else if item.Level == 3 {
|
||
newLevel = "低"
|
||
}
|
||
newStatus := ""
|
||
if item.Status {
|
||
newStatus = "开"
|
||
} else {
|
||
newStatus = "关"
|
||
}
|
||
|
||
operation += fmt.Sprintf("档位: %v -> %v, ", level, newLevel)
|
||
operation += fmt.Sprintf("溶氧: %v -> %v, ", aeration, item.Aeration)
|
||
operation += fmt.Sprintf("开关: %v -> %v, ", status, newStatus)
|
||
|
||
// 先删除后新增
|
||
result2 := GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigAeration)).Where("device_id=?", item.DeviceID).Delete(new(schema.DeviceConfigAeration))
|
||
if result2.Error != nil {
|
||
return operation, errors.WithStack(result2.Error)
|
||
}
|
||
|
||
result := GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigAeration)).Create(item)
|
||
return operation, errors.WithStack(result.Error)
|
||
}
|
||
|
||
func (a *Device) SaveFeedingConfig(ctx context.Context, item *schema.DeviceConfigFeeding) (string, error) {
|
||
operation := ""
|
||
// 查询数据是否存在 存在对比差异,不存在直接记录操作值
|
||
record := new(schema.DeviceConfigFeeding)
|
||
GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigFeeding)).Where("device_id=?", item.DeviceID).First(record)
|
||
feedLevel := ""
|
||
feedTime := ""
|
||
intervalTime := ""
|
||
status := ""
|
||
if record.ID > 0 {
|
||
feedLevel = cast.ToString(record.FeedLevel)
|
||
feedTime = cast.ToString(record.FeedTime)
|
||
intervalTime = cast.ToString(record.IntervalTime)
|
||
if record.Status {
|
||
status = "开"
|
||
} else {
|
||
status = "关"
|
||
}
|
||
}
|
||
// 投料量档位 投料时间(秒) 间隔时间(秒)
|
||
operation += fmt.Sprintf("投料量档位: %v -> %v, ", feedLevel, item.FeedLevel)
|
||
operation += fmt.Sprintf("投料时间: %v -> %v, ", feedTime, item.FeedTime)
|
||
operation += fmt.Sprintf("间隔时间: %v -> %v, ", intervalTime, item.IntervalTime)
|
||
newStatus := ""
|
||
if item.Status {
|
||
newStatus = "开"
|
||
} else {
|
||
newStatus = "关"
|
||
}
|
||
operation += fmt.Sprintf("开关: %v -> %v, ", status, newStatus)
|
||
|
||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||
|
||
// 先删除后新增
|
||
result2 := GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigFeeding)).Where("device_id=?", item.DeviceID).Delete(new(schema.DeviceConfigFeeding))
|
||
if result2.Error != nil {
|
||
return operation, errors.WithStack(result2.Error)
|
||
}
|
||
result := GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigFeeding)).Create(item)
|
||
return operation, errors.WithStack(result.Error)
|
||
}
|
||
|
||
func (a *Device) SaveWaterCirculationConfig(ctx context.Context, item *schema.DeviceConfigWaterCirculation) (string, error) {
|
||
ctx = gormx.WithTenantID(ctx, consts.DefaultTenantID)
|
||
operation := ""
|
||
// 查询数据是否存在 存在对比差异,不存在直接记录操作值
|
||
record := new(schema.DeviceConfigWaterCirculation)
|
||
GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigWaterCirculation)).Where("device_id=?", item.DeviceID).First(record)
|
||
waterTemp := ""
|
||
waterCirculation := ""
|
||
aeration := ""
|
||
filter := ""
|
||
disinfection := ""
|
||
status := ""
|
||
if record.ID > 0 {
|
||
waterTemp = cast.ToString(record.WaterTemp)
|
||
waterCirculation = cast.ToString(record.WaterCirculation)
|
||
aeration = cast.ToString(record.Aeration)
|
||
if record.Filter {
|
||
filter = "开"
|
||
} else {
|
||
filter = "关"
|
||
}
|
||
if record.Disinfection {
|
||
disinfection = "开"
|
||
} else {
|
||
disinfection = "关"
|
||
}
|
||
if record.Status {
|
||
status = "开"
|
||
} else {
|
||
status = "关"
|
||
}
|
||
}
|
||
|
||
newFilter := ""
|
||
newDisinfection := ""
|
||
if item.Filter {
|
||
newFilter = "开"
|
||
} else {
|
||
newFilter = "关"
|
||
}
|
||
if item.Disinfection {
|
||
newDisinfection = "开"
|
||
} else {
|
||
newDisinfection = "关"
|
||
}
|
||
newStatus := ""
|
||
if item.Status {
|
||
newStatus = "开"
|
||
} else {
|
||
newStatus = "关"
|
||
}
|
||
operation += fmt.Sprintf("水温: %v -> %v, ", waterTemp, item.WaterTemp)
|
||
operation += fmt.Sprintf("水流: %v -> %v, ", waterCirculation, item.WaterCirculation)
|
||
operation += fmt.Sprintf("曝气: %v -> %v, ", aeration, item.Aeration)
|
||
operation += fmt.Sprintf("过滤: %v -> %v, ", filter, newFilter)
|
||
operation += fmt.Sprintf("消毒: %v -> %v, ", disinfection, newDisinfection)
|
||
operation += fmt.Sprintf("开关: %v -> %v, ", status, newStatus)
|
||
|
||
// 先删除后新增
|
||
result := GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigWaterCirculation)).Where("device_id=?", item.DeviceID).Delete(new(schema.DeviceConfigWaterCirculation))
|
||
if result.Error != nil {
|
||
return operation, errors.WithStack(result.Error)
|
||
}
|
||
result = GetDeviceDB(ctx, a.DB).Model(new(schema.DeviceConfigWaterCirculation)).Create(item)
|
||
return operation, errors.WithStack(result.Error)
|
||
}
|