读取永辉Excel

This commit is contained in:
苏尹岚
2019-11-13 18:11:58 +08:00
parent 705aa5153c
commit 8df7f897fa

View File

@@ -3,8 +3,8 @@ package yonghui
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"mime/multipart" "mime/multipart"
"strconv"
"unicode" "unicode"
"git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi"
@@ -29,7 +29,51 @@ type SheetParam struct {
} }
var ( var (
sheetMap = make(map[string]*SheetParam) sheetMap = map[string]*SheetParam{
"蔬菜": &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 14,
OrgSkuIdCol: 5,
OrgSkuPriceCol: 8,
SkuRow: 2,
}, "水果": &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 14,
OrgSkuIdCol: 5,
OrgSkuPriceCol: 8,
SkuRow: 2,
}, "肉禽": &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 12,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 1,
}, "净配": &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 12,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 1,
}, "水产": &SheetParam{
SkuIDCol: 1,
SkuPriceCol: 15,
OrgSkuIdCol: 6,
OrgSkuPriceCol: 9,
SkuRow: 1,
}, "干货": &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 13,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 2,
}, "MINI肉禽价格": &SheetParam{
SkuIDCol: 1,
SkuPriceCol: 5,
OrgSkuIdCol: -1,
OrgSkuPriceCol: -1,
SkuRow: 1,
},
}
) )
const ( const (
@@ -43,23 +87,20 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
errMsg string errMsg string
costPrice float64 //成本价 costPrice float64 //成本价
goodsList []*weimobapi.GoodsInfo goodsList []*weimobapi.GoodsInfo
goodsIDListForPutAway []int64 goodsIDListForPutAway []interface{}
) )
db := dao.GetDB() db := dao.GetDB()
if len(files) == 0 { if len(files) == 0 {
return "", errors.New("没有文件上传!") return "", errors.New("没有文件上传!")
} }
//读取excel文件
xlsx, err := excelize.OpenFile(files[0].Filename)
// xlsx, err := excelize.OpenFile("111.xlsx")
if err != nil {
return "", err
}
initSheetMap()
taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) { taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
switch step { switch step {
case 0: case 0:
//读取excel文件
xlsx, err := excelize.OpenFile(files[0].Filename)
if err != nil {
return "", err
}
for k, _ := range sheetMap { for k, _ := range sheetMap {
sheetParam := sheetMap[k] sheetParam := sheetMap[k]
rows, _ := xlsx.GetRows(k) rows, _ := xlsx.GetRows(k)
@@ -111,7 +152,6 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
goods := batchItemList[0].(*weimobapi.GoodsInfo) goods := batchItemList[0].(*weimobapi.GoodsInfo)
goodsDetail := goods.GoodsDetailInfo goodsDetail := goods.GoodsDetailInfo
spuCode := goodsDetail.OuterGoodsCode spuCode := goodsDetail.OuterGoodsCode
goodsIDListForPutAway := []int64{}
if spuCode != "" { if spuCode != "" {
//如果微盟商品里找得到excel中的商品 //如果微盟商品里找得到excel中的商品
if skuMap[spuCode] != 0 { if skuMap[spuCode] != 0 {
@@ -131,10 +171,9 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
} }
} else { } else {
//下架微盟商品 //下架微盟商品
goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID) retVal = []int64{goodsDetail.GoodsID}
} }
} }
retVal = goodsIDListForPutAway
return retVal, err return retVal, err
} }
taskParallel3 := tasksch.NewParallelTask("根据获取的微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc3, goodsList) taskParallel3 := tasksch.NewParallelTask("根据获取的微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc3, goodsList)
@@ -144,9 +183,7 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
return "", err return "", err
} }
// 批量下架微盟商品 // 批量下架微盟商品
for _, v := range goodsIDListForPutAwayInterface { goodsIDListForPutAway = goodsIDListForPutAwayInterface
goodsIDListForPutAway = append(goodsIDListForPutAway, v.(int64))
}
case 3: case 3:
if errMsg == "" { if errMsg == "" {
taskFunc4 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { taskFunc4 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
@@ -297,13 +334,13 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
skuID = cell skuID = cell
} }
if k == skuPriceCol && skuPriceCol >= 0 { if k == skuPriceCol && skuPriceCol >= 0 {
skuPrice = Float64Round(utils.Str2Float64(cell)) skuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0))
} }
if k == orgSkuIDCol && orgSkuIDCol >= 0 { if k == orgSkuIDCol && orgSkuIDCol >= 0 {
orgSkuID = "0" + cell orgSkuID = "0" + cell
} }
if k == orgSkuPriceCol && orgSkuPriceCol >= 0 { if k == orgSkuPriceCol && orgSkuPriceCol >= 0 {
orgSkuPrice = Float64Round(utils.Str2Float64(cell)) orgSkuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0))
} }
} }
} }
@@ -327,61 +364,5 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
} }
func Float64Round(f float64) (flt float64) { func Float64Round(f float64) (flt float64) {
flt, err := strconv.ParseFloat(fmt.Sprintf("%.2f", f), 64) return math.Round(f*100) / 100
if err != nil {
flt = 0
}
return flt
}
func initSheetMap() {
sheetMap["蔬菜"] = &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 14,
OrgSkuIdCol: 5,
OrgSkuPriceCol: 8,
SkuRow: 2,
}
sheetMap["水果"] = &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 14,
OrgSkuIdCol: 5,
OrgSkuPriceCol: 8,
SkuRow: 2,
}
sheetMap["肉禽"] = &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 12,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 1,
}
sheetMap["净配"] = &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 12,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 1,
}
sheetMap["水产"] = &SheetParam{
SkuIDCol: 1,
SkuPriceCol: 15,
OrgSkuIdCol: 6,
OrgSkuPriceCol: 9,
SkuRow: 1,
}
sheetMap["干货"] = &SheetParam{
SkuIDCol: 0,
SkuPriceCol: 13,
OrgSkuIdCol: 4,
OrgSkuPriceCol: 7,
SkuRow: 2,
}
sheetMap["MINI肉禽价格"] = &SheetParam{
SkuIDCol: 1,
SkuPriceCol: 5,
OrgSkuIdCol: -1,
OrgSkuPriceCol: -1,
SkuRow: 1,
}
} }