zhangzengfei
2023-08-17 d9bc2c9e991b2fe925565dfd7d0ec667b64bb52f
report/send.go
@@ -2,7 +2,6 @@
import (
   "encoding/json"
   "io/ioutil"
   "time"
   "kingdee-dbapi/cache"
@@ -130,7 +129,10 @@
         logger.Warn("库存数据上报失败")
         //上报失败, 缓存清空
         invReportedCache = make(map[string]float64, 0)
         for _, v := range list[i:end] {
            key := v.FNumber + v.FBatchNo + v.FStockNo
            delete(invReportedCache, key)
         }
      } else {
         successCnt = end
      }
@@ -139,54 +141,74 @@
   logger.Debug("已上报%d条库存数据", successCnt)
}
// bom数据分bom和bomChild两个表
func SendBom(fData bool) {
   var bomList []kingdee.ICBom
   var bomChildList []kingdee.ICBomChild
   // 上报bom
   bomList := kingdee.BomList(fData)
   logger.Debug("查询到%d条Bom数据", len(bomList))
   b, _ := json.Marshal(bomList)
   ioutil.WriteFile("bomList.tmp", b, 0644)
   bomList = kingdee.BomList(fData)
   logger.Debug("查询到%d条BOM数据", len(bomList))
   // 过滤数据, 判断是否已经上报过, 请求全量数据不过滤, 直接上报
   if fData {
   if !fData {
      var bomNumbers []string
      for i := 0; i < len(bomList); {
         cacheKey := bomList[i].FBOMNumber + bomList[i].FAudDate
         if _, ok := bomReportedCache[cacheKey]; ok {
            bomList = append(bomList[:i], bomList[i+1:]...)
         } else {
            bomReportedCache[cacheKey] = struct{}{}
            bomNumbers = append(bomNumbers, bomList[i].FBOMNumber)
            i++
         }
      }
      if len(bomNumbers) > 0 {
         bomChildList = kingdee.BomChild(bomNumbers)
      }
   } else {
      bomChildList = kingdee.BomChild(nil)
   }
   // 分组上传bom数据
   if len(bomList) == 0 {
      logger.Debug("没有要更新的Bom数据.")
      logger.Debug("没有要更新的BOM数据.")
   } else {
      // 每次发 1000 条
      // 将bom组件按bomId整理
      var bomChildMap = make(map[int][]kingdee.ICBomChild, 0)
      for idx, bomChild := range bomChildList {
         bomChildMap[bomChild.FInterID] = append(bomChildMap[bomChild.FInterID], bomChildList[idx])
      }
      // 给bom添加组件
      for idx, bom := range bomList {
         bomList[idx].Component = bomChildMap[bom.FInterID]
      }
      // 每次发 200 条
      successCnt := 0
      for i := 0; i < len(bomList); i += 1000 {
         end := i + 1000
      for i := 0; i < len(bomList); i += 200 {
         end := i + 200
         if end > len(bomList) {
            end = len(bomList)
         }
         b, _ := json.Marshal(bomList[i:end])
         // TCP协议上报
         ok := nsqclient.Produce(config.Options.BomTopic, b)
         if !ok {
            logger.Warn("BOM数据上报失败")
            //上报失败, 缓存清空
            bomReportedCache = make(map[string]struct{}, 0)
            for _, v := range bomList[i:end] {
               key := v.FBOMNumber + v.FAudDate
               delete(bomReportedCache, key)
            }
         } else {
            successCnt = end
         }
      }
      logger.Debug("已上报%d条BOM数据", successCnt)
   }
   // 上报bom子项
}