headquarters-stock.vue 13.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
<template>
  <div class="app-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-left">
        <h2>总部股份</h2>
        <p class="page-desc">管理总部股份相关数据</p>
      </div>
      <div class="header-right">
        <el-button 
          type="success" 
          icon="el-icon-download"
          @click="handleExport"
          :loading="exportLoading"
        >
          导出
        </el-button>
        <el-button 
          type="primary" 
          icon="el-icon-cpu"
          @click="showCalculateDialog"
          :loading="calculateLoading"
        >
          计算股份
        </el-button>
        <el-button 
          icon="el-icon-refresh" 
          @click="getList"
          :loading="loading"
        >
          刷新
        </el-button>
      </div>
    </div>

    <!-- 搜索区域 -->
    <div class="search-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
        <el-form-item label="统计月份" prop="statisticsMonth">
          <el-date-picker
            v-model="queryParams.statisticsMonth"
            type="month"
            placeholder="选择月份"
            format="yyyyMM"
            value-format="yyyyMM"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <div class="table-container">
      <NCC-table 
        v-loading="loading" 
        :data="list" 
        border 
        stripe
        height="calc(100vh - 420px)"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
      >
        <!-- 基本信息 -->
        <el-table-column prop="StatisticsMonth" label="统计月份" width="150" align="center" fixed="left">
          <template slot-scope="scope">
            <div class="month-name">
              <i class="el-icon-date month-icon"></i>
              <span>{{ formatMonth(scope.row.StatisticsMonth) || '无' }}</span>
            </div>
          </template>
        </el-table-column>

        <!-- 收入信息 -->
        <el-table-column label="收入信息" align="center">
          <el-table-column prop="IncomeGeneral" label="收入-全部" width="140" align="center">
            <template slot-scope="scope">
              <span class="highlight">{{ formatMoney(scope.row.IncomeGeneral) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="IncomeTechDept" label="收入-科技部" width="140" align="center">
            <template slot-scope="scope">
              <span class="highlight">{{ formatMoney(scope.row.IncomeTechDept) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 成本信息 -->
        <el-table-column label="成本信息" align="center">
          <el-table-column prop="CostReimbursement" label="成本-报销" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CostReimbursement) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="CostLabor" label="成本-人工" width="140" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CostLabor) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="CostEducationRent" label="成本-教育部房租" width="160" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CostEducationRent) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="CostWarehouseRent" label="成本-仓库房租" width="160" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CostWarehouseRent) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="CostHQRent" label="成本-总部房租" width="160" align="center">
            <template slot-scope="scope">
              <span>{{ formatMoney(scope.row.CostHQRent) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 利润结果 -->
        <el-table-column label="利润结果" align="center">
          <el-table-column prop="OperationalProfit" label="运营利润" width="140" align="center">
            <template slot-scope="scope">
              <span class="highlight profit">{{ formatMoney(scope.row.OperationalProfit) }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <!-- 操作列 -->
        <el-table-column label="操作" width="120" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button 
              type="text" 
              size="mini"
              icon="el-icon-view"
              @click="showDetail(scope.row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </NCC-table>
    </div>

    <!-- 计算股份弹窗 -->
    <el-dialog
      title="计算总部股份"
      :visible="calculateDialogVisible"
      width="500px"
      :close-on-click-modal="false"
      @close="handleCalculateDialogClose"
    >
      <el-form label-width="100px" label-position="right">
        <el-form-item label="统计月份" required>
          <el-date-picker
            v-model="calculateMonth"
            type="month"
            placeholder="请选择月份"
            format="yyyyMM"
            value-format="yyyyMM"
            style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCalculateDialogClose">取消</el-button>
        <el-button 
          type="primary" 
          @click="handleCalculateConfirm"
          :loading="calculateLoading"
        >
          确定
        </el-button>
      </div>
    </el-dialog>

    <!-- 详情弹窗 -->
    <headquarters-stock-detail-dialog
      :visible="detailVisible"
      :detail-data="currentDetailData"
      @close="handleDetailClose"
    />
  </div>
</template>

<script>
import { getHeadquartersStockList, generateHeadquartersStock } from '@/api/extend/headquartersStock'
import HeadquartersStockDetailDialog from './headquarters-stock-detail-dialog.vue'

export default {
  name: 'HeadquartersStock',
  components: {
    HeadquartersStockDetailDialog
  },
  data() {
    // 获取当前月份
    const getCurrentMonth = () => {
      const now = new Date()
      const year = now.getFullYear()
      const month = String(now.getMonth() + 1).padStart(2, '0')
      return `${year}${month}`
    }

    return {
      loading: false,
      calculateLoading: false,
      exportLoading: false,
      list: [],
      calculateDialogVisible: false,
      calculateMonth: getCurrentMonth(),
      detailVisible: false,
      currentDetailData: null,
      queryParams: {
        statisticsMonth: getCurrentMonth() // yyyyMM 格式
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表数据
    async getList() {
      this.loading = true
      try {
        const params = {
          StatisticsMonth: this.queryParams.statisticsMonth || undefined
        }

        const response = await getHeadquartersStockList(params)

        if (response.code === 200) {
          this.list = response.data.data || []
        } else {
          this.$message.error(response.msg || '获取数据失败')
        }
      } catch (error) {
        console.error('获取总部股份列表失败:', error)
        this.$message.error('获取数据失败')
      } finally {
        this.loading = false
      }
    },

    // 搜索
    handleQuery() {
      this.getList()
    },

    // 重置搜索
    resetQuery() {
      const getCurrentMonth = () => {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        return `${year}${month}`
      }

      this.queryParams = {
        statisticsMonth: getCurrentMonth()
      }
      this.getList()
    },

    // 显示计算股份弹窗
    showCalculateDialog() {
      const getCurrentMonth = () => {
        const now = new Date()
        const year = now.getFullYear()
        const month = String(now.getMonth() + 1).padStart(2, '0')
        return `${year}${month}`
      }
      this.calculateMonth = getCurrentMonth()
      this.calculateDialogVisible = true
    },

    // 关闭计算股份弹窗
    handleCalculateDialogClose() {
      this.calculateDialogVisible = false
    },

    // 确认计算股份
    async handleCalculateConfirm() {
      if (!this.calculateMonth) {
        this.$message.warning('请选择统计月份')
        return
      }

      this.calculateLoading = true
      try {
        const data = {
          statisticsMonth: this.calculateMonth
        }

        const response = await generateHeadquartersStock(data)
        if (response.code === 200) {
          this.$message.success('计算股份成功')
          this.calculateDialogVisible = false
          this.getList()
        } else {
          this.$message.error(response.msg || '计算股份失败')
        }
      } catch (error) {
        console.error('计算股份失败:', error)
        this.$message.error(error.message || '计算股份失败')
      } finally {
        this.calculateLoading = false
      }
    },

    // 显示详情
    showDetail(row) {
      this.currentDetailData = row
      this.detailVisible = true
    },

    // 关闭详情弹窗
    handleDetailClose() {
      this.detailVisible = false
      this.currentDetailData = null
    },

    // 导出功能
    async handleExport() {
      if (!this.list || this.list.length === 0) {
        this.$message.warning('暂无数据可导出')
        return
      }

      this.exportLoading = true
      try {
        // 动态导入 xlsx 库
        const XLSX = await import('xlsx')

        // 构建表头(根据后端DTO字段的中文映射)
        const headers = [
          '统计月份',
          '收入-全部',
          '收入-科技部',
          '成本-报销',
          '成本-人工',
          '成本-教育部房租',
          '成本-仓库房租',
          '成本-总部房租',
          '运营利润'
        ]

        // 构建数据行
        const dataRows = this.list.map(item => [
          this.formatMonth(item.StatisticsMonth) || '无',
          this.formatMoney(item.IncomeGeneral),
          this.formatMoney(item.IncomeTechDept),
          this.formatMoney(item.CostReimbursement),
          this.formatMoney(item.CostLabor),
          this.formatMoney(item.CostEducationRent),
          this.formatMoney(item.CostWarehouseRent),
          this.formatMoney(item.CostHQRent),
          this.formatMoney(item.OperationalProfit)
        ])

        // 合并表头和数据
        const excelData = [headers, ...dataRows]

        // 创建工作表
        const ws = XLSX.utils.aoa_to_sheet(excelData)

        // 设置列宽
        const colWidths = headers.map(() => ({ wch: 15 }))
        ws['!cols'] = colWidths

        // 创建工作簿
        const wb = XLSX.utils.book_new()
        XLSX.utils.book_append_sheet(wb, ws, '总部股份')

        // 生成文件名
        const now = new Date()
        const timestamp = now.getFullYear() + 
          String(now.getMonth() + 1).padStart(2, '0') + 
          String(now.getDate()).padStart(2, '0') + 
          String(now.getHours()).padStart(2, '0') + 
          String(now.getMinutes()).padStart(2, '0') + 
          String(now.getSeconds()).padStart(2, '0')
        const fileName = `总部股份_${timestamp}.xlsx`

        // 导出文件
        XLSX.writeFile(wb, fileName)

        this.$message.success('导出成功')
      } catch (error) {
        console.error('导出失败:', error)
        this.$message.error('导出失败,请重试')
      } finally {
        this.exportLoading = false
      }
    },

    // 格式化金额
    formatMoney(value) {
      if (value === null || value === undefined || value === '') {
        return '0.00'
      }
      return Number(value).toFixed(2)
    },

    // 格式化月份
    formatMonth(value) {
      if (!value || value.length !== 6) {
        return value || '无'
      }
      const year = value.substring(0, 4)
      const month = value.substring(4, 6)
      return `${year}-${month}`
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background: #f5f5f5;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .header-left {
    h2 {
      margin: 0 0 8px 0;
      color: #303133;
      font-size: 20px;
      font-weight: 600;
    }

    .page-desc {
      margin: 0;
      color: #909399;
      font-size: 14px;
    }
  }

  .header-right {
    display: flex;
    gap: 12px;
  }
}

.search-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.table-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.month-name {
  display: flex;
  align-items: center;
  gap: 6px;

  .month-icon {
    color: #409EFF;
    font-size: 16px;
  }
}

.highlight {
  color: #409EFF;
  font-weight: 600;
}

.profit {
  color: #67C23A;
}

// 表格样式优化
::v-deep .el-table {
  .el-table__header-wrapper {
    th {
      background-color: #f5f7fa;
      color: #606266;
      font-weight: 600;
    }
  }

  .el-table__body-wrapper {
    .el-table__row {
      &:hover {
        background-color: #f5f7fa;
      }
    }
  }
}
</style>