gongchunyi
4 小时以前 5b3cbcef771cd23ef8db1bf29dd15e2ddd98744f
src/views/inventoryManagement/stockManagement/FrozenAndThaw.vue
@@ -1,17 +1,38 @@
<template>
  <div>
    <el-dialog
        v-model="isShow"
        :title="operationType === 'frozen' ? '冻结库存' : '解冻库存'"
        width="800"
        @close="closeModal"
      v-model="isShow"
      :title="operationType === 'frozen' ? '冻结库存' : '解冻库存'"
      width="800"
      @close="closeModal"
    >
      <el-form label-width="140px" :model="formState" ref="formRef">
        <el-form-item
            :label="operationType === 'frozen' ? '冻结数量:' : '解冻数量:'"
            prop="lockedQuantity"
          label="库存类型"
          prop="stockType"
          :rules="[
            {
              required: true,
              message: '请选择库存类型',
              trigger: 'change',
            }
          ]"
        >
          <el-input-number v-model="formState.lockedQuantity" :step="1" :min="1" precision="0" style="width: 100%" :max="maxCount" />
          <el-select v-model="formState.stockType" placeholder="请选择库存类型" style="width: 100%">
            <el-option label="合格库存" value="qualified" />
            <el-option label="不合格库存" value="unqualified" />
          </el-select>
        </el-form-item>
        <el-form-item :label="operationType === 'frozen' ? '冻结数量:' : '解冻数量:'" prop="lockedQuantity">
          <el-input-number
            v-model="formState.lockedQuantity"
            :step="0.01"
            :min="inputMin"
            :precision="2"
            style="width: 100%"
            :max="maxCount"
          />
        </el-form-item>
      </el-form>
@@ -26,39 +47,67 @@
</template>
<script setup>
import {ref, computed, getCurrentInstance} from "vue";
import {frozenStockInventory, thawStockInventory} from "@/api/inventoryManagement/stockInventory.js";
import {frozenStockUninventory, thawStockUninventory} from "@/api/inventoryManagement/stockUninventory.js";
import { ref, computed, getCurrentInstance, onMounted, watch } from "vue";
import { frozenStockInventory, thawStockInventory } from "@/api/inventoryManagement/stockInventory.js";
import { frozenStockUninventory, thawStockUninventory } from "@/api/inventoryManagement/stockUninventory.js";
const props = defineProps({
  visible: {
    type: Boolean,
    required: true,
  },
  operationType: {
    type: String,
    required: true,
    default: 'frozen',
    default: "frozen",
  },
  type: {
    type: String,
    required: true,
    default: 'qualified',
    default: "qualified",
  },
  record: {
    type: Object,
    default: () => {},
  }
    default: () => ({}),
  },
});
const emit = defineEmits(['update:visible', 'completed']);
const emit = defineEmits(["update:visible", "completed"]);
// 响应式数据(替代选项式的 data)
const toNumber = (value) => {
  const num = Number(value);
  return Number.isFinite(num) ? num : 0;
};
const getQualifiedUnLockedStock = (row) => {
  return toNumber(
    row?.qualifiedUnLockedQuantity ??
      row?.unLockedQuantity ??
      row?.qualifiedQuantity ??
      row?.qualitity
  );
};
const getUnqualifiedUnLockedStock = (row) => {
  return toNumber(
    row?.unQualifiedUnLockedQuantity ??
      row?.unqualifiedUnLockedQuantity ??
      row?.unQualifiedQuantity ??
      row?.unqualifiedQuantity
  );
};
const getQualifiedLockedStock = (row) => {
  return toNumber(row?.qualifiedLockedQuantity ?? row?.lockedQuantity);
};
const getUnqualifiedLockedStock = (row) => {
  return toNumber(row?.unQualifiedLockedQuantity ?? row?.unqualifiedLockedQuantity);
};
const formState = ref({
  lockedQuantity: 0,
  stockType: "qualified",
  lockedQuantity: 0.01,
});
const isShow = computed({
@@ -66,95 +115,122 @@
    return props.visible;
  },
  set(val) {
    emit('update:visible', val);
    emit("update:visible", val);
  },
});
const maxCount = computed(() => {
  const isQualified = formState.value.stockType === "qualified";
  if (props.operationType === "frozen") {
    return isQualified ? getQualifiedUnLockedStock(props.record) : getUnqualifiedUnLockedStock(props.record);
  }
  return isQualified ? getQualifiedLockedStock(props.record) : getUnqualifiedLockedStock(props.record);
});
let { proxy } = getCurrentInstance()
const inputMin = computed(() => (maxCount.value > 0 ? 0.01 : 0));
const targetStockId = computed(() => {
  if (formState.value.stockType === "unqualified") {
    return props.record?.unQualifiedId ?? props.record?.unqualifiedId ?? props.record?.id;
  }
  return props.record?.qualifiedId ?? props.record?.id;
});
const initFormData = () => {
  formState.value.stockType = props.type === "unqualified" ? "unqualified" : "qualified";
  if (props.operationType === "thaw") {
    formState.value.lockedQuantity = maxCount.value;
  } else {
    formState.value.lockedQuantity = maxCount.value > 0 ? 0.01 : 0;
  }
};
watch(
  () => props.record,
  () => {
    initFormData();
  },
  { deep: true }
);
watch(
  () => props.operationType,
  () => {
    initFormData();
  }
);
watch(
  maxCount,
  (val) => {
    if (val <= 0) {
      formState.value.lockedQuantity = 0;
      return;
    }
    if (!formState.value.lockedQuantity || formState.value.lockedQuantity < 0.01) {
      formState.value.lockedQuantity = 0.01;
    } else if (formState.value.lockedQuantity > val) {
      formState.value.lockedQuantity = val;
    }
  },
  { immediate: true }
);
const { proxy } = getCurrentInstance();
const closeModal = () => {
  // 重置表单数据
  formState.value = {
    lockedQuantity: undefined
    stockType: "qualified",
    lockedQuantity: 0.01,
  };
  isShow.value = false;
};
const maxCount = computed(() => {
  // 冻结库存最大数量为未解冻数量
  if (props.operationType === 'frozen') {
    return props.record.unLockedQuantity
  }
  // 解冻库存最大数量为已冻结数量
  return props.record.lockedQuantity
})
const handleSubmit = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      const data = Object.assign({id: props.record.id}, formState.value);
      if (props.type === 'qualified') {
        // 冻结
        if (props.operationType === 'frozen') {
          frozenStockInventory(data).then(res => {
            if (res.code === 200) {
              // 关闭模态框
              isShow.value = false;
              // 告知父组件已完成
              emit('completed');
              proxy.$modal.msgSuccess("提交成功");
            } else {
              proxy.$modal.msgError(res.msg);
            }
          })
        } else {
          thawStockInventory(data).then(res => {
            if (res.code === 200) {
              // 关闭模态框
              isShow.value = false;
              // 告知父组件已完成
              emit('completed');
              proxy.$modal.msgSuccess("提交成功");
            } else {
              proxy.$modal.msgError(res.msg);
            }
          })
        }
      } else {
        if (props.operationType === 'frozen') {
          frozenStockUninventory(data).then(res => {
            if (res.code === 200) {
              // 关闭模态框
              isShow.value = false;
              // 告知父组件已完成
              emit('completed');
              proxy.$modal.msgSuccess("提交成功");
            } else {
              proxy.$modal.msgError(res.msg);
            }
          })
        } else {
          thawStockUninventory(data).then(res => {
            if (res.code === 200) {
              // 关闭模态框
              isShow.value = false;
              // 告知父组件已完成
              emit('completed');
              proxy.$modal.msgSuccess("提交成功");
            } else {
              proxy.$modal.msgError(res.msg);
            }
          })
        }
      }
  proxy.$refs["formRef"].validate((valid) => {
    if (!valid) return;
    if (!formState.value.stockType) {
      proxy.$modal.msgError("请选择库存类型");
      return;
    }
  })
    if (!formState.value.lockedQuantity || formState.value.lockedQuantity <= 0) {
      proxy.$modal.msgError(props.operationType === "frozen" ? "冻结数量必须大于0" : "解冻数量必须大于0");
      return;
    }
    if (formState.value.lockedQuantity > maxCount.value) {
      proxy.$modal.msgError("操作数量不能超过可操作库存");
      return;
    }
    if (!targetStockId.value) {
      proxy.$modal.msgError("未找到对应库存ID,无法提交");
      return;
    }
    const data = Object.assign({ id: targetStockId.value }, formState.value);
    let submitApi;
    if (formState.value.stockType === "qualified") {
      submitApi = props.operationType === "frozen" ? frozenStockInventory : thawStockInventory;
    } else {
      submitApi = props.operationType === "frozen" ? frozenStockUninventory : thawStockUninventory;
    }
    submitApi(data).then((res) => {
      if (res.code === 200) {
        isShow.value = false;
        emit("completed");
        proxy.$modal.msgSuccess("提交成功");
      } else {
        proxy.$modal.msgError(res.msg);
      }
    });
  });
};
onMounted(() => {
  formState.value.lockedQuantity = maxCount.value;
})
  initFormData();
});
defineExpose({
  closeModal,