From d336f757ed3b9551de6ff0fc7e3829c6d9f0d65a Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Mon, 26 Jun 2023 19:51:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=A5=E4=BB=B7=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/logic/quotationdetaillogic.go | 37 +++++++------------ utils/collect/collect.go | 25 +++++++++++++ utils/collect/collect_test.go | 10 +++++ 3 files changed, 49 insertions(+), 23 deletions(-) diff --git a/server/backend/internal/logic/quotationdetaillogic.go b/server/backend/internal/logic/quotationdetaillogic.go index 17cf4ee6..aaa83fd2 100644 --- a/server/backend/internal/logic/quotationdetaillogic.go +++ b/server/backend/internal/logic/quotationdetaillogic.go @@ -88,16 +88,18 @@ func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, us //获取备注模板 markList, err := l.svcCtx.AllModels.FsQuotationRemarkTemplate.GetAllSelectContent(l.ctx) - var productIds = collect.ArrayColumn[int64](CanteenProduct, "product_id") + var productIds = collect.ArrayColumnTag[int64](CanteenProduct, "product_id") productList, err := l.svcCtx.AllModels.FsProduct.FindAllOnlyByIds(l.ctx, productIds) - collect.Array2MapByKeyTag[int64](productList, "id") + productListMap := collect.Array2MapByKeyTag[int64](productList, "id") //获取size信息 - var sizeIds = collect.ArrayColumn[int64](CanteenProduct, "SizeId") + var sizeIds = collect.ArrayColumnTag[int64](CanteenProduct, "size_id") sizes, err := l.svcCtx.AllModels.FsProductSize.GetAllSelectIdAndCapacityByIds(l.ctx, sizeIds) + sizesMap := collect.Array2MapByKeyTag[int64](sizes, "id") //获取价格信息 productPrice, err := l.svcCtx.AllModels.FsProductPrice.GetAllSelectBySizeId(l.ctx, sizeIds) + productPriceMap := collect.Array2MapByKeyTag[int64](productPrice, "size_id") // product := []map[string]interface{}{} product := []map[string]interface{}{} @@ -107,7 +109,7 @@ func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, us if !qFlag { - if price, ok := productPrice[parr.Get("size_id").Int()]; !ok { + if price, ok := productPriceMap[parr.Get("size_id").Int()]; !ok { priceList = []map[string]int64{ {"num": 1, "price": 0}, {"num": 1, "price": 0}, @@ -134,8 +136,6 @@ func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, us } } - - var ( Id any priceInfo any @@ -185,11 +185,14 @@ func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, us sid = parr.Get("sid").Int() } - productList - - $productList[$parr['product_id']]['title'] - - + product := productListMap[parr.Get("product_id").Int()] + name = product.Title + size = sizesMap[parr.Get("size_id").Int()].Capacity + cycle = *product.ProduceDays + *product.DeliveryDays + img = "" + mark = []string{} + priceInfo = priceList + num = 1 } product = append(product, map[string]interface{}{ @@ -205,18 +208,6 @@ func (l *QuotationDetailLogic) QuotationDetail(req *types.RequestQuotationId, us "num": num, }) - // product = append(product, map[string]interface{}{ - // "id": qFlag ? parr.ID : nil, - // "sId": parr.SID ? parr.SID : nil, - // "isGift": qFlag && parr.IsGift, - // "name": qFlag ? parr.Name : productList[parr.ProductID].Title, - // "size": qFlag ? parr.Size : sizes[parr.SizeId].Capacity, - // "cycle": qFlag ? parr.Cycle : productList[parr.ProductID].ProduceDays + productList[parr.ProductID].DeliveryDays, - // "img": qFlag ? parr.Img : "", - // "mark": qFlag && parr.Remark ? json.Unmarshal(parr.Remark) : nil, - // "priceList": qFlag ? json.Unmarshal(parr.PriceInfo) : priceList, - // "num": qFlag ? parr.Num : 1, - // }) } return resp.SetStatus(basic.CodeOK) diff --git a/utils/collect/collect.go b/utils/collect/collect.go index af25cb3a..1f65c842 100644 --- a/utils/collect/collect.go +++ b/utils/collect/collect.go @@ -18,6 +18,31 @@ func ArrayColumn[R any, T any](arr []T, column string) []R { return result } +func ArrayColumnTag[R any, T any](arrSrc []T, tag string) []R { + var result []R + arr := reflect.ValueOf(arrSrc) + if arr.Len() == 0 { + return result + } + + eleType := arr.Index(0).Elem().Type() + + for j := 0; j < eleType.NumField(); j++ { + if value, ok := eleType.Field(j).Tag.Lookup("json"); ok && value == tag { + + for i := 0; i < arr.Len(); i++ { + srcv := arr.Index(i) + fv := srcv.Elem().Field(j) + result = append(result, fv.Interface().(R)) + } + + return result + } + } + + return result +} + func ArrayIndex[T any](arr []T, index int) (result T, ok bool) { if index < len(arr) { result = arr[index] diff --git a/utils/collect/collect_test.go b/utils/collect/collect_test.go index a1ff745d..d2448a8b 100644 --- a/utils/collect/collect_test.go +++ b/utils/collect/collect_test.go @@ -30,3 +30,13 @@ func TestArray2MapByKeyTag(t *testing.T) { log.Printf("%##v", a) log.Println(len(a)) } + +func TestArrayColumnTag(t *testing.T) { + var abcs []*ABC = []*ABC{ + {1, "2", 3}, + {3, "1", 2}, + } + a := ArrayColumnTag[string](abcs, "b") + log.Printf("%##v", a) + log.Println(len(a)) +}