yaowanxin
2025-09-22 f80d798b83fabb038cf10d745acd34c9c42fed4c
src/views/inventoryManagement/stockManagement/index.vue
@@ -115,7 +115,6 @@
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="12">
            <el-form-item label="出库人:" prop="entryPerson">
              <el-select v-model="form.createUser" placeholder="请选择" clearable>
@@ -123,6 +122,11 @@
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
          <el-form-item label="最低库存:" prop="minStock">
            <el-input v-model="form.minStock" placeholder="请输入最低库存" clearable />
          </el-form-item>
        </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
@@ -149,8 +153,11 @@
  exportStockManage
} from "@/api/inventoryManagement/stockManage.js";
import {
  updateManagement
  updateManagement,addSutockIn,selectProductRecordListByPuechaserId
} from "@/api/inventoryManagement/stockIn.js";
const userStore = useUserStore()
const { proxy } = getCurrentInstance()
const tableData = ref([])
@@ -194,6 +201,7 @@
    inboundBatch: '',
    stockQuantity: '',
    boundTime: '',
    minStock: '', // 新增最低库存字段
  },
  rules: {
    supplierName: [{ required: true, message: '请输入供应商名称', trigger: 'blur' }],
@@ -207,7 +215,8 @@
    taxExclusiveTotalPrice: [{ required: true, message: '请输入不含税总价', trigger: 'blur' }],
    boundTime: [{ required: true, message: '请选择库存时间', trigger: 'change' }],
    inboundTime: [{ required: true, message: '请选择入库时间', trigger: 'change' }],
    inboundPerson: [{ required: true, message: '请选择出库人', trigger: 'change' }]
    inboundPerson: [{ required: true, message: '请选择出库人', trigger: 'change' }],
    minStock: [{ required: true, message: '请输入最低库存', trigger: 'blur' }],
  }
})
const { searchForm, form, rules } = toRefs(data)
@@ -228,11 +237,13 @@
  getStockManagePage({ ...searchForm.value, ...page }).then(res => {
    tableLoading.value = false
    tableData.value = res.data.records
    console.log('res', res)
    // console.log('res', res)
    // tableData.value.map(item => {
    //   item.children = []
    // })
    // total.value = res.total
    total.value = res.data.total
    // 数据加载完成后检查库存
    checkStockAndCreatePurchase();
  }).catch(() => {
    tableLoading.value = false
  })
@@ -294,26 +305,63 @@
  console.log(form.value)
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      // if (productData.value.length > 0) {
      //   form.value.productData = proxy.HaveJson(productData.value)
      // } else {
      //   proxy.$modal.msgWarning('请添加产品信息')
      //   return
      // }
      // let tempFileIds = []
      // if (fileList.value.length > 0) {
      //   tempFileIds = fileList.value.map(item => item.tempId)
      // }
      // form.value.tempFileIds = tempFileIds
      // form.value.type = 1
      updateManagement(form.value).then(res => {
        proxy.$modal.msgSuccess("提交成功")
        closeDia()
        getList()
        // 提交后检查库存并尝试创建请购单
        checkStockAndCreatePurchase();
      })
    }
  })
}
// const handList = () => {
//   selectProductRecordListByPuechaserId().then(res => {
//     productModelList.value = res.data.filter(item => item.productName === value)
//     console.log('productModelList.value', productModelList.value)
//   })
// }
// 检查库存并创建请购单
const checkStockAndCreatePurchase = async () => {
  const stockList = tableData.value;
  handList()
  for (const item of stockList) {
    if (item.inboundNum0 < item.minStock) {
      try {
          const stockInData = {
            nickName: userStore.nickName,// 使用新格式化函数
            details: selectedRows.value.map(product => ({
              id: product.id,
              inboundQuantity: Number(product.quantityStock)
            })),
          };
        console.log('准备提交的数据:', JSON.parse(JSON.stringify(stockInData)));
          // 调用API
          loading.value = true
          await addSutockIn(stockInData)
          proxy.$modal.msgSuccess('新增入库成功')
          loading.value = false
        // // 生成请购单
        // const createRes = await createPurchaseRequest({
        //   productId: item.productId,
        //   requiredQuantity: item.minStock - item.inboundNum0,
        //   supplierId: item.supplierId
        // });
        // if (createRes.code === 200) {
        //   // 流转请购单到采购模块
        //   await transferPurchaseRequest({ requestId: createRes.data.id });
        //   proxy.$modal.msgSuccess(`产品 ${item.productName} 请购单已生成并流转`);
        // }
      } catch (error) {
        console.error(`产品 ${item.productName} 生成请购单失败:`, error);
        proxy.$modal.msgError(`产品 ${item.productName} 生成请购单失败,请手动处理`);
      }
    }
  }
};
// 关闭弹框
const closeDia = () => {
  proxy.resetForm("formRef")
@@ -376,6 +424,14 @@
}
onMounted(() => {
  getList()
  checkStockAndCreatePurchase();
    // 每小时检查一次库存
    const intervalId = setInterval(checkStockAndCreatePurchase, 60 * 60 * 1000);
onUnmounted(() => {
  // 组件卸载时清除定时器
  clearInterval(intervalId);
});
})
</script>