178 lines
5.2 KiB
Go
178 lines
5.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.hpds.cc/Component/logging"
|
|
"hpds-iot-web/config"
|
|
"hpds-iot-web/internal/proto"
|
|
"hpds-iot-web/model"
|
|
"net/http"
|
|
"time"
|
|
"xorm.io/xorm"
|
|
)
|
|
|
|
type DatasetService interface {
|
|
GetOwnerProjectList(ctx context.Context, req proto.OwnerProjectRequest) (rsp *proto.BaseResponse, err error)
|
|
DatasetList(ctx context.Context, req proto.DatasetRequest) (rsp *proto.BaseResponse, err error)
|
|
ImportDataset(ctx context.Context, req proto.ImportDatasetRequest) (rsp *proto.BaseResponse, err error)
|
|
}
|
|
|
|
func NewDatasetService(cfg *config.WebConfig, engine *xorm.Engine, logger *logging.Logger) DatasetService {
|
|
return &repo{
|
|
AppConfig: cfg,
|
|
engine: engine,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (rp *repo) GetOwnerProjectList(ctx context.Context, req proto.OwnerProjectRequest) (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:
|
|
ownerList := make([]model.Owner, 0)
|
|
err := rp.engine.Where("(? = '' or owner_name like ?)", req.Key, "%"+req.Key+"%").
|
|
And("status = 1").Find(&ownerList)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
data := make([]proto.OwnerProjectItem, len(ownerList))
|
|
for k, v := range ownerList {
|
|
projectList := make([]proto.ProjectItem, 0)
|
|
err = rp.engine.Table("project").Cols("concat('"+fmt.Sprintf("%d", v.OwnerId)+"-', project_id) as project_id", "project_name").
|
|
Where("owner_id = ?", v.OwnerId).And("status = 1").Find(&projectList)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
data[k] = proto.OwnerProjectItem{
|
|
OwnerId: v.OwnerId,
|
|
OwnerName: fmt.Sprintf("%s[%s]", v.OwnerName, v.ChargeUser),
|
|
ProjectList: projectList,
|
|
}
|
|
}
|
|
rsp.Code = http.StatusOK
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
rsp.Message = "成功"
|
|
rsp.Data = data
|
|
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) DatasetList(ctx context.Context, req proto.DatasetRequest) (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:
|
|
list := make([]model.Dataset, 0)
|
|
count, err := rp.engine.Where("(? = '' or dataset_name like ?)", req.DatasetName, "%"+req.DatasetName+"%").
|
|
And("(? = '' or create_at >= ?)", req.StartTime, req.StartTime).
|
|
And("(? = '' or create_at < ?)", req.EndTime, req.EndTime).
|
|
And("(? = 0 or owner_id = ?)", req.OwnerId, req.OwnerId).
|
|
And("(? = 0 or project_id = ?)", req.ProjectId, req.ProjectId).
|
|
And("status = 1").FindAndCount(&list)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
data := make([]proto.DatasetItem, len(list))
|
|
for k, v := range list {
|
|
detailList := make([]model.DatasetDetail, 0)
|
|
datasetCount, err := rp.engine.Where("dataset_id = ?", v.DatasetId).FindAndCount(&detailList)
|
|
if err != nil {
|
|
goto ReturnPoint
|
|
}
|
|
data[k] = proto.DatasetItem{
|
|
DatasetId: v.DatasetId,
|
|
DatasetName: v.DatasetName,
|
|
DatasetDesc: v.DatasetDesc,
|
|
StoreName: v.StoreName,
|
|
CategoryId: v.CategoryId,
|
|
ProjectId: v.ProjectId,
|
|
OwnerId: v.OwnerId,
|
|
Creator: v.Creator,
|
|
CreateAt: v.CreateAt,
|
|
DatasetCount: datasetCount,
|
|
}
|
|
}
|
|
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) ImportDataset(ctx context.Context, req proto.ImportDatasetRequest) (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.Dataset{
|
|
DatasetName: req.DatasetName,
|
|
DatasetDesc: req.DatasetDesc,
|
|
CategoryId: req.CategoryId,
|
|
ProjectId: req.ProjectId,
|
|
OwnerId: req.OwnerId,
|
|
StoreName: req.StoreName,
|
|
Creator: req.Creator,
|
|
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.Data = item
|
|
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
|
|
}
|