87 lines
2.4 KiB
Go
87 lines
2.4 KiB
Go
|
|
package excelparse
|
||
|
|
|
||
|
|
import "strings"
|
||
|
|
|
||
|
|
// NativeProductParser handles the MuYu product import template and product exports.
|
||
|
|
type NativeProductParser struct{}
|
||
|
|
|
||
|
|
func (p *NativeProductParser) Name() string { return "木语产品模板" }
|
||
|
|
|
||
|
|
func (p *NativeProductParser) Match(headers []string) bool {
|
||
|
|
idx := buildIndex(headers)
|
||
|
|
if !hasAny(idx, "产品名称", "名称") {
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
return hasAny(idx, "产品码", "色号", "批次号", "纱线配比", "生产工艺")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (p *NativeProductParser) ParseRows(headers []string, rows [][]string) (*ParseResult, error) {
|
||
|
|
idx := buildIndex(headers)
|
||
|
|
seen := make(map[string]struct{}, len(rows))
|
||
|
|
products := make([]ParsedProduct, 0, len(rows))
|
||
|
|
skipped := 0
|
||
|
|
|
||
|
|
for _, row := range rows {
|
||
|
|
name := firstCol(row, idx, "产品名称", "名称")
|
||
|
|
if name == "" {
|
||
|
|
skipped++
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
|
||
|
|
productCode := firstCol(row, idx, "产品码", "Product Code", "product_code")
|
||
|
|
key := productCode
|
||
|
|
if key == "" {
|
||
|
|
key = strings.Join([]string{
|
||
|
|
name,
|
||
|
|
firstCol(row, idx, "规格型号", "规格"),
|
||
|
|
firstCol(row, idx, "颜色"),
|
||
|
|
firstCol(row, idx, "色号"),
|
||
|
|
}, "\x00")
|
||
|
|
}
|
||
|
|
if _, ok := seen[key]; ok {
|
||
|
|
skipped++
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
seen[key] = struct{}{}
|
||
|
|
|
||
|
|
products = append(products, ParsedProduct{
|
||
|
|
ProductName: name,
|
||
|
|
Spec: firstCol(row, idx, "规格型号", "规格"),
|
||
|
|
Color: firstCol(row, idx, "颜色"),
|
||
|
|
ColorNo: firstCol(row, idx, "色号"),
|
||
|
|
ProductCode: productCode,
|
||
|
|
SalesPrice: stripCurrency(firstCol(row, idx, "销售价", "参考售价")),
|
||
|
|
BatchNo: firstCol(row, idx, "批次号"),
|
||
|
|
YarnRatio: firstCol(row, idx, "纱线配比"),
|
||
|
|
WarpWeightGM: stripUnit(firstCol(row, idx, "经线克重(g/m)", "经线克重", "经纱克重")),
|
||
|
|
WeftWeightGM: stripUnit(firstCol(row, idx, "纬线克重(g/m)", "纬线克重", "纬纱克重")),
|
||
|
|
ProductionProcess: firstCol(row, idx, "生产工艺"),
|
||
|
|
Remark: firstCol(row, idx, "备注"),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
return &ParseResult{Products: products, SkippedRows: skipped}, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func hasAny(idx map[string]int, names ...string) bool {
|
||
|
|
for _, name := range names {
|
||
|
|
if _, ok := idx[name]; ok {
|
||
|
|
return true
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return false
|
||
|
|
}
|
||
|
|
|
||
|
|
func firstCol(row []string, idx map[string]int, names ...string) string {
|
||
|
|
for _, name := range names {
|
||
|
|
col, ok := idx[name]
|
||
|
|
if !ok {
|
||
|
|
continue
|
||
|
|
}
|
||
|
|
if val := colVal(row, col); val != "" {
|
||
|
|
return val
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ""
|
||
|
|
}
|