读取永辉Excel

This commit is contained in:
苏尹岚
2019-11-13 17:06:18 +08:00
parent 3c46efe89d
commit 705aa5153c

View File

@@ -20,80 +20,21 @@ import (
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
sheetNames = []string{"蔬菜", "水果", "肉禽", "净配", "水产", "干货", "MINI肉禽价格"}
sheet1Name = "蔬菜"
sheet2Name = "水果"
sheet3Name = "肉禽"
sheet4Name = "净配"
sheet5Name = "水产"
sheet6Name = "干货"
sheet7Name = "MINI肉禽价格"
)
type SheetParam struct {
SkuIDCol int
SkuPriceCol int
OrgSkuIdCol int
OrgSkuPriceCol int
SkuRow int
}
//sheet1 蔬菜
var (
sheet1SkuIDCol = 0 //商品的spu编码
sheet1SkuPriceCol = 14 //对应商品的成本价
sheet1OrgSkuIdCol = 5 //原料的spu编码
sheet1OrgSkuPriceCol = 8 //对应原料的成本价
sheet1SkuRow = 2
)
//sheet2 水果
var (
sheet2SkuIDCol = 0
sheet2SkuPriceCol = 14
sheet2OrgSkuIdCol = 5
sheet2OrgSkuPriceCol = 8
sheet2SkuRow = 2
)
//sheet3 肉禽
var (
sheet3SkuIDCol = 0
sheet3SkuPriceCol = 12
sheet3OrgSkuIdCol = 4
sheet3OrgSkuPriceCol = 7
sheet3SkuRow = 1
)
//sheet4 净配
var (
sheet4SkuIDCol = 0
sheet4SkuPriceCol = 12
sheet4OrgSkuIdCol = 4
sheet4OrgSkuPriceCol = 7
sheet4SkuRow = 1
)
//sheet5 水产
var (
sheet5SkuIDCol = 1
sheet5SkuPriceCol = 15
sheet5OrgSkuIdCol = 6
sheet5OrgSkuPriceCol = 9
sheet5SkuRow = 1
)
//sheet6 干货
var (
sheet6SkuIDCol = 0
sheet6SkuPriceCol = 13
sheet6OrgSkuIdCol = 4
sheet6OrgSkuPriceCol = 7
sheet6SkuRow = 2
)
//sheet7 MINI肉禽价格
var (
sheet7SkuIDCol = 1
sheet7SkuPriceCol = 5
sheet7SkuRow = 1
sheetMap = make(map[string]*SheetParam)
)
const (
parallelCount = 5
UpdateGoodsShelfStatusCount = 50 //微盟下架商品api限制一次50个
)
func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, isAsync bool) (hint string, err error) {
@@ -101,8 +42,8 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
skuMap = make(map[string]float64)
errMsg string
costPrice float64 //成本价
goodsIDListForPutAway []int64
goodsList []*weimobapi.GoodsInfo
goodsIDListForPutAway []int64
)
db := dao.GetDB()
if len(files) == 0 {
@@ -115,105 +56,62 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
if err != nil {
return "", err
}
initSheetMap()
taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
switch step {
case 0:
taskFunc1 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
v := batchItemList[0].(string)
rows, _ := xlsx.GetRows(v)
for k, _ := range sheetMap {
sheetParam := sheetMap[k]
rows, _ := xlsx.GetRows(k)
for rowNum, row := range rows {
switch v {
case sheet1Name:
if rowNum < sheet1SkuRow {
if rowNum < sheetParam.SkuRow {
continue
}
GetCellIntoMap(sheet1SkuIDCol, sheet1SkuPriceCol, sheet1OrgSkuIdCol, sheet1OrgSkuPriceCol, skuMap, true, row, v, rowNum)
GetCellIntoMap(sheetParam.SkuIDCol, sheetParam.SkuPriceCol, sheetParam.OrgSkuIdCol, sheetParam.OrgSkuPriceCol, skuMap, row, k, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet2Name:
if rowNum < sheet2SkuRow {
continue
}
GetCellIntoMap(sheet2SkuIDCol, sheet2SkuPriceCol, sheet2OrgSkuIdCol, sheet2OrgSkuPriceCol, skuMap, true, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet3Name:
if rowNum < sheet3SkuRow {
continue
}
GetCellIntoMap(sheet3SkuIDCol, sheet3SkuPriceCol, sheet3OrgSkuIdCol, sheet3OrgSkuPriceCol, skuMap, true, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet4Name:
if rowNum < sheet4SkuRow {
continue
}
GetCellIntoMap(sheet4SkuIDCol, sheet4SkuPriceCol, sheet4OrgSkuIdCol, sheet4OrgSkuPriceCol, skuMap, true, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet5Name:
if rowNum < sheet5SkuRow {
continue
}
GetCellIntoMap(sheet5SkuIDCol, sheet5SkuPriceCol, sheet5OrgSkuIdCol, sheet5OrgSkuPriceCol, skuMap, true, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet6Name:
if rowNum < sheet6SkuRow {
continue
}
GetCellIntoMap(sheet6SkuIDCol, sheet6SkuPriceCol, sheet6OrgSkuIdCol, sheet6OrgSkuPriceCol, skuMap, true, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
case sheet7Name:
if rowNum < sheet7SkuRow {
continue
}
GetCellIntoMap(sheet7SkuIDCol, sheet7SkuPriceCol, 0, 0, skuMap, false, row, v, rowNum)
if len(skuMap) < 1 {
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", v)
}
default:
return retVal, errors.New("未找到配置的sheet页请检查excel内容排版格式是否发生变化\n")
errMsg += fmt.Sprintf("读取Excel数据失败Excel格式排版可能发生了变化sheetName: [%v]", k)
}
}
if errMsg != "" {
return "", errors.New(errMsg)
}
return retVal, err
}
taskParallel1 := tasksch.NewParallelTask("读取Excel", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc1, sheetNames)
tasksch.HandleTask(taskParallel1, task, true).Run()
_, err = taskParallel1.GetResult(0)
case 1:
//获取微盟所有商品
goodsList = GetGoodsInfoAndDetail()
goodsList, err = GetWeiMobGoodsList()
if err != nil {
baseapi.SugarLogger.Errorf("GetWeiMobGoodsList error:%v", err)
}
taskFunc2 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
v := batchItemList[0].(*weimobapi.GoodsInfo)
goodsDetail, err := api.WeimobAPI.QueryGoodsDetail(v.GoodsID)
if err != nil {
baseapi.SugarLogger.Errorf("QueryGoodsDetail error:%v", err)
}
v.GoodsDetailInfo = goodsDetail
return retVal, err
}
taskParallel2 := tasksch.NewParallelTask("获取微盟商品", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList)
tasksch.HandleTask(taskParallel2, task, true).Run()
_, err = taskParallel2.GetResult(0)
//找出excel上有微盟没有的,有就列出报错,不进行更新
goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList)
delete(skuMap, "")
for k, v := range skuMap {
fmt.Printf("key %v, value %v\n", k, v)
for k, _ := range skuMap {
//表示excel上有微盟上没有
if goodsInfoAndDetailMap[k] == nil {
errMsg += fmt.Sprintf("在微盟上未找到该商品excel商品ID : [%v]\n", k)
}
}
fmt.Println(len(skuMap))
if errMsg != "" {
return "", errors.New(errMsg)
}
case 2:
//找出微盟上有excel上没有的有就更新没有就下架
taskFunc2 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
taskFunc3 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
goods := batchItemList[0].(*weimobapi.GoodsInfo)
goodsDetail := goods.GoodsDetailInfo
spuCode := goodsDetail.OuterGoodsCode
goodsIDListForPutAway := []int64{}
if spuCode != "" {
//如果微盟商品里找得到excel中的商品
if skuMap[spuCode] != 0 {
@@ -236,27 +134,32 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID)
}
}
retVal = goodsIDListForPutAway
return retVal, err
}
taskParallel2 := tasksch.NewParallelTask("获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList)
tasksch.HandleTask(taskParallel2, task, true).Run()
_, err = taskParallel2.GetResult(0)
taskParallel3 := tasksch.NewParallelTask("根据获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc3, goodsList)
tasksch.HandleTask(taskParallel3, task, true).Run()
goodsIDListForPutAwayInterface, err2 := taskParallel3.GetResult(0)
if err = err2; err != nil {
return "", err
}
// 批量下架微盟商品
for _, v := range goodsIDListForPutAwayInterface {
goodsIDListForPutAway = append(goodsIDListForPutAway, v.(int64))
}
case 3:
//批量下架微盟商品
if errMsg == "" {
if len(goodsIDListForPutAway) <= 50 { //api要求最多传50个
PutAwayWeiMobSku(goodsIDListForPutAway)
} else {
for i := 0; i < len(goodsIDListForPutAway)/50+1; i++ {
var int64Slice []int64
if len(goodsIDListForPutAway) > 50*(i+1) {
int64Slice = goodsIDListForPutAway[i*50 : (i+1)*50+1]
} else {
int64Slice = goodsIDListForPutAway[i*50 : len(goodsIDListForPutAway)]
taskFunc4 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
int64Slice := []int64{}
for _, v := range batchItemList {
int64Slice = append(int64Slice, v.(int64))
}
PutAwayWeiMobSku(int64Slice)
return retVal, err
}
}
taskParallel4 := tasksch.NewParallelTask("下架微盟商品", tasksch.NewParallelConfig().SetParallelCount(parallelCount).SetBatchSize(UpdateGoodsShelfStatusCount), ctx, taskFunc4, goodsIDListForPutAway)
tasksch.HandleTask(taskParallel4, task, true).Run()
_, err = taskParallel4.GetResult(0)
}
}
return result, err
@@ -284,28 +187,13 @@ func GetGoodsInfoAndDetailMap(goodsList []*weimobapi.GoodsInfo) (goodsMap map[st
return goodsMap
}
func GetGoodsInfoAndDetail() []*weimobapi.GoodsInfo {
goodsList, err := GetWeiMobGoodsList()
if err != nil {
baseapi.SugarLogger.Errorf("GetWeiMobGoodsList error:%v", err)
}
for _, v := range goodsList {
goodsDetail, err := api.WeimobAPI.QueryGoodsDetail(v.GoodsID)
if err != nil {
baseapi.SugarLogger.Errorf("QueryGoodsDetail error:%v", err)
}
v.GoodsDetailInfo = goodsDetail
}
return goodsList
}
func updateWeiMobGoods(costPrice, salePrice float64, goodsDetail *weimobapi.GoodsDetailInfo) (goodsID int64, skuMap map[string]int64, err error) {
var (
skuListParam []*weimobapi.SkuList
deliveryTypeListSlice []int64
skuListInfo = goodsDetail.SkuList[0]
categoryList []*weimobapi.CategoryList
skuListInfo = goodsDetail.SkuList[0]
skuListParam []*weimobapi.SkuList
categoryID int
deliveryTypeListSlice []int64
)
//查询配送方式
@@ -326,12 +214,7 @@ func updateWeiMobGoods(costPrice, salePrice float64, goodsDetail *weimobapi.Good
//寻找分类子ID
categoryList = goodsDetail.CategoryList
if len(categoryList) > 0 {
for k, v := range categoryList {
if k < len(categoryList)-1 {
continue
}
categoryID = v.CategoryID
}
categoryID = categoryList[len(categoryList)-1].CategoryID
} else {
return 0, nil, errors.New(fmt.Sprintf("未查询到此商品的分类信息goodsID : [%v] ,", goodsDetail.GoodsID))
}
@@ -401,7 +284,7 @@ func IsChineseChar(str string) bool {
return false
}
func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuMap map[string]float64, isHaveOrg bool, row []string, sheetName string, rowNum int) {
func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuMap map[string]float64, row []string, sheetName string, rowNum int) {
var (
skuID string
orgSkuID string
@@ -410,23 +293,21 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
)
for k, cell := range row {
if !IsChineseChar(cell) && cell != "" {
if k == skuIDCol {
if k == skuIDCol && skuIDCol >= 0 {
skuID = cell
}
if k == skuPriceCol {
if k == skuPriceCol && skuPriceCol >= 0 {
skuPrice = Float64Round(utils.Str2Float64(cell))
}
if isHaveOrg {
if k == orgSkuIDCol {
if k == orgSkuIDCol && orgSkuIDCol >= 0 {
orgSkuID = "0" + cell
}
if k == orgSkuPriceCol {
if k == orgSkuPriceCol && orgSkuPriceCol >= 0 {
orgSkuPrice = Float64Round(utils.Str2Float64(cell))
}
}
}
}
if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice && skuID != "" {
if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice {
if skuPrice > skuMap[skuID] {
skuMap[skuID] = skuPrice
}
@@ -434,16 +315,15 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
} else {
skuMap[skuID] = skuPrice
}
if isHaveOrg {
if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice && orgSkuID != "" {
if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice {
if orgSkuPrice > skuMap[orgSkuID] {
skuMap[orgSkuID] = orgSkuPrice
}
// fmt.Sprintf("读取excel表格出错有商品ID重复且价格不同,sheetName : [%v] ,行数 : [%v], 商品编码 [%v] , 价格:[%v]\n", sheetName, rowNum, orgSkuID, orgSkuPrice)
} else {
} else if orgSkuPriceCol >= 0 && orgSkuIDCol >= 0 {
skuMap[orgSkuID] = orgSkuPrice
}
}
delete(skuMap, "")
}
func Float64Round(f float64) (flt float64) {
@@ -453,3 +333,55 @@ func Float64Round(f float64) (flt float64) {
}
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,
}
}