index.vue 8.31 KB
<template>
  <div class="NCC-common-layout">
    <div class="NCC-common-layout-center">
      <el-row class="NCC-common-search-box" :gutter="16">
        <el-form @submit.native.prevent>
          <el-col :span="6">
            <el-form-item label="统计月份">
              <el-date-picker
                v-model="searchForm.statisticsMonth"
                type="month"
                placeholder="选择月份"
                format="yyyy年MM月"
                value-format="yyyyMM"
                :clearable="false"
                @change="handleSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="老师姓名">
              <el-input v-model="searchForm.teacherName" placeholder="请输入老师姓名" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="门店名称">
              <el-input v-model="searchForm.storeName" placeholder="请输入门店名称" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" :loading="loading" @click="handleSearch">查询</el-button>
              <el-button icon="el-icon-refresh-right" @click="handleReset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div class="NCC-common-layout-main NCC-flex-main">
        <div class="NCC-common-head">
          <div class="NCC-common-head-left">
            <h3 class="NCC-common-head-title">科技部耗卡业绩统计</h3>
          </div>
          <div class="NCC-common-head-right">
            <el-button type="text" icon="el-icon-view" @click="openAllDetail">查看全部明细</el-button>
            <el-button type="text" icon="el-icon-download" :loading="exportLoading" @click="handleExportAll">导出明细</el-button>
          </div>
        </div>

        <NCC-table v-loading="loading" :data="tableData" :height="tableHeight">
          <el-table-column prop="TeacherName" label="老师姓名" fixed="left" min-width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <div class="table-cell-with-icon">
                <i class="el-icon-user cell-icon primary"></i>
                <span class="cell-text">{{ scope.row.TeacherName || '无' }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="TotalPerformance" label="总业绩" width="120" align="right">
            <template slot-scope="scope">
              <span class="amount-text">{{ formatMoney(scope.row.TotalPerformance) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="OrderCount" label="耗卡品项数" width="110" align="right">
            <template slot-scope="scope">{{ scope.row.OrderCount || 0 }}</template>
          </el-table-column>
          <el-table-column prop="CreateTime" label="创建时间" width="170" align="center">
            <template slot-scope="scope">{{ formatDateTime(scope.row.CreateTime) }}</template>
          </el-table-column>
          <el-table-column label="操作" width="120" fixed="right" align="left">
            <template slot-scope="scope">
              <el-button type="text" @click="openTeacherDetail(scope.row)">查看明细</el-button>
            </template>
          </el-table-column>
        </NCC-table>

        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.pageIndex"
          :limit.sync="pagination.pageSize"
          @pagination="handleSearch"
        />
      </div>
    </div>
    <TechConsumeDetailDialog ref="detailDialog" />
  </div>
</template>

<script>
import {
  getTechConsumePerformanceStatisticsList,
  exportTechConsumePerformanceDetail
} from '@/api/extend/statistics'
import TechConsumeDetailDialog from './tech-consume-detail-dialog.vue'
import Pagination from '@/components/Pagination'

export default {
  name: 'TechConsumePerformanceStatistics',
  components: { TechConsumeDetailDialog, Pagination },
  data() {
    return {
      loading: false,
      exportLoading: false,
      tableData: [],
      tableHeight: 400,
      searchForm: {
        statisticsMonth: this.getCurrentMonth(),
        teacherName: '',
        storeName: ''
      },
      pagination: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  mounted() {
    this.calculateTableHeight()
    this.handleSearch()
    window.addEventListener('resize', this.calculateTableHeight)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateTableHeight)
  },
  methods: {
    getCurrentMonth() {
      const now = new Date()
      const year = now.getFullYear()
      const month = String(now.getMonth() + 1).padStart(2, '0')
      return `${year}${month}`
    },
    calculateTableHeight() {
      this.$nextTick(() => {
        const mainEl = this.$el.querySelector('.NCC-common-layout-main')
        if (!mainEl) return
        const headEl = mainEl.querySelector('.NCC-common-head')
        const paginationEl = mainEl.querySelector('.pagination-container')
        const headHeight = headEl ? headEl.offsetHeight : 0
        const paginationHeight = paginationEl ? paginationEl.offsetHeight : 52
        this.tableHeight = Math.max(200, mainEl.clientHeight - headHeight - paginationHeight - 16)
      })
    },
    async handleSearch() {
      this.loading = true
      try {
        const params = {
          pageIndex: this.pagination.pageIndex,
          pageSize: this.pagination.pageSize,
          statisticsMonth: this.searchForm.statisticsMonth,
          teacherName: this.searchForm.teacherName,
          storeName: this.searchForm.storeName
        }
        const response = await getTechConsumePerformanceStatisticsList(params)
        this.tableData = response.data.list || []
        this.pagination.total = response.data.pagination.total || 0
        this.calculateTableHeight()
      } catch (error) {
        this.$message.error('查询失败: ' + error.message)
      } finally {
        this.loading = false
      }
    },
    handleReset() {
      this.searchForm = {
        statisticsMonth: this.getCurrentMonth(),
        teacherName: '',
        storeName: ''
      }
      this.pagination.pageIndex = 1
      this.handleSearch()
    },
    formatMoney(value) {
      if (value === null || value === undefined) return '0.00'
      return Number(value).toLocaleString('zh-CN', {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
      })
    },
    formatDateTime(value) {
      if (!value) return '-'
      return new Date(value).toLocaleString('zh-CN')
    },
    buildDetailQuery(teacherId = '', teacherName = '') {
      return {
        statisticsMonth: this.searchForm.statisticsMonth,
        teacherId,
        teacherName: teacherId ? teacherName : this.searchForm.teacherName,
        storeName: this.searchForm.storeName
      }
    },
    openTeacherDetail(row) {
      if (!this.$refs.detailDialog) return
      this.$refs.detailDialog.init(this.buildDetailQuery(row.TeacherId, row.TeacherName))
    },
    openAllDetail() {
      if (!this.searchForm.statisticsMonth) {
        this.$message.warning('请先选择统计月份')
        return
      }
      if (!this.$refs.detailDialog) return
      this.$refs.detailDialog.init(this.buildDetailQuery())
    },
    handleExportAll() {
      if (!this.searchForm.statisticsMonth) {
        this.$message.warning('请先选择统计月份')
        return
      }
      this.exportLoading = true
      exportTechConsumePerformanceDetail({
        ...this.buildDetailQuery(),
        pageIndex: 1,
        pageSize: 20
      }).then(res => {
        this.exportLoading = false
        if (res.code === 200 && res.data && res.data.url) {
          window.open(window.location.origin + res.data.url, '_blank')
          this.$message.success('导出成功')
        } else {
          this.$message.error(res.msg || '导出失败')
        }
      }).catch(() => {
        this.exportLoading = false
        this.$message.error('导出失败,请稍后重试')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.amount-text {
  color: #e6a23c;
  font-weight: 600;
}
</style>