index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <template>
  2. <ContentWrap>
  3. <!-- 搜索工作栏 -->
  4. <el-form
  5. class="-mb-15px"
  6. :model="queryParams"
  7. ref="queryFormRef"
  8. :inline="true"
  9. label-width="68px"
  10. >
  11. <el-form-item :label="t('workOrderMaterial.factory')" prop="factoryId">
  12. <el-select v-model="queryParams.factoryId" clearable filterable :placeholder="t('faultForm.choose')" class="!w-240px" @change="selectedFactoryChange">
  13. <el-option
  14. v-for="item in factoryList"
  15. :key="item.id"
  16. :label="item.factoryName"
  17. :value="item.id!"
  18. />
  19. </el-select>
  20. </el-form-item>
  21. <el-form-item :label="t('workOrderMaterial.storageLocation')" prop="storageLocationId" style="margin-left: 28px">
  22. <el-select v-model="queryParams.storageLocationId" clearable filterable :placeholder="t('faultForm.choose')" class="!w-240px">
  23. <el-option
  24. v-for="item in storageLocationList"
  25. :key="item.id"
  26. :label="item.storageLocationName"
  27. :value="item.id!"
  28. />
  29. </el-select>
  30. </el-form-item>
  31. <el-form-item :label="t('chooseMaintain.materialCode')" prop="materialCode">
  32. <el-input
  33. v-model="queryParams.materialCode"
  34. :placeholder="t('chooseMaintain.materialCode')"
  35. clearable
  36. @keyup.enter="handleQuery"
  37. class="!w-240px"
  38. />
  39. </el-form-item>
  40. <el-form-item :label="t('chooseMaintain.materialName')" prop="materialName">
  41. <el-input
  42. v-model="queryParams.materialName"
  43. :placeholder="t('chooseMaintain.materialName')"
  44. clearable
  45. @keyup.enter="handleQuery"
  46. class="!w-240px"
  47. />
  48. </el-form-item>
  49. <el-form-item :label="t('chooseMaintain.createTime')" prop="createTime">
  50. <el-date-picker
  51. v-model="queryParams.createTime"
  52. value-format="YYYY-MM-DD HH:mm:ss"
  53. type="daterange"
  54. :start-placeholder="t('info.start')"
  55. :end-placeholder="t('info.end')"
  56. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
  57. class="!w-220px"
  58. />
  59. </el-form-item>
  60. <el-form-item :label="t('route.IsItConfigured')" prop="configFlag" label-width="140px">
  61. <el-select
  62. v-model="queryParams.configFlag"
  63. :placeholder="t('faultForm.choose')"
  64. clearable
  65. class="!w-240px"
  66. >
  67. <el-option
  68. v-for="dict in resultOptions"
  69. :key="dict.value"
  70. :label="dict.label"
  71. :value="dict.value"
  72. />
  73. </el-select>
  74. </el-form-item>
  75. <el-form-item>
  76. <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> {{ t('operationFill.search') }}</el-button>
  77. <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> {{ t('operationFill.reset') }}</el-button>
  78. <el-button
  79. type="success"
  80. plain
  81. @click="handleExport"
  82. :loading="exportLoading"
  83. v-hasPermi="['pms:iot-sap-stock:export']"
  84. >
  85. <Icon icon="ep:download" class="mr-5px" /> 导出
  86. </el-button>
  87. </el-form-item>
  88. </el-form>
  89. </ContentWrap>
  90. <!-- ========== 统计信息卡片 ========== -->
  91. <ContentWrap style="margin-bottom: 16px">
  92. <el-card shadow="never" class="stat-card">
  93. <div class="stat-container">
  94. <div class="stat-item">
  95. <span class="stat-label">{{ t('stock.totalQuantity') }}:</span>
  96. <span class="stat-value">{{ totalQuantity.toLocaleString() }}</span>
  97. </div>
  98. <div class="stat-item">
  99. <span class="stat-label">{{ t('stock.totalAmount') }}:</span>
  100. <span class="stat-value">¥ {{ totalAmount.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }}</span>
  101. </div>
  102. </div>
  103. </el-card>
  104. </ContentWrap>
  105. <!-- 列表 -->
  106. <ContentWrap ref="tableContainerRef" class="table-container">
  107. <el-table ref="tableRef" v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="false" style="width: 100%">
  108. <el-table-column :label="t('workOrderMaterial.factory')" align="center" prop="factory" :width="columnWidths.factory"/>
  109. <el-table-column :label="t('workOrderMaterial.storageLocation')" align="center"
  110. prop="projectDepartment" :width="columnWidths.projectDepartment"/>
  111. <el-table-column :label="t('chooseMaintain.materialCode')" align="center"
  112. prop="materialCode" :width="columnWidths.materialCode"/>
  113. <el-table-column :label="t('chooseMaintain.materialName')" align="left"
  114. prop="materialName" :width="columnWidths.materialName"/>
  115. <el-table-column :label="t('route.quantity')" align="center" prop="quantity" :width="columnWidths.quantity"/>
  116. <el-table-column :label="t('workOrderMaterial.unitPrice')" align="center" prop="unitPrice" :width="columnWidths.unitPrice"/>
  117. <el-table-column :label="t('workOrderMaterial.unit')" align="center" prop="unit" :width="columnWidths.unit"/>
  118. <el-table-column :label="t('route.safetyStock')" align="center" prop="safetyStock" :width="columnWidths.safetyStock"/>
  119. <el-table-column
  120. :label="t('chooseMaintain.createTime')"
  121. align="center"
  122. prop="createTime"
  123. :formatter="dateFormatter"
  124. :width="columnWidths.createTime"
  125. />
  126. </el-table>
  127. <!-- 分页 -->
  128. <Pagination
  129. :total="total"
  130. v-model:page="queryParams.pageNo"
  131. v-model:limit="queryParams.pageSize"
  132. @pagination="getList"
  133. />
  134. </ContentWrap>
  135. <!-- 表单弹窗:添加/修改 -->
  136. <IotSapStockForm ref="formRef" @success="getList" />
  137. </template>
  138. <script setup lang="ts">
  139. import { dateFormatter } from '@/utils/formatTime'
  140. import download from '@/utils/download'
  141. import { IotSapStockApi, IotSapStockVO } from '@/api/pms/iotsapstock'
  142. import IotSapStockForm from './IotSapStockForm.vue'
  143. import * as SapOrgApi from "@/api/system/saporg";
  144. /** PMS SAP 库存(通用库存/项目部库存) 列表 */
  145. defineOptions({ name: 'IotSapStock' })
  146. const message = useMessage() // 消息弹窗
  147. const { t } = useI18n() // 国际化
  148. const factoryList = ref([] as SapOrgApi.SapOrgVO[]) // 工厂列表
  149. const storageLocationList = ref([] as SapOrgApi.SapOrgVO[]) // 库存地点列表
  150. // 新增统计变量
  151. const totalQuantity = ref(0) // 总数量
  152. const totalAmount = ref(0) // 总金额
  153. const loading = ref(true) // 列表的加载中
  154. const list = ref<IotSapStockVO[]>([]) // 列表的数据
  155. const total = ref(0) // 列表的总页数
  156. const queryParams = reactive({
  157. pageNo: 1,
  158. pageSize: 10,
  159. deptId: undefined,
  160. factoryId: undefined,
  161. factory: undefined,
  162. storageLocationId: undefined,
  163. projectDepartment: undefined,
  164. materialCode: undefined,
  165. materialName: undefined,
  166. materialGroupName: undefined,
  167. materialGroupId: undefined,
  168. quantity: undefined,
  169. unitPrice: undefined,
  170. unit: undefined,
  171. safetyStock: undefined,
  172. shelvesId: undefined,
  173. cargoLocationId: undefined,
  174. type: undefined,
  175. syncStatus: undefined,
  176. syncTime: [],
  177. syncError: undefined,
  178. sort: undefined,
  179. status: undefined,
  180. remark: undefined,
  181. configFlag: 'A',
  182. createTime: [],
  183. })
  184. const queryFormRef = ref() // 搜索的表单
  185. const exportLoading = ref(false) // 导出的加载中
  186. // 表格容器和表格的引用
  187. const tableContainerRef = ref()
  188. const tableRef = ref()
  189. // 列宽度配置
  190. const columnWidths = ref({
  191. factory: '120px',
  192. projectDepartment: '120px',
  193. materialCode: '120px',
  194. materialName: '200px', // 初始宽度,会被计算覆盖
  195. quantity: '100px',
  196. unitPrice: '100px',
  197. unit: '100px',
  198. safetyStock: '120px',
  199. createTime: '180px'
  200. })
  201. /** 获取滚动条宽度 */
  202. const getScrollbarWidth = () => {
  203. const outer = document.createElement('div');
  204. outer.style.visibility = 'hidden';
  205. outer.style.overflow = 'scroll';
  206. document.body.appendChild(outer);
  207. const inner = document.createElement('div');
  208. outer.appendChild(inner);
  209. const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
  210. outer.parentNode?.removeChild(outer);
  211. return scrollbarWidth;
  212. };
  213. /** 计算文本宽度 */
  214. const getTextWidth = (text: string, fontSize = 14) => {
  215. const span = document.createElement('span');
  216. span.style.visibility = 'hidden';
  217. span.style.position = 'absolute';
  218. span.style.whiteSpace = 'nowrap';
  219. span.style.fontSize = `${fontSize}px`;
  220. span.style.fontFamily = 'inherit';
  221. span.innerText = text;
  222. document.body.appendChild(span);
  223. const width = span.offsetWidth;
  224. document.body.removeChild(span);
  225. return width;
  226. };
  227. /** 计算列宽度 */
  228. const calculateColumnWidths = () => {
  229. const MIN_WIDTH = 80; // 最小列宽
  230. const PADDING = 25; // 列内边距
  231. const FLEXIBLE_COLUMN = 'materialName'; // 可伸缩列
  232. const scrollbarWidth = getScrollbarWidth(); // 动态获取滚动条宽度
  233. if (!tableContainerRef.value?.$el || list.value.length === 0) return;
  234. const containerWidth = tableContainerRef.value.$el.clientWidth;
  235. // 需要自适应的列配置
  236. const autoColumns = [
  237. { key: 'factory', label: t('workOrderMaterial.factory'), getValue: (row) => row.factory },
  238. { key: 'projectDepartment', label: t('workOrderMaterial.storageLocation'), getValue: (row) => row.projectDepartment },
  239. { key: 'materialCode', label: t('chooseMaintain.materialCode'), getValue: (row) => row.materialCode },
  240. { key: 'materialName', label: t('chooseMaintain.materialName'), getValue: (row) => row.materialName },
  241. { key: 'quantity', label: t('route.quantity'), getValue: (row) => String(row.quantity) },
  242. { key: 'unitPrice', label: t('workOrderMaterial.unitPrice'), getValue: (row) => String(row.unitPrice) },
  243. { key: 'unit', label: t('workOrderMaterial.unit'), getValue: (row) => row.unit },
  244. { key: 'safetyStock', label: t('route.safetyStock'), getValue: (row) => String(row.safetyStock) },
  245. {
  246. key: 'createTime',
  247. label: t('chooseMaintain.createTime'),
  248. getValue: (row) => dateFormatter(null, null, row.createTime)
  249. }
  250. ];
  251. const newWidths: Record<string, string> = {};
  252. let totalFixedWidth = 0; // 所有固定列的总宽度
  253. // 计算除可伸缩列外的所有列宽度
  254. autoColumns.forEach(col => {
  255. if (col.key === FLEXIBLE_COLUMN) return;
  256. const headerText = col.label;
  257. const headerWidth = getTextWidth(headerText) * 1.3; // 表头宽度(加粗效果增加30%)
  258. // 计算内容最大宽度
  259. let contentMaxWidth = 0;
  260. list.value.forEach(row => {
  261. const text = col.getValue ? String(col.getValue(row)) : String(row[col.key] || '');
  262. const textWidth = getTextWidth(text);
  263. if (textWidth > contentMaxWidth) contentMaxWidth = textWidth;
  264. });
  265. // 取表头宽度、内容最大宽度和最小宽度的最大值
  266. const finalWidth = Math.max(headerWidth, contentMaxWidth, MIN_WIDTH) + PADDING;
  267. newWidths[col.key] = `${finalWidth}px`;
  268. totalFixedWidth += finalWidth;
  269. });
  270. // 处理可伸缩列(materialName)
  271. const flexibleCol = autoColumns.find(col => col.key === FLEXIBLE_COLUMN);
  272. if (flexibleCol) {
  273. const headerText = flexibleCol.label;
  274. const headerWidth = getTextWidth(headerText) * 1.3;
  275. let contentMaxWidth = 0;
  276. list.value.forEach(row => {
  277. const text = flexibleCol.getValue ? String(flexibleCol.getValue(row)) : String(row[flexibleCol.key] || '');
  278. const textWidth = getTextWidth(text);
  279. if (textWidth > contentMaxWidth) contentMaxWidth = textWidth;
  280. });
  281. const baseWidth = Math.max(headerWidth, contentMaxWidth, MIN_WIDTH) + PADDING;
  282. // 剩余空间 = 容器宽度 - 其他列总宽度 - 垂直滚动条宽度
  283. const remainingWidth = containerWidth - totalFixedWidth - scrollbarWidth;
  284. // 可伸缩列的宽度取剩余空间和基础宽度的最大值
  285. const flexibleWidth = Math.max(remainingWidth, baseWidth);
  286. newWidths[FLEXIBLE_COLUMN] = `${flexibleWidth}px`;
  287. }
  288. // 更新列宽度
  289. columnWidths.value = newWidths;
  290. // 重新布局表格
  291. nextTick(() => {
  292. tableRef.value?.doLayout();
  293. });
  294. };
  295. /** 查询列表 */
  296. const getList = async () => {
  297. loading.value = true
  298. try {
  299. const data = await IotSapStockApi.getIotSapStockPage(queryParams)
  300. list.value = data.list
  301. total.value = data.total
  302. // 从第一条记录中提取统计值
  303. if (data.list && data.list.length > 0) {
  304. // 确保取到有效的数值(第一条记录中的统计值代表整个查询结果)
  305. totalQuantity.value = Number(data.list[0].totalQuantity) || 0
  306. totalAmount.value = Number(data.list[0].totalAmount) || 0
  307. } else {
  308. // 没有数据时重置为0
  309. totalQuantity.value = 0
  310. totalAmount.value = 0
  311. }
  312. // 数据加载完成后计算列宽
  313. nextTick(() => {
  314. calculateColumnWidths()
  315. })
  316. } finally {
  317. loading.value = false
  318. }
  319. // 加载工厂(SAP)列表
  320. factoryList.value = await SapOrgApi.SapOrgApi.getSimpleSapOrgList(1)
  321. // 加载库存地点(SAP)列表
  322. storageLocationList.value = await SapOrgApi.SapOrgApi.getSimpleSapOrgList(3)
  323. }
  324. /** 搜索按钮操作 */
  325. const handleQuery = () => {
  326. queryParams.pageNo = 1
  327. getList()
  328. }
  329. /** 重置按钮操作 */
  330. const resetQuery = () => {
  331. queryFormRef.value.resetFields()
  332. handleQuery()
  333. }
  334. /** 添加/修改操作 */
  335. const formRef = ref()
  336. const openForm = (type: string, id?: number) => {
  337. formRef.value.open(type, id)
  338. }
  339. const selectedFactoryReqVO = ref({
  340. type: 0, // 类型(1工厂 2成本中心 3库位)
  341. factoryCodes: [] // 已经选择的SAP工厂code 列表
  342. })
  343. /** 已经选择了 SAP工厂 */
  344. const selectedFactoryChange = async (selectedId: number | undefined) => {
  345. // 获取选中的factoryCode数组
  346. const selectedFactory = factoryList.value.find(item => item.id === selectedId)
  347. const selectedFactoryCodes = selectedFactory ? [selectedFactory.factoryCode] : []
  348. // 获得已经选择的 SAP 工厂 数组
  349. // const factoryIds = formData.value.factoryIds
  350. console.log('选择的工厂代码:', selectedFactoryCodes)
  351. // 根据选择的 SAP工厂 调用后台接口查询 SAP工厂下属的 库存地点列表
  352. selectedFactoryReqVO.value.type = 3
  353. selectedFactoryReqVO.value.factoryCodes = selectedFactoryCodes
  354. storageLocationList.value = await SapOrgApi.SapOrgApi.getSelectedList(selectedFactoryReqVO.value)
  355. }
  356. // 是否已经配置了安全库存 下拉列表 模拟字典项
  357. const resultOptions = computed(() => [
  358. {
  359. label: '全部',
  360. value: 'A' // 空值会触发 clearable 效果
  361. },
  362. {
  363. label: '是',
  364. value: 'Y' // 空值会触发 clearable 效果
  365. },
  366. {
  367. label: '否',
  368. value: 'N' // 空值会触发 clearable 效果
  369. },
  370. ])
  371. /** 删除按钮操作 */
  372. const handleDelete = async (id: number) => {
  373. try {
  374. // 删除的二次确认
  375. await message.delConfirm()
  376. // 发起删除
  377. await IotSapStockApi.deleteIotSapStock(id)
  378. message.success(t('common.delSuccess'))
  379. // 刷新列表
  380. await getList()
  381. } catch {}
  382. }
  383. /** 导出按钮操作 */
  384. const handleExport = async () => {
  385. try {
  386. // 导出的二次确认
  387. await message.exportConfirm()
  388. // 发起导出
  389. exportLoading.value = true
  390. const data = await IotSapStockApi.exportIotSapStock(queryParams)
  391. download.excel(data, 'PMS SAP 库存(通用库存/项目部库存).xls')
  392. } catch {
  393. } finally {
  394. exportLoading.value = false
  395. }
  396. }
  397. /** 初始化 **/
  398. onMounted(() => {
  399. getList()
  400. // 添加窗口大小变化监听
  401. window.addEventListener('resize', calculateColumnWidths)
  402. })
  403. onUnmounted(() => {
  404. // 移除窗口大小变化监听
  405. window.removeEventListener('resize', calculateColumnWidths)
  406. })
  407. // 监听列表数据变化,重新计算列宽
  408. watch(list, () => {
  409. nextTick(calculateColumnWidths)
  410. }, { deep: true })
  411. </script>
  412. <style scoped>
  413. /* 统计卡片样式 */
  414. .stat-card {
  415. border-radius: 4px;
  416. border: 1px solid #ebeef5;
  417. }
  418. .stat-container {
  419. display: flex;
  420. padding: 1px;
  421. }
  422. .stat-item {
  423. display: flex;
  424. align-items: center;
  425. margin-right: 40px; /* 控制项间距 */
  426. }
  427. .stat-label {
  428. font-weight: bold;
  429. color: #606266;
  430. margin-right: 8px;
  431. }
  432. .stat-value {
  433. font-size: 18px;
  434. font-weight: bold;
  435. color: #409EFF;
  436. }
  437. /* 表格容器样式 - 确保可以水平滚动 */
  438. .table-container {
  439. overflow-x: auto;
  440. }
  441. /* 防止表格内容换行 */
  442. :deep(.el-table) .cell {
  443. white-space: nowrap !important;
  444. overflow: visible !important;
  445. text-overflow: unset !important;
  446. }
  447. /* 确保表格行不换行 */
  448. :deep(.el-table__row) {
  449. white-space: nowrap;
  450. }
  451. /* 表头特别处理 */
  452. :deep(.el-table__header) {
  453. .cell {
  454. display: inline-block;
  455. white-space: nowrap;
  456. width: auto !important;
  457. }
  458. }
  459. /* 表格整体布局优化 */
  460. :deep(.el-table__inner-wrapper) {
  461. min-width: 100% !important;
  462. width: auto !important;
  463. }
  464. /* 单元格内容完全显示 */
  465. :deep(.el-table__body-wrapper) .el-table__cell .cell {
  466. display: block;
  467. overflow: visible;
  468. text-overflow: unset;
  469. }
  470. </style>