CompanyDeviceStat.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <script setup lang="ts">
  2. import {ref, reactive, computed, onMounted, nextTick} from 'vue'
  3. import {
  4. Storehouse_StockOut_Company_Name_List,
  5. Storehouse_Company_Device_Statistics,
  6. Storehouse_Company_Device_Statistics_Excel
  7. } from '@/api/storehouse'
  8. import {useTablePublic} from '@/hooks/useTablePublic'
  9. import {ElMessage} from 'element-plus'
  10. // 左侧公司列表搜索与滚动
  11. const {globalStore} = useTablePublic()
  12. const searchKey = ref('')
  13. const companyAll = ref<string[]>([])
  14. const fetchCompanies = async () => {
  15. const res: any = await Storehouse_StockOut_Company_Name_List({User_tokey: globalStore.GET_User_tokey, T_name: searchKey.value || ''})
  16. companyAll.value = res?.Data || []
  17. }
  18. // 右侧公司设备统计
  19. interface RowItem {
  20. seq?: number
  21. out_number?: string
  22. product_name?: string
  23. device_sn?: string
  24. num?: number
  25. unit?: string
  26. remark?: string
  27. return_number?: string
  28. return_device_sn?: string
  29. return_remark?: string
  30. }
  31. interface DeviceStatItem {
  32. out_total: number
  33. return_total: number
  34. rows: RowItem[]
  35. }
  36. const activeCompany = ref('')
  37. const deviceList = ref<DeviceStatItem[]>([])
  38. // 右表加载状态
  39. const rightLoading = ref(false)
  40. const flatRows = computed(() => {
  41. // 拍平数据,同时将缺省 out_number/product_name 继承为上一条的值,便于合并
  42. const result: any[] = []
  43. deviceList.value.forEach((g) => {
  44. let currentOut = ''
  45. let currentProduct = ''
  46. g.rows.forEach((r: any) => {
  47. if (r.out_number) {
  48. currentOut = r.out_number
  49. if (r.product_name) currentProduct = r.product_name
  50. } else {
  51. r.out_number = currentOut
  52. if (!r.product_name) r.product_name = currentProduct
  53. }
  54. if (!r.product_name && currentProduct) r.product_name = currentProduct
  55. result.push({...r})
  56. })
  57. })
  58. return result
  59. })
  60. // 计算单号合并跨度
  61. const outSpanMap = computed(() => {
  62. const map = new Map<number, number>()
  63. const list = flatRows.value
  64. let i = 0
  65. while (i < list.length) {
  66. let j = i + 1
  67. while (j < list.length && list[j].out_number === list[i].out_number) j++
  68. map.set(i, j - i)
  69. i = j
  70. }
  71. return map
  72. })
  73. // 计算品名在同一单号内的合并跨度
  74. const productSpanMap = computed(() => {
  75. const map = new Map<number, number>()
  76. const list = flatRows.value
  77. let i = 0
  78. while (i < list.length) {
  79. let j = i + 1
  80. while (j < list.length && list[j].out_number === list[i].out_number) j++ // [i, j) 为同一出库单
  81. let start = i
  82. while (start < j) {
  83. let k = start + 1
  84. while (k < j && list[k].product_name === list[start].product_name) k++
  85. map.set(start, k - start)
  86. start = k
  87. }
  88. i = j
  89. }
  90. return map
  91. })
  92. // 表格合并策略
  93. const spanMethod = ({ column, rowIndex }: any) => {
  94. if (column?.property === 'out_number'|| column?.property === 'remark') {
  95. const span = outSpanMap.value.get(rowIndex) || 0
  96. return span > 0 ? { rowspan: span, colspan: 1 } : { rowspan: 0, colspan: 0 }
  97. }
  98. if (column?.property === 'product_name' || column?.property === 'num' || column?.property === 'unit') {
  99. const span = productSpanMap.value.get(rowIndex) || 0
  100. return span > 0 ? { rowspan: span, colspan: 1 } : { rowspan: 0, colspan: 0 }
  101. }
  102. return { rowspan: 1, colspan: 1 }
  103. }
  104. // 退回 SN 行:仅设备编号高亮,行本身不变
  105. const outTotal = computed(() => deviceList.value.reduce((s, i) => s + (i.out_total || 0), 0))
  106. const returnTotal = computed(() => deviceList.value.reduce((s, i) => s + (i.return_total || 0), 0))
  107. // 右侧滚动加载(对拍平后的行做前端增量渲染)
  108. const rightDisplayCount = ref(80)
  109. const rightPageSize = 80
  110. const rightVisibleRows = computed(() => flatRows.value.slice(0, rightDisplayCount.value))
  111. const canLoadMoreRight = computed(() => rightDisplayCount.value < flatRows.value.length)
  112. const onRightScroll = (e: Event) => {
  113. const el = e.target as HTMLElement
  114. if (!el) return
  115. if (el.scrollTop + el.clientHeight >= el.scrollHeight - 10 && canLoadMoreRight.value) {
  116. rightDisplayCount.value = Math.min(rightDisplayCount.value + rightPageSize, flatRows.value.length)
  117. }
  118. }
  119. const fetchDeviceStats = async (company?: string) => {
  120. if (!company) return
  121. try {
  122. rightLoading.value = true
  123. const res: any = await Storehouse_Company_Device_Statistics({User_tokey: globalStore.GET_User_tokey, T_company_name: company})
  124. deviceList.value = Array.isArray(res?.Data) ? res.Data : []
  125. // 重置右侧滚动加载
  126. rightDisplayCount.value = rightPageSize
  127. } finally {
  128. rightLoading.value = false
  129. }
  130. }
  131. const onSelectCompany = (name: string) => {
  132. activeCompany.value = name
  133. fetchDeviceStats(name)
  134. }
  135. // 导出
  136. const exporting = ref(false)
  137. const exportExcel = async () => {
  138. if (!activeCompany.value) {
  139. ElMessage.warning('请选择公司后再导出')
  140. return
  141. }
  142. try {
  143. exporting.value = true
  144. const response: any = await Storehouse_Company_Device_Statistics_Excel({
  145. User_tokey: globalStore.GET_User_tokey,
  146. T_company_name: activeCompany.value
  147. })
  148. // 兼容两种返回:1) blob 文件;2) json { Code, Data: 'url' }
  149. if (response instanceof Blob) {
  150. try {
  151. const text = await response.text()
  152. const json = JSON.parse(text)
  153. if (json?.Data) {
  154. window.open(json.Data)
  155. } else {
  156. // 非 JSON,按文件下载
  157. const blob = new Blob([text], {type: 'application/vnd.ms-excel;charset=utf8'})
  158. const url = window.URL.createObjectURL(blob)
  159. const a = document.createElement('a')
  160. a.href = url
  161. const now = new Date()
  162. const ts = `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}_${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}`
  163. a.download = `公司设备借出退回统计_${activeCompany.value}_${ts}.xlsx`
  164. document.body.appendChild(a)
  165. a.click()
  166. a.remove()
  167. window.URL.revokeObjectURL(url)
  168. }
  169. } catch (_) {
  170. const url = window.URL.createObjectURL(response)
  171. const a = document.createElement('a')
  172. a.href = url
  173. const now = new Date()
  174. const ts = `${now.getFullYear()}${(now.getMonth() + 1).toString().padStart(2, '0')}${now.getDate().toString().padStart(2, '0')}_${now.getHours().toString().padStart(2, '0')}${now.getMinutes().toString().padStart(2, '0')}`
  175. a.download = `公司设备借出退回统计_${activeCompany.value}_${ts}.xlsx`
  176. document.body.appendChild(a)
  177. a.click()
  178. a.remove()
  179. window.URL.revokeObjectURL(url)
  180. }
  181. } else if (response?.Code === 200 && response?.Data) {
  182. window.open(response.Data)
  183. } else {
  184. ElMessage.error('导出失败:未知返回格式')
  185. }
  186. ElMessage.success('导出成功')
  187. } catch (e) {
  188. ElMessage.error('导出失败,请稍后重试')
  189. } finally {
  190. exporting.value = false
  191. }
  192. }
  193. onMounted(() => {
  194. fetchCompanies()
  195. })
  196. </script>
  197. <template>
  198. <div class="company-device-stat">
  199. <div class="left-list">
  200. <div class="left-header">
  201. <h3 class="title">公司名称</h3>
  202. <el-input v-model="searchKey" placeholder="按公司名称搜索" clearable @change="fetchCompanies" />
  203. </div>
  204. <div class="company-scroll">
  205. <el-empty v-if="companyAll.length === 0" description="无数据" />
  206. <el-menu :default-active="activeCompany" class="company-menu" :collapse="false">
  207. <el-menu-item v-for="name in companyAll" :key="name" :index="name" @click="onSelectCompany(name)">
  208. {{ name.trim() }}
  209. </el-menu-item>
  210. </el-menu>
  211. </div>
  212. </div>
  213. <div class="right-content" v-if="activeCompany">
  214. <div class="toolbar">
  215. <h3 class="title">{{ activeCompany }} 设备借出/退回统计</h3>
  216. <div class="actions">
  217. <el-button type="success" :loading="exporting" @click="exportExcel">导出</el-button>
  218. </div>
  219. </div>
  220. <el-card class="box-card">
  221. <div class="stat-info">
  222. <span>借出总数:<b>{{ outTotal }}</b></span>
  223. <span>退回总数:<b>{{ returnTotal }}</b></span>
  224. </div>
  225. <div class="right-table-scroll" @scroll="onRightScroll">
  226. <el-table :data="rightVisibleRows" v-loading="rightLoading" style="width: 100%" :span-method="spanMethod" size="small">
  227. <el-table-column type="index" label="序号" width="60" align="center" />
  228. <el-table-column prop="out_number" label="出库单号" width="160" align="center" show-overflow-tooltip />
  229. <el-table-column prop="product_name" label="品名" width="220" align="center" show-overflow-tooltip />
  230. <el-table-column prop="device_sn" label="设备SN" width="180" align="center" show-overflow-tooltip>
  231. <template #default="{ row }">
  232. <span :class="row.return_device_sn ? 'sn-danger' : ''">{{ row.device_sn }}</span>
  233. </template>
  234. </el-table-column>
  235. <el-table-column prop="num" label="数量" width="80" align="center" />
  236. <el-table-column prop="unit" label="单位" width="80" align="center" />
  237. <el-table-column prop="remark" label="备注" width="160" align="center" show-overflow-tooltip />
  238. <el-table-column prop="return_number" label="退回单号" width="160" align="center" show-overflow-tooltip />
  239. <el-table-column prop="return_device_sn" label="退回SN" width="180" align="center" show-overflow-tooltip />
  240. <el-table-column prop="return_remark" label="退回备注" width="160" align="center" show-overflow-tooltip />
  241. <template #empty>
  242. <el-empty description="暂无数据" />
  243. </template>
  244. </el-table>
  245. <div v-if="canLoadMoreRight" class="right-loading-more">下拉加载更多...</div>
  246. </div>
  247. </el-card>
  248. </div>
  249. </div>
  250. </template>
  251. <style scoped lang="scss">
  252. @import '@/styles/var.scss';
  253. .company-device-stat {
  254. height: 100%;
  255. display: flex;
  256. overflow: hidden;
  257. .title {
  258. width: 100%;
  259. text-align: center;
  260. line-height: 1.7em;
  261. font-size: 20px;
  262. color: #707b84;
  263. }
  264. .left-list {
  265. @include f-direction;
  266. width: 290px;
  267. z-index: 1;
  268. .left-header { padding: 2px; }
  269. .company-scroll {
  270. height: calc(100% - 90px);
  271. overflow: auto;
  272. .company-menu { width: 100%; border-right: none; }
  273. :deep(.el-menu-item){
  274. line-height: 40px;
  275. height: 40px;
  276. padding: 0 15px;
  277. color: var(--el-text-color-regular);
  278. }
  279. :deep(.el-menu-item:hover){
  280. background-color: var(--el-color-primary-light-9);
  281. }
  282. :deep(.el-menu-item.is-active){
  283. // background-color: var(--el-color-primary-light-7);
  284. color: var(--el-color-primary);
  285. font-weight: 600;
  286. }
  287. }
  288. }
  289. .right-content {
  290. @include f-direction;
  291. z-index: 0;
  292. margin-left: 12px;
  293. width: calc(100% - 290px);
  294. .toolbar {
  295. display: flex;
  296. justify-content: space-between;
  297. align-items: center;
  298. margin-bottom: 8px;
  299. }
  300. .box-card { border-radius: 8px; }
  301. .stat-info { display: flex; gap: 16px; padding: 6px 0 12px 0; }
  302. /* 右侧表格行距再小一些:对 tbody 行强制最小高度与内边距 */
  303. :deep(.el-table){ --el-table-row-height: 26px; }
  304. :deep(.el-table__body tr){ height: 24px; }
  305. :deep(.el-table .cell){ padding: 2px 6px; line-height: 20px; }
  306. .right-table-scroll { max-height: calc(100vh - 260px); overflow: auto; }
  307. .right-loading-more { text-align: center; color: #999; padding: 8px 0; }
  308. /* 仅退回SN的设备编号标红 */
  309. :deep(.sn-danger){ color: var(--el-color-danger); }
  310. }
  311. }
  312. </style>