mongo.go重构

This commit is contained in:
2025-12-30 11:03:11 +08:00
parent c150f38a87
commit 14a9af542d
10 changed files with 90 additions and 143 deletions

View File

@@ -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"
)
// OrderYearlyStatisticsDAO 订单年度统计DAO
type OrderYearlyStatisticsDAO struct {
NoCache bool
}
var OrderYearlyStatisticsDAOInstance = &OrderYearlyStatisticsDAO{
NoCache: false,
}
func (dao *OrderYearlyStatisticsDAO) SetNoCache() {
OrderYearlyStatisticsDAOInstance.NoCache = true
}
var OrderYearlyStatisticsDAOInstance = &OrderYearlyStatisticsDAO{}
// GenerateStatistics 使用Go代码生成年度统计数据
func (dao *OrderYearlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int) (*entity.OrderYearlyStatistics, error) {
@@ -42,7 +36,7 @@ func (dao *OrderYearlyStatisticsDAO) GenerateStatistics(ctx context.Context, ten
}
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)
}
@@ -230,7 +224,7 @@ func (dao *OrderYearlyStatisticsDAO) calculateYearOverYearGrowth(ctx context.Con
}
var lastYearOrders []*entity.OrderBase
err := mongo.Find(ctx, dao.NoCache, filter, &lastYearOrders, consts.OrderCollection)
err := mongo.DB().Find(ctx, filter, &lastYearOrders, consts.OrderCollection)
if err != nil || len(lastYearOrders) == 0 {
return 0.0
}
@@ -247,7 +241,7 @@ func (dao *OrderYearlyStatisticsDAO) calculateYearOverYearGrowth(ctx context.Con
// Save 保存年度统计数据
func (dao *OrderYearlyStatisticsDAO) Save(ctx context.Context, statistics *entity.OrderYearlyStatistics) error {
_, err := mongo.Insert(ctx, []interface{}{statistics}, consts.OrderYearlyStatisticsCollection)
_, err := mongo.DB().Insert(ctx, []interface{}{statistics}, consts.OrderYearlyStatisticsCollection)
return err
}
@@ -262,7 +256,7 @@ func (dao *OrderYearlyStatisticsDAO) Update(ctx context.Context, statistics *ent
"$set": statistics,
}
_, err := mongo.Update(ctx, filter, update, consts.OrderYearlyStatisticsCollection)
_, err := mongo.DB().Update(ctx, filter, update, consts.OrderYearlyStatisticsCollection)
return err
}
@@ -275,7 +269,7 @@ func (dao *OrderYearlyStatisticsDAO) GetByTenantAndDate(ctx context.Context, ten
"reportDate": reportDate,
}
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderYearlyStatisticsCollection)
err := mongo.DB().FindOne(ctx, filter, &result, consts.OrderYearlyStatisticsCollection)
if err != nil {
return nil, err
}