refactor(internal/models/order): 优化订单利润列表查询性能

- 使用并发查询来处理大量订单 ID- 采用 CopyOnWriteList以提高线程安全性
-优化了数据处理流程,提高了整体性能
This commit is contained in:
danial
2025-03-05 21:50:42 +08:00
parent 8b89345e63
commit 2e2790d8be

View File

@@ -1,6 +1,7 @@
package order package order
import ( import (
datastructure "github.com/duke-git/lancet/v2/datastructure/list"
"github.com/duke-git/lancet/v2/slice" "github.com/duke-git/lancet/v2/slice"
"strings" "strings"
"time" "time"
@@ -54,21 +55,28 @@ func GetOrderProfitByBankOrderId(bankOrderId string) OrderProfitInfo {
func GetOrderProfitListByBankOrderIdList(bankOrderIdList []string) []OrderProfitInfo { func GetOrderProfitListByBankOrderIdList(bankOrderIdList []string) []OrderProfitInfo {
o := orm.NewOrm() o := orm.NewOrm()
orderProfitList := make([]OrderProfitInfo, 0) orderProfitList := datastructure.NewCopyOnWriteList([]OrderProfitInfo{})
orderProfitListUnsafe := make([]OrderProfitInfo, 0)
if len(bankOrderIdList) == 0 { if len(bankOrderIdList) == 0 {
return orderProfitList orderProfitList.ForEach(func(info OrderProfitInfo) {
orderProfitListUnsafe = append(orderProfitListUnsafe, info)
})
return orderProfitListUnsafe
} }
bankOrderIdList = slice.Unique(bankOrderIdList) bankOrderIdList = slice.Unique(bankOrderIdList)
slice.ForEach(slice.Chunk(bankOrderIdList, 100), func(index int, item []string) { slice.ForEachConcurrent(slice.Chunk(bankOrderIdList, 100), func(index int, item []string) {
tmpOrderProfitList := make([]OrderProfitInfo, 0) tmpOrderProfitListTmp := make([]OrderProfitInfo, 0)
_, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("bank_order_id__in", item).All(&tmpOrderProfitList) _, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("bank_order_id__in", item).All(&tmpOrderProfitListTmp)
if err != nil { if err != nil {
logs.Error("GetOrderProfitByBankOrderId fail", err) logs.Error("GetOrderProfitByBankOrderId fail", err)
return return
} }
orderProfitList = append(orderProfitList, tmpOrderProfitList...) orderProfitList.AddAll(tmpOrderProfitListTmp)
}, 20)
orderProfitList.ForEach(func(info OrderProfitInfo) {
orderProfitListUnsafe = append(orderProfitListUnsafe, info)
}) })
return orderProfitList return orderProfitListUnsafe
} }
func GetAllOrderProfit(params map[string]string) []OrderProfitInfo { func GetAllOrderProfit(params map[string]string) []OrderProfitInfo {