zhangzengfei
2023-06-29 1bed0e1cecc21294580e3447516a90d4b3e270a7
report/tasks.go
@@ -3,11 +3,12 @@
import (
   "encoding/json"
   "io/ioutil"
   "kingdee-dbapi/logger"
   "time"
   "kingdee-dbapi/cache"
   "kingdee-dbapi/config"
   "kingdee-dbapi/kingdee"
   "kingdee-dbapi/logger"
   "kingdee-dbapi/models"
   "kingdee-dbapi/nsqclient"
)
@@ -55,7 +56,7 @@
   //ok := nsqclient.HttpPost(config.Options.OrderTopic, b)
   if len(list) == 0 {
      logger.Debug("没有新的订单需要上报")
      logger.Debug("没有新的订单数据")
      return
   }
@@ -78,8 +79,22 @@
   }
}
var invReportedCache = make(map[string]float64, 0)
var fullLoad bool
func SendInventory() {
   var list []kingdee.Inventory
   // 设置每天凌晨1点上报一次全量数据
   hour := time.Now().Hour()
   if hour == 1 {
      if fullLoad == false {
         invReportedCache = make(map[string]float64, 0)
         fullLoad = true
      }
   } else {
      fullLoad = false
   }
   if config.Options.Debug {
      data, err := ioutil.ReadFile(inventoryLocalStore)
@@ -95,10 +110,41 @@
      }
   } else {
      list = kingdee.ICInventory()
      logger.Debug("查询到%d条库存数据", len(list))
      // 先过滤一遍数据, 格瑞米发现有同一个产品同批号同仓库, 有多条库存记录的情况
      // 将类似的数据库存数累计到一起
      var filterMap = make(map[string]float64, 0)
      for i := 0; i < len(list); {
         cacheKey := list[i].FNumber + list[i].FBatchNo + list[i].FStockNo
         if qty, ok := filterMap[cacheKey]; ok {
            filterMap[cacheKey] = list[i].FUnitQty + qty
            list = append(list[:i], list[i+1:]...)
         } else {
            filterMap[cacheKey] = list[i].FUnitQty
            i++
         }
      }
      // 过滤数据, 判断是否已经上报过
      for i := 0; i < len(list); {
         cacheKey := list[i].FNumber + list[i].FBatchNo + list[i].FStockNo
         if qty, ok := invReportedCache[cacheKey]; ok && qty == list[i].FUnitQty {
            list = append(list[:i], list[i+1:]...)
         } else {
            invReportedCache[cacheKey] = list[i].FUnitQty
            i++
         }
      }
      if len(list) == 0 {
         logger.Debug("没有要更新的库存数据.")
         return
      }
   }
   // 每次发 100 条
   // 每次发 1000 条
   successCnt := 0
   for i := 0; i < len(list); i += 1000 {
      end := i + 1000
@@ -119,9 +165,13 @@
      ok := nsqclient.Produce(config.Options.InventoryTopic, b)
      if !ok {
         logger.Warn("库存数据上报失败")
         //上报失败, 缓存清空
         invReportedCache = make(map[string]float64, 0)
      } else {
         successCnt = end
      }
   }
   logger.Debug("已上报%d条库存数据", successCnt)
}