2025-12-06 10:38:48 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2025-12-10 15:41:52 +08:00
|
|
|
|
"cid/model/entity"
|
2025-12-30 10:55:35 +08:00
|
|
|
|
|
2026-02-24 16:24:47 +08:00
|
|
|
|
"gitea.com/red-future/common/beans"
|
2026-02-24 17:17:10 +08:00
|
|
|
|
"gitea.com/red-future/common/db/mongo"
|
2025-12-10 15:41:52 +08:00
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
2025-12-06 10:38:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-30 10:55:35 +08:00
|
|
|
|
var CIDRequest = &cidRequestDao{}
|
2025-12-29 15:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
type cidRequestDao struct {
|
|
|
|
|
|
}
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
// Create 创建CID请求记录
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *cidRequestDao) Create(ctx context.Context, request *entity.CidRequest) (id string, err error) {
|
2025-12-30 10:55:35 +08:00
|
|
|
|
ids, err := mongo.DB().Insert(ctx, []interface{}{request}, entity.CidRequestCollection)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
2025-12-10 15:41:52 +08:00
|
|
|
|
return "", err
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(ids) > 0 {
|
|
|
|
|
|
id = ids[0].(string)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetHistory 获取CID请求历史
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *cidRequestDao) GetHistory(ctx context.Context, userId string, page, size int) (list []*entity.CidRequest, total int64, err error) {
|
|
|
|
|
|
filter := bson.M{"userId": userId}
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
2026-01-08 11:07:58 +08:00
|
|
|
|
// 分页查询,使用common/mongo的Find方法,自动处理分页、租户等
|
|
|
|
|
|
pageBean := &beans.Page{PageNum: int64(page), PageSize: int64(size)}
|
|
|
|
|
|
total, err = mongo.DB().Find(ctx, filter, &list, entity.CidRequestCollection, pageBean, nil)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetStatistics 获取统计信息
|
2025-12-10 15:41:52 +08:00
|
|
|
|
func (d *cidRequestDao) GetStatistics(ctx context.Context, userId string) (stats map[string]interface{}, err error) {
|
2025-12-06 10:38:48 +08:00
|
|
|
|
stats = make(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
|
|
// 总请求数
|
2025-12-30 10:55:35 +08:00
|
|
|
|
totalRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId}, entity.CidRequestCollection)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
stats["total_requests"] = totalRequests
|
|
|
|
|
|
|
|
|
|
|
|
// 成功请求数
|
2025-12-30 10:55:35 +08:00
|
|
|
|
successfulRequests, err := mongo.DB().Count(ctx, bson.M{"userId": userId, "status": "completed"}, entity.CidRequestCollection)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
stats["successful_requests"] = successfulRequests
|
|
|
|
|
|
|
2025-12-10 15:41:52 +08:00
|
|
|
|
// 平均处理时间需要单独计算,MongoDB聚合查询
|
|
|
|
|
|
// 这里简化处理,返回0
|
|
|
|
|
|
stats["average_process_time"] = 0
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|