Ver código fonte

海柔快上-增加缓存格口

zhaohuanhuan 5 meses atrás
pai
commit
e57896a592
1 arquivos alterados com 153 adições e 80 exclusões
  1. 153 80
      src/views/robot/putaway/index.vue

+ 153 - 80
src/views/robot/putaway/index.vue

@@ -21,7 +21,7 @@
             v-model="wallNo"
             placeholder="请扫描二次分拨墙号"
             label="二次分拨墙号:"
-            @search="scanType=2"
+            @search="_handlerScan(wallNo)"
             left-icon=""
             :class="[scanType===3?'search-input-barcode':'','van-hairline--bottom']"
             @focus="scanType=3"
@@ -158,8 +158,11 @@ const locationType = {
   'ST': '过渡库位',
   'WB': '组装工作区',
 }
-// 格口数据
-const bin = ref('') //当前格口
+// 格口管理相关变量
+const bin = ref('') //当前分配的格口号
+const BIN_STORAGE_KEY = 'BINDATA' //本地存储键名
+const MAX_BIN_NUMBER = 5 //最大格口限制
+
 // lotNumber与格口号的映射关系
 const lotNumberToBinMap = new Map()
 //推荐库位
@@ -254,6 +257,8 @@ const _handlerScan = (code) => {
       }
     } else if (scanType.value == 3) {
       wallNo.value = code
+      // 处理格口数据初始化和检查
+      handleWallDataInitialization()
       scanType.value = 2
     }
   }
@@ -300,58 +305,144 @@ const _getRecommendedLocation = async (lotNum, owner) => {
     console.error(err)
   }
 }
-// 设置库位
+// 格口数据管理相关函数
 const locationActive = ref([])
-const setLocation = (item) => {
-    locationActive.value = item
 
-    // 根据lotNumber分配固定格口
-    const lotNumber = barcodeActive.value.lotNumber
-    let isNewBin = false // 标记是否是新分配的格口
+// 获取存储的格口数据
+const getBinStorageData = () => {
+  return JSON.parse(localStorage.getItem(BIN_STORAGE_KEY)) || {}
+}
 
-    // 如果这个lotNumber已经有分配的格口,使用现有的
-    if (lotNumberToBinMap.has(lotNumber)) {
-      bin.value = lotNumberToBinMap.get(lotNumber).toString()
-    } else {
-      // 如果没有,分配下一个可用的格口
-      const nextBinNumber = lotNumberToBinMap.size + 1
-      if (nextBinNumber > 60) {
-        showToast({ duration: 3000, message: '格口号超过60,不进行后续操作' })
-        scanError()
-        return
-      }
-      bin.value = nextBinNumber.toString()
-      lotNumberToBinMap.set(lotNumber, nextBinNumber)
-      isNewBin = true // 标记为新分配的格口
-    }
+// 保存格口数据到本地存储
+const saveBinStorageData = (data) => {
+  localStorage.setItem(BIN_STORAGE_KEY, JSON.stringify(data))
+}
 
-    const data = {
-      warehouse,
-      equipment: wallNo.value,
-      container: containerNo.value,
-      barcode: barcodeActive.value.barcode,
-      bin:bin.value,
-      location: locationActive.value[0].location,
-      lotNum: barcodeActive.value.lotNumber,
-      sysVersion:'V6'
-    }
-    showLoading()
-    setBindAllocateWall(data).then(res => {
-      tips.value = `请将${searchBarcode.value},放入分拨墙-${wallNo.value}-《${data.bin}》格口`
-      playVoiceBin(Number(bin.value))
-      closeLoading()
-    }).catch(err => {
-      // 如果是新分配的格口且接口请求失败,删除刚刚分配的格口信息
-      if (isNewBin) {
-        lotNumberToBinMap.delete(lotNumber)
-      }
-      searchBarcode.value = ''
-      locationActive.value = []
-      bin.value = ''
-      tips.value = err.message
-      scanError()
-      closeLoading()
+// 初始化容器和分拨墙数据结构
+const initializeBinData = (storedData, container, wall) => {
+  if (!storedData[container]) {
+    storedData[container] = {}
+  }
+  if (!storedData[container][wall]) {
+    storedData[container][wall] = {}
+  }
+}
+
+// 处理分拨墙数据初始化
+const handleWallDataInitialization = () => {
+  const storedData = getBinStorageData()
+  initializeBinData(storedData, containerNo.value, wallNo.value)
+  const existingBinData = storedData[containerNo.value][wallNo.value]
+  console.log(existingBinData, "existingBinData")
+  if (existingBinData && Object.keys(existingBinData).length > 0) {
+    const maxValue = Math.max(...Object.values(existingBinData))
+    showConfirmDialog({
+      title: '格口已存在',
+      message: `容器 ${containerNo.value} 和分拨墙 ${wallNo.value} 下已存在格口号 ${maxValue}, 是否继续使用?`,
     })
+      .then(() => {
+        scanSuccess()
+        console.log(`继续使用分拨墙格口号累加`)
+      })
+      .catch(() => {
+        delete storedData[containerNo.value][wallNo.value]
+        scanSuccess()
+        showNotify({ type: 'danger', duration: 3000, message: `分拨墙:${wallNo.value},分配格口已删除` })
+        saveBinStorageData(storedData)
+      })
+      .finally(() => {
+        scanType.value = 2
+      })
+  }
+}
+// 计算下一个可用的格口号
+const calculateNextBinNumber = (existingBinData) => {
+  let nextBinNumber = 1
+  for (let lot in existingBinData) {
+    nextBinNumber = Math.max(nextBinNumber, existingBinData[lot] + 1)
+  }
+  return nextBinNumber
+}
+
+// 检查格口是否超限
+const isBinExceeded = (binNumber) => {
+  return binNumber > MAX_BIN_NUMBER
+}
+// 分配或获取格口号
+const allocateBinNumber = (storedData, container, wall, lotNumber) => {
+  const existingBinData = storedData[container][wall]
+
+  // 如果相同的lotNumber已存在,使用相同格口
+  if (existingBinData[lotNumber]) {
+    return existingBinData[lotNumber]
+  }
+
+  // 否则分配新的格口号
+  const nextBinNumber = calculateNextBinNumber(existingBinData)
+  existingBinData[lotNumber] = nextBinNumber
+  return nextBinNumber
+}
+
+// 重置扫描状态
+const resetScanState = () => {
+  searchBarcode.value = ''
+  locationActive.value = []
+  bin.value = ''
+  barcodeActive.value = ''
+  scanType.value = 3
+}
+
+// 设置库位
+const setLocation = (item) => {
+  locationActive.value = item
+  const storedData = getBinStorageData()
+  const container = containerNo.value
+  const wall = wallNo.value
+  const lotNumber = barcodeActive.value.lotNumber
+  // 初始化数据结构
+  initializeBinData(storedData, container, wall)
+  // 分配格口号
+  const binNumber = allocateBinNumber(storedData, container, wall, lotNumber)
+  // 检查格口是否超限
+  if (isBinExceeded(binNumber)) {
+    scanError()
+    tips.value = `格口超过${MAX_BIN_NUMBER},请重新切换二次分拨墙号`
+    showNotify({ type: 'danger', duration: 3000, message: `格口超过${MAX_BIN_NUMBER},请重新切换二次分拨墙号` })
+    resetScanState()
+    return
+  }
+  bin.value = binNumber
+  // 构建提交数据
+  const submitData = {
+    warehouse,
+    equipment: wallNo.value,
+    container: containerNo.value,
+    barcode: barcodeActive.value.barcode,
+    bin: bin.value,
+    location: locationActive.value[0].location,
+    lotNum: barcodeActive.value.lotNumber,
+    sysVersion: 'V6'
+  }
+  // 提交格口绑定数据,请求成功后再缓存数据
+  submitBinBinding(submitData, storedData)
+}
+
+// 提交格口绑定数据
+const submitBinBinding = (data, storedData) => {
+  showLoading()
+  setBindAllocateWall(data).then(() => {
+    // 接口请求成功后再缓存数据
+    saveBinStorageData(storedData)
+    tips.value = `请将${searchBarcode.value},放入分拨墙-${wallNo.value}-《${data.bin}》格口`
+    playVoiceBin(Number(bin.value))
+    closeLoading()
+  }).catch(err => {
+    // saveBinStorageData(storedData)
+    resetScanState()
+    tips.value = err.message
+    scanError()
+    closeLoading()
+  })
 }
 // 数据刷新
 const loadData = () => {
@@ -380,40 +471,22 @@ onUnmounted(() => {
   closeListener()
 })
 
-
-
-//删除分拨
-const onClickRight = () => {
-  showConfirmDialog({
-    title: '温馨提示',
-    message:'您正在进行释放分拨墙是否继续?',
-    keyboardEnabled:false
-  }).then(() => {
-    showLoading()
-    const params={warehouse,container:containerNo.value}
-    finishTask(params).then(res=>{
-      showNotify({ type: 'success', duration: 3000, message: `解绑成功` })
-      scanSuccess()
-      reset()
-      loadData()
-    }).catch(err=>{
-      scanError()
-    }).finally(() => {
-      closeLoading()
-    })
-  }).catch(() => {})
-}
-const reset=()=>{
-  containerNo.value=''
-  wallNo.value=''
-  scanType.value=3
+// 重置所有状态
+const reset = () => {
+  containerNo.value = ''
+  wallNo.value = ''
+  scanType.value = 3
   searchBarcode.value = ''
-  bin.value=''
+  bin.value = ''
   locationActive.value = []
-  tips.value='请扫描容器号'
+  tips.value = '请扫描容器号'
   lotNumberToBinMap.clear() // 清空lotNumber与格口的映射关系
 }
-window.onRefresh = loadData
+
+// 注册页面刷新函数
+if (typeof window !== 'undefined') {
+  ;(window as any).onRefresh = loadData
+}
 </script>
 <style scoped lang="sass">
 .container