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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user