month-stat.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <script setup lang="ts">
  2. import { reactive, onMounted, ref, nextTick } from "vue";
  3. import { getMonthStatList, syncMonthStat } from "@/api/stat";
  4. import { getStationList } from "@/api/station";
  5. import { useRenderIcon } from "@/components/ReIcon/src/hooks";
  6. import { useStationScope } from "@/hooks/useStationScope";
  7. import { ElMessage, ElMessageBox } from "element-plus";
  8. defineOptions({ name: "AdminMonthStat" });
  9. const queryRef = ref();
  10. const state = reactive({
  11. syncMonth: "",
  12. formQuery: {
  13. stationId: "",
  14. statMonth: ""
  15. },
  16. stationOptions: [] as Array<{ stationId: string; stationName: string }>,
  17. pageQuery: {
  18. pageNum: 1,
  19. pageSize: 10,
  20. total: 0
  21. },
  22. tableData: {
  23. height: 500,
  24. data: [] as Array<any>,
  25. loading: false,
  26. columns: [
  27. { label: "统计月份", prop: "statMonth", width: 120, fixed: "left" },
  28. { label: "站点名称", prop: "stationName", width: 180 },
  29. { label: "总收入", prop: "totalIncome", width: 130 },
  30. { label: "退款金额", prop: "totalRefund", width: 130 },
  31. { label: "跨店收入", prop: "crossIncome", width: 130 },
  32. { label: "跨店支出", prop: "crossExpend", width: 130 },
  33. { label: "消费金额", prop: "consumption", width: 130 },
  34. { label: "平台服务费", prop: "platformFee", width: 140 },
  35. { label: "充值笔数", prop: "rechargeCount", width: 100 },
  36. { label: "退款笔数", prop: "refundCount", width: 100 },
  37. { label: "注册人数", prop: "registrations", width: 100 },
  38. { label: "活跃用户", prop: "activeUserCount", width: 100 },
  39. { label: "订单数量", prop: "ordersCount", width: 100 },
  40. { label: "创建时间", prop: "createTime", width: 180 }
  41. ]
  42. }
  43. });
  44. //站点筛选与全局当前站点联动(回调懒引用,规避 loadData 声明顺序的 TDZ 错误)
  45. useStationScope(state.formQuery, (refresh?: boolean) => loadData(refresh));
  46. onMounted(() => {
  47. const now = new Date();
  48. const lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1);
  49. state.syncMonth = lastMonth.getFullYear() + "-" + String(lastMonth.getMonth() + 1).padStart(2, "0");
  50. loadStations();
  51. loadData();
  52. nextTick(() => {
  53. const bodyHeight = document.body.clientHeight;
  54. const queryHeight = queryRef.value?.$el?.clientHeight || 0;
  55. state.tableData.height = bodyHeight - queryHeight - 260;
  56. });
  57. });
  58. const loadStations = () => {
  59. getStationList({ pageNum: 1, pageSize: 200 }).then((res: any) => {
  60. state.stationOptions = res?.list || res?.records || [];
  61. }).catch(() => {});
  62. };
  63. const loadData = (refresh: boolean = false) => {
  64. if (refresh) {
  65. state.pageQuery.pageNum = 1;
  66. }
  67. state.tableData.loading = true;
  68. const params: any = { ...state.pageQuery };
  69. if (state.formQuery.stationId) params.stationId = state.formQuery.stationId;
  70. if (state.formQuery.statMonth) params.statMonth = state.formQuery.statMonth;
  71. getMonthStatList(params)
  72. .then((res: any) => {
  73. state.tableData.data = res?.list || res?.records || [];
  74. state.pageQuery.total = res?.total || 0;
  75. })
  76. .catch(() => {
  77. state.tableData.data = [];
  78. ElMessage.error("加载月统计数据失败");
  79. })
  80. .finally(() => {
  81. state.tableData.loading = false;
  82. });
  83. };
  84. const handleSizeChange = (size: number) => {
  85. state.pageQuery.pageSize = size;
  86. loadData(true);
  87. };
  88. const handleCurrentChange = (page: number) => {
  89. state.pageQuery.pageNum = page;
  90. loadData();
  91. };
  92. const handleSearch = () => loadData(true);
  93. const handleReset = () => {
  94. state.formQuery = { stationId: "", statMonth: "" };
  95. loadData(true);
  96. };
  97. const handleSyncMonthStat = () => {
  98. if (!state.syncMonth) {
  99. ElMessage.warning("请输入同步月份");
  100. return;
  101. }
  102. ElMessageBox.confirm(
  103. `确认重新同步 ${state.syncMonth} 的统计数据吗?将同步所有站点的数据。`,
  104. "重新同步月统计",
  105. { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning" }
  106. ).then(() => {
  107. const loading = ElMessage({ message: "同步中...", type: "info", duration: 0 });
  108. syncMonthStat({ statMonth: state.syncMonth })
  109. .then(() => {
  110. loading.close();
  111. ElMessage.success("同步完成");
  112. loadData(true);
  113. })
  114. .catch(() => {
  115. loading.close();
  116. ElMessage.error("同步失败");
  117. });
  118. }).catch(() => {});
  119. };
  120. const formatMoney = (value: number) => {
  121. if (value === null || value === undefined) return "¥0.00";
  122. return `¥${(value / 100).toFixed(2)}`;
  123. };
  124. const moneyFields = ["totalIncome", "totalRefund", "crossIncome", "crossExpend", "consumption", "platformFee"];
  125. </script>
  126. <template>
  127. <div class="page-container">
  128. <el-card shadow="hover">
  129. <template #header>
  130. <span class="card-header">月统计</span>
  131. </template>
  132. <el-form ref="queryRef" :model="state.formQuery" inline class="search-form">
  133. <el-form-item label="站点">
  134. <el-select
  135. v-model="state.formQuery.stationId"
  136. placeholder="选择站点"
  137. clearable
  138. style="width: 200px"
  139. >
  140. <el-option
  141. v-for="item in state.stationOptions"
  142. :key="item.stationId"
  143. :label="item.stationName"
  144. :value="item.stationId"
  145. />
  146. </el-select>
  147. </el-form-item>
  148. <el-form-item label="统计月份">
  149. <el-input
  150. v-model="state.formQuery.statMonth"
  151. placeholder="如 2026-05"
  152. clearable
  153. style="width: 200px"
  154. />
  155. </el-form-item>
  156. <el-form-item>
  157. <el-button
  158. type="primary"
  159. :icon="useRenderIcon('ri/search-line')"
  160. @click="handleSearch"
  161. >
  162. 查询
  163. </el-button>
  164. <el-button
  165. :icon="useRenderIcon('ri/refresh-line')"
  166. @click="handleReset"
  167. >
  168. 重置
  169. </el-button>
  170. </el-form-item>
  171. <el-form-item>
  172. <el-input
  173. v-model="state.syncMonth"
  174. placeholder="同步月份 如 2026-05"
  175. clearable
  176. style="width: 200px"
  177. />
  178. <el-button
  179. type="warning"
  180. :icon="useRenderIcon('ri/loop-right-line')"
  181. @click="handleSyncMonthStat"
  182. style="margin-left: 8px"
  183. >
  184. 重新同步
  185. </el-button>
  186. </el-form-item>
  187. </el-form>
  188. <el-table
  189. v-loading="state.tableData.loading"
  190. :data="state.tableData.data"
  191. :height="state.tableData.height"
  192. border
  193. stripe
  194. >
  195. <template #empty>
  196. <el-empty description="暂无数据" />
  197. </template>
  198. <el-table-column
  199. v-for="col in state.tableData.columns"
  200. :key="col.prop"
  201. :prop="col.prop"
  202. :label="col.label"
  203. :width="col.width"
  204. :fixed="(col as any).fixed"
  205. show-overflow-tooltip
  206. >
  207. <template #default="{ row }">
  208. <template v-if="moneyFields.includes(col.prop)">
  209. {{ formatMoney(row[col.prop]) }}
  210. </template>
  211. <template v-else>
  212. {{ row[col.prop] }}
  213. </template>
  214. </template>
  215. </el-table-column>
  216. </el-table>
  217. <div class="pagination-container">
  218. <el-pagination
  219. v-model:current-page="state.pageQuery.pageNum"
  220. v-model:page-size="state.pageQuery.pageSize"
  221. :total="state.pageQuery.total"
  222. :page-sizes="[10, 20, 50, 100]"
  223. layout="total, sizes, prev, pager, next, jumper"
  224. @size-change="handleSizeChange"
  225. @current-change="handleCurrentChange"
  226. />
  227. </div>
  228. </el-card>
  229. </div>
  230. </template>
  231. <style scoped lang="scss">
  232. .page-container {
  233. padding: 20px;
  234. }
  235. .card-header { font-size: 16px; font-weight: 600; }
  236. .search-form {
  237. margin-bottom: 16px;
  238. }
  239. .pagination-container {
  240. display: flex;
  241. justify-content: flex-end;
  242. margin-top: 20px;
  243. }
  244. </style>