Newer
Older
carbon-metering-front / src / views / energyConsumption / data / index.vue
<!-- 综合能耗数据列表 -->
<script lang="ts" setup name="EnergyConsumptionList">
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import detailDialog from './components/detailDialog.vue'
import { exportFile } from '@/utils/exportUtils'
import { batchRemoveEnergyConsumption, exportEnergyConsumption, getEnergyConsumptionList, importEnergyConsumption, removeEnergyConsumption, templateDownload } from '@/api/api/energy/data'
// 查询参数
const searchQuery = ref({
  offset: 1,
  limit: 20,
  deptName: '',
  createTimeEnd: '',
  createTimeStart: '',
})
// 查询时间段范围
const TimeRanges = ref()
watch(() => TimeRanges.value, (newVal: string[]) => {
  if (newVal && newVal.length) {
    searchQuery.value.createTimeStart = newVal[0]
    searchQuery.value.createTimeEnd = newVal[1]
  }
  else {
    searchQuery.value.createTimeStart = ''
    searchQuery.value.createTimeEnd = ''
  }
}, { deep: true })
const loadingTable = ref(true)
const columns = ref([
  { text: '单位名称', value: 'deptName', align: 'center' },
  { text: '办公楼用电(千瓦时)', value: 'electricityUsage', align: 'center' },
  { text: '办公楼用电碳排(KG)', value: 'electricityCarbonEmissions', align: 'center' },
  { text: '办公楼取暖费(元)', value: 'buildingHeatingCosts', align: 'center' },
  { text: '办公楼取暖费碳排(KG)', value: 'buildingCarbonEmissions', align: 'center' },
  { text: '生产车辆用油费(元)', value: 'vehicleCosts', align: 'center' },
  { text: '生产车辆用油碳排(KG)', value: 'vehicleCarbonEmissions', align: 'center' },
  { text: '燃气使用费(元)', value: 'gasUsageFee', align: 'center' },
  { text: '燃气使用碳排(KG)', value: 'gasCarbonEmissions', align: 'center' },
  { text: '煤炭使用费(元)', value: 'coalUsageFee', align: 'center' },
  { text: '煤炭使用碳排(KG)', value: 'coalCarbonEmissions', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center' },
  { text: '备注', value: 'remarks', align: 'center' },
]) // 表格
const total = ref<number>(0)
const list = ref<any[]>([])
const search = () => {
  loadingTable.value = true
  getEnergyConsumptionList(searchQuery.value).then((res) => {
    list.value = res.data.rows
    total.value = res.data.total
    loadingTable.value = false
  }).catch(() => {
    loadingTable.value = false
  })
}
search()
const reset = () => {
  searchQuery.value = {
    offset: 1,
    limit: 20,
    deptName: '',
    createTimeEnd: '',
    createTimeStart: '',
  }
  TimeRanges.value = ''
  search()
}
// 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    searchQuery.value.limit = val.size
  }
  if (val && val.page) {
    searchQuery.value.offset = val.page
  }
  search()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 删除
const del = (row: any) => {
  console.log(row, 'row')
  ElMessageBox.confirm(
    '确认删除该数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      removeEnergyConsumption({ id: row.id }).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        search()
      })
    })
    .catch(() => {
    })
}
// 批量删除
const batchDel = () => {
  if (!selectList.value.length) {
    ElMessage.warning('请选择需要删除的数据')
    return
  }
  ElMessageBox.confirm(
    '确认删除选中的数据吗?',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  )
    .then(() => {
      batchRemoveEnergyConsumption({ ids: selectList.value.map((item: any) => item.id) }).then((res) => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        selectList.value = []
        search()
      })
    })
    .catch(() => {
    })
}
// 导出
const exportList = () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  exportEnergyConsumption({
    deptName: searchQuery.value.deptName,
    createTimeEnd: searchQuery.value.createTimeEnd,
    createTimeStart: searchQuery.value.createTimeStart,
  }).then((res) => {
    exportFile(res.data, '综合能耗数据管理')
    loading.close()
  }).catch(() => {
    loading.close()
  })
}
// 导入
const fileRef = ref() // 文件上传input
const importFun = () => {
  fileRef.value.click()
}
const onFileChange = (event: any) => {
  console.log(fileRef.value.value, '123')
  if (event.target.files?.length !== 0) {
    // 创建formdata对象
    const loading = ElLoading.service({
      lock: true,
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const file = event.target.files[0]
    const fd = new FormData()
    fd.append('file', file)
    importEnergyConsumption(fd).then((res) => {
      if (res.code === 200) {
        ElMessage.success('文件上传成功')
        loading.close()
        search()
      }
      else {
        ElMessage.error(res.message)
        loading.close()
      }
      fileRef.value.value = ''
    }).catch(() => {
      loading.close()
      fileRef.value.value = ''
    })
  }
}
// 详情
const detailRef = ref()
const detail = (row: any) => {
  detailRef.value.initDialog(row.id)
}
// 模板下载
const downloadFun = () => {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(255, 255, 255, 0.8)',
  })
  templateDownload().then((res) => {
    exportFile(res.data, '综合能耗数据管理模板')
    loading.close()
  }).catch((_) => {
    loading.close()
  })
}
</script>

<template>
  <div>
    <detail-dialog ref="detailRef" />
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <!-- <dept-select v-model="searchQuery.deptName" placeholder="所在部门" :dept-show="true" :need-top="false" /> -->
        <el-input v-model="searchQuery.deptName" placeholder="单位名称" type="text" />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="TimeRanges" type="datetimerange" format="YYYY-MM-DD HH:mm:ss"
          value-format="YYYY-MM-DD HH:mm:ss" range-separator="至" start-placeholder="创建开始时间" end-placeholder="创建结束时间"
          clearable
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="download" title="模板下载" @click="downloadFun" />
        <icon-button icon="icon-import" title="导入" @click="importFun" />
        <icon-button icon="icon-export" title="导出" @click="exportList" />
        <icon-button icon="icon-delete" title="批量删除" @click="batchDel" />
        <input v-show="false" ref="fileRef" type="file" @change="onFileChange">
      </template>
      <!-- 表格区域 -->
      <normal-table
        :data="list" :total="total" :columns="columns" :query="searchQuery" :list-loading="loadingTable"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multiSelect="multiSelect"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" width="120">
            <template #default="{ row }">
              <el-button type="primary" link size="small" @click="detail(row)">
                查看
              </el-button>
              <el-button type="danger" link size="small" @click="del(row)">
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </div>
</template>