diff --git a/product/internal/logic/getproductlistlogic.go b/product/internal/logic/getproductlistlogic.go index a75a4bba..83f81ce0 100644 --- a/product/internal/logic/getproductlistlogic.go +++ b/product/internal/logic/getproductlistlogic.go @@ -14,7 +14,6 @@ import ( "fusenapi/utils/image" "github.com/zeromicro/go-zero/core/stores/sqlx" "sort" - "strconv" "strings" "time" @@ -94,14 +93,17 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login //存储产品最小价格 mapProductMinPrice := make(map[int64]int64) for _, v := range productPriceList { - priceSlic := strings.Split(v.Price, ",") - sort.Strings(priceSlic) - min, err := strconv.ParseInt(priceSlic[0], 10, 64) + priceStrSlic := strings.Split(v.Price, ",") + priceSlice, err := format.StrSlicToIntSlice(priceStrSlic) if err != nil { logx.Error(err) - return &types.Response{Code: 510, Message: "parse min product price err"}, nil + return &types.Response{Code: 510, Message: err.Error()}, nil } - mapProductMinPrice[v.ProductId] = min + if len(priceSlice) == 0 { + continue + } + sort.Ints(priceSlice) + mapProductMinPrice[v.ProductId] = int64(priceSlice[0]) } //获取模板 productTemplateModel := model.NewFsProductTemplateV2Model(l.svcCtx.MysqlConn) diff --git a/utils/format/str_to_int_slice.go b/utils/format/str_to_int_slice.go new file mode 100644 index 00000000..e6f28713 --- /dev/null +++ b/utils/format/str_to_int_slice.go @@ -0,0 +1,21 @@ +package format + +import ( + "strconv" +) + +// 字符串切片转int切片 +func StrSlicToIntSlice(input []string) ([]int, error) { + priceSlic := make([]int, 0, len(input)) + for _, p := range input { + if p == "" { + continue + } + price, err := strconv.Atoi(p) + if err != nil { + return nil, err + } + priceSlic = append(priceSlic, price) + } + return priceSlic, nil +}