MachineTransferToOtherAreaModal.vue 7.3 KB
<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="起始点位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fromLocationCode">
                <a-input placeholder="请输入起始点位" :disabled="true" v-model="model.fromLocationCode"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="出库口" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="toPort">
              <j-search-select-tag
                placeholder="请选择出库口"
                v-model="model.toPort"
                dict="port,name,code,type='2'&&zone_code='A'"
                :pageSize="5"
                :async="true">
              </j-search-select-tag>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>
import {getAction, httpAction} from '@/api/manage'
import {validateDuplicateValue} from '@/utils/util'
import {createAgv, createTransferTask} from '@/api/api'

export default {
  name: "MachineTransferToOtherAreaModal",
  components: {},
  data() {
    return {
      title: "操作",
      width: 500,
      fromPortList: [], // 起始机台点位列表
      toPortList: [],   // 目标机台点位列表
      machineList: [],
      querySource: {},
      visible: false,
      model: {
        fromMachineCode: '', // 起始机台
        fromLocationCode: '', // 起始点位
        toMachineCode: '',   // 目标机台
        toLocationCode: ''   // 目标点位
      },
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 16},
      },
      confirmLoading: false,
      validatorRules: {
        fromMachineCode: [
          {required: true, message: '请选择起始机台!', trigger: 'change'},
        ],
        fromLocationCode: [
          {required: true, message: '请选择起始点位!', trigger: 'change'},
        ],
        toPort: [
          {required: true, message: '请选择目标出库口!', trigger: 'change'},
        ]
      },
      url: {
        add: "/task/taskHeader/createTransferTask",
      },
      modelDefault: {} // 备份原始值
    }
  },
  created() {
    // 备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    /**
     * 切换机台获取点位列表
     * @param {String} machineCode 机台编码
     * @param {String} type 类型(from-起始/to-目标)
     */
    changeMachine(machineCode, type) {
      // 清空对应点位选择
      this.model[`${type}LocationCode`] = '';
      // 获取对应机台的点位列表
      this.getPortList(machineCode, type);
    },

    /**
     * 获取机台点位列表
     * @param {String} machineCode 机台编码
     * @param {String} type 类型(from-起始/to-目标)
     */
    async getPortList(machineCode, type) {
      if (!machineCode) return;

      let params = {"machineCode": machineCode};
      try {
        const res = await getAction("/config/location/list", params);
        if (res.success) {
          this[`${type}PortList`] = res.result.records;
        }
        if (res.code === 510) {
          this.$message.warning(res.message);
        }
      } catch (error) {
        this.$message.error('获取点位列表失败');
        console.error(error);
      }
    },

    /**
     * 新增操作
     */
    add() {
      this.model = JSON.parse(JSON.stringify(this.modelDefault));
      this.visible = true;
      // 清空点位列表
      this.fromPortList = [];
      this.toPortList = [];
    },

    /**
     * 编辑操作
     * @param record
     */
    edit(record) {
      this.model = JSON.parse(JSON.stringify(this.modelDefault));
      this.model.fromLocationCode = record['fromLocationCode'];
      this.visible = true;
      this.getMachineList(record['zoneCode']);
      // 清空点位列表
      this.fromPortList = [];
      this.toPortList = [];
    },

    /**
     * 获取机台列表
     * @param {String} zoneCode 区域编码
     */
    async getMachineList(zoneCode) {
      if (!zoneCode) return;

      let params = {"zoneCode": zoneCode};
      try {
        const res = await getAction("/config/location/getMachineListByZoneCode", params);
        if (res.success) {
          this.machineList = res.result;
        }
        if (res.code === 510) {
          this.$message.warning(res.message);
        }
      } catch (error) {
        this.$message.error('获取机台列表失败');
        console.error(error);
      }
    },

    /**
     * 关闭弹窗
     */
    close() {
      this.$emit('close');
      this.visible = false;
      if (this.$refs.form) {
        this.$refs.form.clearValidate();
      }
      // 清空点位列表
      this.fromPortList = [];
      this.toPortList = [];
    },

    /**
     * 确认提交
     */
    async handleOk() {
      try {
        // 触发表单验证
        const valid = await this.$refs.form.validate();
        if (!valid) return;

        this.confirmLoading = true;
        // 构建后端需要的参数结构(关键:映射点位字段 + 补充必填字段)
        const taskParams = {
          // 核心:点位字段映射
          fromPort: this.model.fromLocationCode,
          toPort: this.model.toLocationCode,
          // 机台编码(后端实体有machineCode字段,可传递)
          //machineCode: `${this.model.fromMachineCode}_${this.model.toMachineCode}`, // 按需拼接或单独传递
          // 补充后端AgvTask的必填字段(根据业务逻辑设置默认值或从页面补充)
          taskType: null, // 任务类型(示例:1=转移任务,需根据实际字典值调整)
          priority: 10, // 优先级(示例默认值)
          status: 0, // 初始状态(示例:0=待执行,需根据实际字典值调整)
          zoneCode: this.querySource.zoneCode, // 库区编码(从父组件/查询条件获取)
          // 其他可选字段(按需补充)
          preTaskNo: 0,
          containerCode: '', // 容器编码(如需可在页面新增输入项)
          carno: '', // 小车编码(如需可在页面新增输入项)
          backWarehouse: 0,
          productionDetailId: 0,
          checkContainer: 0
        };
        // 提交创建任务
        const res = await createAgv(taskParams);
        if (res.success) {
          this.$message.success(res.message);
          this.$emit('ok');
          // 重置表单
          this.model = JSON.parse(JSON.stringify(this.modelDefault));
        } else {
          this.$message.warning(res.message);
        }
      } catch (error) {
        this.$message.error('提交失败,请重试');
        console.error(error);
      } finally {
        this.confirmLoading = false;
        this.close();
      }
    },

    /**
     * 取消操作
     */
    handleCancel() {
      this.close();
    }
  }
}
</script>

<style scoped>
/* 可选:添加一些样式优化 */
.ant-select {
  margin-bottom: 0;
}
</style>