zhangwencui
7 天以前 bbb54adc8d9fd6ae6b87ac036c8b21f3ad7daef4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<script lang="ts" setup>
import type { FormType } from '../data';
 
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MesProWorkOrderApi } from '#/api/mes/pro/workorder';
import type { MesProWorkOrderBomApi } from '#/api/mes/pro/workorder/bom';
 
import { computed } from 'vue';
 
import { useVbenModal } from '@vben/common-ui';
import {
  MesProWorkOrderStatusEnum,
  MesProWorkOrderTypeEnum,
} from '@vben/constants';
 
import { message } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  deleteWorkOrderBom,
  getWorkOrderBomPage,
} from '#/api/mes/pro/workorder/bom';
import { $t } from '#/locales';
 
import { useBomGridColumns } from '../data';
import BomForm from './bom-form.vue';
 
const props = defineProps<{
  formType: FormType;
  workOrder: MesProWorkOrderApi.WorkOrder;
  workOrderId: number;
}>();
 
const emit = defineEmits<{
  generateWorkOrder: [row: MesProWorkOrderBomApi.WorkOrderBom];
}>();
 
const isEditable = computed(
  () =>
    // 编辑态草稿可增删改 BOM
    ['create', 'update'].includes(props.formType) &&
    props.workOrder.status === MesProWorkOrderStatusEnum.PREPARE,
);
const isConfirmed = computed(
  () =>
    // 已确认态可生成子订单
    props.workOrder.status === MesProWorkOrderStatusEnum.CONFIRMED,
);
 
const [BomFormModal, bomFormModalApi] = useVbenModal({
  connectedComponent: BomForm,
  destroyOnClose: true,
});
 
/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}
 
/** 添加 BOM 物料 */
function handleCreate() {
  bomFormModalApi
    .setData({
      productId: props.workOrder.productId,
      workOrderId: props.workOrderId,
    })
    .open();
}
 
/** 编辑 BOM 物料 */
function handleEdit(row: MesProWorkOrderBomApi.WorkOrderBom) {
  bomFormModalApi
    .setData({
      id: row.id,
      productId: props.workOrder.productId,
      workOrderId: props.workOrderId,
    })
    .open();
}
 
/** 删除 BOM 物料 */
async function handleDelete(row: MesProWorkOrderBomApi.WorkOrderBom) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.itemName]),
    duration: 0,
  });
  try {
    await deleteWorkOrderBom(row.id!);
    message.success($t('ui.actionMessage.deleteSuccess', [row.itemName]));
    handleRefresh();
  } finally {
    hideLoading();
  }
}
 
/** 从 BOM 行生成子订单(通知父组件) */
function handleGenerateWorkOrder(row: MesProWorkOrderBomApi.WorkOrderBom) {
  emit('generateWorkOrder', row);
}
 
/** 是否展示生成子订单按钮:已确认 + 自制 + 产品类型 BOM 行 */
function showGenerate(row: MesProWorkOrderBomApi.WorkOrderBom) {
  return (
    isConfirmed.value &&
    props.workOrder.type === MesProWorkOrderTypeEnum.SELF &&
    row.itemOrProduct === 'PRODUCT'
  );
}
 
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useBomGridColumns(isEditable.value, isConfirmed.value),
    height: 400,
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }) => {
          if (!props.workOrderId) {
            return { list: [], total: 0 };
          }
          return await getWorkOrderBomPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            workOrderId: props.workOrderId,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
    },
  } as VxeTableGridOptions<MesProWorkOrderBomApi.WorkOrderBom>,
});
</script>
 
<template>
  <div>
    <BomFormModal @success="handleRefresh" />
    <Grid table-title="订单 BOM">
      <template v-if="isEditable" #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: '添加物料',
              type: 'primary',
              icon: ACTION_ICON.ADD,
              onClick: handleCreate,
            },
          ]"
        />
      </template>
      <template #actions="{ row }">
        <TableAction
          :actions="[
            {
              label: $t('common.edit'),
              type: 'link',
              icon: ACTION_ICON.EDIT,
              ifShow: isEditable,
              onClick: handleEdit.bind(null, row),
            },
            {
              label: $t('common.delete'),
              type: 'link',
              danger: true,
              icon: ACTION_ICON.DELETE,
              ifShow: isEditable,
              popConfirm: {
                title: $t('ui.actionMessage.deleteConfirm', [row.itemName]),
                confirm: handleDelete.bind(null, row),
              },
            },
            {
              label: '生成订单',
              type: 'link',
              ifShow: showGenerate(row),
              onClick: handleGenerateWorkOrder.bind(null, row),
            },
          ]"
        />
      </template>
    </Grid>
  </div>
</template>