src/views/basicData/mdm/data.ts
@@ -1,22 +1,40 @@
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() {
@@ -35,8 +53,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(formApi?: VbenFormApi): VbenFormSchema[] {
export function useFormSchema(formApi?: VbenFormApi, categoryTreeData: any[] = []): VbenFormSchema[] {
  return [
    {
      component: "Input",
@@ -49,26 +90,13 @@
    {
      component: "Input",
      fieldName: "code",
      label: "物料编码",
      label: "产品编号",
      componentProps: {
        maxLength: 64,
        placeholder: "请输入物料编码",
        placeholder: "选择产品类型并保存后自动生成",
        disabled: true,
      },
      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: () => '生成' },
        ),
      help: "系统按产品类型自动生成九位编号,例如原材料 100100001",
    },
    {
      component: "Input",
@@ -80,19 +108,32 @@
      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' },
      },
    },
    {
      component: 'Select',
      fieldName: 'itemType',
      label: '产品类型',
      rules: 'selectRequired',
      componentProps: {
        placeholder: '请选择产品类型',
        options: [
          { label: '原材料(1001)', value: 1 },
          { label: '半成品(1002)', value: 2 },
          { label: '成品(1003)', value: 3 },
          { label: '辅材(1004)', value: 4 },
          { label: '其他(1005)', value: 5 },
        ],
      },
    },
    {
@@ -168,11 +209,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,
      },
    },
    {
@@ -181,6 +240,56 @@
      label: "条码",
      componentProps: {
        placeholder: "请输入条码",
      },
    },
    {
      component: 'Divider',
      fieldName: 'basicDataDivider',
      label: '基础数据',
      formItemClass: 'col-span-2',
      componentProps: { orientation: 'left', plain: true },
    },
    {
      component: 'Textarea',
      fieldName: 'technicalParameters',
      label: '技术参数',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入产品技术参数',
        rows: 4,
      },
    },
    {
      component: "InputNumber",
      fieldName: "safetyStock",
      label: "库存预警数量",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入库存预警数量",
      },
    },
    {
      component: "InputNumber",
      fieldName: "minStock",
      label: "最低库存",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入最低库存",
      },
    },
    {
      component: "InputNumber",
      fieldName: "maxStock",
      label: "最高库存",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入最高库存",
      },
    },
    {
@@ -217,54 +326,10 @@
      },
    },
    {
      component: "InputNumber",
      fieldName: "safetyStock",
      label: "安全库存",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入安全库存",
      },
    },
    {
      component: "InputNumber",
      fieldName: "minStock",
      label: "最低库存",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入最低库存",
      },
    },
    {
      component: "InputNumber",
      fieldName: "maxStock",
      label: "最高库存",
      componentProps: {
        class: "!w-full",
        min: 0,
        precision: 2,
        placeholder: "请输入最高库存",
      },
    },
    {
      component: 'Switch',
      fieldName: 'isBatchManaged',
      label: '批次管理',
      defaultValue: false,
      componentProps: {
        class: "w-auto",
        checkedChildren: "是",
        unCheckedChildren: "否",
      },
      rules: z.boolean().default(false),
    },
    {
      component: "Switch",
      fieldName: "syncMes",
      label: "同步到 MES",
      componentProps: {
        class: "w-auto",
        checkedChildren: "是",
@@ -281,6 +346,17 @@
        min: 0,
        placeholder: "请输入有效期",
      },
    },
    {
      component: 'Switch',
      fieldName: 'syncMes',
      label: '同步到MES',
      defaultValue: true,
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(true),
    },
    {
      component: "RadioGroup",
@@ -431,7 +507,7 @@
  return [
    {
      fieldName: "code",
      label: "物料编码",
      label: "产品编号",
      component: "Input",
      componentProps: {
        placeholder: "请输入物料编码",
@@ -466,7 +542,7 @@
    { type: "checkbox", width: 40 },
    {
      field: "code",
      title: "物料编码",
      title: "产品编号",
      minWidth: 120,
    },
    {
@@ -490,69 +566,72 @@
      title: "单位",
      minWidth: 80,
    },
    {
      field: "unitMeasureName2",
      title: "辅单位1",
      minWidth: 80,
    },
    {
      field: "unitMeasureRate1",
      title: "辅单位1换算比率",
      minWidth: 130,
    },
    {
      field: "unitMeasureName3",
      title: "辅单位2",
      minWidth: 80,
    },
    {
      field: "unitMeasureRate2",
      title: "辅单位2换算比率",
      minWidth: 130,
    },
    // 辅单位1/辅单位1换算比率/辅单位2/辅单位2换算比率:物料列表不再展示(表单里仍可维护),需要恢复就取消注释
    // {
    //   field: "unitMeasureName2",
    //   title: "辅单位1",
    //   minWidth: 80,
    // },
    // {
    //   field: "unitMeasureRate1",
    //   title: "辅单位1换算比率",
    //   minWidth: 130,
    // },
    // {
    //   field: "unitMeasureName3",
    //   title: "辅单位2",
    //   minWidth: 80,
    // },
    // {
    //   field: "unitMeasureRate2",
    //   title: "辅单位2换算比率",
    //   minWidth: 130,
    // },
    {
      field: "warehouseName",
      title: "默认仓库",
      minWidth: 100,
    },
    // 销售价:物料列表不再展示,需要恢复就取消注释
    // {
    //   field: "salesPrice",
    //   title: "销售价",
    //   minWidth: 100,
    //   formatter: ({ cellValue }) => {
    //     return cellValue != null ? `¥${Number(cellValue).toFixed(2)}` : "";
    //   },
    // },
    {
      field: "purchasePrice",
      title: "采购价",
      minWidth: 100,
      formatter: ({ cellValue }) => {
        return cellValue != null ? `¥${Number(cellValue).toFixed(2)}` : "";
      },
    },
    {
      field: "salesPrice",
      title: "销售价",
      minWidth: 100,
      formatter: ({ cellValue }) => {
        return cellValue != null ? `¥${Number(cellValue).toFixed(2)}` : "";
      },
      field: "createTime",
      title: "创建时间",
      minWidth: 180,
      formatter: 'formatDate',
    },
    {
      field: "isBatchManaged",
      title: "批次管理",
      minWidth: 80,
      width: 80,
      fixed: "right",
      slots: { default: "isBatchManaged" },
    },
    {
      field: "syncedMes",
      title: "同步状态",
      width: 100,
      fixed: "right",
      slots: { default: "syncedMes" },
    },
    {
      field: "status",
      title: "状态",
      minWidth: 80,
      width: 80,
      fixed: "right",
      cellRender: {
        name: "CellDict",
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: "createTime",
      title: "创建时间",
      minWidth: 180,
      formatter: "formatDateTime",
    },
    {
      title: "操作",
      width: 180,
      fixed: "right",
@@ -560,3 +639,27 @@
    },
  ];
}
/** 物料导入的表单 */
export function useImportFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'file',
      label: '物料数据',
      component: 'Upload',
      rules: 'required',
      help: '仅允许导入 xls、xlsx 格式文件',
    },
    {
      fieldName: 'updateSupport',
      label: '是否覆盖',
      component: 'Switch',
      componentProps: {
        checkedChildren: '是',
        unCheckedChildren: '否',
      },
      rules: z.boolean().default(false),
      help: '是否更新已经存在的物料数据',
    },
  ];
}