From 14a9af542dfdbefd186e8f5a849fabf85fd2d86c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Tue, 30 Dec 2025 11:03:11 +0800 Subject: [PATCH] =?UTF-8?q?mongo.go=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dao/order_daily_statistics_dao.go | 30 ++++++-------- dao/order_dao.go | 37 +++++++---------- dao/order_monthly_statistics_dao.go | 30 ++++++-------- dao/order_quarterly_statistics_dao.go | 26 +++++------- dao/order_statistics_base_dao.go | 20 ++++----- dao/order_yearly_statistics_dao.go | 24 +++++------ dao/payment_dao.go | 58 +++++++++------------------ go.mod | 4 +- go.sum | 3 ++ update.sql | 1 + 10 files changed, 90 insertions(+), 143 deletions(-) create mode 100644 update.sql diff --git a/dao/order_daily_statistics_dao.go b/dao/order_daily_statistics_dao.go index 67bb475..6b8afa0 100644 --- a/dao/order_daily_statistics_dao.go +++ b/dao/order_daily_statistics_dao.go @@ -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 } diff --git a/dao/order_dao.go b/dao/order_dao.go index 0d18d2c..2c73ecc 100644 --- a/dao/order_dao.go +++ b/dao/order_dao.go @@ -17,17 +17,10 @@ import ( ) type order struct { - NoCache bool } // Order 订单数据访问对象 -var Order = &order{ - NoCache: false, -} - -func (d *order) SetNoCache() { - Order.NoCache = true -} +var Order = &order{} // getCollection 根据订单状态获取对应的集合 func (d *order) getCollection(status consts.OrderStatus) (string, error) { @@ -56,7 +49,7 @@ func (d *order) CreatePendingOrder(ctx context.Context, order *entity.OrderPendi order.CreatedAt = time.Now() order.UpdatedAt = time.Now() - _, err = mongo.Insert(ctx, []interface{}{order}, collection) + _, err = mongo.DB().Insert(ctx, []interface{}{order}, collection) return err } @@ -109,7 +102,7 @@ func (d *order) findOrderByNo(collection string, ctx context.Context, orderNo st "order_no": orderNo, } - err := mongo.FindOne(ctx, d.NoCache, filter, result, collection) + err := mongo.DB().FindOne(ctx, filter, result, collection) if err != nil { return errors.New("order not found") } @@ -136,7 +129,7 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons "order_no": orderNo, } - err = mongo.FindOne(ctx, d.NoCache, filter, &orderData, srcCollection) + err = mongo.DB().FindOne(ctx, filter, &orderData, srcCollection) if err != nil { return err } @@ -148,13 +141,13 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons } // 插入到目标集合 - _, err = mongo.Insert(ctx, []interface{}{orderData}, destCollection) + _, err = mongo.DB().Insert(ctx, []interface{}{orderData}, destCollection) if err != nil { return err } // 从源集合删除 - _, err = mongo.Delete(ctx, filter, srcCollection) + _, err = mongo.DB().Delete(ctx, filter, srcCollection) return err } @@ -170,7 +163,7 @@ func (d *order) UpdatePendingOrder(ctx context.Context, orderNo string, update b filter := bson.M{ "order_no": orderNo, } - _, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection) + _, err = mongo.DB().Update(ctx, filter, bson.M{"$set": update}, collection) return err } @@ -189,7 +182,7 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu } // 计算总数 - total, err := mongo.Count(ctx, d.NoCache, filter, collection) + total, err := mongo.DB().Count(ctx, filter, collection) if err != nil { return nil, 0, err } @@ -220,25 +213,25 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu switch status { case consts.OrderStatusPending: var orders []entity.OrderPending - if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil { + if err := mongo.DB().Find(ctx, filter, &orders, collection, findOptions...); err != nil { return nil, 0, err } return orders, total, nil case consts.OrderStatusPaid: var orders []entity.OrderPaid - if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil { + if err := mongo.DB().Find(ctx, filter, &orders, collection, findOptions...); err != nil { return nil, 0, err } return orders, total, nil case consts.OrderStatusShipped: var orders []entity.OrderShipped - if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil { + if err := mongo.DB().Find(ctx, filter, &orders, collection, findOptions...); err != nil { return nil, 0, err } return orders, total, nil case consts.OrderStatusCompleted: var orders []entity.OrderCompleted - if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil { + if err := mongo.DB().Find(ctx, filter, &orders, collection, findOptions...); err != nil { return nil, 0, err } return orders, total, nil @@ -258,7 +251,7 @@ func (d *order) GetPendingOrder(ctx context.Context, orderNo string) (*entity.Or filter := bson.M{"order_no": orderNo} var order entity.OrderPending - if err := mongo.FindOne(ctx, d.NoCache, filter, &order, collection); err != nil { + if err := mongo.DB().FindOne(ctx, filter, &order, collection); err != nil { if err.Error() == "mongo: no documents in result" { return nil, nil } @@ -279,7 +272,7 @@ func (d *order) GetExpiredPendingOrders(ctx context.Context) ([]entity.OrderPend } var orders []entity.OrderPending - if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection); err != nil { + if err := mongo.DB().Find(ctx, filter, &orders, collection); err != nil { return nil, err } @@ -300,7 +293,7 @@ func (d *order) UpdatePayInfo(ctx context.Context, orderNo string, payInfo entit "pay_info": payInfo, "updated_at": time.Now(), } - _, err = mongo.Update(ctx, filter, bson.M{"$set": update}, collection) + _, err = mongo.DB().Update(ctx, filter, bson.M{"$set": update}, collection) return err } diff --git a/dao/order_monthly_statistics_dao.go b/dao/order_monthly_statistics_dao.go index 4b749d5..68ee3da 100644 --- a/dao/order_monthly_statistics_dao.go +++ b/dao/order_monthly_statistics_dao.go @@ -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 } diff --git a/dao/order_quarterly_statistics_dao.go b/dao/order_quarterly_statistics_dao.go index 4840e9b..e18ddae 100644 --- a/dao/order_quarterly_statistics_dao.go +++ b/dao/order_quarterly_statistics_dao.go @@ -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" ) // OrderQuarterlyStatisticsDAO 订单季度统计DAO type OrderQuarterlyStatisticsDAO struct { - NoCache bool } -var OrderQuarterlyStatisticsDAOInstance = &OrderQuarterlyStatisticsDAO{ - NoCache: false, -} - -func (dao *OrderQuarterlyStatisticsDAO) SetNoCache() { - OrderQuarterlyStatisticsDAOInstance.NoCache = true -} +var OrderQuarterlyStatisticsDAOInstance = &OrderQuarterlyStatisticsDAO{} // GenerateStatistics 使用Go代码生成季度统计数据 func (dao *OrderQuarterlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int, quarter int) (*entity.OrderQuarterlyStatistics, error) { @@ -54,7 +48,7 @@ func (dao *OrderQuarterlyStatisticsDAO) GenerateStatistics(ctx context.Context, } 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) } @@ -255,7 +249,7 @@ func (dao *OrderQuarterlyStatisticsDAO) calculateQuarterOverQuarterGrowth(ctx co } var lastQuarterOrders []*entity.OrderBase - err := mongo.Find(ctx, dao.NoCache, filter, &lastQuarterOrders, consts.OrderCollection) + err := mongo.DB().Find(ctx, filter, &lastQuarterOrders, consts.OrderCollection) if err != nil || len(lastQuarterOrders) == 0 { return 0.0 } @@ -286,7 +280,7 @@ func (dao *OrderQuarterlyStatisticsDAO) calculateYearOverYearGrowth(ctx context. } 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 } @@ -303,7 +297,7 @@ func (dao *OrderQuarterlyStatisticsDAO) calculateYearOverYearGrowth(ctx context. // Save 保存季度统计数据 func (dao *OrderQuarterlyStatisticsDAO) Save(ctx context.Context, statistics *entity.OrderQuarterlyStatistics) error { - _, err := mongo.Insert(ctx, []interface{}{statistics}, consts.OrderQuarterlyStatisticsCollection) + _, err := mongo.DB().Insert(ctx, []interface{}{statistics}, consts.OrderQuarterlyStatisticsCollection) return err } @@ -318,7 +312,7 @@ func (dao *OrderQuarterlyStatisticsDAO) Update(ctx context.Context, statistics * "$set": statistics, } - _, err := mongo.Update(ctx, filter, update, consts.OrderQuarterlyStatisticsCollection) + _, err := mongo.DB().Update(ctx, filter, update, consts.OrderQuarterlyStatisticsCollection) return err } @@ -331,7 +325,7 @@ func (dao *OrderQuarterlyStatisticsDAO) GetByTenantAndDate(ctx context.Context, "reportDate": reportDate, } - err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderQuarterlyStatisticsCollection) + err := mongo.DB().FindOne(ctx, filter, &result, consts.OrderQuarterlyStatisticsCollection) if err != nil { return nil, err } diff --git a/dao/order_statistics_base_dao.go b/dao/order_statistics_base_dao.go index 813c1d5..c6fb36c 100644 --- a/dao/order_statistics_base_dao.go +++ b/dao/order_statistics_base_dao.go @@ -5,25 +5,19 @@ import ( "fmt" "time" - "gitee.com/red-future---jilin-g/common/mongo" "order/consts" "order/model/entity" + "gitee.com/red-future---jilin-g/common/mongo" + "go.mongodb.org/mongo-driver/v2/bson" ) // OrderStatisticsBaseDAO 订单统计基础DAO type OrderStatisticsBaseDAO struct { - NoCache bool } -var OrderStatisticsBaseDAOInstance = &OrderStatisticsBaseDAO{ - NoCache: false, -} - -func (dao *OrderStatisticsBaseDAO) SetNoCache() { - OrderStatisticsBaseDAOInstance.NoCache = true -} +var OrderStatisticsBaseDAOInstance = &OrderStatisticsBaseDAO{} // OrderStats 订单统计结果 type OrderStats struct { @@ -54,28 +48,28 @@ func (dao *OrderStatisticsBaseDAO) GetOrderStats(ctx context.Context, tenantID i // 查询待支付订单 var pendingOrders []*entity.OrderPending - 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 paidOrders []*entity.OrderPaid - err = mongo.Find(ctx, dao.NoCache, filter, &paidOrders, consts.OrderPaidCollection) + err = mongo.DB().Find(ctx, filter, &paidOrders, 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 completedOrders []*entity.OrderCompleted - err = mongo.Find(ctx, dao.NoCache, filter, &completedOrders, consts.OrderCompletedCollection) + err = mongo.DB().Find(ctx, filter, &completedOrders, consts.OrderCompletedCollection) if err != nil { return nil, fmt.Errorf("查询已完成订单数据失败: %v", err) } diff --git a/dao/order_yearly_statistics_dao.go b/dao/order_yearly_statistics_dao.go index 4fb1f35..5805f2d 100644 --- a/dao/order_yearly_statistics_dao.go +++ b/dao/order_yearly_statistics_dao.go @@ -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 } diff --git a/dao/payment_dao.go b/dao/payment_dao.go index df6df0c..950fb70 100644 --- a/dao/payment_dao.go +++ b/dao/payment_dao.go @@ -4,28 +4,22 @@ import ( "context" "time" - "gitee.com/red-future---jilin-g/common/mongo" - "go.mongodb.org/mongo-driver/v2/bson" "order/consts" "order/model/entity" + + "gitee.com/red-future---jilin-g/common/mongo" + "go.mongodb.org/mongo-driver/v2/bson" ) type paymentConfig struct { - NoCache bool } // PaymentConfig 支付配置数据访问对象 -var PaymentConfig = &paymentConfig{ - NoCache: false, -} - -func (d *paymentConfig) SetNoCache() { - PaymentConfig.NoCache = true -} +var PaymentConfig = &paymentConfig{} // Create 创建支付配置 func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error { - _, err := mongo.Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection) + _, err := mongo.DB().Insert(ctx, []interface{}{config}, consts.PaymentConfigCollection) return err } @@ -39,7 +33,7 @@ func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payM "enabled": true, } - err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection) + err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection) if err != nil { return nil, nil } @@ -75,7 +69,7 @@ func (d *paymentConfig) Update(ctx context.Context, config *entity.PaymentConfig }, } - _, err := mongo.Update(ctx, filter, update, consts.PaymentConfigCollection) + _, err := mongo.DB().Update(ctx, filter, update, consts.PaymentConfigCollection) return err } @@ -84,7 +78,7 @@ func (d *paymentConfig) GetByID(ctx context.Context, id bson.ObjectID) (*entity. var config entity.PaymentConfig filter := bson.M{"_id": id} - err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection) + err := mongo.DB().FindOne(ctx, filter, &config, consts.PaymentConfigCollection) if err != nil { return nil, err } @@ -97,7 +91,7 @@ func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]e var configs []entity.PaymentConfig filter := bson.M{"tenant_id": tenantID} - err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection) + err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection) if err != nil { return nil, err } @@ -109,7 +103,7 @@ func (d *paymentConfig) GetByTenantID(ctx context.Context, tenantID string) ([]e func (d *paymentConfig) Delete(ctx context.Context, id bson.ObjectID) error { filter := bson.M{"_id": id} - _, err := mongo.Delete(ctx, filter, consts.PaymentConfigCollection) + _, err := mongo.DB().Delete(ctx, filter, consts.PaymentConfigCollection) return err } func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]entity.PaymentConfig, error) { @@ -117,29 +111,22 @@ func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]en filter := bson.M{"tenant_id": tenantID} - err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection) + err := mongo.DB().Find(ctx, filter, &configs, consts.PaymentConfigCollection) return configs, err } type paymentRecord struct { - NoCache bool } // PaymentRecord 支付记录数据访问对象 -var PaymentRecord = &paymentRecord{ - NoCache: false, -} - -func (d *paymentRecord) SetNoCache() { - PaymentRecord.NoCache = true -} +var PaymentRecord = &paymentRecord{} // Create 创建支付记录 func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error { record.CreatedAt = time.Now() record.UpdatedAt = time.Now() - _, err := mongo.Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection) + _, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.PaymentRecordCollection) return err } @@ -152,7 +139,7 @@ func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo stri "order_no": orderNo, } - err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.PaymentRecordCollection) + err := mongo.DB().FindOne(ctx, filter, &record, consts.PaymentRecordCollection) if err != nil { return nil, nil } @@ -170,29 +157,22 @@ func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, tra "updated_at": time.Now(), } - _, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection) + _, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.PaymentRecordCollection) return err } type refundRecord struct { - NoCache bool } // RefundRecord 退款记录数据访问对象 -var RefundRecord = &refundRecord{ - NoCache: false, -} - -func (d *refundRecord) SetNoCache() { - RefundRecord.NoCache = true -} +var RefundRecord = &refundRecord{} // Create 创建退款记录 func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error { record.CreatedAt = time.Now() record.UpdatedAt = time.Now() - _, err := mongo.Insert(ctx, []interface{}{record}, consts.RefundRecordCollection) + _, err := mongo.DB().Insert(ctx, []interface{}{record}, consts.RefundRecordCollection) return err } @@ -205,7 +185,7 @@ func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo str "refund_no": refundNo, } - err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.RefundRecordCollection) + err := mongo.DB().FindOne(ctx, filter, &record, consts.RefundRecordCollection) if err != nil { return nil, nil } @@ -222,6 +202,6 @@ func (d *refundRecord) UpdateRefundStatus(ctx context.Context, id, status, refun "updated_at": time.Now(), } - _, err := mongo.Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection) + _, err := mongo.DB().Update(ctx, filter, bson.M{"$set": update}, consts.RefundRecordCollection) return err } diff --git a/go.mod b/go.mod index 4cd8403..91dc3e9 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module order go 1.25.3 require ( - gitee.com/red-future---jilin-g/common v0.2.6 + gitee.com/red-future---jilin-g/common v0.2.9 github.com/gogf/gf/contrib/drivers/mysql/v2 v2.9.6 github.com/gogf/gf/contrib/nosql/redis/v2 v2.9.6 github.com/gogf/gf/v2 v2.9.6 @@ -11,7 +11,7 @@ require ( go.mongodb.org/mongo-driver/v2 v2.4.0 ) -//replace gitee.com/red-future---jilin-g/common v0.2.6 => ../common +//replace gitee.com/red-future---jilin-g/common v0.2.9 => ../common require ( github.com/BurntSushi/toml v1.5.0 // indirect diff --git a/go.sum b/go.sum index 1907426..9a71b8c 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,9 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT gitee.com/red-future---jilin-g/common v0.2.5 h1:0T4chOMw9aXbA9RvP8d0814xLbisjzrMPhgqbCTa12M= gitee.com/red-future---jilin-g/common v0.2.5/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= gitee.com/red-future---jilin-g/common v0.2.6/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= +gitee.com/red-future---jilin-g/common v0.2.7/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= +gitee.com/red-future---jilin-g/common v0.2.9 h1:lqYUnqTQLEcvxfFBqRMpetMudQze5cQlM4fKDouu2CU= +gitee.com/red-future---jilin-g/common v0.2.9/go.mod h1:TFCNnI7801DamWd0m/M2CT5T090jFXXGLNDY4iZ2WY8= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho= diff --git a/update.sql b/update.sql new file mode 100644 index 0000000..807d512 --- /dev/null +++ b/update.sql @@ -0,0 +1 @@ +-----------张斌2025-06-16 15:00:00-------------- \ No newline at end of file