2023-01-13 11:26:39 +08:00
|
|
|
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)
|
2023-04-24 15:21:17 +08:00
|
|
|
DatasetInfo(ctx context.Context, req proto.DatasetItemRequest) (rsp *proto.BaseResponse, err error)
|
2023-01-13 11:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-04-24 15:21:17 +08:00
|
|
|
st := rp.engine.Where("(? = '' or dataset_name like ?)", req.DatasetName, "%"+req.DatasetName+"%").
|
2023-01-13 11:26:39 +08:00
|
|
|
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).
|
2023-04-24 15:21:17 +08:00
|
|
|
And("status = 1")
|
|
|
|
if len(req.ProjectId) > 0 {
|
|
|
|
st.In("project_id", req.ProjectId)
|
|
|
|
}
|
|
|
|
count, err := st.Limit(int(req.Size), int(((req.Page)-1)*req.Size)).FindAndCount(&list)
|
2023-01-13 11:26:39 +08:00
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
data := make([]proto.DatasetItem, len(list))
|
|
|
|
for k, v := range list {
|
2023-03-24 08:42:28 +08:00
|
|
|
detailList := make([]model.FileManager, 0)
|
2023-04-24 15:21:17 +08:00
|
|
|
datasetCount, err := rp.engine.Where("dataset_id = ?", v.DatasetId).And("data_type=1").FindAndCount(&detailList)
|
2023-01-13 11:26:39 +08:00
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
2023-03-24 08:42:28 +08:00
|
|
|
fm := new(model.FileManager)
|
2023-04-24 15:21:17 +08:00
|
|
|
datasetSize, err := rp.engine.Where("dataset_id = ?", v.DatasetId).And("data_type=1").SumInt(fm, "file_size")
|
2023-03-24 08:42:28 +08:00
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
2023-01-13 11:26:39 +08:00
|
|
|
data[k] = proto.DatasetItem{
|
|
|
|
DatasetId: v.DatasetId,
|
|
|
|
DatasetName: v.DatasetName,
|
|
|
|
DatasetDesc: v.DatasetDesc,
|
2023-03-23 18:03:09 +08:00
|
|
|
StoreName: v.StoreName,
|
2023-01-13 11:26:39 +08:00
|
|
|
CategoryId: v.CategoryId,
|
|
|
|
ProjectId: v.ProjectId,
|
|
|
|
OwnerId: v.OwnerId,
|
|
|
|
Creator: v.Creator,
|
|
|
|
CreateAt: v.CreateAt,
|
|
|
|
DatasetCount: datasetCount,
|
2023-03-24 08:42:28 +08:00
|
|
|
DatasetSize: datasetSize,
|
2023-01-13 11:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
2023-03-23 18:03:09 +08:00
|
|
|
StoreName: req.StoreName,
|
2023-01-13 11:26:39 +08:00
|
|
|
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
|
|
|
|
}
|
2023-04-24 15:21:17 +08:00
|
|
|
|
|
|
|
func (rp *repo) DatasetInfo(ctx context.Context, req proto.DatasetItemRequest) (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 := new(model.Dataset)
|
|
|
|
var b bool
|
|
|
|
b, err = rp.engine.ID(req.DatasetId).Get(item)
|
|
|
|
if err != nil {
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
if !b {
|
|
|
|
err = fmt.Errorf("未能找到对应的数据集")
|
|
|
|
goto ReturnPoint
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Code = http.StatusOK
|
|
|
|
rsp.Status = http.StatusText(http.StatusOK)
|
|
|
|
rsp.Message = "成功"
|
|
|
|
rsp.Err = 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
|
|
|
|
}
|