feat: 自动化控制日志模块
This commit is contained in:
parent
4b5da9d2b2
commit
17ff791383
3
.gitignore
vendored
3
.gitignore
vendored
@ -31,4 +31,5 @@ data/
|
||||
|
||||
.idea/
|
||||
ifms
|
||||
gin-admin-cli
|
||||
gin-admin-cli
|
||||
.vscode/
|
3
go.mod
3
go.mod
@ -2,7 +2,7 @@ module ifms
|
||||
|
||||
go 1.23.0
|
||||
|
||||
toolchain go1.24.0
|
||||
// toolchain go1.24.0
|
||||
|
||||
require (
|
||||
github.com/BurntSushi/toml v1.2.1
|
||||
@ -33,7 +33,6 @@ require (
|
||||
github.com/redis/go-redis/v9 v9.0.4
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rs/xid v1.4.0
|
||||
github.com/shopspring/decimal v1.4.0
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/spf13/cast v1.5.1
|
||||
github.com/stretchr/testify v1.10.0
|
||||
|
2
go.sum
2
go.sum
@ -344,8 +344,6 @@ github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQ
|
||||
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
|
139
goadmin/auto_control/device_log.yaml
Normal file
139
goadmin/auto_control/device_log.yaml
Normal file
@ -0,0 +1,139 @@
|
||||
- name: DeviceRunLog
|
||||
comment: 设备运行日志表
|
||||
disable_pagination: false
|
||||
fill_gorm_commit: true
|
||||
fill_router_prefix: true
|
||||
fields:
|
||||
- name: DeviceID
|
||||
type: string
|
||||
comment: 设备ID
|
||||
gorm_tag: "size:32;index"
|
||||
form:
|
||||
binding_tag: "required,max=32"
|
||||
- name: DeviceModel
|
||||
type: string
|
||||
comment: 设备型号
|
||||
gorm_tag: "size:64"
|
||||
form:
|
||||
binding_tag: "required,max=64"
|
||||
- name: DeviceName
|
||||
type: string
|
||||
comment: 设备名称
|
||||
gorm_tag: "size:64"
|
||||
form:
|
||||
binding_tag: "required,max=64"
|
||||
- name: RelatedPondIds
|
||||
type: string
|
||||
comment: 关联鱼池/车间ID
|
||||
gorm_tag: "type:text"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: RelatedPond
|
||||
type: string
|
||||
comment: 关联鱼池/车间
|
||||
gorm_tag: "type:text"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: OperatorUserID
|
||||
type: int
|
||||
comment: 操作人ID
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: Operator
|
||||
type: string
|
||||
comment: 操作人
|
||||
gorm_tag: "size:32"
|
||||
form:
|
||||
binding_tag: "required,max=32"
|
||||
- name: DeviceLevel
|
||||
type: string
|
||||
comment: 设备档位
|
||||
gorm_tag: "size:16"
|
||||
form:
|
||||
binding_tag: "required,max=16"
|
||||
- name: OxygenContent
|
||||
type: float64
|
||||
comment: 溶解氧含量mg/l
|
||||
gorm_tag: "type:decimal(10,2)"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: DeviceStatus
|
||||
type: bool
|
||||
comment: 设备状态(true=在线,false=离线)
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: SwitchStatus
|
||||
type: bool
|
||||
comment: 开关状态(true=开,false=关)
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: Type
|
||||
type: int
|
||||
comment: 设备类型(1增氧机、2投喂机、3水循环)
|
||||
form:
|
||||
binding_tag: "required"
|
||||
|
||||
- name: DeviceControlLog
|
||||
comment: 设备操作日志表
|
||||
disable_pagination: false
|
||||
fill_gorm_commit: true
|
||||
fill_router_prefix: true
|
||||
fields:
|
||||
- name: DeviceID
|
||||
type: string
|
||||
comment: 设备ID
|
||||
gorm_tag: "size:32;index"
|
||||
form:
|
||||
binding_tag: "required,max=32"
|
||||
- name: DeviceModel
|
||||
type: string
|
||||
comment: 设备型号
|
||||
gorm_tag: "size:64"
|
||||
form:
|
||||
binding_tag: "required,max=64"
|
||||
- name: DeviceName
|
||||
type: string
|
||||
comment: 设备名称
|
||||
gorm_tag: "size:64"
|
||||
form:
|
||||
binding_tag: "required,max=64"
|
||||
- name: RelatedPondIds
|
||||
type: string
|
||||
comment: 关联鱼池/车间ID
|
||||
gorm_tag: "type:text"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: RelatedPond
|
||||
type: string
|
||||
comment: 关联鱼池/车间
|
||||
gorm_tag: "type:text"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: OperatorUserID
|
||||
type: int
|
||||
comment: 操作人ID
|
||||
gorm_tag: "index"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: Operator
|
||||
type: string
|
||||
comment: 操作人
|
||||
gorm_tag: "size:32"
|
||||
form:
|
||||
binding_tag: "required,max=32"
|
||||
- name: Command
|
||||
type: string
|
||||
comment: 下达指令
|
||||
gorm_tag: "type:text"
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: CommandStatus
|
||||
type: bool
|
||||
comment: 指令状态(true=成功,false=失败)
|
||||
form:
|
||||
binding_tag: "required"
|
||||
- name: Type
|
||||
type: int
|
||||
comment: 设备类型(1增氧机、2投喂机、3水循环)
|
||||
form:
|
||||
binding_tag: "required"
|
132
internal/mods/autocontrol/api/device_control_log.api.go
Normal file
132
internal/mods/autocontrol/api/device_control_log.api.go
Normal file
@ -0,0 +1,132 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"ifms/internal/mods/autocontrol/biz"
|
||||
"ifms/internal/mods/autocontrol/schema"
|
||||
"ifms/pkg/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 设备操作日志表
|
||||
type DeviceControlLog struct {
|
||||
DeviceControlLogBIZ *biz.DeviceControlLog
|
||||
}
|
||||
|
||||
// @Tags DeviceControlLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Query device control log list
|
||||
// @Param current query int true "pagination index" default(1)
|
||||
// @Param pageSize query int true "pagination size" default(10)
|
||||
// @Success 200 {object} util.ResponseResult{data=[]schema.DeviceControlLog}
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-control-logs [get]
|
||||
func (a *DeviceControlLog) Query(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
var params schema.DeviceControlLogQueryParam
|
||||
if err := util.ParseQuery(c, ¶ms); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.DeviceControlLogBIZ.Query(ctx, params)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResPage(c, result.Data, result.PageResult)
|
||||
}
|
||||
|
||||
// @Tags DeviceControlLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Get device control log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Success 200 {object} util.ResponseResult{data=schema.DeviceControlLog}
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-control-logs/{id} [get]
|
||||
func (a *DeviceControlLog) Get(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item, err := a.DeviceControlLogBIZ.Get(ctx, c.Param("id"))
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResSuccess(c, item)
|
||||
}
|
||||
|
||||
// @Tags DeviceControlLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Create device control log record
|
||||
// @Param body body schema.DeviceControlLogForm true "Request body"
|
||||
// @Success 200 {object} util.ResponseResult{data=schema.DeviceControlLog}
|
||||
// @Failure 400 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-control-logs [post]
|
||||
func (a *DeviceControlLog) Create(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item := new(schema.DeviceControlLogForm)
|
||||
if err := util.ParseJSON(c, item); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
} else if err := item.Validate(); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.DeviceControlLogBIZ.Create(ctx, item)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResSuccess(c, result)
|
||||
}
|
||||
|
||||
// @Tags DeviceControlLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Update device control log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Param body body schema.DeviceControlLogForm true "Request body"
|
||||
// @Success 200 {object} util.ResponseResult
|
||||
// @Failure 400 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-control-logs/{id} [put]
|
||||
func (a *DeviceControlLog) Update(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item := new(schema.DeviceControlLogForm)
|
||||
if err := util.ParseJSON(c, item); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
} else if err := item.Validate(); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := a.DeviceControlLogBIZ.Update(ctx, c.Param("id"), item)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResOK(c)
|
||||
}
|
||||
|
||||
// @Tags DeviceControlLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Delete device control log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Success 200 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-control-logs/{id} [delete]
|
||||
func (a *DeviceControlLog) Delete(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
err := a.DeviceControlLogBIZ.Delete(ctx, c.Param("id"))
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResOK(c)
|
||||
}
|
132
internal/mods/autocontrol/api/device_run_log.api.go
Normal file
132
internal/mods/autocontrol/api/device_run_log.api.go
Normal file
@ -0,0 +1,132 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"ifms/internal/mods/autocontrol/biz"
|
||||
"ifms/internal/mods/autocontrol/schema"
|
||||
"ifms/pkg/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 设备运行日志表
|
||||
type DeviceRunLog struct {
|
||||
DeviceRunLogBIZ *biz.DeviceRunLog
|
||||
}
|
||||
|
||||
// @Tags DeviceRunLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Query device run log list
|
||||
// @Param current query int true "pagination index" default(1)
|
||||
// @Param pageSize query int true "pagination size" default(10)
|
||||
// @Success 200 {object} util.ResponseResult{data=[]schema.DeviceRunLog}
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-run-logs [get]
|
||||
func (a *DeviceRunLog) Query(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
var params schema.DeviceRunLogQueryParam
|
||||
if err := util.ParseQuery(c, ¶ms); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.DeviceRunLogBIZ.Query(ctx, params)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResPage(c, result.Data, result.PageResult)
|
||||
}
|
||||
|
||||
// @Tags DeviceRunLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Get device run log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Success 200 {object} util.ResponseResult{data=schema.DeviceRunLog}
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-run-logs/{id} [get]
|
||||
func (a *DeviceRunLog) Get(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item, err := a.DeviceRunLogBIZ.Get(ctx, c.Param("id"))
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResSuccess(c, item)
|
||||
}
|
||||
|
||||
// @Tags DeviceRunLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Create device run log record
|
||||
// @Param body body schema.DeviceRunLogForm true "Request body"
|
||||
// @Success 200 {object} util.ResponseResult{data=schema.DeviceRunLog}
|
||||
// @Failure 400 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-run-logs [post]
|
||||
func (a *DeviceRunLog) Create(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item := new(schema.DeviceRunLogForm)
|
||||
if err := util.ParseJSON(c, item); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
} else if err := item.Validate(); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := a.DeviceRunLogBIZ.Create(ctx, item)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResSuccess(c, result)
|
||||
}
|
||||
|
||||
// @Tags DeviceRunLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Update device run log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Param body body schema.DeviceRunLogForm true "Request body"
|
||||
// @Success 200 {object} util.ResponseResult
|
||||
// @Failure 400 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-run-logs/{id} [put]
|
||||
func (a *DeviceRunLog) Update(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
item := new(schema.DeviceRunLogForm)
|
||||
if err := util.ParseJSON(c, item); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
} else if err := item.Validate(); err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
err := a.DeviceRunLogBIZ.Update(ctx, c.Param("id"), item)
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResOK(c)
|
||||
}
|
||||
|
||||
// @Tags DeviceRunLogAPI
|
||||
// @Security ApiKeyAuth
|
||||
// @Summary Delete device run log record by ID
|
||||
// @Param id path string true "unique id"
|
||||
// @Success 200 {object} util.ResponseResult
|
||||
// @Failure 401 {object} util.ResponseResult
|
||||
// @Failure 500 {object} util.ResponseResult
|
||||
// @Router /api/v1/autocontrol/device-run-logs/{id} [delete]
|
||||
func (a *DeviceRunLog) Delete(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
err := a.DeviceRunLogBIZ.Delete(ctx, c.Param("id"))
|
||||
if err != nil {
|
||||
util.ResError(c, err)
|
||||
return
|
||||
}
|
||||
util.ResOK(c)
|
||||
}
|
106
internal/mods/autocontrol/biz/device_control_log.biz.go
Normal file
106
internal/mods/autocontrol/biz/device_control_log.biz.go
Normal file
@ -0,0 +1,106 @@
|
||||
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
|
||||
})
|
||||
}
|
106
internal/mods/autocontrol/biz/device_run_log.biz.go
Normal file
106
internal/mods/autocontrol/biz/device_run_log.biz.go
Normal file
@ -0,0 +1,106 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"ifms/internal/mods/autocontrol/dal"
|
||||
"ifms/internal/mods/autocontrol/schema"
|
||||
"ifms/pkg/errors"
|
||||
"ifms/pkg/util"
|
||||
)
|
||||
|
||||
// 设备运行日志表
|
||||
type DeviceRunLog struct {
|
||||
Trans *util.Trans
|
||||
DeviceRunLogDAL *dal.DeviceRunLog
|
||||
}
|
||||
|
||||
// Query device run logs from the data access object based on the provided parameters and options.
|
||||
func (a *DeviceRunLog) Query(ctx context.Context, params schema.DeviceRunLogQueryParam) (*schema.DeviceRunLogQueryResult, error) {
|
||||
params.Pagination = true
|
||||
|
||||
result, err := a.DeviceRunLogDAL.Query(ctx, params, schema.DeviceRunLogQueryOptions{
|
||||
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 run log from the data access object.
|
||||
func (a *DeviceRunLog) Get(ctx context.Context, id string) (*schema.DeviceRunLog, error) {
|
||||
deviceRunLog, err := a.DeviceRunLogDAL.Get(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if deviceRunLog == nil {
|
||||
return nil, errors.NotFound("", "Device run log not found")
|
||||
}
|
||||
return deviceRunLog, nil
|
||||
}
|
||||
|
||||
// Create a new device run log in the data access object.
|
||||
func (a *DeviceRunLog) Create(ctx context.Context, formItem *schema.DeviceRunLogForm) (*schema.DeviceRunLog, error) {
|
||||
deviceRunLog := &schema.DeviceRunLog{
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
if err := formItem.FillTo(deviceRunLog); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err := a.Trans.Exec(ctx, func(ctx context.Context) error {
|
||||
if err := a.DeviceRunLogDAL.Create(ctx, deviceRunLog); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deviceRunLog, nil
|
||||
}
|
||||
|
||||
// Update the specified device run log in the data access object.
|
||||
func (a *DeviceRunLog) Update(ctx context.Context, id string, formItem *schema.DeviceRunLogForm) error {
|
||||
deviceRunLog, err := a.DeviceRunLogDAL.Get(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if deviceRunLog == nil {
|
||||
return errors.NotFound("", "Device run log not found")
|
||||
}
|
||||
|
||||
if err := formItem.FillTo(deviceRunLog); err != nil {
|
||||
return err
|
||||
}
|
||||
deviceRunLog.UpdatedAt = time.Now()
|
||||
|
||||
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
||||
if err := a.DeviceRunLogDAL.Update(ctx, deviceRunLog); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Delete the specified device run log from the data access object.
|
||||
func (a *DeviceRunLog) Delete(ctx context.Context, id string) error {
|
||||
exists, err := a.DeviceRunLogDAL.Exists(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !exists {
|
||||
return errors.NotFound("", "Device run log not found")
|
||||
}
|
||||
|
||||
return a.Trans.Exec(ctx, func(ctx context.Context) error {
|
||||
if err := a.DeviceRunLogDAL.Delete(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
91
internal/mods/autocontrol/dal/device_control_log.dal.go
Normal file
91
internal/mods/autocontrol/dal/device_control_log.dal.go
Normal file
@ -0,0 +1,91 @@
|
||||
package dal
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"ifms/internal/mods/autocontrol/schema"
|
||||
"ifms/pkg/errors"
|
||||
"ifms/pkg/util"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Get device control log storage instance
|
||||
func GetDeviceControlLogDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
|
||||
return util.GetDB(ctx, defDB).Model(new(schema.DeviceControlLog))
|
||||
}
|
||||
|
||||
// 设备操作日志表
|
||||
type DeviceControlLog struct {
|
||||
DB *gorm.DB
|
||||
}
|
||||
|
||||
// Query device control logs from the database based on the provided parameters and options.
|
||||
func (a *DeviceControlLog) Query(ctx context.Context, params schema.DeviceControlLogQueryParam, opts ...schema.DeviceControlLogQueryOptions) (*schema.DeviceControlLogQueryResult, error) {
|
||||
var opt schema.DeviceControlLogQueryOptions
|
||||
if len(opts) > 0 {
|
||||
opt = opts[0]
|
||||
}
|
||||
|
||||
db := GetDeviceControlLogDB(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.DeviceControlLogs
|
||||
pageResult, err := util.WrapPageQuery(ctx, db, params.PaginationParam, opt.QueryOptions, &list)
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
queryResult := &schema.DeviceControlLogQueryResult{
|
||||
PageResult: pageResult,
|
||||
Data: list,
|
||||
}
|
||||
return queryResult, nil
|
||||
}
|
||||
|
||||
// Get the specified device control log from the database.
|
||||
func (a *DeviceControlLog) Get(ctx context.Context, id string, opts ...schema.DeviceControlLogQueryOptions) (*schema.DeviceControlLog, error) {
|
||||
var opt schema.DeviceControlLogQueryOptions
|
||||
if len(opts) > 0 {
|
||||
opt = opts[0]
|
||||
}
|
||||
|
||||
item := new(schema.DeviceControlLog)
|
||||
ok, err := util.FindOne(ctx, GetDeviceControlLogDB(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 control log exists in the database.
|
||||
func (a *DeviceControlLog) Exists(ctx context.Context, id string) (bool, error) {
|
||||
ok, err := util.Exists(ctx, GetDeviceControlLogDB(ctx, a.DB).Where("id=?", id))
|
||||
return ok, errors.WithStack(err)
|
||||
}
|
||||
|
||||
// Create a new device control log.
|
||||
func (a *DeviceControlLog) Create(ctx context.Context, item *schema.DeviceControlLog) error {
|
||||
result := GetDeviceControlLogDB(ctx, a.DB).Create(item)
|
||||
return errors.WithStack(result.Error)
|
||||
}
|
||||
|
||||
// Update the specified device control log in the database.
|
||||
func (a *DeviceControlLog) Update(ctx context.Context, item *schema.DeviceControlLog) error {
|
||||
result := GetDeviceControlLogDB(ctx, a.DB).Where("id=?", item.ID).Select("*").Omit("created_at").Updates(item)
|
||||
return errors.WithStack(result.Error)
|
||||
}
|
||||
|
||||
// Delete the specified device control log from the database.
|
||||
func (a *DeviceControlLog) Delete(ctx context.Context, id string) error {
|
||||
result := GetDeviceControlLogDB(ctx, a.DB).Where("id=?", id).Delete(new(schema.DeviceControlLog))
|
||||
return errors.WithStack(result.Error)
|
||||
}
|
91
internal/mods/autocontrol/dal/device_run_log.dal.go
Normal file
91
internal/mods/autocontrol/dal/device_run_log.dal.go
Normal file
@ -0,0 +1,91 @@
|
||||
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)
|
||||
}
|
56
internal/mods/autocontrol/main.go
Normal file
56
internal/mods/autocontrol/main.go
Normal file
@ -0,0 +1,56 @@
|
||||
package autocontrol
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ifms/internal/config"
|
||||
|
||||
"ifms/internal/mods/autocontrol/api"
|
||||
"ifms/internal/mods/autocontrol/schema"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type AutoControl struct {
|
||||
DB *gorm.DB
|
||||
DeviceRunLogAPI *api.DeviceRunLog
|
||||
DeviceControlLogAPI *api.DeviceControlLog
|
||||
}
|
||||
|
||||
func (a *AutoControl) AutoMigrate(ctx context.Context) error {
|
||||
return a.DB.AutoMigrate(new(schema.DeviceRunLog), new(schema.DeviceControlLog))
|
||||
}
|
||||
|
||||
func (a *AutoControl) Init(ctx context.Context) error {
|
||||
if config.C.Storage.DB.AutoMigrate {
|
||||
if err := a.AutoMigrate(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *AutoControl) RegisterV1Routers(ctx context.Context, v1 *gin.RouterGroup) error {
|
||||
v1 = v1.Group("autocontrol")
|
||||
deviceRunLog := v1.Group("device-run-logs")
|
||||
{
|
||||
deviceRunLog.GET("", a.DeviceRunLogAPI.Query)
|
||||
deviceRunLog.GET(":id", a.DeviceRunLogAPI.Get)
|
||||
deviceRunLog.POST("", a.DeviceRunLogAPI.Create)
|
||||
// deviceRunLog.PUT(":id", a.DeviceRunLogAPI.Update)
|
||||
// deviceRunLog.DELETE(":id", a.DeviceRunLogAPI.Delete)
|
||||
}
|
||||
deviceControlLog := v1.Group("device-control-logs")
|
||||
{
|
||||
deviceControlLog.GET("", a.DeviceControlLogAPI.Query)
|
||||
deviceControlLog.GET(":id", a.DeviceControlLogAPI.Get)
|
||||
deviceControlLog.POST("", a.DeviceControlLogAPI.Create)
|
||||
// deviceControlLog.PUT(":id", a.DeviceControlLogAPI.Update)
|
||||
// deviceControlLog.DELETE(":id", a.DeviceControlLogAPI.Delete)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *AutoControl) Release(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
79
internal/mods/autocontrol/schema/device_control_log.go
Normal file
79
internal/mods/autocontrol/schema/device_control_log.go
Normal file
@ -0,0 +1,79 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ifms/pkg/util"
|
||||
)
|
||||
|
||||
// 设备操作日志表
|
||||
type DeviceControlLog struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement;comment:Unique ID;"` // Unique ID
|
||||
DeviceID string `json:"device_id" gorm:"size:32;index;comment:设备ID;"` // 设备ID
|
||||
DeviceModel string `json:"device_model" gorm:"size:64;comment:设备型号;"` // 设备型号
|
||||
DeviceName string `json:"device_name" gorm:"size:64;comment:设备名称;"` // 设备名称
|
||||
RelatedPondIds string `json:"related_pond_ids" gorm:"type:text;comment:关联鱼池/车间ID;"` // 关联鱼池/车间ID
|
||||
RelatedPond string `json:"related_pond" gorm:"type:text;comment:关联鱼池/车间;"` // 关联鱼池/车间
|
||||
OperatorUserID int `json:"operator_user_id" gorm:"index;comment:操作人ID;"` // 操作人ID
|
||||
Operator string `json:"operator" gorm:"size:32;comment:操作人;"` // 操作人
|
||||
Command string `json:"command" gorm:"type:text;comment:下达指令;"` // 下达指令
|
||||
CommandStatus bool `json:"command_status" gorm:"comment:指令状态(true=成功,false=失败);"` // 指令状态(true=成功,false=失败)
|
||||
Type int `json:"type" gorm:"comment:设备类型(1增氧机、2投喂机、3水循环);"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
CreatedAt time.Time `json:"created_at" gorm:"index;comment:Create time;"` // Create time
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"index;comment:Update time;"` // Update time
|
||||
}
|
||||
|
||||
// Defining the query parameters for the `DeviceControlLog` struct.
|
||||
type DeviceControlLogQueryParam struct {
|
||||
util.PaginationParam
|
||||
Type *int `form:"type"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
DeviceName string `form:"device_name"` // 设备名称(模糊查询)
|
||||
}
|
||||
|
||||
// Defining the query options for the `DeviceControlLog` struct.
|
||||
type DeviceControlLogQueryOptions struct {
|
||||
util.QueryOptions
|
||||
}
|
||||
|
||||
// Defining the query result for the `DeviceControlLog` struct.
|
||||
type DeviceControlLogQueryResult struct {
|
||||
Data DeviceControlLogs
|
||||
PageResult *util.PaginationResult
|
||||
}
|
||||
|
||||
// Defining the slice of `DeviceControlLog` struct.
|
||||
type DeviceControlLogs []*DeviceControlLog
|
||||
|
||||
// Defining the data structure for creating a `DeviceControlLog` struct.
|
||||
type DeviceControlLogForm struct {
|
||||
DeviceID string `json:"device_id" binding:"required,max=32"` // 设备ID
|
||||
DeviceModel string `json:"device_model" binding:"required,max=64"` // 设备型号
|
||||
DeviceName string `json:"device_name" binding:"required,max=64"` // 设备名称
|
||||
RelatedPondIds string `json:"related_pond_ids" binding:"required"` // 关联鱼池/车间ID
|
||||
RelatedPond string `json:"related_pond" binding:"required"` // 关联鱼池/车间
|
||||
OperatorUserID int `json:"operator_user_id" binding:"required"` // 操作人ID
|
||||
Operator string `json:"operator" binding:"required,max=32"` // 操作人
|
||||
Command string `json:"command" binding:"required"` // 下达指令
|
||||
CommandStatus bool `json:"command_status" binding:"required"` // 指令状态(true=成功,false=失败)
|
||||
Type int `json:"type" binding:"required"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
}
|
||||
|
||||
// A validation function for the `DeviceControlLogForm` struct.
|
||||
func (a *DeviceControlLogForm) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert `DeviceControlLogForm` to `DeviceControlLog` object.
|
||||
func (a *DeviceControlLogForm) FillTo(deviceControlLog *DeviceControlLog) error {
|
||||
deviceControlLog.DeviceID = a.DeviceID
|
||||
deviceControlLog.DeviceModel = a.DeviceModel
|
||||
deviceControlLog.DeviceName = a.DeviceName
|
||||
deviceControlLog.RelatedPondIds = a.RelatedPondIds
|
||||
deviceControlLog.RelatedPond = a.RelatedPond
|
||||
deviceControlLog.OperatorUserID = a.OperatorUserID
|
||||
deviceControlLog.Operator = a.Operator
|
||||
deviceControlLog.Command = a.Command
|
||||
deviceControlLog.CommandStatus = a.CommandStatus
|
||||
deviceControlLog.Type = a.Type
|
||||
return nil
|
||||
}
|
85
internal/mods/autocontrol/schema/device_run_log.go
Normal file
85
internal/mods/autocontrol/schema/device_run_log.go
Normal file
@ -0,0 +1,85 @@
|
||||
package schema
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"ifms/pkg/util"
|
||||
)
|
||||
|
||||
// 设备运行日志表
|
||||
type DeviceRunLog struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;autoIncrement;comment:Unique ID;"` // Unique ID
|
||||
DeviceID string `json:"device_id" gorm:"size:32;index;comment:设备ID;"` // 设备ID
|
||||
DeviceModel string `json:"device_model" gorm:"size:64;comment:设备型号;"` // 设备型号
|
||||
DeviceName string `json:"device_name" gorm:"size:64;comment:设备名称;"` // 设备名称
|
||||
RelatedPondIds string `json:"related_pond_ids" gorm:"type:text;comment:关联鱼池/车间ID;"` // 关联鱼池/车间ID
|
||||
RelatedPond string `json:"related_pond" gorm:"type:text;comment:关联鱼池/车间;"` // 关联鱼池/车间
|
||||
OperatorUserID int `json:"operator_user_id" gorm:"comment:操作人ID;"` // 操作人ID
|
||||
Operator string `json:"operator" gorm:"size:32;comment:操作人;"` // 操作人
|
||||
DeviceLevel string `json:"device_level" gorm:"size:16;comment:设备档位;"` // 设备档位
|
||||
OxygenContent float64 `json:"oxygen_content" gorm:"type:decimal(10,2);comment:溶解氧含量mg/l;"` // 溶解氧含量mg/l
|
||||
DeviceStatus bool `json:"device_status" gorm:"comment:设备状态(true=在线,false=离线);"` // 设备状态(true=在线,false=离线)
|
||||
SwitchStatus bool `json:"switch_status" gorm:"comment:开关状态(true=开,false=关);"` // 开关状态(true=开,false=关)
|
||||
Type int `json:"type" gorm:"comment:设备类型(1增氧机、2投喂机、3水循环);"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
CreatedAt time.Time `json:"created_at" gorm:"index;comment:Create time;"` // Create time
|
||||
UpdatedAt time.Time `json:"updated_at" gorm:"index;comment:Update time;"` // Update time
|
||||
}
|
||||
|
||||
// Defining the query parameters for the `DeviceRunLog` struct.
|
||||
type DeviceRunLogQueryParam struct {
|
||||
util.PaginationParam
|
||||
Type *int `form:"type"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
DeviceName string `form:"device_name"` // 设备名称(模糊查询)
|
||||
}
|
||||
|
||||
// Defining the query options for the `DeviceRunLog` struct.
|
||||
type DeviceRunLogQueryOptions struct {
|
||||
util.QueryOptions
|
||||
}
|
||||
|
||||
// Defining the query result for the `DeviceRunLog` struct.
|
||||
type DeviceRunLogQueryResult struct {
|
||||
Data DeviceRunLogs
|
||||
PageResult *util.PaginationResult
|
||||
}
|
||||
|
||||
// Defining the slice of `DeviceRunLog` struct.
|
||||
type DeviceRunLogs []*DeviceRunLog
|
||||
|
||||
// Defining the data structure for creating a `DeviceRunLog` struct.
|
||||
type DeviceRunLogForm struct {
|
||||
DeviceID string `json:"device_id" binding:"required,max=32"` // 设备ID
|
||||
DeviceModel string `json:"device_model" binding:"required,max=64"` // 设备型号
|
||||
DeviceName string `json:"device_name" binding:"required,max=64"` // 设备名称
|
||||
RelatedPondIds string `json:"related_pond_ids" binding:"required"` // 关联鱼池/车间ID
|
||||
RelatedPond string `json:"related_pond" binding:"required"` // 关联鱼池/车间
|
||||
OperatorUserID int `json:"operator_user_id" binding:"required"` // 操作人ID
|
||||
Operator string `json:"operator" binding:"required,max=32"` // 操作人
|
||||
DeviceLevel string `json:"device_level" binding:"required,max=16"` // 设备档位
|
||||
OxygenContent float64 `json:"oxygen_content" binding:"required"` // 溶解氧含量mg/l
|
||||
DeviceStatus bool `json:"device_status" binding:"required"` // 设备状态(true=在线,false=离线)
|
||||
SwitchStatus bool `json:"switch_status" binding:"required"` // 开关状态(true=开,false=关)
|
||||
Type int `json:"type" binding:"required"` // 设备类型(1增氧机、2投喂机、3水循环)
|
||||
}
|
||||
|
||||
// A validation function for the `DeviceRunLogForm` struct.
|
||||
func (a *DeviceRunLogForm) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Convert `DeviceRunLogForm` to `DeviceRunLog` object.
|
||||
func (a *DeviceRunLogForm) FillTo(deviceRunLog *DeviceRunLog) error {
|
||||
deviceRunLog.DeviceID = a.DeviceID
|
||||
deviceRunLog.DeviceModel = a.DeviceModel
|
||||
deviceRunLog.DeviceName = a.DeviceName
|
||||
deviceRunLog.RelatedPondIds = a.RelatedPondIds
|
||||
deviceRunLog.RelatedPond = a.RelatedPond
|
||||
deviceRunLog.OperatorUserID = a.OperatorUserID
|
||||
deviceRunLog.Operator = a.Operator
|
||||
deviceRunLog.DeviceLevel = a.DeviceLevel
|
||||
deviceRunLog.OxygenContent = a.OxygenContent
|
||||
deviceRunLog.DeviceStatus = a.DeviceStatus
|
||||
deviceRunLog.SwitchStatus = a.SwitchStatus
|
||||
deviceRunLog.Type = a.Type
|
||||
return nil
|
||||
}
|
19
internal/mods/autocontrol/wire.go
Normal file
19
internal/mods/autocontrol/wire.go
Normal file
@ -0,0 +1,19 @@
|
||||
package autocontrol
|
||||
|
||||
import (
|
||||
"ifms/internal/mods/autocontrol/api"
|
||||
"ifms/internal/mods/autocontrol/biz"
|
||||
"ifms/internal/mods/autocontrol/dal"
|
||||
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
var Set = wire.NewSet(
|
||||
wire.Struct(new(AutoControl), "*"),
|
||||
wire.Struct(new(dal.DeviceRunLog), "*"),
|
||||
wire.Struct(new(biz.DeviceRunLog), "*"),
|
||||
wire.Struct(new(api.DeviceRunLog), "*"),
|
||||
wire.Struct(new(dal.DeviceControlLog), "*"),
|
||||
wire.Struct(new(biz.DeviceControlLog), "*"),
|
||||
wire.Struct(new(api.DeviceControlLog), "*"),
|
||||
)
|
@ -3,10 +3,13 @@ package mods
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/wire"
|
||||
"ifms/internal/mods/digital"
|
||||
"ifms/internal/mods/rbac"
|
||||
|
||||
"ifms/internal/mods/autocontrol"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/wire"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -18,11 +21,13 @@ var Set = wire.NewSet(
|
||||
wire.Struct(new(Mods), "*"),
|
||||
rbac.Set,
|
||||
digital.Set,
|
||||
autocontrol.Set,
|
||||
)
|
||||
|
||||
type Mods struct {
|
||||
RBAC *rbac.RBAC
|
||||
DIGITAL *digital.DIGITAL
|
||||
RBAC *rbac.RBAC
|
||||
DIGITAL *digital.DIGITAL
|
||||
AutoControl *autocontrol.AutoControl
|
||||
}
|
||||
|
||||
func (a *Mods) Init(ctx context.Context) error {
|
||||
@ -32,6 +37,9 @@ func (a *Mods) Init(ctx context.Context) error {
|
||||
if err := a.DIGITAL.Init(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AutoControl.Init(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -52,6 +60,9 @@ func (a *Mods) RegisterRouters(ctx context.Context, e *gin.Engine) error {
|
||||
if err := a.DIGITAL.RegisterV1Routers(ctx, v1); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AutoControl.RegisterV1Routers(ctx, v1); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -63,6 +74,10 @@ func (a *Mods) Release(ctx context.Context) error {
|
||||
if err := a.DIGITAL.Release(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := a.AutoControl.Release(
|
||||
ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"ifms/internal/mods/rbac/biz"
|
||||
"ifms/internal/mods/rbac/schema"
|
||||
"ifms/pkg/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -3,9 +3,6 @@ package biz
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/LyricTian/captcha"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
"ifms/internal/config"
|
||||
"ifms/internal/consts"
|
||||
"ifms/internal/mods/rbac/dal"
|
||||
@ -21,6 +18,10 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/LyricTian/captcha"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Login management for RBAC
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Code generated by Wire. DO NOT EDIT.
|
||||
|
||||
//go:generate go run -mod=mod github.com/google/wire/cmd/wire
|
||||
//go:generate go run github.com/google/wire/cmd/wire
|
||||
//go:build !wireinject
|
||||
// +build !wireinject
|
||||
|
||||
@ -9,10 +9,14 @@ package wirex
|
||||
import (
|
||||
"context"
|
||||
"ifms/internal/mods"
|
||||
"ifms/internal/mods/autocontrol"
|
||||
api3 "ifms/internal/mods/autocontrol/api"
|
||||
biz3 "ifms/internal/mods/autocontrol/biz"
|
||||
dal3 "ifms/internal/mods/autocontrol/dal"
|
||||
"ifms/internal/mods/digital"
|
||||
api5 "ifms/internal/mods/digital/api"
|
||||
biz4 "ifms/internal/mods/digital/biz"
|
||||
dal4 "ifms/internal/mods/digital/dal"
|
||||
api2 "ifms/internal/mods/digital/api"
|
||||
biz2 "ifms/internal/mods/digital/biz"
|
||||
dal2 "ifms/internal/mods/digital/dal"
|
||||
"ifms/internal/mods/rbac"
|
||||
"ifms/internal/mods/rbac/api"
|
||||
"ifms/internal/mods/rbac/biz"
|
||||
@ -123,24 +127,49 @@ func BuildInjector(ctx context.Context) (*Injector, func(), error) {
|
||||
LoggerAPI: apiLogger,
|
||||
Casbinx: casbinx,
|
||||
}
|
||||
|
||||
diagram := &dal4.Diagram{
|
||||
diagram := &dal2.Diagram{
|
||||
DB: db,
|
||||
}
|
||||
bizDiagram := &biz4.Diagram{
|
||||
bizDiagram := &biz2.Diagram{
|
||||
Trans: trans,
|
||||
DiagramDAL: diagram,
|
||||
}
|
||||
apiDiagram := &api5.Diagram{
|
||||
apiDiagram := &api2.Diagram{
|
||||
DiagramBIZ: bizDiagram,
|
||||
}
|
||||
digitalDIGITAL := &digital.DIGITAL{
|
||||
DB: db,
|
||||
DiagramAPI: apiDiagram,
|
||||
}
|
||||
deviceRunLog := &dal3.DeviceRunLog{
|
||||
DB: db,
|
||||
}
|
||||
bizDeviceRunLog := &biz3.DeviceRunLog{
|
||||
Trans: trans,
|
||||
DeviceRunLogDAL: deviceRunLog,
|
||||
}
|
||||
apiDeviceRunLog := &api3.DeviceRunLog{
|
||||
DeviceRunLogBIZ: bizDeviceRunLog,
|
||||
}
|
||||
deviceControlLog := &dal3.DeviceControlLog{
|
||||
DB: db,
|
||||
}
|
||||
bizDeviceControlLog := &biz3.DeviceControlLog{
|
||||
Trans: trans,
|
||||
DeviceControlLogDAL: deviceControlLog,
|
||||
}
|
||||
apiDeviceControlLog := &api3.DeviceControlLog{
|
||||
DeviceControlLogBIZ: bizDeviceControlLog,
|
||||
}
|
||||
autoControl := &autocontrol.AutoControl{
|
||||
DB: db,
|
||||
DeviceRunLogAPI: apiDeviceRunLog,
|
||||
DeviceControlLogAPI: apiDeviceControlLog,
|
||||
}
|
||||
modsMods := &mods.Mods{
|
||||
RBAC: rbacRBAC,
|
||||
DIGITAL: digitalDIGITAL,
|
||||
RBAC: rbacRBAC,
|
||||
DIGITAL: digitalDIGITAL,
|
||||
AutoControl: autoControl,
|
||||
}
|
||||
injector := &Injector{
|
||||
DB: db,
|
||||
|
@ -334,8 +334,8 @@ func ExecCommand(timeout time.Duration, name string, args ...string) (result []b
|
||||
cmd := exec.Command(name, args...)
|
||||
|
||||
cmd.SysProcAttr = &syscall.SysProcAttr{
|
||||
Setpgid: true,
|
||||
Pgid: 0,
|
||||
// Setpgid: true,
|
||||
// Pgid: 0,
|
||||
}
|
||||
|
||||
stdin, err := cmd.StdinPipe()
|
||||
|
@ -1,26 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"ifms/internal/mods/trace/dal"
|
||||
"ifms/internal/wirex"
|
||||
"ifms/pkg/util"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlanStatus(t *testing.T) {
|
||||
db, _, err := wirex.InitDB(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
plan := &dal.DailyTask{DB: db}
|
||||
data, err := plan.GetDailyTasksStatus(context.Background(), "123", "")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
format, err := util.MarshalWithTimeFormat(data, "2006-01-02 15:04:05")
|
||||
fmt.Println(format)
|
||||
fmt.Println(data)
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"ifms/internal/mods/trace/dal"
|
||||
"ifms/internal/mods/trace/schema"
|
||||
"ifms/internal/wirex"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPlan(t *testing.T) {
|
||||
|
||||
db, _, err := wirex.InitDB(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
plan := dal.BreedingPlan{DB: db}
|
||||
|
||||
param := schema.BreedingPlanForm{BreedingVariety: "test", FishSource: "test", FishStage: "test"}
|
||||
|
||||
list, err := plan.QueryList(context.Background(), param)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println(len(list))
|
||||
}
|
||||
|
||||
func TestBatchNum(t *testing.T) {
|
||||
var todayList []dal.BreedingPlan
|
||||
i := len(todayList)
|
||||
// 获取当前时间
|
||||
now := time.Now()
|
||||
// 格式化年份为两位数(例如 2025 -> 25)
|
||||
year := now.Year() % 100
|
||||
yearStr := fmt.Sprintf("%02d", year)
|
||||
// 格式化日期为 "0406" 的格式
|
||||
dateStr := now.Format("0102")
|
||||
// 格式化序号为两位数
|
||||
sequenceStr := fmt.Sprintf("%02d", i+1)
|
||||
// 拼接最终的字符串
|
||||
fmt.Println(fmt.Sprintf("%s年%s%s%s批", yearStr, "鲈鱼", dateStr, sequenceStr))
|
||||
}
|
@ -1,189 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
jsontime "github.com/liamylian/jsontime/v2/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"ifms/internal/mods/rbac/schema"
|
||||
trace_schema "ifms/internal/mods/trace/schema"
|
||||
"ifms/pkg/convert"
|
||||
"ifms/pkg/crypto/hash"
|
||||
"ifms/pkg/util"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUser(t *testing.T) {
|
||||
e := tester(t)
|
||||
|
||||
menuFormItem := schema.MenuForm{
|
||||
Code: "user",
|
||||
Name: "User management",
|
||||
Description: "User management",
|
||||
Sequence: 7,
|
||||
Type: "page",
|
||||
Path: "/system/user",
|
||||
Properties: `{"icon":"user"}`,
|
||||
Status: schema.MenuStatusEnabled,
|
||||
}
|
||||
|
||||
var menu schema.Menu
|
||||
e.POST(baseAPI + "/menus").WithJSON(menuFormItem).
|
||||
Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &menu})
|
||||
|
||||
assert := assert.New(t)
|
||||
assert.NotEmpty(menu.ID)
|
||||
assert.Equal(menuFormItem.Code, menu.Code)
|
||||
assert.Equal(menuFormItem.Name, menu.Name)
|
||||
assert.Equal(menuFormItem.Description, menu.Description)
|
||||
assert.Equal(menuFormItem.Sequence, menu.Sequence)
|
||||
assert.Equal(menuFormItem.Type, menu.Type)
|
||||
assert.Equal(menuFormItem.Path, menu.Path)
|
||||
assert.Equal(menuFormItem.Properties, menu.Properties)
|
||||
assert.Equal(menuFormItem.Status, menu.Status)
|
||||
|
||||
roleFormItem := schema.RoleForm{
|
||||
Code: "user",
|
||||
Name: "Normal",
|
||||
//Menus: schema.RoleMenus{
|
||||
// {MenuID: menu.ID},
|
||||
//},
|
||||
Description: "Normal",
|
||||
Sequence: 8,
|
||||
}
|
||||
|
||||
var role schema.Role
|
||||
e.POST(baseAPI + "/roles").WithJSON(roleFormItem).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &role})
|
||||
assert.NotEmpty(role.ID)
|
||||
assert.Equal(roleFormItem.Code, role.Code)
|
||||
assert.Equal(roleFormItem.Name, role.Name)
|
||||
assert.Equal(roleFormItem.Description, role.Description)
|
||||
assert.Equal(roleFormItem.Sequence, role.Sequence)
|
||||
//assert.Equal(len(roleFormItem.Menus), len(role.Menus))
|
||||
|
||||
userFormItem := schema.UserForm{
|
||||
Username: "test",
|
||||
Name: "Test",
|
||||
Password: hash.MD5String("test"),
|
||||
Phone: "0720",
|
||||
Email: "test@gmail.com",
|
||||
Remark: "test user",
|
||||
Roles: schema.UserRoles{{RoleID: role.ID}},
|
||||
}
|
||||
|
||||
var user schema.User
|
||||
e.POST(baseAPI + "/users").WithJSON(userFormItem).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &user})
|
||||
assert.NotEmpty(user.ID)
|
||||
assert.Equal(userFormItem.Username, user.Username)
|
||||
assert.Equal(userFormItem.Name, user.Name)
|
||||
assert.Equal(userFormItem.Phone, user.Phone)
|
||||
assert.Equal(userFormItem.Email, user.Email)
|
||||
assert.Equal(userFormItem.Remark, user.Remark)
|
||||
assert.Equal(len(userFormItem.Roles), len(user.Roles))
|
||||
|
||||
var users schema.Users
|
||||
e.GET(baseAPI+"/users").WithQuery("username", userFormItem.Username).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &users})
|
||||
assert.GreaterOrEqual(len(users), 1)
|
||||
|
||||
newName := "Test 1"
|
||||
newStatus := schema.UserStatusFreezed
|
||||
user.Name = newName
|
||||
e.PUT(baseAPI + "/users/" + convert.ToString(user.ID)).WithJSON(user).Expect().Status(http.StatusOK)
|
||||
|
||||
var getUser schema.User
|
||||
e.GET(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusOK).JSON().Decode(&util.ResponseResult{Data: &getUser})
|
||||
assert.Equal(newName, getUser.Name)
|
||||
assert.Equal(newStatus, getUser.Status)
|
||||
|
||||
e.DELETE(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusOK)
|
||||
e.GET(baseAPI + "/users/" + convert.ToString(user.ID)).Expect().Status(http.StatusNotFound)
|
||||
|
||||
e.DELETE(baseAPI + "/roles/" + convert.ToString(role.ID)).Expect().Status(http.StatusOK)
|
||||
e.GET(baseAPI + "/roles/" + convert.ToString(role.ID)).Expect().Status(http.StatusNotFound)
|
||||
|
||||
e.DELETE(baseAPI + "/menus/" + menu.ID).Expect().Status(http.StatusOK)
|
||||
e.GET(baseAPI + "/menus/" + menu.ID).Expect().Status(http.StatusNotFound)
|
||||
}
|
||||
func TestTenant(t *testing.T) {
|
||||
//db, _, err := wirex.InitDB(context.Background())
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
//}
|
||||
//user := &dal.User{DB: db}
|
||||
//user.Get(context.Background(), 1, schema.UserQueryOptions{})
|
||||
|
||||
md5 := util.EncryptToMD5("111111")
|
||||
fmt.Println(md5)
|
||||
|
||||
//dt := "2020-11-08T08:18:46Z"
|
||||
//date, err := util.SimpleCSTDate(dt)
|
||||
//
|
||||
//if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
//}
|
||||
//fmt.Println(date)
|
||||
}
|
||||
|
||||
type MyStruct struct {
|
||||
Timestamp trace_schema.CustomTime `json:"timestamp"`
|
||||
}
|
||||
|
||||
func TestJSON(t *testing.T) {
|
||||
// 示例 JSON 数据
|
||||
jsonData := `{"timestamp": "2020-11-08T08:18:46Z"}`
|
||||
|
||||
// 反序列化 JSON 数据
|
||||
var data MyStruct
|
||||
if err := json.Unmarshal([]byte(jsonData), &data); err != nil {
|
||||
fmt.Println("Error unmarshaling JSON:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Unmarshaled time:", data.Timestamp.Time) // 输出: 2020-11-08 08:18:46 +0000 UTC
|
||||
|
||||
// 序列化回 JSON
|
||||
jsonDataOut, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling JSON:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Marshaled JSON:", string(jsonDataOut)) // 输出: {"timestamp":"2020-11-08 08:18:46"}
|
||||
now := time.Now()
|
||||
data = MyStruct{trace_schema.CustomTime{Time: now}}
|
||||
//data.Timestamp.Time = time.Now()
|
||||
fmt.Println(data)
|
||||
|
||||
}
|
||||
|
||||
type Factory struct {
|
||||
ID int64 `json:"id" gorm:"primaryKey;comment:Unique ID;"` // Unique ID
|
||||
TenantId string `json:"tenant_id" gorm:"size:36;comment:租户ID;"` // 租户ID
|
||||
Address string `json:"address" gorm:"size:255;comment:工厂地址;"` // 工厂地址
|
||||
WorkShopLayout string `json:"work_shop_layout" gorm:"type:text;comment:工厂布局;"` // 工厂布局
|
||||
Notes string `json:"notes" gorm:"size:1024;comment:备注说明;"` // 备注说明
|
||||
Operator string `json:"operator" gorm:"size:50;comment:操作人;"` // 操作人
|
||||
CreatedAt time.Time `json:"created_at" gorm:"index;comment:Upload time;"` // Upload time
|
||||
}
|
||||
|
||||
func TestJSON1(t *testing.T) {
|
||||
jsonData := &Factory{
|
||||
ID: 1,
|
||||
TenantId: "888",
|
||||
Address: "1号车间",
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
//b, _ := util.MarshalWithTimeFormat(jsonData, "2006-01-02 15:04:05")
|
||||
//fmt.Println(string(b))
|
||||
|
||||
//fromInt := decimal.NewFromInt(1)
|
||||
//fmt.Println(fromInt)
|
||||
|
||||
jsontime.SetDefaultTimeFormat(time.DateTime, time.Local)
|
||||
fmt.Println(time.Local)
|
||||
var json = jsontime.ConfigWithCustomTimeFormat
|
||||
bytes, _ := json.Marshal(jsonData)
|
||||
fmt.Printf("%s", bytes)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user