瀏覽代碼

feat: 仿真测试记录查询&下载

HeWang 8 月之前
父節點
當前提交
9f4974cd7f

+ 16 - 0
biz/dal/mysql/common.go

@@ -0,0 +1,16 @@
+package mysql
+
+import (
+	"gorm.io/gorm"
+)
+
+func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB {
+	return func(db *gorm.DB) *gorm.DB {
+		if page <= 0 {
+			page = 1
+		}
+
+		offset := (page - 1) * pageSize
+		return db.Offset(offset).Limit(pageSize)
+	}
+}

+ 4 - 1
biz/dal/mysql/init.go

@@ -3,6 +3,7 @@ package mysql
 import (
 	"gorm.io/driver/mysql"
 	"gorm.io/gorm"
+	"gorm.io/gorm/logger"
 	"pji_desktop_http/biz/dal/query"
 	"time"
 )
@@ -14,7 +15,9 @@ var db *gorm.DB
 
 func Init() {
 	var err error
-	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
+	db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
+		Logger: logger.Default.LogMode(logger.Info),
+	})
 	if err != nil {
 		panic(err)
 	}

+ 29 - 6
biz/dal/mysql/simulation_test_record.go

@@ -3,7 +3,6 @@ package mysql
 import (
 	"context"
 	"fmt"
-	"gorm.io/gen/field"
 	"pji_desktop_http/biz/dal/query"
 	"pji_desktop_http/biz/model"
 )
@@ -25,13 +24,37 @@ func AddSimulationTestRecords(ctx context.Context, records []*model.SimulationTe
 //	return records, err
 //}
 
-func QuerySimulationTestRecords(ctx context.Context, record *model.SimulationTestRecord) ([]*model.SimulationTestRecord, error) {
+func QuerySimulationTestRecords(ctx context.Context, record *model.SimulationTestRecord, pageFlag bool, page, pageSize int) ([]*model.SimulationTestRecord, int64, error) {
 	r := query.SimulationTestRecord
-	records, err := r.WithContext(ctx).Where(field.Attrs(record)).Order(r.Round.Asc()).Find()
+	q := r.WithContext(ctx)
+	var records []*model.SimulationTestRecord
+	var count int64
+	var err error
+	if record.DeviceType != "" {
+		q = q.Where(r.DeviceType.Eq(record.DeviceType))
+	}
+	if record.AlgoEvaluationLevel != "" {
+		fmt.Println("AlgoEvaluationLevel", record.AlgoEvaluationLevel)
+		q = q.Where(r.AlgoEvaluationLevel.Eq(record.AlgoEvaluationLevel))
+	}
+	if record.DeviceName != "" {
+		q = q.Where(r.DeviceName.Like("%" + record.DeviceName + "%"))
+	}
+	if record.AlgoImageName != "" {
+		q = q.Where(r.AlgoImageName.Like("%" + record.AlgoImageName + "%"))
+	}
+	if pageFlag {
+		offset := (page - 1) * pageSize
+		records, count, err = q.Order(r.TestTime.Desc(), r.Round.Asc()).FindByPage(offset, pageSize)
+	} else {
+		records, err = q.Order(r.TestTime.Desc(), r.Round.Asc()).Find()
+	}
+
 	if err != nil {
 		fmt.Println("Query simulation records failed:", err.Error())
-		return nil, err
+		return nil, 0, err
 	}
-	fmt.Print("Query simulation records successfully:", records)
-	return records, err
+
+	fmt.Println("Query simulation records successfully:", len(records))
+	return records, count, err
 }

+ 15 - 4
biz/handler/simulation_service/simulation_service.go

@@ -342,17 +342,28 @@ func QueryTestRecord(ctx context.Context, c *app.RequestContext) {
 	err := c.BindAndValidate(&record)
 	fmt.Println("record", record)
 
-	records, err := mysql.QuerySimulationTestRecords(ctx, &record)
+	var pageFlag bool
+	if c.Query("page") != "" && c.Query("pageSize") != "" {
+		pageFlag = true
+	} else {
+		pageFlag = false
+	}
+
+	page, _ := strconv.Atoi(c.Query("page"))
+	pageSize, _ := strconv.Atoi(c.Query("pageSize"))
+
+	records, count, err := mysql.QuerySimulationTestRecords(ctx, &record, pageFlag, page, pageSize)
 	if err != nil {
-		c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "仿真测试记录查询失败", Total: 0})
 		return
 	}
+
 	output, err := json.Marshal(records)
 	if err != nil {
-		c.JSON(consts.StatusOK, entity.HttpResult{Status: false, Code: "", Message: "仿真测试记录查询失败", Details: ""})
+		c.JSON(consts.StatusOK, entity.Response{Status: false, Code: "", Message: "仿真测试记录查询失败", Total: 0})
 		return
 	}
-	c.JSON(consts.StatusOK, entity.HttpResult{Status: true, Code: "", Message: "仿真测试记录查询成功", Details: string(output)})
+	c.JSON(consts.StatusOK, entity.Response{Status: true, Code: "", Message: "仿真测试记录查询成功", Data: string(output), Total: int(count)})
 }
 
 // DownloadFileByKeys 给定object key数组从oss下载(打包)文件

+ 19 - 20
common/entity/http_result.go

@@ -1,7 +1,5 @@
 package entity
 
-import "encoding/json"
-
 var DefaultSuccessHttpResult = &HttpResult{Status: true, Code: "1000", Message: "请求成功。"}
 var DefaultFailHttpResult = &HttpResult{Status: true, Code: "2000", Message: "请求失败。"}
 
@@ -13,23 +11,24 @@ type HttpResult struct {
 }
 
 type Response struct {
-	Code  int         `json:"code"`  // 错误码
-	Msg   string      `json:"msg"`   // 错误描述
-	Data  interface{} `json:"data"`  // 返回数据
-	Total interface{} `json:"total"` // 分页的total数量
+	Status  bool   `json:"status"`
+	Code    string `json:"code"`  // 错误码
+	Message string `json:"msg"`   // 错误描述
+	Data    string `json:"data"`  // 返回数据
+	Total   int    `json:"total"` // 分页的total数量
 }
 
-// ToString 返回 JSON 格式的错误详情
-func (res *Response) ToString() string {
-	err := &struct {
-		Code int         `json:"code"`
-		Msg  string      `json:"msg"`
-		Data interface{} `json:"data"`
-	}{
-		Code: res.Code,
-		Msg:  res.Msg,
-		Data: res.Data,
-	}
-	raw, _ := json.Marshal(err)
-	return string(raw)
-}
+//// ToString 返回 JSON 格式的错误详情
+//func (res *Response) ToString() string {
+//	err := &struct {
+//		Code int         `json:"code"`
+//		Msg  string      `json:"msg"`
+//		Data interface{} `json:"data"`
+//	}{
+//		Code: res.Code,
+//		Msg:  res.Msg,
+//		Data: res.Data,
+//	}
+//	raw, _ := json.Marshal(err)
+//	return string(raw)
+//}