zhang_12370
5 天以前 c6d13e58d85fbaaceb49d4c24401b50143050173
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="queryParams" class="search-form">
      <el-form-item label="搜索">
        <el-input
          v-model="queryParams.searchAll"
          placeholder="请输入关键词"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
 
    <!-- 主要内容区域 -->
    <el-card>
      <!-- 操作按钮 -->
      <div class="toolbar">
        <el-button type="success" :icon="Plus" @click="openDialog('add')">
          新增加工
        </el-button>
        <el-button
          type="danger"
          :icon="Delete"
          :disabled="!selectedRows.length"
          @click="() => deleteSelected(delPM)"
        >
          删除
        </el-button>
      </div>
      <!-- 数据表格 -->
      <ETable
        :showOverflowTooltip="false"
        :loading="loading"
        :table-data="tableData"
        :columns="columns"
        :current-page="queryParams.current"
        :page-size="queryParams.size"
        @selection-change="handleSelectionChange"
        @edit="(row) => openDialog('edit', row)"
        :show-selection="true"
        :border="true"
        @viewRow="(row) => (viewRow('viewRow', row))"
        :operations="['edit', 'viewRow']"
        :operationsWidth="200"
        :show-overflow-tooltip="false"
        style="width: 100%; height: calc(100vh - 26em)"
      >
        <template #coalId="{ row }">
          <div class="coal-tags">
            <template v-if="row.coalId">
              <el-tag
                v-for="coal in parseCoalArray(row.coalId)"
                :key="coal"
                size="small"
                type="primary"
                class="coal-tag"
              >
                {{ getDisplayCoalName(coal) }}
              </el-tag>
            </template>
            <span v-else class="no-data">--</span>
          </div>
        </template>
      </ETable>
      <!-- 分页组件 -->
      <Pagination
        :layout="'total, prev, pager, next, jumper'"
        :total="total"
        v-model:page="queryParams.current"
        :limit="queryParams.size"
        @pagination="handlePageChange"
      />
    </el-card>
 
    <!-- 生产对话框 -->
    <!-- handleProductionAndProcessing -->
    <ProductionDialog
      v-model:visible="dialogVisible"
      ref="dialogRef"
      :type="dialogType"
      @update:productionAndProcessing="handleProductionAndProcessing"
      @success="handleDialogSuccess"
    />
  </div>
</template>
 
<script setup>
import { onMounted, ref } from "vue";
import { ElMessage } from "element-plus";
import { Plus, Delete } from "@element-plus/icons-vue";
import ProductionDialog from "./components/ProductionDialog.vue";
import ETable from "@/components/Table/ETable.vue";
import Pagination from "@/components/Pagination/index.vue";
import { getProductionMasterList, delPM } from "@/api/production";
import { parseCoalArray } from "@/utils/production";
import { useTableData } from "./components/useTableData.js";
import { useDialog } from "./components/useDialog.js";
import { useCoalData } from "./components/useCoalData.js";
import { getCoalInfoList } from "@/api/production";
 
// 煤种信息列表
const coalInfoList = ref([]);
 
// 表格列配置
const columns = [
  { prop: "coalId", label: "煤种", minWidth: 150, slot: true },
  { prop: "productionQuantity", label: "生产数量", minWidth: 120 },
  { prop: "laborCost", label: "人工成本", minWidth: 150 },
  { prop: "energyConsumptionCost", label: "能耗成本", minWidth: 120 },
  { prop: "equipmentDepreciation", label: "设备折旧", minWidth: 143 },
  { prop: "totalCost", label: "总成本", minWidth: 150 },
];
 
// 使用表格数据组合式函数
const {
  tableData,
  loading,
  total,
  selectedRows,
  queryParams,
  getList,
  handleSearch,
  handleReset,
  handlePageChange,
  handleSelectionChange,
  deleteSelected,
} = useTableData(getProductionMasterList, { pageSize: 10 });
 
// 使用对话框组合式函数
const {
  dialogVisible,
  dialogType,
  dialogRef,
  openDialog,
  viewRow,
  handleDialogSuccess: onDialogSuccess,
} = useDialog();
 
// 使用煤种数据组合式函数
const { getCoalNameById, getCoalData } = useCoalData();
 
// 获取煤种显示名称(带备用逻辑)
const getDisplayCoalName = (coalId) => {
  // 优先使用 useCoalData 的方法
  let name = getCoalNameById(coalId);
 
  // 如果没有找到,尝试从 coalInfoList 中查找
  if (name === coalId && coalInfoList.value.length > 0) {
    const found = coalInfoList.value.find((item) => item.id == coalId);
    name = found ? found.coal : coalId;
  }
 
  return name || coalId;
};
 
// 处理生产数据更新
const handleProductionAndProcessing = (row, rows) => {
  const index = tableData.value.findIndex((item) => item.id === rows.id);
  if (index !== -1) {
    tableData.value[index] = { ...tableData.value[index], ...row };
  }
};
 
// 对话框成功回调
const handleDialogSuccess = () => {
  onDialogSuccess(() => {
    getList();
    ElMessage.success("操作成功");
  });
};
 
// 组件挂载时加载数据
onMounted(async () => {
  try {
    // 并行加载煤种数据和表格数据
    await Promise.all([
      getCoalData(), // 预加载煤种数据
      (async () => {
        const res = await getCoalInfoList();
        if (res.code === 200) {
          coalInfoList.value = res.data;
        }
      })(),
    ]);
 
    // 加载表格数据
    getList();
  } catch (error) {
    ElMessage.error("数据加载失败,请刷新页面重试");
  }
});
</script>
 
<style scoped lang="scss">
.production-container {
  padding: 20px;
 
  .el-card:nth-child(1) {
    margin-bottom: 20px;
  }
}
 
.search-bar {
  margin-bottom: 20px;
  display: flex;
  gap: 10px;
 
  .el-input {
    width: 20%;
  }
}
 
.search-form {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  margin-bottom: 20px;
 
  .el-form-item {
    margin-right: 10px;
  }
 
  .el-button {
    margin-left: 10px;
  }
}
 
.coal-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
 
  .coal-tag {
    margin-right: 4px;
    margin-bottom: 4px;
 
    &:last-child {
      margin-right: 0;
    }
  }
 
  .no-data {
    color: #999;
    font-style: italic;
  }
}
</style>