2025-12-06 10:38:48 +08:00
|
|
|
|
package dao
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
2025-12-10 15:41:52 +08:00
|
|
|
|
"cid/model/entity"
|
|
|
|
|
|
"gitee.com/red-future---jilin-g/common/mongo"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
2025-12-06 10:38:48 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-12-29 15:27:00 +08:00
|
|
|
|
var CIDRequest = &cidRequestDao{
|
|
|
|
|
|
NoCache: true,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type cidRequestDao struct {
|
|
|
|
|
|
NoCache bool
|
|
|
|
|
|
}
|
2025-12-06 10:38:48 +08:00
|
|
|
|
|
2025-12-29 15:27:00 +08:00
|
|
|
|
func (d *cidRequestDao) SetNoCache() {
|
|
|
|
|
|
CIDRequest.NoCache = true
|
|
|
|
|
|
}
|
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-19 09:42:39 +08:00
|
|
|
|
ids, err := mongo.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
|
|
|
|
|
|
|
|
|
|
// 获取总数
|
2025-12-29 15:27:00 +08:00
|
|
|
|
total, err = mongo.Count(ctx, d.NoCache, filter, entity.CidRequestCollection)
|
2025-12-06 10:38:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分页查询
|
|
|
|
|
|
offset := (page - 1) * size
|
2025-12-29 15:27:00 +08:00
|
|
|
|
err = mongo.Find(ctx, d.NoCache, filter, &list, entity.CidRequestCollection,
|
2025-12-10 15:41:52 +08:00
|
|
|
|
options.Find().SetSort(bson.M{"createdAt": -1}).
|
|
|
|
|
|
SetSkip(int64(offset)).
|
|
|
|
|
|
SetLimit(int64(size)))
|
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-29 15:27:00 +08:00
|
|
|
|
totalRequests, err := mongo.Count(ctx, d.NoCache, 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-29 15:27:00 +08:00
|
|
|
|
successfulRequests, err := mongo.Count(ctx, d.NoCache, 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
|
|
|
|
|
|
}
|