mongo.go重构
This commit is contained in:
@@ -5,27 +5,21 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/do"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
"order/consts"
|
||||
"order/model/entity"
|
||||
|
||||
"gitee.com/red-future---jilin-g/common/do"
|
||||
"gitee.com/red-future---jilin-g/common/mongo"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
"go.mongodb.org/mongo-driver/v2/mongo/options"
|
||||
)
|
||||
|
||||
// OrderDailyStatisticsDAO 订单日统计DAO
|
||||
type OrderDailyStatisticsDAO struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
var OrderDailyStatisticsDAOInstance = &OrderDailyStatisticsDAO{
|
||||
NoCache: false,
|
||||
}
|
||||
|
||||
func (dao *OrderDailyStatisticsDAO) SetNoCache() {
|
||||
OrderDailyStatisticsDAOInstance.NoCache = true
|
||||
}
|
||||
var OrderDailyStatisticsDAOInstance = &OrderDailyStatisticsDAO{}
|
||||
|
||||
// GenerateStatistics 使用Go代码生成日统计数据
|
||||
func (dao *OrderDailyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, date time.Time) (*entity.OrderDailyStatistics, error) {
|
||||
@@ -43,7 +37,7 @@ func (dao *OrderDailyStatisticsDAO) GenerateStatistics(ctx context.Context, tena
|
||||
}
|
||||
|
||||
var orders []*entity.OrderBase
|
||||
err := mongo.Find(ctx, dao.NoCache, filter, &orders, consts.OrderCollection)
|
||||
err := mongo.DB().Find(ctx, filter, &orders, consts.OrderCollection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询订单数据失败: %v", err)
|
||||
}
|
||||
@@ -200,7 +194,7 @@ func (dao *OrderDailyStatisticsDAO) findPeakHour(hourlyOrders []int64) int {
|
||||
|
||||
// Save 保存日统计数据
|
||||
func (dao *OrderDailyStatisticsDAO) Save(ctx context.Context, statistics *entity.OrderDailyStatistics) error {
|
||||
_, err := mongo.Insert(ctx, []interface{}{statistics}, consts.OrderDailyStatisticsCollection)
|
||||
_, err := mongo.DB().Insert(ctx, []interface{}{statistics}, consts.OrderDailyStatisticsCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -215,7 +209,7 @@ func (dao *OrderDailyStatisticsDAO) Update(ctx context.Context, statistics *enti
|
||||
"$set": statistics,
|
||||
}
|
||||
|
||||
_, err := mongo.Update(ctx, filter, update, consts.OrderDailyStatisticsCollection)
|
||||
_, err := mongo.DB().Update(ctx, filter, update, consts.OrderDailyStatisticsCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -228,7 +222,7 @@ func (dao *OrderDailyStatisticsDAO) GetByTenantAndDate(ctx context.Context, tena
|
||||
"reportDate": reportDate,
|
||||
}
|
||||
|
||||
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderDailyStatisticsCollection)
|
||||
err := mongo.DB().FindOne(ctx, filter, &result, consts.OrderDailyStatisticsCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -255,7 +249,7 @@ func (dao *OrderDailyStatisticsDAO) GetListByTenant(ctx context.Context, tenantI
|
||||
}
|
||||
|
||||
// 获取总数
|
||||
totalCount, err := mongo.Count(ctx, dao.NoCache, filter, consts.OrderDailyStatisticsCollection)
|
||||
totalCount, err := mongo.DB().Count(ctx, filter, consts.OrderDailyStatisticsCollection)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -264,8 +258,8 @@ func (dao *OrderDailyStatisticsDAO) GetListByTenant(ctx context.Context, tenantI
|
||||
skip := int64((pageNum - 1) * pageSize)
|
||||
var results []*entity.OrderDailyStatistics
|
||||
|
||||
// 使用mongo.Find进行分页查询
|
||||
err = mongo.Find(ctx, dao.NoCache, filter, &results, consts.OrderDailyStatisticsCollection,
|
||||
// 使用mongo.DB().Find进行分页查询
|
||||
err = mongo.DB().Find(ctx, filter, &results, consts.OrderDailyStatisticsCollection,
|
||||
options.Find().SetSkip(skip).SetLimit(int64(pageSize)).SetSort(bson.D{{Key: "reportDate", Value: -1}}))
|
||||
|
||||
if err != nil {
|
||||
@@ -282,6 +276,6 @@ func (dao *OrderDailyStatisticsDAO) DeleteByTenantAndDate(ctx context.Context, t
|
||||
"reportDate": reportDate,
|
||||
}
|
||||
|
||||
_, err := mongo.Delete(ctx, filter, consts.OrderDailyStatisticsCollection)
|
||||
_, err := mongo.DB().Delete(ctx, filter, consts.OrderDailyStatisticsCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user