gaoluyang
2026-08-03 75242bbba28ae8902ddf86d33febb497d79e5fc3
src/views/mes/pro/task/data.ts
@@ -1,40 +1,45 @@
import type { VbenFormApi, VbenFormSchema } from '#/adapter/form';
import type { VbenFormApi, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesProTaskApi } from '#/api/mes/pro/task';
import type { MesProWorkOrderApi } from '#/api/mes/pro/workorder';
import type { Ref } from 'vue';
import { markRaw } from 'vue';
import { DICT_TYPE } from '../../../../packages/constants/src';
import { getDictOptions } from '../../../../packages/effects/hooks/src';
import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { message } from 'ant-design-vue';
import CrmCustomerSelect from '#/components/crm-customer-select.vue';
import { calculateTaskDuration } from '#/api/mes/pro/task';
import { getRangePickerDefaultProps } from '#/utils';
import { MdClientSelect } from '#/views/mes/md/client/components';
import { MdItemSelect } from '#/views/mes/md/item/components';
import { MdWorkstationSelect } from '#/views/mes/md/workstation/components';
import { ProProcessSelect } from '#/views/mes/pro/process/components';
import { RouteColorPicker } from '#/views/mes/pro/route/components';
import { ProProcessSelect } from '#/views/mes/process-design/process/components';
import { RouteColorPicker } from '#/views/mes/process-design/route/components';
import { ProWorkOrderSelect } from '#/views/mes/pro/workorder/components';
/** 待排产工单列表的搜索表单 */
/** 待排产订单列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '工单编码',
      label: '订单编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入工单编码',
        placeholder: '请输入订单编码',
      },
    },
    {
      fieldName: 'name',
      label: '工单名称',
      label: '订单名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入工单名称',
        placeholder: '请输入订单名称',
      },
    },
    {
@@ -57,7 +62,7 @@
    {
      fieldName: 'clientId',
      label: '客户',
      component: markRaw(MdClientSelect),
      component: markRaw(CrmCustomerSelect),
      componentProps: {
        placeholder: '请选择客户',
      },
@@ -74,12 +79,12 @@
  ];
}
/** 待排产工单列表的字段 */
/** 待排产订单列表的字段 */
export function useGridColumns(): VxeTableGridOptions<MesProWorkOrderApi.WorkOrder>['columns'] {
  return [
    {
      field: 'code',
      title: '工单编码',
      title: '订单编码',
      fixed: 'left',
      width: 200,
      treeNode: true,
@@ -87,12 +92,12 @@
    },
    {
      field: 'name',
      title: '工单名称',
      title: '订单名称',
      minWidth: 150,
    },
    {
      field: 'orderSourceType',
      title: '工单来源',
      title: '订单来源',
      width: 100,
      cellRender: {
        name: 'CellDict',
@@ -126,7 +131,7 @@
    },
    {
      field: 'quantity',
      title: '工单数量',
      title: '订单数量',
      width: 100,
    },
    {
@@ -166,19 +171,19 @@
    },
    {
      title: '操作',
      width: 100,
      width: 160,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
/** 排产对话框只读工单信息的表单 */
/** 排产对话框只读订单信息的表单 */
export function useScheduleFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '工单编码',
      label: '订单编码',
      component: 'Input',
      componentProps: {
        disabled: true,
@@ -186,7 +191,7 @@
    },
    {
      fieldName: 'name',
      label: '工单名称',
      label: '订单名称',
      component: 'Input',
      componentProps: {
        disabled: true,
@@ -194,7 +199,7 @@
    },
    {
      fieldName: 'orderSourceType',
      label: '工单来源',
      label: '订单来源',
      component: 'Select',
      componentProps: {
        disabled: true,
@@ -214,7 +219,7 @@
    },
    {
      fieldName: 'type',
      label: '工单类型',
      label: '订单类型',
      component: 'Select',
      componentProps: {
        disabled: true,
@@ -247,7 +252,7 @@
    },
    {
      fieldName: 'quantity',
      label: '工单数量',
      label: '订单数量',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
@@ -258,7 +263,7 @@
    {
      fieldName: 'clientId',
      label: '客户',
      component: markRaw(MdClientSelect),
      component: markRaw(CrmCustomerSelect),
      componentProps: {
        disabled: true,
      },
@@ -279,12 +284,12 @@
        class: '!w-full',
        disabled: true,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'status',
      label: '工单状态',
      label: '订单状态',
      component: 'Select',
      componentProps: {
        disabled: true,
@@ -347,8 +352,8 @@
    },
    {
      field: 'duration',
      title: '生产时长',
      width: 80,
      title: '生产时长(h)',
      width: 120,
    },
    {
      field: 'endTime',
@@ -376,7 +381,7 @@
}
/** 生产任务新增/修改的表单 */
export function useTaskFormSchema(formApi?: VbenFormApi): VbenFormSchema[] {
export function useTaskFormSchema(formApi?: VbenFormApi, workOrderQuantity?: Ref<number | undefined>): VbenFormSchema[] {
  return [
    {
      fieldName: 'workstationId',
@@ -384,6 +389,7 @@
      component: markRaw(MdWorkstationSelect),
      componentProps: {
        placeholder: '请选择工作站',
        onChange: () => recalcDuration(formApi),
      },
      rules: 'selectRequired',
    },
@@ -391,11 +397,13 @@
      fieldName: 'quantity',
      label: '排产数量',
      component: 'InputNumber',
      description: () => workOrderQuantity?.value != null ? `订单数量:${workOrderQuantity.value}` : '',
      componentProps: {
        class: '!w-full',
        min: 0.01,
        placeholder: '请输入排产数量',
        precision: 2,
        onChange: () => recalcDuration(formApi),
      },
      rules: 'required',
    },
@@ -410,17 +418,23 @@
      component: 'DatePicker',
      componentProps: {
        class: '!w-full',
        placeholder: '请选择开始时间',
        placeholder: '请先选择工作站',
        showTime: true,
        valueFormat: 'x',
        // 开始时间变更:重新计算结束时间
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
        onChange: () => recalcEndTime(formApi),
      },
      dependencies: {
        triggerFields: ['workstationId'],
        componentProps: (values) => ({
          disabled: !values.workstationId,
          placeholder: values.workstationId ? '请选择开始时间' : '请先选择工作站',
        }),
      },
      rules: 'required',
    },
    {
      fieldName: 'duration',
      label: '生产时长',
      label: '生产时长(h)',
      component: 'InputNumber',
      componentProps: {
        class: '!w-full',
@@ -440,7 +454,7 @@
        class: '!w-full',
        disabled: true,
        showTime: true,
        valueFormat: 'x',
        valueFormat: 'YYYY-MM-DD HH:mm:ss',
      },
    },
    {
@@ -456,18 +470,37 @@
  ];
}
/** 计算结束时间:开始时间 + 生产时长 × 8 小时 */
/** 选择工作站或修改数量后,自动计算生产时长 */
async function recalcDuration(formApi?: VbenFormApi) {
  if (!formApi) return;
  const values = await formApi.getValues();
  if (!values.workstationId || !values.quantity) return;
  try {
    const res = await calculateTaskDuration(values.workstationId, values.quantity);
    await formApi.setFieldValue('duration', res.duration);
    if (res.duration === 0) {
      message.warning('该工作站未绑定设备,或绑定设备未配置生产效率');
    }
    // 重新计算结束时间
    await recalcEndTime(formApi);
  } catch {
    // 计算失败不阻塞用户操作
  }
}
/** 计算结束时间:开始时间 + 生产时长(小时) */
async function recalcEndTime(formApi?: VbenFormApi) {
  if (!formApi) {
    return;
  }
  const values = await formApi.getValues();
  if (values.startTime && values.duration) {
    const start = Number(values.startTime);
    await formApi.setFieldValue(
      'endTime',
      start + values.duration * 8 * 3600 * 1000,
    );
    const start = new Date(values.startTime).getTime();
    const endTime = start + values.duration * 3600 * 1000;
    const pad = (n: number) => String(n).padStart(2, '0');
    const d = new Date(endTime);
    const formatted = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(d.getHours())}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
    await formApi.setFieldValue('endTime', formatted);
  }
}
@@ -476,11 +509,11 @@
  return [
    {
      fieldName: 'workOrderId',
      label: '生产工单',
      label: '生产订单',
      component: markRaw(ProWorkOrderSelect),
      componentProps: {
        allowClear: true,
        placeholder: '请选择生产工单',
        placeholder: '请选择生产订单',
      },
    },
    {
@@ -553,7 +586,7 @@
      width: 170,
      formatter: 'formatDateTime',
    },
    { field: 'duration', title: '生产时长', width: 100 },
    { field: 'duration', title: '生产时长(h)', width: 120 },
    {
      field: 'endTime',
      title: '预计完成时间',