refactor(internal/models/order): 优化订单利润列表查询性能
- 使用并发查询来处理大量订单 ID- 采用 CopyOnWriteList以提高线程安全性 -优化了数据处理流程,提高了整体性能
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package order
|
||||
|
||||
import (
|
||||
datastructure "github.com/duke-git/lancet/v2/datastructure/list"
|
||||
"github.com/duke-git/lancet/v2/slice"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -54,21 +55,28 @@ func GetOrderProfitByBankOrderId(bankOrderId string) OrderProfitInfo {
|
||||
|
||||
func GetOrderProfitListByBankOrderIdList(bankOrderIdList []string) []OrderProfitInfo {
|
||||
o := orm.NewOrm()
|
||||
orderProfitList := make([]OrderProfitInfo, 0)
|
||||
orderProfitList := datastructure.NewCopyOnWriteList([]OrderProfitInfo{})
|
||||
orderProfitListUnsafe := make([]OrderProfitInfo, 0)
|
||||
if len(bankOrderIdList) == 0 {
|
||||
return orderProfitList
|
||||
orderProfitList.ForEach(func(info OrderProfitInfo) {
|
||||
orderProfitListUnsafe = append(orderProfitListUnsafe, info)
|
||||
})
|
||||
return orderProfitListUnsafe
|
||||
}
|
||||
bankOrderIdList = slice.Unique(bankOrderIdList)
|
||||
slice.ForEach(slice.Chunk(bankOrderIdList, 100), func(index int, item []string) {
|
||||
tmpOrderProfitList := make([]OrderProfitInfo, 0)
|
||||
_, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("bank_order_id__in", item).All(&tmpOrderProfitList)
|
||||
slice.ForEachConcurrent(slice.Chunk(bankOrderIdList, 100), func(index int, item []string) {
|
||||
tmpOrderProfitListTmp := make([]OrderProfitInfo, 0)
|
||||
_, err := o.QueryTable(ORDER_PROFIT_INFO).Filter("bank_order_id__in", item).All(&tmpOrderProfitListTmp)
|
||||
if err != nil {
|
||||
logs.Error("GetOrderProfitByBankOrderId fail:", err)
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user