Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄成意
/
go_sku_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a68834bb
authored
Feb 28, 2025
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
重量转换
parent
d46316c8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
3 deletions
model/ly_sku.go
pkg/common/function.go
service/service_ly.go
model/ly_sku.go
View file @
a68834bb
...
...
@@ -150,8 +150,13 @@ type SpuExtra struct {
Thumbnail
string
`bson:"thumbnail" json:"thumbnail"`
}
`bson:"image_list" json:"image_list"`
SpuDetail
string
`bson:"spu_detail" json:"spu_detail"`
SpuId
string
`bson:"spu_id" json:"spu_id"`
SpuDetail
string
`bson:"spu_detail" json:"spu_detail"`
SpuId
string
`bson:"spu_id" json:"spu_id"`
Height
string
`json:"height" bson:"height"`
Weight
string
`json:"-" bson:"weight"`
TransformedWeight
int
`json:"weight"`
Width
string
`json:"width" bson:"width"`
Length
string
`json:"length" bson:"length"`
}
// 为什么不直接映射到结构,而要用gjson,因为redis存的数据结构不一定正常,可能类型不一致
...
...
pkg/common/function.go
View file @
a68834bb
...
...
@@ -625,3 +625,41 @@ func MulFloat(first float64, args ...float64) float64 {
func
DivFloat
(
first
float64
,
second
float64
)
float64
{
return
((
first
*
1000000000
)
/
(
second
*
1000000000
))
}
// 将重量单位统一转换为克,同时清洗数据
func
ConvertToGrams
(
input
string
)
(
int
,
error
)
{
// 清洗数据:去除左右空格,去除中间空格
input
=
strings
.
ReplaceAll
(
strings
.
TrimSpace
(
input
),
" "
,
""
)
// 分离数字和单位
var
numStr
,
unit
string
for
i
,
char
:=
range
input
{
if
char
>=
'0'
&&
char
<=
'9'
||
char
==
'.'
{
numStr
+=
string
(
char
)
}
else
{
unit
=
input
[
i
:
]
break
}
}
// 将数字部分转换为浮点数
num
,
err
:=
strconv
.
ParseFloat
(
numStr
,
64
)
if
err
!=
nil
{
return
0
,
fmt
.
Errorf
(
"无效的数字: %s"
,
numStr
)
}
// 将单位转换为小写,方便匹配
unit
=
strings
.
ToLower
(
unit
)
// 根据单位返回对应的克数
switch
unit
{
case
"kg"
,
"千克"
:
return
int
(
num
*
1000
),
nil
case
"g"
,
"克"
:
return
int
(
num
),
nil
case
"t"
,
"吨"
:
return
int
(
num
*
1000000
),
nil
default
:
return
0
,
fmt
.
Errorf
(
"未知的重量单位: %s"
,
unit
)
}
}
\ No newline at end of file
service/service_ly.go
View file @
a68834bb
package
service
import
(
"fmt"
"go_sku_server/model"
"go_sku_server/pkg/common"
"go_sku_server/pkg/gredis"
"go_sku_server/pkg/logger"
"go_sku_server/pkg/mongo"
...
...
@@ -291,6 +293,10 @@ func (ls *LyService) GetSpuExtra(spuId string) (spuExtra model.SpuExtra) {
if
err
!=
nil
&&
err
!=
mgo
.
ErrNotFound
{
logger
.
Select
(
"sku_query"
)
.
Error
(
err
.
Error
())
}
fmt
.
Println
(
spuExtra
.
Weight
)
if
spuExtra
.
Weight
!=
""
{
spuExtra
.
TransformedWeight
,
_
=
common
.
ConvertToGrams
(
spuExtra
.
Weight
)
}
//兼容老版本数据
if
len
(
spuExtra
.
ImageList
)
==
0
{
...
...
@@ -304,7 +310,6 @@ func (ls *LyService) GetSpuExtra(spuId string) (spuExtra model.SpuExtra) {
err
=
mongodb
.
DB
(
"ichunt"
)
.
C
(
"spu_extra"
)
.
Find
(
bson
.
M
{
"spu_id"
:
spuId
})
.
One
(
&
oldSpuExtra
)
for
_
,
image
:=
range
oldSpuExtra
.
OldImageList
{
spuExtra
.
ImageList
=
append
(
spuExtra
.
ImageList
,
struct
{
Name
string
`bson:"name" json:"name"`
Thumbnail
string
`bson:"thumbnail" json:"thumbnail"`
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment