statistics.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <div class="app-container">
  3. <!-- 查询条件 -->
  4. <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
  5. <!-- 公司选择 -->
  6. <el-form-item label="统计维度">
  7. <el-select v-model="queryParams.statisticsType" placeholder="请选择统计维度" filterable clearable size="small">
  8. <el-option v-for="(option, index) in dimensionStatistics" :key="index" :value="option.value" :label="option.text"></el-option>
  9. </el-select>
  10. </el-form-item>
  11. <!-- 项目选择 -->
  12. <el-form-item label="选择公司">
  13. <el-select v-model="queryParams.companyId" placeholder="请选择公司" filterable clearable size="small">
  14. <el-option v-for="dict in companyList" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
  15. </el-select>
  16. </el-form-item>
  17. <!-- 时间选择 -->
  18. <el-form-item label="年月">
  19. <el-date-picker
  20. v-model="timeRange"
  21. type="daterange"
  22. placeholder="选择年月"
  23. :value-format="'yyyy-MM-dd'"
  24. :picker-options="pickerOptions"
  25. @change="handleDateData"
  26. ></el-date-picker>
  27. </el-form-item>
  28. <el-form-item>
  29. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  30. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  31. </el-form-item>
  32. </el-form>
  33. <el-row :gutter="10" class="mb8">
  34. <el-col :span="1.5">
  35. <el-button
  36. type="warning"
  37. plain
  38. icon="el-icon-download"
  39. size="mini"
  40. :loading="exportLoading"
  41. @click="handleExport"
  42. v-hasPermi="['course:courseTrafficLog:statisticsExport']"
  43. >导出</el-button>
  44. </el-col>
  45. <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
  46. </el-row>
  47. <!-- 表格 -->
  48. <el-table border v-loading="loading" :data="courseTrafficLogList" @selection-change="handleSelectionChange"
  49. show-summary :summary-method="getSummaries">
  50. <el-table-column type="selection" width="55" align="center" />
  51. <el-table-column label="公司" align="center" prop="companyName" />
  52. <el-table-column v-if="showDept" label="部门" align="center" prop="deptName" />
  53. <el-table-column label="统计范围" align="center" prop="dateRange" />
  54. <el-table-column label="使用流量" align="center" prop="formattedTotalTraffic">
  55. </el-table-column>
  56. <el-table-column label="金额" align="center" prop="totalAmount">
  57. </el-table-column>
  58. </el-table>
  59. <!-- 分页 -->
  60. <pagination
  61. v-show="total > 0"
  62. :total="total"
  63. :page.sync="queryParams.pageNum"
  64. :limit.sync="queryParams.pageSize"
  65. @pagination="getList"
  66. />
  67. </div>
  68. </template>
  69. <script>
  70. import { listCourseTrafficLog, exportCourseTrafficLog,statisticsSummaryList,exportStatisticsSummary } from "@/api/course/courseTrafficLog";
  71. import { courseList } from "@/api/course/courseRedPacketLog";
  72. import { allList } from "@/api/company/company";
  73. export default {
  74. data() {
  75. return {
  76. showDept:false,
  77. dimensionStatistics:[
  78. {text:"按公司",value:1},
  79. {text:"按部门",value:2}
  80. ],
  81. timeRange: null, // 时间范围单独绑定
  82. companyList: [],
  83. projectOptions: [],
  84. pickerOptions: {
  85. shortcuts: [
  86. {
  87. text: '今日',
  88. onClick(picker) {
  89. const start = new Date();
  90. const end = new Date();
  91. picker.$emit('pick', [start, end]);
  92. }
  93. },
  94. {
  95. text: '昨日',
  96. onClick(picker) {
  97. const start = new Date();
  98. start.setDate(start.getDate() - 1);
  99. const end = new Date(start);
  100. picker.$emit('pick', [start, end]);
  101. }
  102. },
  103. {
  104. text: '本周',
  105. onClick(picker) {
  106. const now = new Date();
  107. const day = now.getDay() || 7;
  108. const start = new Date(now);
  109. start.setDate(now.getDate() - day + 1);
  110. const end = new Date();
  111. picker.$emit('pick', [start, end]);
  112. }
  113. },
  114. {
  115. text: '本月',
  116. onClick(picker) {
  117. const start = new Date(new Date().setDate(1));
  118. const end = new Date();
  119. picker.$emit('pick', [start, end]);
  120. }
  121. }
  122. ]
  123. },
  124. courseLists: [],
  125. loading: false,
  126. exportLoading: false,
  127. showSearch: true,
  128. total: 0,
  129. courseTrafficLogList: [],
  130. queryParams: {
  131. tabType: "", // 当前 tab 类型
  132. startDate: null,
  133. endDate: null,
  134. pageNum: 1,
  135. pageSize: 10,
  136. statisticsType: null,//统计维度 1、按照公司 2、按照部门
  137. companyId:null,
  138. project: null,
  139. courseId: null
  140. }
  141. };
  142. },
  143. created() {
  144. courseList().then(res => this.courseLists = res.list);
  145. this.getDicts("sys_course_project").then(res => this.projectOptions = res.data);
  146. this.getAllCompany();
  147. this.getList();
  148. },
  149. methods: {
  150. getSummaries(param) {
  151. // 原有的汇总方法
  152. const { columns, data } = param;
  153. const sums = [];
  154. columns.forEach((column, index) => {
  155. if (index === 0) {
  156. sums[index] = '总计';
  157. return;
  158. }
  159. if (column.property === 'formattedTotalTraffic') {
  160. sums[index] = this.getTotalTraffic();
  161. } else if (column.property === 'totalAmount') {
  162. sums[index] = '¥' + this.getTotalAmount();
  163. } else {
  164. sums[index] = '';
  165. }
  166. });
  167. return sums;
  168. },
  169. // 计算总流量(不四舍五入)
  170. getTotalTraffic() {
  171. if (!this.courseTrafficLogList || this.courseTrafficLogList.length === 0) {
  172. return '0';
  173. }
  174. let total = 0;
  175. // 方法B:从格式化字符串中提取数值(如果formattedTotalTraffic类似 "1.23GB")
  176. total = this.courseTrafficLogList.reduce((sum, item) => {
  177. // 移除非数字字符,只保留数字和小数点
  178. if (item.formattedTotalTraffic) {
  179. const numStr = item.formattedTotalTraffic.replace(/[^\d.]/g, '');
  180. return sum + parseFloat(numStr || 0);
  181. }
  182. return sum;
  183. }, 0);
  184. // 保留所有小数,不四舍五入
  185. return total.toFixed(10).replace(/\.?0+$/, '');
  186. },
  187. // 计算总金额(不四舍五入)
  188. getTotalAmount() {
  189. if (!this.courseTrafficLogList || this.courseTrafficLogList.length === 0) {
  190. return '0';
  191. }
  192. const total = this.courseTrafficLogList.reduce((sum, item) => {
  193. return sum + (parseFloat(item.totalAmount) || 0);
  194. }, 0);
  195. // 保留两位小数,不四舍五入
  196. return Math.floor(total * 100) / 100;
  197. },
  198. handleTabClick(tab) {
  199. this.queryParams.tabType = tab.name;
  200. this.queryParams.pageNum = 1;
  201. // 清理互斥参数
  202. if (tab.name !== "project") this.queryParams.project = null;
  203. if (tab.name !== "course") this.queryParams.courseId = null;
  204. if (tab.name !== "company") this.queryParams.companyId = null;
  205. this.getList();
  206. },
  207. handleDateData() {
  208. if (this.timeRange) {
  209. this.queryParams.startDate = this.timeRange[0];
  210. this.queryParams.endDate = this.timeRange[1];
  211. } else {
  212. this.queryParams.startDate = null;
  213. this.queryParams.endDate = null;
  214. }
  215. },
  216. getAllCompany() {
  217. allList().then(res => this.companyList = res.rows);
  218. },
  219. getList() {
  220. this.loading = true;
  221. if(this.queryParams.statisticsType == 2){
  222. this.showDept = true;
  223. }else{
  224. this.showDept = false;
  225. }
  226. statisticsSummaryList(this.queryParams).then(res => {
  227. this.courseTrafficLogList = res.rows;
  228. this.total = res.total;
  229. }).finally(() => {
  230. this.loading = false;
  231. });
  232. },
  233. handleQuery() {
  234. this.queryParams.pageNum = 1;
  235. this.getList();
  236. },
  237. resetQuery() {
  238. this.timeRange = null;
  239. this.queryParams = {
  240. ...this.queryParams,
  241. startDate: null,
  242. endDate: null,
  243. companyId: null,
  244. project: null,
  245. courseId: null,
  246. pageNum: 1,
  247. };
  248. this.getList();
  249. },
  250. formatTrafficData(traffic) {
  251. if (traffic < 1024) return `${traffic} B`;
  252. if (traffic < 1024 ** 2) return `${(traffic / 1024).toFixed(2)} KB`;
  253. if (traffic < 1024 ** 3) return `${(traffic / 1024 ** 2).toFixed(2)} MB`;
  254. return `${(traffic / 1024 ** 3).toFixed(2)} GB`;
  255. },
  256. handleSelectionChange(selection) {
  257. this.ids = selection.map(item => item.logId);
  258. },
  259. handleExport() {
  260. this.$confirm("确认导出数据?", "提示").then(() => {
  261. this.exportLoading = true;
  262. return exportStatisticsSummary(this.queryParams);
  263. }).then(res => {
  264. this.download(res.msg);
  265. }).finally(() => {
  266. this.exportLoading = false;
  267. });
  268. }
  269. }
  270. };
  271. </script>