common版本更新v0.2.6;数据库查询接口增加是否从缓存中查询数据开关

This commit is contained in:
2025-12-29 15:58:48 +08:00
parent ca7b1cd989
commit c150f38a87
9 changed files with 129 additions and 56 deletions

View File

@@ -15,9 +15,17 @@ import (
)
// OrderDailyStatisticsDAO 订单日统计DAO
type OrderDailyStatisticsDAO struct{}
type OrderDailyStatisticsDAO struct {
NoCache bool
}
var OrderDailyStatisticsDAOInstance = &OrderDailyStatisticsDAO{}
var OrderDailyStatisticsDAOInstance = &OrderDailyStatisticsDAO{
NoCache: false,
}
func (dao *OrderDailyStatisticsDAO) SetNoCache() {
OrderDailyStatisticsDAOInstance.NoCache = true
}
// GenerateStatistics 使用Go代码生成日统计数据
func (dao *OrderDailyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, date time.Time) (*entity.OrderDailyStatistics, error) {
@@ -35,7 +43,7 @@ func (dao *OrderDailyStatisticsDAO) GenerateStatistics(ctx context.Context, tena
}
var orders []*entity.OrderBase
err := mongo.Find(ctx, filter, &orders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &orders, consts.OrderCollection)
if err != nil {
return nil, fmt.Errorf("查询订单数据失败: %v", err)
}
@@ -220,7 +228,7 @@ func (dao *OrderDailyStatisticsDAO) GetByTenantAndDate(ctx context.Context, tena
"reportDate": reportDate,
}
err := mongo.FindOne(ctx, filter, &result, consts.OrderDailyStatisticsCollection)
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderDailyStatisticsCollection)
if err != nil {
return nil, err
}
@@ -247,7 +255,7 @@ func (dao *OrderDailyStatisticsDAO) GetListByTenant(ctx context.Context, tenantI
}
// 获取总数
totalCount, err := mongo.Count(ctx, filter, consts.OrderDailyStatisticsCollection)
totalCount, err := mongo.Count(ctx, dao.NoCache, filter, consts.OrderDailyStatisticsCollection)
if err != nil {
return nil, 0, err
}
@@ -257,7 +265,7 @@ func (dao *OrderDailyStatisticsDAO) GetListByTenant(ctx context.Context, tenantI
var results []*entity.OrderDailyStatistics
// 使用mongo.Find进行分页查询
err = mongo.Find(ctx, filter, &results, consts.OrderDailyStatisticsCollection,
err = mongo.Find(ctx, dao.NoCache, filter, &results, consts.OrderDailyStatisticsCollection,
options.Find().SetSkip(skip).SetLimit(int64(pageSize)).SetSort(bson.D{{Key: "reportDate", Value: -1}}))
if err != nil {

View File

@@ -16,10 +16,18 @@ import (
"gitee.com/red-future---jilin-g/common/mongo"
)
type order struct{}
type order struct {
NoCache bool
}
// Order 订单数据访问对象
var Order = &order{}
var Order = &order{
NoCache: false,
}
func (d *order) SetNoCache() {
Order.NoCache = true
}
// getCollection 根据订单状态获取对应的集合
func (d *order) getCollection(status consts.OrderStatus) (string, error) {
@@ -101,7 +109,7 @@ func (d *order) findOrderByNo(collection string, ctx context.Context, orderNo st
"order_no": orderNo,
}
err := mongo.FindOne(ctx, filter, result, collection)
err := mongo.FindOne(ctx, d.NoCache, filter, result, collection)
if err != nil {
return errors.New("order not found")
}
@@ -128,7 +136,7 @@ func (d *order) MoveOrderToStatus(ctx context.Context, fromStatus, toStatus cons
"order_no": orderNo,
}
err = mongo.FindOne(ctx, filter, &orderData, srcCollection)
err = mongo.FindOne(ctx, d.NoCache, filter, &orderData, srcCollection)
if err != nil {
return err
}
@@ -181,7 +189,7 @@ func (d *order) ListOrdersByStatus(ctx context.Context, status consts.OrderStatu
}
// 计算总数
total, err := mongo.Count(ctx, filter, collection)
total, err := mongo.Count(ctx, d.NoCache, filter, collection)
if err != nil {
return nil, 0, err
}
@@ -212,25 +220,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, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, 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, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, 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, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, 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, filter, &orders, collection, findOptions...); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection, findOptions...); err != nil {
return nil, 0, err
}
return orders, total, nil
@@ -250,7 +258,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, filter, &order, collection); err != nil {
if err := mongo.FindOne(ctx, d.NoCache, filter, &order, collection); err != nil {
if err.Error() == "mongo: no documents in result" {
return nil, nil
}
@@ -271,7 +279,7 @@ func (d *order) GetExpiredPendingOrders(ctx context.Context) ([]entity.OrderPend
}
var orders []entity.OrderPending
if err := mongo.Find(ctx, filter, &orders, collection); err != nil {
if err := mongo.Find(ctx, d.NoCache, filter, &orders, collection); err != nil {
return nil, err
}

View File

@@ -14,9 +14,17 @@ import (
)
// OrderMonthlyStatisticsDAO 订单月统计DAO
type OrderMonthlyStatisticsDAO struct{}
type OrderMonthlyStatisticsDAO struct {
NoCache bool
}
var OrderMonthlyStatisticsDAOInstance = &OrderMonthlyStatisticsDAO{}
var OrderMonthlyStatisticsDAOInstance = &OrderMonthlyStatisticsDAO{
NoCache: false,
}
func (dao *OrderMonthlyStatisticsDAO) SetNoCache() {
OrderMonthlyStatisticsDAOInstance.NoCache = true
}
// GenerateStatistics 使用Go代码生成月统计数据
func (dao *OrderMonthlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int, month int) (*entity.OrderMonthlyStatistics, error) {
@@ -36,28 +44,28 @@ func (dao *OrderMonthlyStatisticsDAO) GenerateStatistics(ctx context.Context, te
"$lt": endDate,
},
}
err := mongo.Find(ctx, filter, &pendingOrders, consts.OrderPendingCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &pendingOrders, consts.OrderPendingCollection)
if err != nil {
return nil, fmt.Errorf("查询待支付订单数据失败: %v", err)
}
// 查询已支付订单
var paidOrderEntities []*entity.OrderPaid
err = mongo.Find(ctx, filter, &paidOrderEntities, consts.OrderPaidCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &paidOrderEntities, consts.OrderPaidCollection)
if err != nil {
return nil, fmt.Errorf("查询已支付订单数据失败: %v", err)
}
// 查询已发货订单
var shippedOrders []*entity.OrderShipped
err = mongo.Find(ctx, filter, &shippedOrders, consts.OrderShippedCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &shippedOrders, consts.OrderShippedCollection)
if err != nil {
return nil, fmt.Errorf("查询已发货订单数据失败: %v", err)
}
// 查询已完成订单
var completedOrderEntities []*entity.OrderCompleted
err = mongo.Find(ctx, filter, &completedOrderEntities, consts.OrderCompletedCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &completedOrderEntities, consts.OrderCompletedCollection)
if err != nil {
return nil, fmt.Errorf("查询已完成订单数据失败: %v", err)
}
@@ -294,7 +302,7 @@ func (dao *OrderMonthlyStatisticsDAO) calculateMonthOverMonthGrowth(ctx context.
}
var lastMonthOrders []*entity.OrderBase
err := mongo.Find(ctx, filter, &lastMonthOrders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &lastMonthOrders, consts.OrderCollection)
if err != nil || len(lastMonthOrders) == 0 {
return 0.0
}
@@ -339,7 +347,7 @@ func (dao *OrderMonthlyStatisticsDAO) GetByTenantAndDate(ctx context.Context, te
"reportDate": reportDate,
}
err := mongo.FindOne(ctx, filter, &result, consts.OrderMonthlyStatisticsCollection)
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderMonthlyStatisticsCollection)
if err != nil {
return nil, err
}

View File

@@ -14,9 +14,17 @@ import (
)
// OrderQuarterlyStatisticsDAO 订单季度统计DAO
type OrderQuarterlyStatisticsDAO struct{}
type OrderQuarterlyStatisticsDAO struct {
NoCache bool
}
var OrderQuarterlyStatisticsDAOInstance = &OrderQuarterlyStatisticsDAO{}
var OrderQuarterlyStatisticsDAOInstance = &OrderQuarterlyStatisticsDAO{
NoCache: false,
}
func (dao *OrderQuarterlyStatisticsDAO) SetNoCache() {
OrderQuarterlyStatisticsDAOInstance.NoCache = true
}
// GenerateStatistics 使用Go代码生成季度统计数据
func (dao *OrderQuarterlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int, quarter int) (*entity.OrderQuarterlyStatistics, error) {
@@ -46,7 +54,7 @@ func (dao *OrderQuarterlyStatisticsDAO) GenerateStatistics(ctx context.Context,
}
var orders []*entity.OrderBase
err := mongo.Find(ctx, filter, &orders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &orders, consts.OrderCollection)
if err != nil {
return nil, fmt.Errorf("查询订单数据失败: %v", err)
}
@@ -247,7 +255,7 @@ func (dao *OrderQuarterlyStatisticsDAO) calculateQuarterOverQuarterGrowth(ctx co
}
var lastQuarterOrders []*entity.OrderBase
err := mongo.Find(ctx, filter, &lastQuarterOrders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &lastQuarterOrders, consts.OrderCollection)
if err != nil || len(lastQuarterOrders) == 0 {
return 0.0
}
@@ -278,7 +286,7 @@ func (dao *OrderQuarterlyStatisticsDAO) calculateYearOverYearGrowth(ctx context.
}
var lastYearOrders []*entity.OrderBase
err := mongo.Find(ctx, filter, &lastYearOrders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &lastYearOrders, consts.OrderCollection)
if err != nil || len(lastYearOrders) == 0 {
return 0.0
}
@@ -323,7 +331,7 @@ func (dao *OrderQuarterlyStatisticsDAO) GetByTenantAndDate(ctx context.Context,
"reportDate": reportDate,
}
err := mongo.FindOne(ctx, filter, &result, consts.OrderQuarterlyStatisticsCollection)
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderQuarterlyStatisticsCollection)
if err != nil {
return nil, err
}

View File

@@ -13,9 +13,17 @@ import (
)
// OrderStatisticsBaseDAO 订单统计基础DAO
type OrderStatisticsBaseDAO struct{}
type OrderStatisticsBaseDAO struct {
NoCache bool
}
var OrderStatisticsBaseDAOInstance = &OrderStatisticsBaseDAO{}
var OrderStatisticsBaseDAOInstance = &OrderStatisticsBaseDAO{
NoCache: false,
}
func (dao *OrderStatisticsBaseDAO) SetNoCache() {
OrderStatisticsBaseDAOInstance.NoCache = true
}
// OrderStats 订单统计结果
type OrderStats struct {
@@ -46,28 +54,28 @@ func (dao *OrderStatisticsBaseDAO) GetOrderStats(ctx context.Context, tenantID i
// 查询待支付订单
var pendingOrders []*entity.OrderPending
err := mongo.Find(ctx, filter, &pendingOrders, consts.OrderPendingCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &pendingOrders, consts.OrderPendingCollection)
if err != nil {
return nil, fmt.Errorf("查询待支付订单数据失败: %v", err)
}
// 查询已支付订单
var paidOrders []*entity.OrderPaid
err = mongo.Find(ctx, filter, &paidOrders, consts.OrderPaidCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &paidOrders, consts.OrderPaidCollection)
if err != nil {
return nil, fmt.Errorf("查询已支付订单数据失败: %v", err)
}
// 查询已发货订单
var shippedOrders []*entity.OrderShipped
err = mongo.Find(ctx, filter, &shippedOrders, consts.OrderShippedCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &shippedOrders, consts.OrderShippedCollection)
if err != nil {
return nil, fmt.Errorf("查询已发货订单数据失败: %v", err)
}
// 查询已完成订单
var completedOrders []*entity.OrderCompleted
err = mongo.Find(ctx, filter, &completedOrders, consts.OrderCompletedCollection)
err = mongo.Find(ctx, dao.NoCache, filter, &completedOrders, consts.OrderCompletedCollection)
if err != nil {
return nil, fmt.Errorf("查询已完成订单数据失败: %v", err)
}

View File

@@ -14,9 +14,17 @@ import (
)
// OrderYearlyStatisticsDAO 订单年度统计DAO
type OrderYearlyStatisticsDAO struct{}
type OrderYearlyStatisticsDAO struct {
NoCache bool
}
var OrderYearlyStatisticsDAOInstance = &OrderYearlyStatisticsDAO{}
var OrderYearlyStatisticsDAOInstance = &OrderYearlyStatisticsDAO{
NoCache: false,
}
func (dao *OrderYearlyStatisticsDAO) SetNoCache() {
OrderYearlyStatisticsDAOInstance.NoCache = true
}
// GenerateStatistics 使用Go代码生成年度统计数据
func (dao *OrderYearlyStatisticsDAO) GenerateStatistics(ctx context.Context, tenantID int64, year int) (*entity.OrderYearlyStatistics, error) {
@@ -34,7 +42,7 @@ func (dao *OrderYearlyStatisticsDAO) GenerateStatistics(ctx context.Context, ten
}
var orders []*entity.OrderBase
err := mongo.Find(ctx, filter, &orders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &orders, consts.OrderCollection)
if err != nil {
return nil, fmt.Errorf("查询订单数据失败: %v", err)
}
@@ -222,7 +230,7 @@ func (dao *OrderYearlyStatisticsDAO) calculateYearOverYearGrowth(ctx context.Con
}
var lastYearOrders []*entity.OrderBase
err := mongo.Find(ctx, filter, &lastYearOrders, consts.OrderCollection)
err := mongo.Find(ctx, dao.NoCache, filter, &lastYearOrders, consts.OrderCollection)
if err != nil || len(lastYearOrders) == 0 {
return 0.0
}
@@ -267,7 +275,7 @@ func (dao *OrderYearlyStatisticsDAO) GetByTenantAndDate(ctx context.Context, ten
"reportDate": reportDate,
}
err := mongo.FindOne(ctx, filter, &result, consts.OrderYearlyStatisticsCollection)
err := mongo.FindOne(ctx, dao.NoCache, filter, &result, consts.OrderYearlyStatisticsCollection)
if err != nil {
return nil, err
}

View File

@@ -10,10 +10,18 @@ import (
"order/model/entity"
)
type paymentConfig struct{}
type paymentConfig struct {
NoCache bool
}
// PaymentConfig 支付配置数据访问对象
var PaymentConfig = new(paymentConfig)
var PaymentConfig = &paymentConfig{
NoCache: false,
}
func (d *paymentConfig) SetNoCache() {
PaymentConfig.NoCache = true
}
// Create 创建支付配置
func (d *paymentConfig) Create(ctx context.Context, config *entity.PaymentConfig) error {
@@ -31,7 +39,7 @@ func (d *paymentConfig) GetByTenantAndMethod(ctx context.Context, tenantID, payM
"enabled": true,
}
err := mongo.FindOne(ctx, filter, &config, consts.PaymentConfigCollection)
err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, nil
}
@@ -76,7 +84,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, filter, &config, consts.PaymentConfigCollection)
err := mongo.FindOne(ctx, d.NoCache, filter, &config, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
@@ -89,7 +97,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, filter, &configs, consts.PaymentConfigCollection)
err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection)
if err != nil {
return nil, err
}
@@ -109,14 +117,22 @@ func (d *paymentConfig) ListByTenant(ctx context.Context, tenantID string) ([]en
filter := bson.M{"tenant_id": tenantID}
err := mongo.Find(ctx, filter, &configs, consts.PaymentConfigCollection)
err := mongo.Find(ctx, d.NoCache, filter, &configs, consts.PaymentConfigCollection)
return configs, err
}
type paymentRecord struct{}
type paymentRecord struct {
NoCache bool
}
// PaymentRecord 支付记录数据访问对象
var PaymentRecord = new(paymentRecord)
var PaymentRecord = &paymentRecord{
NoCache: false,
}
func (d *paymentRecord) SetNoCache() {
PaymentRecord.NoCache = true
}
// Create 创建支付记录
func (d *paymentRecord) Create(ctx context.Context, record *entity.PaymentRecord) error {
@@ -136,7 +152,7 @@ func (d *paymentRecord) GetByOrderNo(ctx context.Context, tenantID, orderNo stri
"order_no": orderNo,
}
err := mongo.FindOne(ctx, filter, &record, consts.PaymentRecordCollection)
err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.PaymentRecordCollection)
if err != nil {
return nil, nil
}
@@ -158,10 +174,18 @@ func (d *paymentRecord) UpdateStatus(ctx context.Context, id string, status, tra
return err
}
type refundRecord struct{}
type refundRecord struct {
NoCache bool
}
// RefundRecord 退款记录数据访问对象
var RefundRecord = new(refundRecord)
var RefundRecord = &refundRecord{
NoCache: false,
}
func (d *refundRecord) SetNoCache() {
RefundRecord.NoCache = true
}
// Create 创建退款记录
func (d *refundRecord) Create(ctx context.Context, record *entity.RefundRecord) error {
@@ -181,7 +205,7 @@ func (d *refundRecord) GetByRefundNo(ctx context.Context, tenantID, refundNo str
"refund_no": refundNo,
}
err := mongo.FindOne(ctx, filter, &record, consts.RefundRecordCollection)
err := mongo.FindOne(ctx, d.NoCache, filter, &record, consts.RefundRecordCollection)
if err != nil {
return nil, nil
}

4
go.mod
View File

@@ -3,7 +3,7 @@ module order
go 1.25.3
require (
gitee.com/red-future---jilin-g/common v0.2.5
gitee.com/red-future---jilin-g/common v0.2.6
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.1 => ../common
//replace gitee.com/red-future---jilin-g/common v0.2.6 => ../common
require (
github.com/BurntSushi/toml v1.5.0 // indirect

1
go.sum
View File

@@ -1,6 +1,7 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
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=
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=