wlzboy
3 天以前 40a8157440e3b906da8f52e07d939d78c3f4c313
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
<template>
  <div class="app-container">
    <!-- 查询条件 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
      <el-form-item label="分公司" prop="deptIds">
        <el-select
          v-model="queryParams.deptIds"
          placeholder="全部分公司"
          clearable
          filterable
          multiple
          collapse-tags
          style="width: 240px"
        >
          <el-option
            v-for="dept in branchList"
            :key="dept.deptId"
            :label="dept.deptName"
            :value="dept.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="时间范围" prop="dateRange">
        <el-date-picker
          v-model="dateRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
 
    <!-- 操作栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['task:stat:export']"
        >导出Excel</el-button>
      </el-col>
    </el-row>
 
    <!-- 数据表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      style="width: 100%"
      :span-method="mergeDeptRows"
    >
      <el-table-column label="分公司" prop="deptName" align="center" min-width="140" />
      <el-table-column label="日期" prop="statDate" align="center" width="120" />
      <el-table-column label="录单数量" prop="orderCount" align="center" width="120">
        <template slot-scope="scope">
          <el-tag type="primary">{{ scope.row.orderCount }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
 
    <!-- 汇总信息 -->
    <div v-if="tableData.length > 0" style="margin-top: 12px; color: #606266; font-size: 13px;">
      共 <strong>{{ tableData.length }}</strong> 条记录,合计录单
      <strong style="color: #E6A23C; font-size: 16px;">{{ totalCount }}</strong> 单
    </div>
  </div>
</template>
 
<script>
import { getDeptOrderStat, exportDeptOrderStat } from "@/api/task";
import { listAllBranches } from "@/api/system/dept";
 
export default {
  name: "DeptOrderStat",
  data() {
    const now = new Date();
    const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
    const fmt = d => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
    return {
      loading: false,
      exportLoading: false,
      tableData: [],
      branchList: [],
      // 默认选择当月
      dateRange: [fmt(firstDay), fmt(now)],
      queryParams: {
        deptIds: []
      },
      // 合并行相关
      mergeMap: {}
    };
  },
  computed: {
    totalCount() {
      return this.tableData.reduce((sum, row) => sum + (row.orderCount || 0), 0);
    }
  },
  created() {
    this.loadBranchList();
    this.getList();
  },
  methods: {
    /** 加载分公司列表 */
    loadBranchList() {
      listAllBranches().then(res => {
        this.branchList = res.data || [];
      }).catch(() => {
        this.branchList = [];
      });
    },
 
    /** 查询统计数据 */
    getList() {
      if (!this.dateRange || this.dateRange.length !== 2) {
        this.$message.warning("请选择时间范围");
        return;
      }
      this.loading = true;
      const params = {
        startDate: this.dateRange[0],
        endDate: this.dateRange[1],
        deptIds: this.queryParams.deptIds && this.queryParams.deptIds.length > 0
          ? this.queryParams.deptIds.join(",")
          : undefined
      };
      getDeptOrderStat(params).then(res => {
        this.tableData = res.data || [];
        this.buildMergeMap();
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
 
    /** 构建合并行映射 */
    buildMergeMap() {
      const map = {};
      const data = this.tableData;
      let i = 0;
      while (i < data.length) {
        let j = i;
        while (j < data.length && data[j].deptName === data[i].deptName) {
          j++;
        }
        map[i] = j - i;
        for (let k = i + 1; k < j; k++) {
          map[k] = 0;
        }
        i = j;
      }
      this.mergeMap = map;
    },
 
    /** 合并分公司列(相同分公司行合并) */
    mergeDeptRows({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const span = this.mergeMap[rowIndex];
        if (span !== undefined) {
          return { rowspan: span, colspan: span === 0 ? 0 : 1 };
        }
      }
    },
 
    /** 搜索 */
    handleQuery() {
      this.getList();
    },
 
    /** 重置 */
    resetQuery() {
      const now = new Date();
      const firstDay = new Date(now.getFullYear(), now.getMonth(), 1);
      const fmt = d => `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
      this.dateRange = [fmt(firstDay), fmt(now)];
      this.queryParams.deptIds = [];
      this.getList();
    },
 
    /** 导出 Excel */
    handleExport() {
      if (!this.dateRange || this.dateRange.length !== 2) {
        this.$message.warning("请选择时间范围");
        return;
      }
      this.exportLoading = true;
      const params = {
        startDate: this.dateRange[0],
        endDate: this.dateRange[1],
        deptIds: this.queryParams.deptIds && this.queryParams.deptIds.length > 0
          ? this.queryParams.deptIds.join(",")
          : undefined
      };
      exportDeptOrderStat(params).then(res => {
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' });
        const url = window.URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = `分公司录单统计_${params.startDate}_${params.endDate}.xlsx`;
        a.click();
        window.URL.revokeObjectURL(url);
        this.exportLoading = false;
      }).catch(() => {
        this.exportLoading = false;
      });
    }
  }
};
</script>