读取永辉Excel文件
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"git.rosy.net.cn/baseapi/platformapi/weimobapi"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
@@ -99,11 +100,11 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
|
||||
var (
|
||||
skuMap = make(map[string]float64)
|
||||
errMsg string
|
||||
// costPrice float64 //成本价
|
||||
// goodsIDListForPutAway []int64
|
||||
// goodsList []*weimobapi.GoodsInfo
|
||||
costPrice float64 //成本价
|
||||
goodsIDListForPutAway []int64
|
||||
goodsList []*weimobapi.GoodsInfo
|
||||
)
|
||||
// db := dao.GetDB()
|
||||
db := dao.GetDB()
|
||||
if len(files) == 0 {
|
||||
return "", errors.New("没有文件上传!")
|
||||
}
|
||||
@@ -127,117 +128,141 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
|
||||
continue
|
||||
}
|
||||
GetCellIntoMap(sheet1SkuIDCol, sheet1SkuPriceCol, sheet1OrgSkuIdCol, sheet1OrgSkuPriceCol, skuMap, true, row, v, 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")
|
||||
}
|
||||
}
|
||||
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()
|
||||
// //找出excel上有,微盟没有的,有就列出报错,不进行更新
|
||||
// goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList)
|
||||
// for k, _ := range skuMap {
|
||||
// //表示excel上有,微盟上没有
|
||||
// if goodsInfoAndDetailMap[k] == nil {
|
||||
// errMsg += fmt.Sprintf("在微盟上未找到该商品!excel商品ID : [%v]\n", k)
|
||||
// }
|
||||
// }
|
||||
// if errMsg != "" {
|
||||
// return "", errors.New(errMsg)
|
||||
// }
|
||||
// case 2:
|
||||
// //找出微盟上有,excel上没有的,有就更新,没有就下架
|
||||
// taskFunc2 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
// goods := batchItemList[0].(*weimobapi.GoodsInfo)
|
||||
// goodsDetail := goods.GoodsDetailInfo
|
||||
// spuCode := goodsDetail.OuterGoodsCode
|
||||
// if spuCode != "" {
|
||||
// //如果微盟商品里找得到excel中的商品
|
||||
// if skuMap[spuCode] != 0 {
|
||||
// //获取京西库商品
|
||||
// skuList, _ := dao.GetSkus(db, nil, []int{int(utils.Str2Int64(goodsDetail.SkuMap.SingleSku.OuterSkuCode))}, nil, nil)
|
||||
// if len(skuList) == 0 {
|
||||
// errMsg += fmt.Sprintf("在京西库中未找到该商品!name_id : [%v] \n", goodsDetail.SkuMap.SingleSku.OuterSkuCode)
|
||||
// } else {
|
||||
// if skuList[0].Unit != "份" {
|
||||
// costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode])
|
||||
// } else {
|
||||
// costPrice = skuMap[spuCode]
|
||||
// }
|
||||
// if errMsg == "" {
|
||||
// _, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail)
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// //下架微盟商品
|
||||
// goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID)
|
||||
// }
|
||||
// }
|
||||
// return retVal, err
|
||||
// }
|
||||
// taskParallel2 := tasksch.NewParallelTask("获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList)
|
||||
// tasksch.HandleTask(taskParallel2, task, true).Run()
|
||||
// _, err = taskParallel2.GetResult(0)
|
||||
// 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)]
|
||||
// }
|
||||
// PutAwayWeiMobSku(int64Slice)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
case 1:
|
||||
//获取微盟所有商品
|
||||
goodsList = GetGoodsInfoAndDetail()
|
||||
//找出excel上有,微盟没有的,有就列出报错,不进行更新
|
||||
goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList)
|
||||
delete(skuMap, "")
|
||||
for k, v := range skuMap {
|
||||
fmt.Printf("key %v, value %v\n", k, v)
|
||||
//表示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) {
|
||||
goods := batchItemList[0].(*weimobapi.GoodsInfo)
|
||||
goodsDetail := goods.GoodsDetailInfo
|
||||
spuCode := goodsDetail.OuterGoodsCode
|
||||
if spuCode != "" {
|
||||
//如果微盟商品里找得到excel中的商品
|
||||
if skuMap[spuCode] != 0 {
|
||||
//获取京西库商品
|
||||
skuList, _ := dao.GetSkus(db, nil, []int{int(utils.Str2Int64(goodsDetail.SkuMap.SingleSku.OuterSkuCode))}, nil, nil)
|
||||
if len(skuList) == 0 {
|
||||
errMsg += fmt.Sprintf("在京西库中未找到该商品!name_id : [%v] \n", goodsDetail.SkuMap.SingleSku.OuterSkuCode)
|
||||
} else {
|
||||
if skuList[0].Unit != "份" {
|
||||
costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode])
|
||||
} else {
|
||||
costPrice = skuMap[spuCode]
|
||||
}
|
||||
if errMsg == "" {
|
||||
_, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//下架微盟商品
|
||||
goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID)
|
||||
}
|
||||
}
|
||||
return retVal, err
|
||||
}
|
||||
taskParallel2 := tasksch.NewParallelTask("获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList)
|
||||
tasksch.HandleTask(taskParallel2, task, true).Run()
|
||||
_, err = taskParallel2.GetResult(0)
|
||||
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)]
|
||||
}
|
||||
PutAwayWeiMobSku(int64Slice)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
taskSeq := tasksch.NewSeqTask("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, taskSeqFunc, 4)
|
||||
taskSeq := tasksch.NewSeqTask("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, taskSeqFunc, 2)
|
||||
tasksch.HandleTask(taskSeq, nil, true).Run()
|
||||
for k, v := range skuMap {
|
||||
fmt.Println(k, v)
|
||||
}
|
||||
if errMsg != "" {
|
||||
baseapi.SugarLogger.Debugf(errMsg)
|
||||
}
|
||||
@@ -401,7 +426,7 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
|
||||
}
|
||||
}
|
||||
}
|
||||
if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice {
|
||||
if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice && skuID != "" {
|
||||
if skuPrice > skuMap[skuID] {
|
||||
skuMap[skuID] = skuPrice
|
||||
}
|
||||
@@ -410,7 +435,7 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM
|
||||
skuMap[skuID] = skuPrice
|
||||
}
|
||||
if isHaveOrg {
|
||||
if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice {
|
||||
if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice && orgSkuID != "" {
|
||||
if orgSkuPrice > skuMap[orgSkuID] {
|
||||
skuMap[orgSkuID] = orgSkuPrice
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxstore/yonghui"
|
||||
@@ -36,8 +35,6 @@ func (c *YongHuiController) LoadExcelByYongHui() {
|
||||
} else if c.Ctx.Input.IsPost() {
|
||||
c.callLoadExcelByYongHui(func(params *tYonghuiLoadExcelByYongHuiParams) (retVal interface{}, errCode string, err error) {
|
||||
r := c.Ctx.Request
|
||||
fmt.Println(r)
|
||||
fmt.Println(r.MultipartForm)
|
||||
files := r.MultipartForm.File["userfiles"]
|
||||
retVal, err = yonghui.LoadExcelByYongHui(params.Ctx, files, params.IsAsync)
|
||||
return retVal, "", err
|
||||
|
||||
Reference in New Issue
Block a user