ProductProductionTable.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <template>
  2. <el-card class="box-card">
  3. <div class="header-container">
  4. <div class="left">
  5. <span class="title">生产概况</span>
  6. <span class="update-time" v-if="updateTime">{{ updateTime }}</span>
  7. </div>
  8. <div class="right">
  9. <div class="filter-group">
  10. <slot name="query-buttons"></slot>
  11. <el-button
  12. type="text"
  13. icon="el-icon-download"
  14. @click="handleExport"
  15. style="
  16. padding: 0;
  17. font-size: 16px;
  18. color: #909399;
  19. margin-left: 8px;
  20. "
  21. />
  22. </div>
  23. </div>
  24. </div>
  25. <div class="table-container">
  26. <el-table
  27. v-loading="loading"
  28. :data="localTableData"
  29. style="width: 100%"
  30. border
  31. size="small"
  32. :height="localTableData.length > 10 ? 450 : null"
  33. :header-cell-style="{ background: '#f5f7fa' }"
  34. >
  35. <el-table-column type="index" label="序号" width="50" align="center" />
  36. <el-table-column
  37. prop="goodsName"
  38. label="货品名称"
  39. width="150"
  40. align="center"
  41. sortable
  42. />
  43. <el-table-column
  44. prop="model"
  45. label="货品型号"
  46. width="80"
  47. align="center"
  48. sortable
  49. />
  50. <el-table-column
  51. prop="orderStatus"
  52. label="货品状态"
  53. width="100"
  54. align="center"
  55. sortable
  56. >
  57. <template slot-scope="scope">
  58. <el-tag
  59. :type="getStatusType(scope.row.orderStatus)"
  60. size="mini"
  61. effect="plain"
  62. >
  63. {{ scope.row.orderStatus }}
  64. </el-tag>
  65. </template>
  66. </el-table-column>
  67. <el-table-column
  68. prop="progress"
  69. label="进度"
  70. align="center"
  71. width="250"
  72. sortable
  73. >
  74. <template slot-scope="scope">
  75. <div
  76. class="progress-wrapper"
  77. @mouseenter="
  78. showTooltip(
  79. $event,
  80. (
  81. (scope.row.installedQuantity / scope.row.totalQuantity) *
  82. 100
  83. ).toFixed(0)
  84. )
  85. "
  86. @mouseleave="hideTooltip"
  87. >
  88. <div class="progress-container">
  89. <div class="progress-bar">
  90. <div class="custom-progress">
  91. <div
  92. class="progress-inner"
  93. :style="{
  94. width:
  95. Math.min(
  96. Math.round(
  97. (scope.row.installedQuantity /
  98. scope.row.totalQuantity) *
  99. 100
  100. ),
  101. 100
  102. ) + '%',
  103. backgroundColor: getProgressColor(
  104. scope.row.orderStatus
  105. ),
  106. }"
  107. >
  108. <span
  109. class="progress-text"
  110. v-if="scope.row.installedQuantity > 0"
  111. >
  112. {{ scope.row.installedQuantity }}
  113. </span>
  114. </div>
  115. <span class="total-text">
  116. {{ scope.row.totalQuantity }}
  117. </span>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </template>
  123. </el-table-column>
  124. <el-table-column prop="totalQuantity" label="计划量" align="center" />
  125. <el-table-column
  126. prop="installedQuantity"
  127. label="已完成量"
  128. align="center"
  129. />
  130. <el-table-column
  131. prop="uninstalledQuantity"
  132. label="未完成量"
  133. align="center"
  134. />
  135. <el-table-column
  136. prop="yesterdayQuantity"
  137. label="昨日完成量"
  138. align="center"
  139. />
  140. <el-table-column
  141. prop="todayQuantity"
  142. label="今日完成量"
  143. align="center"
  144. />
  145. <el-table-column prop="rate" label="完成率" align="center" sortable>
  146. <template slot-scope="scope">
  147. {{
  148. (
  149. (scope.row.installedQuantity / scope.row.totalQuantity) *
  150. 100
  151. ).toFixed(1) + "%"
  152. }}
  153. </template>
  154. </el-table-column>
  155. </el-table>
  156. </div>
  157. </el-card>
  158. </template>
  159. <script>
  160. import { GetDataByName, GetDataByNames } from "@/api/common";
  161. import * as XLSX from "xlsx";
  162. export default {
  163. name: "ProductProductionTable",
  164. data() {
  165. return {
  166. loading: false,
  167. localTableData: [],
  168. tooltip: null,
  169. tooltipTimeout: null,
  170. };
  171. },
  172. props: {
  173. updateTime: {
  174. type: String,
  175. required: true,
  176. },
  177. },
  178. created() {
  179. this.initData();
  180. },
  181. methods: {
  182. getStatusType(status) {
  183. switch (status) {
  184. case "生产完成":
  185. return "success";
  186. case "待生产":
  187. return "warning";
  188. case "未生产":
  189. return "info";
  190. case "生产中":
  191. return "primary";
  192. case "已关单":
  193. return "danger";
  194. case "未开始":
  195. return "info";
  196. default:
  197. return "info";
  198. }
  199. },
  200. getProgressColor(status) {
  201. switch (status) {
  202. case "生产完成":
  203. return "#67C23A";
  204. case "待生产":
  205. return "#E6A23C";
  206. case "生产中":
  207. return "#409EFF";
  208. case "已关单":
  209. return "#F56C6C";
  210. case "未生产":
  211. return "#909399";
  212. default:
  213. return "#909399";
  214. }
  215. },
  216. async initData() {
  217. try {
  218. await this.fetchTableData();
  219. } catch (error) {
  220. console.error("初始化数据失败:", error);
  221. }
  222. },
  223. async fetchTableData() {
  224. try {
  225. this.loading = true;
  226. await new Promise((resolve) => setTimeout(resolve, 1000));
  227. const send_data = {
  228. name: "getProductionSummaryByGoods",
  229. parammaps: {},
  230. };
  231. GetDataByName(send_data)
  232. .then((response) => {
  233. this.localTableData = response.data.list || [];
  234. })
  235. .catch((error) => {
  236. console.error("获取数据失败:", error);
  237. });
  238. this.$emit("data-loaded");
  239. } catch (error) {
  240. console.error("获取生产列表失败:", error);
  241. this.$message.error("获取生产列表失败");
  242. } finally {
  243. this.loading = false;
  244. }
  245. },
  246. showTooltip(event, percentage) {
  247. // 清除之前的tooltip和定时器
  248. this.hideTooltip();
  249. // 创建新的tooltip
  250. this.tooltip = document.createElement("div");
  251. this.tooltip.className = "progress-tooltip";
  252. this.tooltip.textContent = percentage + "%";
  253. document.body.appendChild(this.tooltip);
  254. const rect = event.target.getBoundingClientRect();
  255. const tooltipRect = this.tooltip.getBoundingClientRect();
  256. this.tooltip.style.left =
  257. rect.left + rect.width / 2 - tooltipRect.width / 2 + "px";
  258. this.tooltip.style.top = rect.top - tooltipRect.height - 8 + "px";
  259. // 使用 requestAnimationFrame 确保动画流畅
  260. requestAnimationFrame(() => {
  261. if (this.tooltip) {
  262. this.tooltip.classList.add("show");
  263. }
  264. });
  265. // 添加安全清理机制
  266. this.tooltipTimeout = setTimeout(() => {
  267. this.hideTooltip();
  268. }, 3000); // 3秒后自动清理
  269. },
  270. hideTooltip() {
  271. // 清除定时器
  272. if (this.tooltipTimeout) {
  273. clearTimeout(this.tooltipTimeout);
  274. this.tooltipTimeout = null;
  275. }
  276. // 清除tooltip
  277. if (this.tooltip) {
  278. this.tooltip.classList.remove("show");
  279. const tooltipToRemove = this.tooltip;
  280. setTimeout(() => {
  281. if (tooltipToRemove && tooltipToRemove.parentNode) {
  282. tooltipToRemove.parentNode.removeChild(tooltipToRemove);
  283. }
  284. }, 200);
  285. this.tooltip = null;
  286. }
  287. },
  288. // 获取格式化的日期时间
  289. getFormattedDateTime() {
  290. const now = new Date();
  291. const year = now.getFullYear();
  292. const month = String(now.getMonth() + 1).padStart(2, "0");
  293. const day = String(now.getDate()).padStart(2, "0");
  294. const hours = String(now.getHours()).padStart(2, "0");
  295. const minutes = String(now.getMinutes()).padStart(2, "0");
  296. const seconds = String(now.getSeconds()).padStart(2, "0");
  297. return `${year}${month}${day}${hours}${minutes}${seconds}`;
  298. },
  299. // 导出功能
  300. async handleExport() {
  301. try {
  302. if (this.localTableData.length === 0) {
  303. this.$message.warning("暂无数据可导出");
  304. return;
  305. }
  306. // 定义表头
  307. const headers = [
  308. "序号",
  309. "货品名称",
  310. "货品型号",
  311. "货品状态",
  312. "进度",
  313. "计划量",
  314. "已完成量",
  315. "未完成量",
  316. "昨日完成量",
  317. "今日完成量",
  318. "完成率",
  319. ];
  320. // 格式化数据
  321. const rows = this.localTableData.map((item, index) => [
  322. index + 1,
  323. item.goodsName || "",
  324. item.model || "",
  325. item.orderStatus || "",
  326. `${((item.installedQuantity / item.totalQuantity) * 100).toFixed(
  327. 2
  328. )}%`,
  329. item.totalQuantity || 0,
  330. item.installedQuantity || 0,
  331. item.uninstalledQuantity || 0,
  332. item.yesterdayQuantity || 0,
  333. item.todayQuantity || 0,
  334. `${((item.installedQuantity / item.totalQuantity) * 100).toFixed(
  335. 1
  336. )}%`,
  337. ]);
  338. // 创建工作簿
  339. const wb = XLSX.utils.book_new();
  340. // 组合表头和数据
  341. const wsData = [headers, ...rows];
  342. // 创建工作表
  343. const ws = XLSX.utils.aoa_to_sheet(wsData);
  344. // 设置列宽
  345. const colWidths = [
  346. { wch: 8 }, // 序号
  347. { wch: 30 }, // 货品名称
  348. { wch: 15 }, // 货品型号
  349. { wch: 12 }, // 货品状态
  350. { wch: 10 }, // 进度
  351. { wch: 10 }, // 计划量
  352. { wch: 12 }, // 已完成量
  353. { wch: 12 }, // 未完成量
  354. { wch: 12 }, // 昨日完成量
  355. { wch: 12 }, // 今日完成量
  356. { wch: 10 }, // 完成率
  357. ];
  358. ws["!cols"] = colWidths;
  359. // 添加工作表到工作簿
  360. XLSX.utils.book_append_sheet(wb, ws, "生产概况(按货品)");
  361. // 生成文件名
  362. const fileName = `生产概况(按货品)_${this.getFormattedDateTime()}.xlsx`;
  363. // 导出文件
  364. XLSX.writeFile(wb, fileName);
  365. this.$message.success("导出成功");
  366. } catch (error) {
  367. console.error("导出失败:", error);
  368. this.$message.error("导出失败");
  369. }
  370. },
  371. },
  372. beforeDestroy() {
  373. // 组件销毁时清理所有相关资源
  374. this.hideTooltip();
  375. if (this.tooltipTimeout) {
  376. clearTimeout(this.tooltipTimeout);
  377. }
  378. },
  379. };
  380. </script>
  381. <style lang="scss" scoped>
  382. .box-card {
  383. border-radius: 8px;
  384. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  385. transition: all 0.3s ease;
  386. min-height: 200px;
  387. height: auto;
  388. .header-container {
  389. margin-bottom: 15px;
  390. display: flex;
  391. justify-content: space-between;
  392. align-items: center;
  393. .left {
  394. display: flex;
  395. align-items: center;
  396. min-width: 200px;
  397. .title {
  398. font-size: 13px;
  399. color: #333;
  400. }
  401. .update-time {
  402. margin-left: 8px;
  403. font-size: 13px;
  404. color: #666;
  405. }
  406. }
  407. .right {
  408. flex: 1;
  409. display: flex;
  410. justify-content: flex-start;
  411. padding-left: 40px;
  412. .filter-group {
  413. display: flex;
  414. align-items: center;
  415. }
  416. }
  417. }
  418. .el-card__header {
  419. padding: 16px 20px;
  420. border-bottom: 1px solid #ebeef5;
  421. }
  422. :deep(.el-card__body) {
  423. padding: 16px;
  424. transition: all 0.3s ease;
  425. }
  426. }
  427. .table-container {
  428. position: relative;
  429. padding-bottom: 0;
  430. }
  431. .progress-wrapper {
  432. padding: 6px 0;
  433. width: 100%;
  434. height: 100%;
  435. cursor: pointer;
  436. }
  437. .progress-container {
  438. display: flex;
  439. align-items: center;
  440. justify-content: center;
  441. .progress-bar {
  442. width: 100%;
  443. padding: 0 20px;
  444. .custom-progress {
  445. width: 100%;
  446. height: 12px;
  447. background-color: #ebeef5;
  448. border-radius: 100px;
  449. overflow: hidden;
  450. position: relative;
  451. .progress-inner {
  452. height: 100%;
  453. transition: all 0.3s ease;
  454. border-radius: 100px;
  455. position: relative;
  456. .progress-text {
  457. position: absolute;
  458. right: 6px;
  459. top: 50%;
  460. transform: translateY(-50%);
  461. color: #fff;
  462. font-size: 10px;
  463. line-height: 1;
  464. text-shadow: 0 0 1px rgba(0, 0, 0, 0.2);
  465. white-space: nowrap;
  466. }
  467. }
  468. .total-text {
  469. position: absolute;
  470. right: 6px;
  471. top: 50%;
  472. transform: translateY(-50%);
  473. color: #606266;
  474. font-size: 10px;
  475. line-height: 1;
  476. white-space: nowrap;
  477. }
  478. }
  479. }
  480. }
  481. :deep(.el-table) {
  482. font-size: 12px;
  483. .el-table__header-wrapper {
  484. position: sticky;
  485. top: 0;
  486. z-index: 1;
  487. }
  488. .el-table__header th {
  489. padding: 8px 0;
  490. background-color: #f5f7fa;
  491. }
  492. .el-table__body td {
  493. padding: 8px 0;
  494. }
  495. .el-table__body tr:hover > td {
  496. background-color: #f5f7fa !important;
  497. }
  498. }
  499. :deep(.el-table__body-wrapper) {
  500. overflow-y: auto;
  501. &::-webkit-scrollbar {
  502. width: 6px;
  503. height: 6px;
  504. }
  505. &::-webkit-scrollbar-thumb {
  506. border-radius: 3px;
  507. background: #c0c4cc;
  508. }
  509. &::-webkit-scrollbar-track {
  510. border-radius: 3px;
  511. background: #f5f7fa;
  512. }
  513. }
  514. </style>
  515. <style lang="scss">
  516. .progress-tooltip {
  517. position: fixed;
  518. background: rgba(0, 0, 0, 0.8);
  519. color: #fff;
  520. padding: 4px 8px;
  521. border-radius: 4px;
  522. font-size: 12px;
  523. pointer-events: none;
  524. z-index: 9999;
  525. opacity: 0;
  526. transform: translateY(5px);
  527. transition: all 0.2s ease;
  528. &::after {
  529. content: "";
  530. position: absolute;
  531. bottom: -4px;
  532. left: 50%;
  533. transform: translateX(-50%);
  534. border-left: 4px solid transparent;
  535. border-right: 4px solid transparent;
  536. border-top: 4px solid rgba(0, 0, 0, 0.8);
  537. }
  538. &.show {
  539. opacity: 1;
  540. transform: translateY(0);
  541. }
  542. }
  543. </style>