index.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. {% extends "base.html" %}
  2. {% block title %}交易汇总列表{% endblock %}
  3. {% block styles %}
  4. <style>
  5. /* 表格水平滚动样式 */
  6. .table-responsive {
  7. border: 1px solid #dee2e6;
  8. border-radius: 0.375rem;
  9. overflow-x: auto;
  10. }
  11. .table-responsive::-webkit-scrollbar { height: 8px; }
  12. .table-responsive::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
  13. .table-responsive::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; }
  14. .table-responsive::-webkit-scrollbar-thumb:hover { background: #a8a8a8; }
  15. </style>
  16. {% endblock %}
  17. {% block content %}
  18. <div class="container-fluid">
  19. <div class="row">
  20. <div class="col-12">
  21. <div class="card">
  22. <div class="card-header">
  23. <h3 class="card-title">交易汇总列表</h3>
  24. <div class="card-tools">
  25. <button id="syncDataBtn" class="btn btn-primary btn-sm">同步数据</button>
  26. <a href="{{ url_for('trade.export') }}" class="btn btn-success btn-sm">导出Excel</a>
  27. </div>
  28. </div>
  29. <div class="card-body">
  30. <!-- 筛选表单 -->
  31. <form id="filterForm" class="mb-3">
  32. <div class="row">
  33. <div class="col-md-3">
  34. <div class="form-group">
  35. <label>时间范围</label>
  36. <div class="input-group">
  37. <input type="date" class="form-control" name="start_time">
  38. <div class="input-group-append">
  39. <span class="input-group-text">至</span>
  40. </div>
  41. <input type="date" class="form-control" name="end_time">
  42. </div>
  43. </div>
  44. </div>
  45. <div class="col-md-2">
  46. <div class="form-group">
  47. <label>合约名称</label>
  48. <input type="text" class="form-control" name="name">
  49. </div>
  50. </div>
  51. <div class="col-md-2">
  52. <div class="form-group">
  53. <label>合约代码</label>
  54. <input type="text" class="form-control" name="contract_code">
  55. </div>
  56. </div>
  57. <div class="col-md-2">
  58. <div class="form-group">
  59. <label>交易类型</label>
  60. <select class="form-control" name="trade_type">
  61. <option value="">全部</option>
  62. <option value="0">模拟交易</option>
  63. <option value="1">实盘交易</option>
  64. </select>
  65. </div>
  66. </div>
  67. <div class="col-md-2">
  68. <div class="form-group">
  69. <label>持仓方向</label>
  70. <select class="form-control" name="position_type">
  71. <option value="">全部</option>
  72. <option value="0">多头</option>
  73. <option value="1">空头</option>
  74. </select>
  75. </div>
  76. </div>
  77. <div class="col-md-1">
  78. <div class="form-group">
  79. <label>&nbsp;</label>
  80. <button type="submit" class="btn btn-primary form-control">查询</button>
  81. </div>
  82. </div>
  83. </div>
  84. </form>
  85. <!-- 数据表格 -->
  86. <div class="table-responsive">
  87. <table class="table table-striped table-hover">
  88. <thead>
  89. <tr>
  90. <th>ID</th>
  91. <th>开仓时间</th>
  92. <th>平仓时间</th>
  93. <th>合约代码</th>
  94. <th>名称</th>
  95. <th>账户</th>
  96. <th>操作策略</th>
  97. <th>持仓方向</th>
  98. <th>持仓手数</th>
  99. <th>持仓成本</th>
  100. <th>平均售价</th>
  101. <th>单笔收益</th>
  102. <th>投资收益率</th>
  103. <th>持仓天数</th>
  104. <th>年化收益率</th>
  105. <th>操作</th>
  106. </tr>
  107. </thead>
  108. <tbody id="tradeList">
  109. <!-- 数据将通过JavaScript动态加载 -->
  110. </tbody>
  111. </table>
  112. </div>
  113. <!-- 分页控件和每页数量选择器 -->
  114. <div class="d-flex justify-content-center align-items-center mt-3">
  115. <nav aria-label="Page navigation" class="me-3">
  116. <ul class="pagination mb-0" id="pagination">
  117. <!-- 分页按钮将通过JavaScript动态加载 -->
  118. </ul>
  119. </nav>
  120. <div class="d-flex align-items-center">
  121. <label for="itemsPerPageSelect" class="col-form-label me-2 mb-0">每页:</label>
  122. <select class="form-select form-select-sm" id="itemsPerPageSelect" style="width: auto;">
  123. <option value="10" selected>10</option>
  124. <option value="20">20</option>
  125. <option value="50">50</option>
  126. </select>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. </div>
  132. </div>
  133. </div>
  134. {% endblock %}
  135. {% block scripts %}
  136. <script>
  137. $(document).ready(function() {
  138. let currentPage = 1;
  139. let currentItemsPerPage = parseInt($('#itemsPerPageSelect').val()) || 10;
  140. // 从 sessionStorage 恢复状态
  141. const savedFiltersJSON = sessionStorage.getItem('tradeFilters');
  142. if (savedFiltersJSON) {
  143. const savedFilters = JSON.parse(savedFiltersJSON);
  144. Object.keys(savedFilters).forEach(key => {
  145. $(`[name="${key}"]`).val(savedFilters[key]);
  146. });
  147. }
  148. const savedPage = sessionStorage.getItem('tradeCurrentPage');
  149. if (savedPage) {
  150. currentPage = parseInt(savedPage, 10);
  151. }
  152. const savedItemsPerPage = sessionStorage.getItem('tradeItemsPerPage');
  153. if (savedItemsPerPage) {
  154. currentItemsPerPage = parseInt(savedItemsPerPage, 10);
  155. $('#itemsPerPageSelect').val(currentItemsPerPage);
  156. }
  157. // 加载交易汇总列表
  158. function loadTrades(page = 1, filters = {}) {
  159. currentPage = page;
  160. filters.page = page;
  161. filters.limit = currentItemsPerPage;
  162. // 保存当前状态到 sessionStorage
  163. sessionStorage.setItem('tradeFilters', JSON.stringify(getFilters()));
  164. sessionStorage.setItem('tradeCurrentPage', currentPage);
  165. sessionStorage.setItem('tradeItemsPerPage', currentItemsPerPage);
  166. console.log("开始加载交易汇总数据...");
  167. console.log("筛选条件:", filters);
  168. // 显示加载提示
  169. $('#tradeList').html('<tr><td colspan="16" class="text-center">数据加载中...</td></tr>');
  170. $('#pagination').empty(); // 清空分页
  171. $.ajax({
  172. url: "{{ url_for('trade.get_list') }}",
  173. type: "GET",
  174. data: filters,
  175. dataType: "json",
  176. success: function(response) {
  177. console.log("加载交易汇总数据成功", response);
  178. if (response.code === 0) {
  179. let html = '';
  180. if (response.data && response.data.length > 0) {
  181. console.log(`找到${response.data.length}条交易汇总记录`);
  182. response.data.forEach(function(item) {
  183. // 调试信息:检查close_time值
  184. if (item.id <= 5) { // 只对前5条记录输出调试信息,避免控制台过于冗长
  185. console.log(`交易ID ${item.id}: close_time = ${item.close_time}, 是否显示平仓按钮: ${!item.close_time}`);
  186. }
  187. html += `
  188. <tr>
  189. <td>${item.id || ''}</td>
  190. <td>${item.open_time || ''}</td>
  191. <td>${item.close_time || '-'}</td>
  192. <td>${item.contract_code || ''}</td>
  193. <td>${item.name || ''}</td>
  194. <td>${item.account || ''}</td>
  195. <td>${item.strategy_name || '-'}</td>
  196. <td>${getPositionTypeText(item.position_type)}</td>
  197. <td>${item.position_volume || ''}</td>
  198. <td>${item.past_position_cost !== undefined ? item.past_position_cost : '-'}</td>
  199. <td>${item.average_sale_price !== undefined ? item.average_sale_price : '-'}</td>
  200. <td>${item.single_profit !== undefined ? Math.round(item.single_profit) : '-'}</td>
  201. <td>${item.investment_profit_rate ? (item.investment_profit_rate * 100).toFixed(2) + '%' : '-'}</td>
  202. <td>${item.holding_days !== undefined ? item.holding_days : '-'}</td>
  203. <td>${item.annual_profit_rate ? (item.annual_profit_rate * 100).toFixed(2) + '%' : '-'}</td>
  204. <td>
  205. <div class="btn-group">
  206. <button type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  207. 操作
  208. </button>
  209. <div class="dropdown-menu">
  210. <a class="dropdown-item" href="/trade/detail/${item.id}">详情</a>
  211. ${!item.close_time ? `
  212. <div class="dropdown-divider"></div>
  213. <a class="dropdown-item text-warning" href="/transaction/add?close_for=${item.id}">
  214. <i class="fas fa-times-circle"></i> 平仓
  215. </a>
  216. ` : ''}
  217. </div>
  218. </div>
  219. </td>
  220. </tr>
  221. `;
  222. });
  223. } else {
  224. console.log("没有找到交易汇总记录");
  225. html = '<tr><td colspan="16" class="text-center">暂无数据</td></tr>';
  226. }
  227. $('#tradeList').html(html);
  228. renderPagination(response.count, page, currentItemsPerPage);
  229. } else {
  230. console.error('加载交易汇总失败:', response.msg);
  231. $('#tradeList').html(`<tr><td colspan="16" class="text-center text-danger">加载失败: ${response.msg}</td></tr>`);
  232. }
  233. },
  234. error: function(xhr, status, error) {
  235. console.error('加载交易汇总异常:', error);
  236. console.error('状态:', status);
  237. console.error('响应:', xhr.responseText);
  238. $('#tradeList').html(`<tr><td colspan="16" class="text-center text-danger">加载异常,请查看控制台</td></tr>`);
  239. }
  240. });
  241. }
  242. // 渲染分页控件
  243. function renderPagination(totalItems, currentPage, itemsPerPage) {
  244. const totalPages = Math.ceil(totalItems / itemsPerPage);
  245. let paginationHtml = '';
  246. if (totalPages <= 1) {
  247. $('#pagination').empty();
  248. // 如果只有一页或没有数据,也隐藏每页数量选择器
  249. $('#itemsPerPageSelect').closest('div').hide();
  250. return;
  251. } else {
  252. // 确保选择器可见
  253. $('#itemsPerPageSelect').closest('div').show();
  254. }
  255. // 上一页按钮
  256. paginationHtml += `<li class="page-item ${currentPage === 1 ? 'disabled' : ''}">
  257. <a class="page-link" href="#" data-page="${currentPage - 1}" aria-label="Previous">
  258. <span aria-hidden="true">&laquo;</span>
  259. </a>
  260. </li>`;
  261. // 页码按钮 (只显示部分页码)
  262. const maxPagesToShow = 5;
  263. let startPage = Math.max(1, currentPage - Math.floor(maxPagesToShow / 2));
  264. let endPage = Math.min(totalPages, startPage + maxPagesToShow - 1);
  265. if (endPage - startPage + 1 < maxPagesToShow) {
  266. startPage = Math.max(1, endPage - maxPagesToShow + 1);
  267. }
  268. if (startPage > 1) {
  269. paginationHtml += `<li class="page-item"><a class="page-link" href="#" data-page="1">1</a></li>`;
  270. if (startPage > 2) {
  271. paginationHtml += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
  272. }
  273. }
  274. for (let i = startPage; i <= endPage; i++) {
  275. paginationHtml += `<li class="page-item ${i === currentPage ? 'active' : ''}">
  276. <a class="page-link" href="#" data-page="${i}">${i}</a>
  277. </li>`;
  278. }
  279. if (endPage < totalPages) {
  280. if (endPage < totalPages - 1) {
  281. paginationHtml += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
  282. }
  283. paginationHtml += `<li class="page-item"><a class="page-link" href="#" data-page="${totalPages}">${totalPages}</a></li>`;
  284. }
  285. // 下一页按钮
  286. paginationHtml += `<li class="page-item ${currentPage === totalPages ? 'disabled' : ''}">
  287. <a class="page-link" href="#" data-page="${currentPage + 1}" aria-label="Next">
  288. <span aria-hidden="true">&raquo;</span>
  289. </a>
  290. </li>`;
  291. $('#pagination').html(paginationHtml);
  292. // 绑定分页按钮点击事件
  293. $('#pagination .page-link').on('click', function(e) {
  294. e.preventDefault();
  295. const page = $(this).data('page');
  296. if (page && page !== currentPage) {
  297. const filters = getFilters();
  298. loadTrades(page, filters);
  299. }
  300. });
  301. }
  302. // 获取当前筛选条件
  303. function getFilters() {
  304. const filters = {};
  305. $('#filterForm').serializeArray().forEach(function(item) {
  306. if (item.value) {
  307. filters[item.name] = item.value;
  308. }
  309. });
  310. return filters;
  311. }
  312. // 获取持仓方向文本
  313. function getPositionTypeText(type) {
  314. const types = {
  315. 0: '多头',
  316. 1: '空头'
  317. };
  318. return types[type] || '未知';
  319. }
  320. // 表单提交处理
  321. $('#filterForm').on('submit', function(e) {
  322. e.preventDefault();
  323. const filters = getFilters();
  324. loadTrades(1, filters); // 筛选后总是回到第一页
  325. });
  326. // 每页显示数量变化处理
  327. $('#itemsPerPageSelect').on('change', function() {
  328. currentItemsPerPage = parseInt($(this).val());
  329. const filters = getFilters();
  330. loadTrades(1, filters); // 更改每页数量后回到第一页
  331. });
  332. // 同步数据按钮点击事件
  333. $('#syncDataBtn').on('click', function() {
  334. if (confirm('确定要从交易明细中全面同步交易汇总数据吗?这可能需要一些时间。')) {
  335. const loadingIndicator = $('<div class="overlay"><i class="fas fa-2x fa-sync-alt fa-spin"></i></div>');
  336. $('.card').append(loadingIndicator);
  337. $.ajax({
  338. url: "{{ url_for('trade.sync_all') }}",
  339. type: 'POST',
  340. success: function(res) {
  341. loadingIndicator.remove();
  342. if (res.code === 0) {
  343. alert(res.msg || '同步成功!');
  344. loadTrades(currentPage, getFilters()); // 刷新当前页
  345. } else {
  346. alert('同步失败: ' + (res.msg || '未知错误'));
  347. }
  348. },
  349. error: function() {
  350. loadingIndicator.remove();
  351. alert('请求失败,请检查网络或联系管理员。');
  352. }
  353. });
  354. }
  355. });
  356. // 初始加载
  357. loadTrades(currentPage, getFilters());
  358. });
  359. </script>
  360. {% endblock %}