6 天以前 5dbc69b4e792dcce998038822b92402366bec47f
src/views/basicData/mdm/data.ts
@@ -1,16 +1,45 @@
import type { VbenFormSchema } from "#/adapter/form";
import type { VbenFormApi, VbenFormSchema } from "#/adapter/form";
import type { VxeTableGridOptions } from "#/adapter/vxe-table";
import { h } from 'vue';
import { CommonStatusEnum, DICT_TYPE } from "#/packages/constants/src";
import { getDictOptions } from "#/packages/effects/hooks/src";
import { z } from '#/adapter/form';
import { generateAutoCode } from '#/api/mes/md/autocode/record';
import { MesAutoCodeRuleCode } from '@vben/constants';
import { getItemTypeSimpleList } from '#/api/mes/md/item/type';
import { getUnitPage } from '#/api/mdm/unit';
import { getWarehouseSimpleList } from '#/api/mes/wm/warehouse';
import { Button } from 'ant-design-vue';
// 缓存单位列表
let unitListCache: any[] | null = null;
let unitListPromise: Promise<any[]> | null = null;
// 缓存仓库列表
let warehouseListCache: any[] | null = null;
let warehouseListPromise: Promise<any[]> | null = null;
/** 将扁平分类列表转为 TreeSelect 所需的树形结构 */
export function buildCategoryTree(list: any[]): any[] {
  const map = new Map<number, any>();
  const roots: any[] = [];
  for (const item of list) {
    map.set(item.id, { label: item.name, value: item.id, children: [] });
  }
  for (const item of list) {
    const node = map.get(item.id)!;
    if (item.parentId && map.has(item.parentId)) {
      map.get(item.parentId)!.children.push(node);
    } else {
      roots.push(node);
    }
  }
  return roots;
}
/** 获取单位列表(带缓存) */
async function getUnitListCached() {
@@ -29,8 +58,31 @@
  return unitListPromise;
}
/** 获取仓库列表(带缓存) */
async function getWarehouseListCached() {
  if (warehouseListCache) {
    return warehouseListCache;
  }
  if (warehouseListPromise) {
    return warehouseListPromise;
  }
  warehouseListPromise = getWarehouseSimpleList().then((res) => {
    warehouseListCache = res || [];
    return warehouseListCache;
  }).finally(() => {
    warehouseListPromise = null;
  });
  return warehouseListPromise;
}
/** 清除单位列表缓存 */
export function clearUnitListCache() {
  unitListCache = null;
  warehouseListCache = null;
}
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
export function useFormSchema(formApi?: VbenFormApi, categoryTreeData: any[] = []): VbenFormSchema[] {
  return [
    {
      component: "Input",
@@ -45,9 +97,24 @@
      fieldName: "code",
      label: "物料编码",
      componentProps: {
        maxLength: 64,
        placeholder: "请输入物料编码",
      },
      rules: "required",
      rules: z.string().min(1, '物料编码不能为空').max(64),
      suffix: () =>
        h(
          Button,
          {
            type: 'default',
            onClick: async () => {
              const code = await generateAutoCode(
                MesAutoCodeRuleCode.MD_ITEM_CODE,
              );
              await formApi?.setFieldValue('code', code);
            },
          },
          { default: () => '生成' },
        ),
    },
    {
      component: "Input",
@@ -59,19 +126,16 @@
      rules: "required",
    },
    {
      component: "ApiSelect",
      component: "TreeSelect",
      fieldName: "categoryId",
      label: "物料分类",
      rules: "selectRequired",
      componentProps: {
        placeholder: "请选择物料分类",
        allowClear: true,
        api: async () => {
          const res = await getItemTypeSimpleList();
          return res || [];
        },
        labelField: "name",
        valueField: "id",
        treeDefaultExpandAll: true,
        treeData: categoryTreeData,
        fieldNames: { label: 'label', value: 'value', children: 'children' },
      },
    },
    {
@@ -147,11 +211,29 @@
      },
    },
    {
      component: 'Input',
      component: 'ApiSelect',
      fieldName: 'warehouseId',
      label: '默认仓库',
      componentProps: {
        placeholder: '请输入默认仓库ID',
        placeholder: '请选择默认仓库',
        allowClear: true,
        api: getWarehouseListCached,
        labelField: 'name',
        valueField: 'id',
        onChange: async (value: number | undefined) => {
          const code = value
            ? ((await getWarehouseListCached()).find((w: any) => w.id === value)?.code || '')
            : '';
          formApi?.setFieldValue('warehouseCode', code);
        },
      },
    },
    {
      component: 'Input',
      fieldName: 'warehouseCode',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
@@ -241,17 +323,6 @@
      rules: z.boolean().default(false),
    },
    {
      component: "Switch",
      fieldName: "syncMes",
      label: "同步到 MES",
      componentProps: {
        class: "w-auto",
        checkedChildren: "是",
        unCheckedChildren: "否",
      },
      rules: z.boolean().default(false),
    },
    {
      component: "InputNumber",
      fieldName: "expiryDay",
      label: "有效期(天)",
@@ -260,6 +331,17 @@
        min: 0,
        placeholder: "请输入有效期",
      },
    },
    {
      component: 'Switch',
      fieldName: 'syncMes',
      label: '同步到MES',
      defaultValue: true,
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(true),
    },
    {
      component: "RadioGroup",
@@ -511,27 +593,36 @@
      },
    },
    {
      field: "isBatchManaged",
      title: "批次管理",
      minWidth: 80,
      slots: { default: "isBatchManaged" },
    },
    {
      field: "status",
      title: "状态",
      minWidth: 80,
      cellRender: {
        name: "CellDict",
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: "createTime",
      title: "创建时间",
      minWidth: 180,
      formatter: "formatDateTime",
    },
    {
      field: "isBatchManaged",
      title: "批次管理",
      width: 80,
      fixed: "right",
      slots: { default: "isBatchManaged" },
    },
    {
      field: "syncedMes",
      title: "同步状态",
      width: 100,
      fixed: "right",
      slots: { default: "syncedMes" },
    },
    {
      field: "status",
      title: "状态",
      width: 80,
      fixed: "right",
      cellRender: {
        name: "CellDict",
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      title: "操作",
      width: 180,
      fixed: "right",