mongo.go重构
This commit is contained in:
@@ -5,26 +5,20 @@ 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"
|
||||
)
|
||||
|
||||
// OrderMonthlyStatisticsDAO 订单月统计DAO
|
||||
type OrderMonthlyStatisticsDAO struct {
|
||||
NoCache bool
|
||||
}
|
||||
|
||||
var OrderMonthlyStatisticsDAOInstance = &OrderMonthlyStatisticsDAO{
|
||||
NoCache: false,
|
||||
}
|
||||
|
||||
func (dao *OrderMonthlyStatisticsDAO) SetNoCache() {
|
||||
OrderMonthlyStatisticsDAOInstance.NoCache = true
|
||||
}
|
||||
var OrderMonthlyStatisticsDAOInstance = &OrderMonthlyStatisticsDAO{}
|
||||
|
||||
// GenerateStatistics 使用Go代码生成月统计数据
|
||||
func (dao *OrderMonthlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int, month int) (*entity.OrderMonthlyStatistics, error) {
|
||||
@@ -44,28 +38,28 @@ func (dao *OrderMonthlyStatisticsDAO) GenerateStatistics(ctx context.Context, te
|
||||
"$lt": endDate,
|
||||
},
|
||||
}
|
||||
err := mongo.Find(ctx, dao.NoCache, filter, &pendingOrders, consts.OrderPendingCollection)
|
||||
err := mongo.DB().Find(ctx, filter, &pendingOrders, consts.OrderPendingCollection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询待支付订单数据失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询已支付订单
|
||||
var paidOrderEntities []*entity.OrderPaid
|
||||
err = mongo.Find(ctx, dao.NoCache, filter, &paidOrderEntities, consts.OrderPaidCollection)
|
||||
err = mongo.DB().Find(ctx, filter, &paidOrderEntities, consts.OrderPaidCollection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已支付订单数据失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询已发货订单
|
||||
var shippedOrders []*entity.OrderShipped
|
||||
err = mongo.Find(ctx, dao.NoCache, filter, &shippedOrders, consts.OrderShippedCollection)
|
||||
err = mongo.DB().Find(ctx, filter, &shippedOrders, consts.OrderShippedCollection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已发货订单数据失败: %v", err)
|
||||
}
|
||||
|
||||
// 查询已完成订单
|
||||
var completedOrderEntities []*entity.OrderCompleted
|
||||
err = mongo.Find(ctx, dao.NoCache, filter, &completedOrderEntities, consts.OrderCompletedCollection)
|
||||
err = mongo.DB().Find(ctx, filter, &completedOrderEntities, consts.OrderCompletedCollection)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("查询已完成订单数据失败: %v", err)
|
||||
}
|
||||
@@ -302,7 +296,7 @@ func (dao *OrderMonthlyStatisticsDAO) calculateMonthOverMonthGrowth(ctx context.
|
||||
}
|
||||
|
||||
var lastMonthOrders []*entity.OrderBase
|
||||
err := mongo.Find(ctx, dao.NoCache, filter, &lastMonthOrders, consts.OrderCollection)
|
||||
err := mongo.DB().Find(ctx, filter, &lastMonthOrders, consts.OrderCollection)
|
||||
if err != nil || len(lastMonthOrders) == 0 {
|
||||
return 0.0
|
||||
}
|
||||
@@ -319,7 +313,7 @@ func (dao *OrderMonthlyStatisticsDAO) calculateMonthOverMonthGrowth(ctx context.
|
||||
|
||||
// Save 保存月统计数据
|
||||
func (dao *OrderMonthlyStatisticsDAO) Save(ctx context.Context, statistics *entity.OrderMonthlyStatistics) error {
|
||||
_, err := mongo.Insert(ctx, []interface{}{statistics}, consts.OrderMonthlyStatisticsCollection)
|
||||
_, err := mongo.DB().Insert(ctx, []interface{}{statistics}, consts.OrderMonthlyStatisticsCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -334,7 +328,7 @@ func (dao *OrderMonthlyStatisticsDAO) Update(ctx context.Context, statistics *en
|
||||
"$set": statistics,
|
||||
}
|
||||
|
||||
_, err := mongo.Update(ctx, filter, update, consts.OrderMonthlyStatisticsCollection)
|
||||
_, err := mongo.DB().Update(ctx, filter, update, consts.OrderMonthlyStatisticsCollection)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -347,7 +341,7 @@ func (dao *OrderMonthlyStatisticsDAO) GetByTenantAndDate(ctx context.Context, te
|
||||
"reportDate": reportDate,
|
||||
}
|
||||
|
||||
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderMonthlyStatisticsCollection)
|
||||
err := mongo.DB().FindOne(ctx, filter, &result, consts.OrderMonthlyStatisticsCollection)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user