209 lines
5.6 KiB
Go
209 lines
5.6 KiB
Go
|
package service
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"go.uber.org/zap"
|
||
|
"hpds-iot-web/internal/proto"
|
||
|
"hpds-iot-web/model"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
"xorm.io/xorm"
|
||
|
)
|
||
|
|
||
|
type ModelService interface {
|
||
|
ModelList(ctx context.Context, req proto.ModelRequest) (rsp *proto.BaseResponse, err error)
|
||
|
AddModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
|
||
|
EditModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
|
||
|
DelModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error)
|
||
|
}
|
||
|
|
||
|
func NewModelService(engine *xorm.Engine, logger *zap.Logger) ModelService {
|
||
|
return &repo{
|
||
|
engine: engine,
|
||
|
logger: logger,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (rp *repo) ModelList(ctx context.Context, req proto.ModelRequest) (rsp *proto.BaseResponse, err error) {
|
||
|
rsp = new(proto.BaseResponse)
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
err = fmt.Errorf("超时/取消")
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Message = "超时/取消"
|
||
|
rsp.Err = ctx.Err()
|
||
|
return rsp, ctx.Err()
|
||
|
default:
|
||
|
data := make([]model.Model, 0)
|
||
|
count, err := rp.engine.Where("(? = '' or model_name like ?)", req.ModelName, "%"+req.ModelName+"%").
|
||
|
And("status = 1").Limit(int(req.Size), int(((req.Page)-1)*req.Size)).
|
||
|
FindAndCount(&data)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
rsp.Code = http.StatusOK
|
||
|
rsp.Status = http.StatusText(http.StatusOK)
|
||
|
rsp.Message = "成功"
|
||
|
rsp = FillPaging(count, req.Page, req.Size, data, rsp)
|
||
|
rsp.Err = err
|
||
|
return rsp, err
|
||
|
}
|
||
|
ReturnPoint:
|
||
|
if err != nil {
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Err = err
|
||
|
rsp.Message = "失败"
|
||
|
}
|
||
|
return rsp, err
|
||
|
}
|
||
|
|
||
|
func (rp *repo) AddModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
|
||
|
rsp = new(proto.BaseResponse)
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
err = fmt.Errorf("超时/取消")
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Message = "超时/取消"
|
||
|
rsp.Err = ctx.Err()
|
||
|
return rsp, ctx.Err()
|
||
|
default:
|
||
|
item := &model.Model{
|
||
|
ModelName: req.ModelName,
|
||
|
ModelVersion: req.ModelVersion,
|
||
|
ModelDesc: req.ModelDesc,
|
||
|
ModelFiles: req.ModelFiles,
|
||
|
ModelParamsFiles: req.ModelParamsFiles,
|
||
|
ModelExecScript: req.ModelExecScript,
|
||
|
Status: 1,
|
||
|
CreateAt: time.Now().Unix(),
|
||
|
UpdateAt: time.Now().Unix(),
|
||
|
}
|
||
|
_, err = rp.engine.Insert(item)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
|
||
|
rsp.Code = http.StatusOK
|
||
|
rsp.Status = http.StatusText(http.StatusOK)
|
||
|
rsp.Message = "新增模型成功"
|
||
|
rsp.Err = ctx.Err()
|
||
|
rsp.Data = item
|
||
|
return rsp, err
|
||
|
}
|
||
|
ReturnPoint:
|
||
|
if err != nil {
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Err = err
|
||
|
rsp.Message = "失败"
|
||
|
}
|
||
|
return rsp, err
|
||
|
}
|
||
|
func (rp *repo) EditModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
|
||
|
rsp = new(proto.BaseResponse)
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
err = fmt.Errorf("超时/取消")
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Message = "超时/取消"
|
||
|
rsp.Err = ctx.Err()
|
||
|
return rsp, ctx.Err()
|
||
|
default:
|
||
|
var h bool
|
||
|
item := new(model.Model)
|
||
|
h, err = rp.engine.ID(req.ModelId).Get(item)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
if !h {
|
||
|
err = fmt.Errorf("未能找到对应的模型")
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
if len(req.ModelName) > 0 {
|
||
|
item.ModelName = req.ModelName
|
||
|
}
|
||
|
if len(req.ModelVersion) > 0 {
|
||
|
item.ModelVersion = req.ModelVersion
|
||
|
}
|
||
|
if len(req.ModelDesc) > 0 {
|
||
|
item.ModelDesc = req.ModelDesc
|
||
|
}
|
||
|
if len(req.ModelFiles) > 0 {
|
||
|
item.ModelFiles = req.ModelFiles
|
||
|
}
|
||
|
if len(req.ModelParamsFiles) > 0 {
|
||
|
item.ModelParamsFiles = req.ModelParamsFiles
|
||
|
}
|
||
|
if len(req.ModelExecScript) > 0 {
|
||
|
item.ModelExecScript = req.ModelExecScript
|
||
|
}
|
||
|
item.UpdateAt = time.Now().Unix()
|
||
|
_, err = rp.engine.ID(req.ModelId).AllCols().Update(item)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
rsp.Code = http.StatusOK
|
||
|
rsp.Status = http.StatusText(http.StatusOK)
|
||
|
rsp.Message = "修改模型成功"
|
||
|
rsp.Err = ctx.Err()
|
||
|
rsp.Data = item
|
||
|
return rsp, err
|
||
|
}
|
||
|
ReturnPoint:
|
||
|
if err != nil {
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Err = err
|
||
|
rsp.Message = "失败"
|
||
|
}
|
||
|
return rsp, err
|
||
|
}
|
||
|
func (rp *repo) DelModel(ctx context.Context, req proto.ModelItemRequest) (rsp *proto.BaseResponse, err error) {
|
||
|
rsp = new(proto.BaseResponse)
|
||
|
select {
|
||
|
case <-ctx.Done():
|
||
|
err = fmt.Errorf("超时/取消")
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Message = "超时/取消"
|
||
|
rsp.Err = ctx.Err()
|
||
|
return rsp, ctx.Err()
|
||
|
default:
|
||
|
var h bool
|
||
|
item := new(model.Model)
|
||
|
h, err = rp.engine.ID(req.ModelId).Get(item)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
if !h {
|
||
|
err = fmt.Errorf("未能找到对应的模型")
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
item.Status = 0
|
||
|
item.UpdateAt = time.Now().Unix()
|
||
|
_, err = rp.engine.ID(req.ModelId).AllCols().Update(item)
|
||
|
if err != nil {
|
||
|
goto ReturnPoint
|
||
|
}
|
||
|
rsp.Code = http.StatusOK
|
||
|
rsp.Status = http.StatusText(http.StatusOK)
|
||
|
rsp.Message = "删除模型成功"
|
||
|
rsp.Err = ctx.Err()
|
||
|
rsp.Data = item
|
||
|
return rsp, err
|
||
|
}
|
||
|
ReturnPoint:
|
||
|
if err != nil {
|
||
|
rsp.Code = http.StatusInternalServerError
|
||
|
rsp.Status = http.StatusText(http.StatusInternalServerError)
|
||
|
rsp.Err = err
|
||
|
rsp.Message = "失败"
|
||
|
}
|
||
|
return rsp, err
|
||
|
}
|