DailyStatistics.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <script setup lang="ts">
  2. import dayjs from 'dayjs'
  3. import { IotRyDailyReportApi } from '@/api/pms/iotrydailyreport'
  4. import { useDebounceFn } from '@vueuse/core'
  5. import CountTo from '@/components/count-to1.vue'
  6. import * as echarts from 'echarts'
  7. import UnfilledReportDialog from '../UnfilledReportDialog.vue'
  8. import { Motion, AnimatePresence } from 'motion-v'
  9. import download from '@/utils/download'
  10. interface Query {
  11. pageNo: number
  12. pageSize: number
  13. deptId: number
  14. contractName?: string
  15. taskName?: string
  16. createTime: string[]
  17. projectClassification: 1 | 2
  18. }
  19. const props = defineProps<{
  20. query: Query
  21. deptName: string
  22. refreshKey: number
  23. }>()
  24. const totalWorkKeys: [string, string, string, string, number][] = [
  25. ['totalFootage', 'M', '累计进尺', 'i-solar:ruler-bold text-sky', 2],
  26. [
  27. 'utilizationRate',
  28. '%',
  29. '设备利用率',
  30. 'i-material-symbols:check-circle-outline-rounded text-emerald',
  31. 0
  32. ],
  33. [
  34. 'totalPowerConsumption',
  35. 'MWh',
  36. '累计用电量',
  37. 'i-material-symbols:electric-bolt-outline-rounded text-sky',
  38. 2
  39. ],
  40. [
  41. 'totalFuelConsumption',
  42. '升',
  43. '累计油耗',
  44. 'i-material-symbols:directions-car-outline-rounded text-sky',
  45. 2
  46. ],
  47. [
  48. 'averageFuelConsumption',
  49. '升',
  50. '平均油耗',
  51. 'i-material-symbols:directions-car-outline-rounded text-sky',
  52. 2
  53. ],
  54. ['totalCount', '个', '总数', 'i-tabler:report-analytics text-sky', 0],
  55. [
  56. 'alreadyReported',
  57. '个',
  58. '已填报',
  59. 'i-material-symbols:check-circle-outline-rounded text-emerald',
  60. 0
  61. ],
  62. ['notReported', '个', '未填报', 'i-material-symbols:cancel-outline-rounded text-rose', 0]
  63. ]
  64. const totalWork = ref({
  65. totalCount: 0,
  66. alreadyReported: 0,
  67. notReported: 0,
  68. totalFuelConsumption: 0,
  69. totalPowerConsumption: 0,
  70. totalFootage: 0,
  71. averageFuelConsumption: 0,
  72. utilizationRate: 0
  73. })
  74. const totalLoading = ref(false)
  75. const getTotal = useDebounceFn(async () => {
  76. totalLoading.value = true
  77. const { pageNo, pageSize, ...other } = props.query
  78. try {
  79. let res1: any[]
  80. if (props.query.createTime && props.query.createTime.length === 2) {
  81. res1 = await IotRyDailyReportApi.ryDailyReportStatistics({
  82. createTime: props.query.createTime,
  83. projectClassification: props.query.projectClassification
  84. })
  85. totalWork.value.totalCount = res1[0].count
  86. totalWork.value.alreadyReported = res1[1].count
  87. totalWork.value.notReported = res1[2].count
  88. }
  89. const res2 = await IotRyDailyReportApi.totalWorkload(other)
  90. totalWork.value = {
  91. ...totalWork.value,
  92. totalFootage: 0,
  93. ...res2,
  94. totalPowerConsumption: (res2.totalPowerConsumption || 0) / 1000,
  95. totalGasInjection: (res2.totalGasInjection || 0) / 10000,
  96. totalFuelConsumption: res2.totalFuelConsumption || 0,
  97. averageFuelConsumption: res2.averageFuelConsumption || 0,
  98. utilizationRate: Number(((res2.utilizationRate || 0) * 100).toFixed(2))
  99. }
  100. } finally {
  101. totalLoading.value = false
  102. }
  103. }, 1000)
  104. interface List {
  105. id: number | null
  106. name: string | null
  107. type: '1' | '2' | '3'
  108. cumulativeGasInjection: number | null
  109. cumulativeWaterInjection: number | null
  110. cumulativePowerConsumption: number | null
  111. cumulativeFuelConsumption: number | null
  112. transitTime: number | null
  113. nonProductiveTime: number | null
  114. averageFuelConsumption: number | null
  115. utilizationRate: number | null
  116. }
  117. const list = ref<List[]>([])
  118. const type = ref('2')
  119. const columns = (type: string) => {
  120. return [
  121. {
  122. label: type === '2' ? '项目部' : '队伍',
  123. prop: 'name'
  124. },
  125. {
  126. label: '累计进尺(M)',
  127. prop: 'cumulativeFootage'
  128. },
  129. {
  130. label: '累计用电量(MWh)',
  131. prop: 'cumulativePowerConsumption'
  132. },
  133. {
  134. label: '累计油耗(升)',
  135. prop: 'cumulativeFuelConsumption'
  136. },
  137. {
  138. label: '平均油耗(升)',
  139. prop: 'averageFuelConsumption'
  140. },
  141. {
  142. label: '平均时效(%)',
  143. prop: 'transitTime'
  144. },
  145. {
  146. label: '非生产时效(%)',
  147. prop: 'nonProductiveTime'
  148. },
  149. {
  150. label: '设备利用率(%)',
  151. prop: 'utilizationRate',
  152. action: true
  153. }
  154. ]
  155. }
  156. const listLoading = ref(false)
  157. const formatter = (row: List, column: any) => {
  158. if (column.property === 'transitTime') {
  159. return (Number(row.transitTime ?? 0) * 100).toFixed(2) + '%'
  160. } else if (column.property === 'nonProductiveTime') {
  161. return (Number(row.nonProductiveTime ?? 0) * 100).toFixed(2) + '%'
  162. } else if (column.property === 'utilizationRate') {
  163. return (Number(row.utilizationRate ?? 0) * 100).toFixed(2) + '%'
  164. } else return row[column.property] ?? 0
  165. }
  166. const getList = useDebounceFn(async () => {
  167. listLoading.value = true
  168. try {
  169. const res = await IotRyDailyReportApi.getIotRyDailyReportSummary(props.query)
  170. const { list: reslist } = res
  171. type.value = reslist[0]?.type || '2'
  172. list.value = reslist.map(
  173. ({ id, projectDeptId, projectDeptName, teamId, teamName, sort, taskId, type, ...other }) => ({
  174. id: type === '2' ? projectDeptId : teamId,
  175. name: type === '2' ? projectDeptName : teamName,
  176. ...other,
  177. cumulativePowerConsumption: ((other.cumulativePowerConsumption || 0) / 1000).toFixed(2),
  178. cumulativeFuelConsumption: (other.cumulativeFuelConsumption || 0).toFixed(2),
  179. averageFuelConsumption: (other.averageFuelConsumption || 0).toFixed(2),
  180. utilizationRate: other.utilizationRate || 0
  181. })
  182. )
  183. } finally {
  184. listLoading.value = false
  185. }
  186. }, 1000)
  187. const tab = ref<'表格' | '看板'>('表格')
  188. const currentTab = ref<'表格' | '看板'>('表格')
  189. const direction = ref<'left' | 'right'>('right')
  190. const handleSelectTab = (val: '表格' | '看板') => {
  191. tab.value = val
  192. direction.value = val === '看板' ? 'right' : 'left'
  193. nextTick(() => {
  194. currentTab.value = val
  195. setTimeout(() => {
  196. render()
  197. })
  198. })
  199. }
  200. const chartRef = ref<HTMLDivElement | null>(null)
  201. let chart: echarts.ECharts | null = null
  202. let chartContainerEl: HTMLDivElement | null = null
  203. const xAxisData = ref<string[]>([])
  204. const legend = ref<string[][]>([
  205. // ['累计油耗 (万升)', 'cumulativeFuelConsumption'],
  206. ['油耗 (升)', 'cumulativeFuelConsumption'],
  207. ['进尺 (M)', 'cumulativeFootage'],
  208. ['用电量 (KWh)', 'cumulativePowerConsumption'],
  209. // ['累计用电量 (MWh)', 'cumulativePowerConsumption'],
  210. ['平均时效 (%)', 'transitTime'],
  211. ['设备利用率 (%)', 'utilizationRate']
  212. ])
  213. const chartData = ref<Record<string, number[]>>({
  214. cumulativeFuelConsumption: [],
  215. cumulativeFootage: [],
  216. cumulativePowerConsumption: [],
  217. transitTime: [],
  218. utilizationRate: []
  219. })
  220. let chartLoading = ref(false)
  221. const getChart = useDebounceFn(async () => {
  222. chartLoading.value = true
  223. try {
  224. // 创建查询参数,如果 createTime 为空则不传
  225. const params: any = {
  226. deptId: props.query.deptId,
  227. projectClassification: props.query.projectClassification
  228. }
  229. // 只有 createTime 有值时才添加
  230. if (props.query.createTime && props.query.createTime.length === 2) {
  231. params.createTime = props.query.createTime
  232. }
  233. const res = await IotRyDailyReportApi.getIotRyDailyReportSummaryPolyline(props.query)
  234. chartData.value = {
  235. cumulativeFuelConsumption: res.map((item) => item.cumulativeFuelConsumption || 0),
  236. // cumulativeFuelConsumption: res.map((item) => (item.cumulativeFuelConsumption || 0) / 10000),
  237. cumulativeFootage: res.map((item) => item.cumulativeFootage || 0),
  238. cumulativePowerConsumption: res.map((item) => item.cumulativePowerConsumption || 0),
  239. // cumulativePowerConsumption: res.map((item) => (item.cumulativePowerConsumption || 0) / 1000),
  240. transitTime: res.map((item) => (item.transitTime || 0) * 100),
  241. utilizationRate: res.map((item) => Number(((item.utilizationRate || 0) * 100).toFixed(2)))
  242. }
  243. xAxisData.value = res.map((item) => item.reportDate || '')
  244. } finally {
  245. chartLoading.value = false
  246. }
  247. }, 1000)
  248. const resizer = () => {
  249. chart?.resize()
  250. }
  251. onUnmounted(() => {
  252. window.removeEventListener('resize', resizer)
  253. chart?.dispose()
  254. chart = null
  255. chartContainerEl = null
  256. })
  257. const selectedLegends = ref<Record<string, boolean>>({})
  258. const intervalArr = ref<number[]>([])
  259. const maxInterval = ref(0)
  260. const minInterval = ref(0)
  261. const calcIntervals = () => {
  262. let maxVal = -Infinity
  263. let minVal = Infinity
  264. let hasData = false
  265. for (const [name, key] of legend.value) {
  266. if (selectedLegends.value[name] !== false) {
  267. const dataset = chartData.value[key] || []
  268. if (dataset.length > 0) {
  269. hasData = true
  270. for (const val of dataset) {
  271. if (val > maxVal) maxVal = val
  272. if (val < minVal) minVal = val
  273. }
  274. }
  275. }
  276. }
  277. if (!hasData) {
  278. maxVal = 10000
  279. minVal = 0
  280. } else {
  281. minVal = minVal > 0 ? 0 : minVal
  282. }
  283. const maxDigits = (Math.floor(maxVal) + '').length
  284. const minDigits = minVal === 0 ? 0 : (Math.floor(Math.abs(minVal)) + '').length
  285. const interval = Math.max(maxDigits, minDigits)
  286. maxInterval.value = interval
  287. minInterval.value = minDigits
  288. const arr = [0]
  289. for (let i = 1; i <= interval; i++) {
  290. arr.push(Math.pow(10, i))
  291. }
  292. intervalArr.value = arr
  293. }
  294. const mapDataValue = (value: number) => {
  295. if (value === 0) return 0
  296. const isPositive = value > 0
  297. const absItem = Math.abs(value)
  298. if (!intervalArr.value.length) return value
  299. const min_value = Math.max(...intervalArr.value.filter((v) => v <= absItem))
  300. const min_index = intervalArr.value.findIndex((v) => v === min_value)
  301. const denominator =
  302. min_index < intervalArr.value.length - 1
  303. ? intervalArr.value[min_index + 1] - intervalArr.value[min_index]
  304. : intervalArr.value[min_index] || 1
  305. const new_value = (absItem - min_value) / denominator + min_index
  306. return isPositive ? new_value : -new_value
  307. }
  308. const getSeries = () => {
  309. return legend.value.map(([name, key]) => ({
  310. name,
  311. type: 'line',
  312. smooth: true,
  313. showSymbol: true,
  314. data: chartData.value[key].map((value) => mapDataValue(value))
  315. }))
  316. }
  317. const initChart = () => {
  318. if (!chartRef.value) return
  319. if (chart && chartContainerEl === chartRef.value) {
  320. return
  321. }
  322. chart?.dispose()
  323. chart = echarts.init(chartRef.value, undefined, { renderer: 'canvas' })
  324. chartContainerEl = chartRef.value
  325. window.addEventListener('resize', resizer)
  326. chart.on('legendselectchanged', (params: any) => {
  327. selectedLegends.value = params.selected
  328. calcIntervals()
  329. chart?.setOption({
  330. yAxis: {
  331. min: -minInterval.value,
  332. max: maxInterval.value
  333. },
  334. series: getSeries()
  335. })
  336. })
  337. }
  338. const render = () => {
  339. if (!chartRef.value) return
  340. initChart()
  341. legend.value.forEach(([name]) => {
  342. selectedLegends.value[name] = true
  343. })
  344. calcIntervals()
  345. chart?.setOption(
  346. {
  347. tooltip: {
  348. trigger: 'axis',
  349. axisPointer: { type: 'line' },
  350. formatter: (params: any) => {
  351. let d = `${params[0].axisValueLabel}<br>`
  352. let item = params.map((el: any) => {
  353. const realValue = chartData.value[legend.value[el.componentIndex][1]][el.dataIndex]
  354. return `<div class="flex items-center justify-between mt-1 gap-1">
  355. <span>${el.marker} ${el.seriesName}</span>
  356. <span>${realValue.toFixed(2)} ${el.seriesName.split(' ')[1] || ''}</span>
  357. </div>`
  358. })
  359. return d + item.join('')
  360. }
  361. },
  362. legend: {
  363. data: legend.value.map(([name]) => name),
  364. selected: selectedLegends.value,
  365. show: true
  366. },
  367. xAxis: {
  368. type: 'category',
  369. data: xAxisData.value
  370. },
  371. yAxis: {
  372. type: 'value',
  373. min: -minInterval.value,
  374. max: maxInterval.value,
  375. interval: 1,
  376. axisLabel: {
  377. formatter: (v: number) => {
  378. const num = v === 0 ? 0 : v > 0 ? Math.pow(10, v) : -Math.pow(10, -v)
  379. return num.toLocaleString()
  380. }
  381. }
  382. },
  383. series: getSeries()
  384. },
  385. true
  386. )
  387. }
  388. const handleQuery = () => {
  389. getChart().then(() => {
  390. render()
  391. })
  392. getList()
  393. getTotal()
  394. }
  395. watch(
  396. () => [
  397. props.refreshKey,
  398. props.query.deptId,
  399. props.query.contractName,
  400. props.query.taskName,
  401. props.query.createTime?.[0],
  402. props.query.createTime?.[1],
  403. props.query.projectClassification
  404. ],
  405. () => {
  406. if (!props.query.createTime) {
  407. totalWork.value.totalCount = 0
  408. totalWork.value.notReported = 0
  409. totalWork.value.alreadyReported = 0
  410. }
  411. handleQuery()
  412. },
  413. { immediate: true }
  414. )
  415. const exportChart = () => {
  416. if (!chart) return
  417. let img = new Image()
  418. img.src = chart.getDataURL({
  419. type: 'png',
  420. pixelRatio: 1,
  421. backgroundColor: '#fff'
  422. })
  423. img.onload = function () {
  424. let canvas = document.createElement('canvas')
  425. canvas.width = img.width
  426. canvas.height = img.height
  427. let ctx = canvas.getContext('2d')
  428. ctx?.drawImage(img, 0, 0)
  429. let dataURL = canvas.toDataURL('image/png')
  430. let a = document.createElement('a')
  431. let event = new MouseEvent('click')
  432. a.href = dataURL
  433. a.download = `瑞鹰钻井日报统计数据.png`
  434. a.dispatchEvent(event)
  435. }
  436. }
  437. const exportData = async () => {
  438. const res = await IotRyDailyReportApi.exportRyDailyReportStatistics(props.query)
  439. download.excel(res, '瑞鹰钻井日报统计数据.xlsx')
  440. }
  441. const exportAll = async () => {
  442. if (tab.value === '看板') exportChart()
  443. else exportData()
  444. }
  445. const message = useMessage()
  446. const unfilledDialogRef = ref()
  447. const openUnfilledDialog = () => {
  448. // 检查是否选择了创建时间
  449. if (!props.query.createTime || props.query.createTime.length === 0) {
  450. message.warning('请先选择创建时间范围')
  451. return
  452. }
  453. // 打开弹窗
  454. unfilledDialogRef.value?.open()
  455. }
  456. const router = useRouter()
  457. const tolist = (id: number, non: boolean = false) => {
  458. const { pageNo, pageSize, ...rest } = props.query
  459. router.push({
  460. path: '/iotdayilyreport/IotRyDailyReport',
  461. query: {
  462. ...rest,
  463. deptId: id,
  464. ...(non ? { nonProductFlag: 'Y' } : {})
  465. }
  466. })
  467. }
  468. </script>
  469. <template>
  470. <div class="grid grid-rows-[96px_1fr] gap-2 h-full min-h-0">
  471. <div class="grid grid-cols-8 gap-4">
  472. <div
  473. v-for="info in totalWorkKeys"
  474. :key="info[0]"
  475. class="bg-white dark:bg-[#1d1e1f] rounded-lg shadow px-1 py-2 flex flex-col items-center justify-center gap-0.5 min-w-0">
  476. <div class="size-6.5" :class="info[3]"></div>
  477. <count-to
  478. class="text-xl font-medium leading-6"
  479. :start-val="0"
  480. :end-val="totalWork[info[0]]"
  481. :decimals="info[4]"
  482. @click="info[2] === '未填报' ? openUnfilledDialog() : ''">
  483. <span class="text-xs leading-8 text-[var(--el-text-color-regular)]">暂无数据</span>
  484. </count-to>
  485. <div
  486. class="text-xs font-medium leading-5 text-[var(--el-text-color-regular)] whitespace-nowrap">
  487. {{ info[1] ? info[2] + '(' + info[1] + ')' : info[2] }}
  488. </div>
  489. </div>
  490. </div>
  491. <div class="bg-white dark:bg-[#1d1e1f] rounded-lg shadow flex flex-col p-3 gap-2 min-h-0">
  492. <div class="flex h-10 items-center justify-between">
  493. <el-button-group>
  494. <el-button
  495. size="default"
  496. :type="tab === '表格' ? 'primary' : 'default'"
  497. @click="handleSelectTab('表格')"
  498. >表格
  499. </el-button>
  500. <el-button
  501. size="default"
  502. :type="tab === '看板' ? 'primary' : 'default'"
  503. @click="handleSelectTab('看板')"
  504. >看板
  505. </el-button>
  506. </el-button-group>
  507. <h3 class="text-xl font-medium">{{ `${props.deptName}-${tab}` }}</h3>
  508. <el-button size="default" type="primary" @click="exportAll">导出</el-button>
  509. </div>
  510. <div class="flex-1 relative min-h-0">
  511. <el-auto-resizer class="absolute">
  512. <template #default="{ height }">
  513. <Motion
  514. as="div"
  515. :style="{ position: 'relative', overflow: 'hidden' }"
  516. :animate="{ height: `${height}px`, width: `100%` }"
  517. :transition="{ type: 'spring', stiffness: 200, damping: 25, duration: 0.3 }">
  518. <AnimatePresence :initial="false" mode="sync">
  519. <Motion
  520. :key="currentTab"
  521. as="div"
  522. :initial="{ x: direction === 'left' ? '-100%' : '100%', opacity: 0 }"
  523. :animate="{ x: '0%', opacity: 1 }"
  524. :exit="{ x: direction === 'left' ? '50%' : '-50%', opacity: 0 }"
  525. :transition="{ type: 'tween', stiffness: 300, damping: 30, duration: 0.3 }"
  526. :style="{ position: 'absolute', left: 0, right: 0, top: 0 }">
  527. <div :style="{ width: `100%`, height: `${height}px` }">
  528. <zm-table
  529. v-if="currentTab === '表格'"
  530. :loading="listLoading"
  531. :data="list"
  532. :height="height"
  533. show-border>
  534. <template v-for="item in columns(type)" :key="item.prop">
  535. <zm-table-column
  536. v-if="item.prop !== 'name' && item.prop !== 'nonProductiveTime'"
  537. :label="item.label"
  538. :prop="item.prop"
  539. :formatter="formatter"
  540. :action="item.action" />
  541. <zm-table-column
  542. v-else-if="item.prop === 'name'"
  543. :label="item.label"
  544. :prop="item.prop">
  545. <template #default="{ row }">
  546. <el-button text type="primary" @click.prevent="tolist(row.id)">{{
  547. row.name
  548. }}</el-button>
  549. </template>
  550. </zm-table-column>
  551. <zm-table-column v-else :label="item.label" :prop="item.prop">
  552. <template #default="{ row }">
  553. <el-button
  554. v-if="row.nonProductiveTime > 0"
  555. text
  556. type="primary"
  557. @click.prevent="tolist(row.id, true)">
  558. {{ (Number(row.nonProductiveTime ?? 0) * 100).toFixed(2) + '%' }}
  559. </el-button>
  560. <span v-else>
  561. {{ (Number(row.nonProductiveTime ?? 0) * 100).toFixed(2) + '%' }}
  562. </span>
  563. </template>
  564. </zm-table-column>
  565. </template>
  566. </zm-table>
  567. <div
  568. ref="chartRef"
  569. v-loading="chartLoading"
  570. :key="dayjs().valueOf()"
  571. v-else
  572. :style="{ width: `100%`, height: `${height}px` }">
  573. </div>
  574. </div>
  575. </Motion>
  576. </AnimatePresence>
  577. </Motion>
  578. </template>
  579. </el-auto-resizer>
  580. </div>
  581. </div>
  582. </div>
  583. <UnfilledReportDialog ref="unfilledDialogRef" :query-params="props.query" />
  584. </template>