index.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. {% extends "base.html" %}
  2. {% block title %}交易记录列表{% endblock %}
  3. {% block content %}
  4. <div class="container-fluid">
  5. <div class="row">
  6. <div class="col-12">
  7. <div class="card">
  8. <div class="card-header">
  9. <h3 class="card-title">交易记录列表</h3>
  10. <div class="card-tools">
  11. <a href="{{ url_for('transaction.add') }}" class="btn btn-primary btn-sm">新增交易</a>
  12. <a href="{{ url_for('transaction.import_view') }}" class="btn btn-success btn-sm">导入交易</a>
  13. </div>
  14. </div>
  15. <div class="card-body">
  16. <!-- 筛选表单 -->
  17. <form id="filterForm" class="mb-3">
  18. <div class="row">
  19. <div class="col-md-3">
  20. <div class="form-group">
  21. <label>时间范围</label>
  22. <div class="input-group">
  23. <input type="date" class="form-control" name="start_time">
  24. <div class="input-group-append">
  25. <span class="input-group-text">至</span>
  26. </div>
  27. <input type="date" class="form-control" name="end_time">
  28. </div>
  29. </div>
  30. </div>
  31. <div class="col-md-2">
  32. <div class="form-group">
  33. <label>合约名称</label>
  34. <input type="text" class="form-control" name="name">
  35. </div>
  36. </div>
  37. <div class="col-md-2">
  38. <div class="form-group">
  39. <label>合约代码</label>
  40. <input type="text" class="form-control" name="contract_code">
  41. </div>
  42. </div>
  43. <div class="col-md-2">
  44. <div class="form-group">
  45. <label>交易类型</label>
  46. <select class="form-control" name="trade_type">
  47. <option value="">全部</option>
  48. <option value="0">模拟交易</option>
  49. <option value="1">实盘交易</option>
  50. </select>
  51. </div>
  52. </div>
  53. <div class="col-md-3">
  54. <div class="form-group">
  55. <label>交易状态 (可多选)</label>
  56. <select class="form-control select2" name="trade_status[]" multiple="multiple" data-placeholder="选择状态">
  57. <option value="0">进行中</option>
  58. <option value="1">已暂停</option>
  59. <option value="2">暂停进行</option>
  60. <option value="3">已结束</option>
  61. </select>
  62. </div>
  63. </div>
  64. </div>
  65. <div class="row">
  66. <div class="col-md-12 text-end">
  67. <button type="submit" class="btn btn-primary">查询</button>
  68. </div>
  69. </div>
  70. </form>
  71. <!-- 数据表格 - 添加列冻结功能 -->
  72. <div class="table-responsive" style="overflow-x: auto;">
  73. <table class="table table-bordered table-striped table-sm sticky-table" style="min-width: 1800px;">
  74. <thead>
  75. <tr>
  76. <th class="sticky-left-1" style="min-width: 60px;">ID</th>
  77. <th class="sticky-left-2" style="min-width: 140px;">成交时间</th>
  78. <th class="sticky-left-3" style="min-width: 100px;">合约代码</th>
  79. <th style="min-width: 80px;">名称</th>
  80. <th style="min-width: 100px;">账户</th>
  81. <th style="min-width: 80px;">交易类别</th>
  82. <th style="min-width: 120px;">操作策略</th>
  83. <th style="min-width: 80px;">多空仓位</th>
  84. <th style="min-width: 100px;">K线形态</th>
  85. <th style="min-width: 100px;">成交价格</th>
  86. <th style="min-width: 100px;">成交手数</th>
  87. <th style="min-width: 100px;">保证金</th>
  88. <th style="min-width: 100px;">最新价格</th>
  89. <th style="min-width: 100px;">实际收益率</th>
  90. <th style="min-width: 100px;">实际收益</th>
  91. <th style="min-width: 100px;">止损价格</th>
  92. <th style="min-width: 100px;">止损比例</th>
  93. <th style="min-width: 100px;">止损收益</th>
  94. <th class="sticky-right" style="min-width: 120px;">操作</th>
  95. </tr>
  96. </thead>
  97. <tbody id="transactionList">
  98. <!-- 数据将通过JavaScript动态加载 -->
  99. </tbody>
  100. </table>
  101. </div>
  102. <!-- 分页控件和每页数量选择器 -->
  103. <div class="d-flex justify-content-center align-items-center mt-3">
  104. <nav aria-label="Page navigation" class="me-3">
  105. <ul class="pagination mb-0" id="pagination">
  106. <!-- 分页按钮将通过JavaScript动态加载 -->
  107. </ul>
  108. </nav>
  109. <div class="d-flex align-items-center">
  110. <label for="itemsPerPageSelect" class="col-form-label me-2 mb-0">每页:</label>
  111. <select class="form-select form-select-sm" id="itemsPerPageSelect" style="width: auto;">
  112. <option value="10">10</option>
  113. <option value="20" selected>20</option>
  114. <option value="50">50</option>
  115. <option value="100">100</option>
  116. </select>
  117. </div>
  118. </div>
  119. </div>
  120. </div>
  121. </div>
  122. </div>
  123. </div>
  124. {% endblock %}
  125. {% block styles %}
  126. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css">
  127. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@ttskch/select2-bootstrap4-theme@x.x.x/dist/select2-bootstrap4.min.css">
  128. <style>
  129. .select2-container--bootstrap4 .select2-selection--multiple {
  130. min-height: calc(1.5em + .75rem + 2px);
  131. }
  132. .select2-container--bootstrap4 .select2-selection--multiple .select2-selection__rendered {
  133. padding-bottom: 0;
  134. }
  135. /* 表格水平滚动样式 */
  136. .table-responsive {
  137. border: 1px solid #dee2e6;
  138. border-radius: 0.375rem;
  139. }
  140. .table-responsive::-webkit-scrollbar {
  141. height: 8px;
  142. }
  143. .table-responsive::-webkit-scrollbar-track {
  144. background: #f1f1f1;
  145. border-radius: 4px;
  146. }
  147. .table-responsive::-webkit-scrollbar-thumb {
  148. background: #c1c1c1;
  149. border-radius: 4px;
  150. }
  151. .table-responsive::-webkit-scrollbar-thumb:hover {
  152. background: #a8a8a8;
  153. }
  154. /* 列冻结样式 */
  155. .sticky-table {
  156. position: relative;
  157. }
  158. .sticky-table thead th {
  159. position: sticky;
  160. top: 0;
  161. z-index: 10;
  162. background-color: #f8f9fa; /* 统一表头背景色 */
  163. }
  164. /* 冻结列统一样式 */
  165. .sticky-table .sticky-left-1,
  166. .sticky-table .sticky-left-2,
  167. .sticky-table .sticky-left-3,
  168. .sticky-table .sticky-right {
  169. position: sticky;
  170. z-index: 11; /* 确保冻结列在普通表头之上 */
  171. }
  172. /* 左侧冻结列 */
  173. .sticky-left-1 { left: 0; }
  174. .sticky-left-2 { left: 60px; }
  175. .sticky-left-3 { left: 200px; }
  176. /* 右侧冻结列 */
  177. .sticky-right { right: 0; }
  178. /* 冻结列表头和内容单元格背景色 */
  179. .sticky-table th.sticky-left-1,
  180. .sticky-table th.sticky-left-2,
  181. .sticky-table th.sticky-left-3,
  182. .sticky-table th.sticky-right {
  183. background-color: #f8f9fa; /* 冻结列表头背景色 */
  184. }
  185. .sticky-table td.sticky-left-1,
  186. .sticky-table td.sticky-left-2,
  187. .sticky-table td.sticky-left-3,
  188. .sticky-table td.sticky-right {
  189. background-color: #fff; /* 冻结内容列背景色 */
  190. }
  191. /* 修复斑马纹表格下的背景色问题 */
  192. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-left-1,
  193. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-left-2,
  194. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-left-3,
  195. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-right {
  196. background-color: #f9f9f9; /* 斑马纹行冻结列背景色 */
  197. }
  198. /* 添加边框以示区分 */
  199. .sticky-left-3 { border-right: 2px solid #dee2e6; }
  200. .sticky-right { border-left: 2px solid #dee2e6; }
  201. </style>
  202. {% endblock %}
  203. {% block scripts %}
  204. <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
  205. <script>
  206. function formatNumber(num, precision = 3) {
  207. if (num === null || num === undefined || isNaN(num)) {
  208. return '-';
  209. }
  210. let fixedNum = Number(parseFloat(num).toFixed(precision));
  211. return fixedNum.toString();
  212. }
  213. function formatPercentage(num, precision = 2) {
  214. if (num === null || num === undefined || isNaN(num)) {
  215. return '-';
  216. }
  217. let percentage = parseFloat(num) * 100;
  218. return formatNumber(percentage, precision) + '%';
  219. }
  220. // 判断是否可以显示平仓选项
  221. function canShowCloseOption(item) {
  222. // 只有开仓交易(position_type: 0=开多, 2=开空)且状态不是已结束(3)的情况下才显示平仓选项
  223. return (item.position_type === 0 || item.position_type === 2) && item.trade_status !== 3;
  224. }
  225. $(document).ready(function() {
  226. $('.select2').select2({
  227. theme: 'bootstrap4',
  228. width: '100%'
  229. });
  230. let currentPage = 1;
  231. let currentItemsPerPage = parseInt($('#itemsPerPageSelect').val()) || 20;
  232. // Restore state from sessionStorage on page load or set defaults
  233. const savedFiltersJSON = sessionStorage.getItem('transactionFilters');
  234. if (savedFiltersJSON) {
  235. const savedFilters = JSON.parse(savedFiltersJSON);
  236. $('#filterForm [name="start_time"]').val(savedFilters.start_time);
  237. $('#filterForm [name="end_time"]').val(savedFilters.end_time);
  238. $('#filterForm [name="name"]').val(savedFilters.name);
  239. $('#filterForm [name="contract_code"]').val(savedFilters.contract_code);
  240. $('#filterForm [name="trade_type"]').val(savedFilters.trade_type);
  241. if (savedFilters.trade_status) {
  242. $('#filterForm [name="trade_status[]"]').val(savedFilters.trade_status).trigger('change');
  243. } else {
  244. // 如果没有保存的交易状态,设置默认选中"进行中"和"暂停进行"
  245. $('#filterForm [name="trade_status[]"]').val(['0', '2']).trigger('change');
  246. }
  247. } else {
  248. // 如果没有保存的筛选条件,设置默认选中"进行中"和"暂停进行"
  249. $('#filterForm [name="trade_status[]"]').val(['0', '2']).trigger('change');
  250. }
  251. const savedPage = sessionStorage.getItem('transactionCurrentPage');
  252. if (savedPage) {
  253. currentPage = parseInt(savedPage, 10);
  254. }
  255. const savedItemsPerPage = sessionStorage.getItem('transactionItemsPerPage');
  256. if (savedItemsPerPage) {
  257. currentItemsPerPage = parseInt(savedItemsPerPage, 10);
  258. $('#itemsPerPageSelect').val(currentItemsPerPage);
  259. }
  260. function loadTransactions(page = 1, filters = {}) {
  261. currentPage = page;
  262. filters.page = page;
  263. filters.limit = currentItemsPerPage;
  264. // Save current state to sessionStorage
  265. sessionStorage.setItem('transactionFilters', JSON.stringify(getFilters()));
  266. sessionStorage.setItem('transactionCurrentPage', currentPage);
  267. sessionStorage.setItem('transactionItemsPerPage', currentItemsPerPage);
  268. $('#transactionList').html(`<tr><td colspan="19" class="text-center">数据加载中...</td></tr>`);
  269. $('#pagination').empty();
  270. $.get("{{ url_for('transaction.get_list') }}", filters, function(response) {
  271. if (response.code === 0) {
  272. let html = '';
  273. if (response.data && response.data.length > 0) {
  274. response.data.forEach(function(item) {
  275. html += `
  276. <tr>
  277. <td class="sticky-left-1">${item.id || '-'}</td>
  278. <td class="sticky-left-2">${item.transaction_time || '-'}</td>
  279. <td class="sticky-left-3">${item.contract_code || '-'}</td>
  280. <td>${item.name || '-'}</td>
  281. <td>${item.account || '-'}</td>
  282. <td>${getTradeTypeText(item.trade_type)}</td>
  283. <td>${item.strategy_name || '-'}</td>
  284. <td>${getPositionTypeText(item.position_type)}</td>
  285. <td>${item.candle_pattern || '-'}</td>
  286. <td>${formatNumber(item.price, 3)}</td>
  287. <td>${formatNumber(item.volume, 0)}</td>
  288. <td>${formatNumber(item.margin, 2)}</td>
  289. <td>${formatNumber(item.latest_price, 3)}</td>
  290. <td class="${getProfitLossClass(item.actual_profit_rate)}">${formatPercentage(item.actual_profit_rate, 2)}</td>
  291. <td class="${getProfitLossClass(item.actual_profit)}">${formatNumber(item.actual_profit, 2)}</td>
  292. <td>${formatNumber(item.stop_loss_price, 3)}</td>
  293. <td class="${getProfitLossClass(item.stop_loss_rate)}">${formatPercentage(item.stop_loss_rate, 2)}</td>
  294. <td class="${getProfitLossClass(item.stop_loss_profit)}">${formatNumber(item.stop_loss_profit, 2)}</td>
  295. <td class="sticky-right">
  296. <div class="btn-group">
  297. <button type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  298. 操作
  299. </button>
  300. <div class="dropdown-menu">
  301. <a class="dropdown-item" href="/transaction/detail/view/${item.id}">详情</a>
  302. <a class="dropdown-item" href="/transaction/edit/${item.id}">编辑</a>
  303. ${canShowCloseOption(item) ? `
  304. <div class="dropdown-divider"></div>
  305. <a class="dropdown-item text-warning" href="/transaction/add?close_for_transaction=${item.id}">
  306. <i class="fas fa-times-circle"></i> 平仓
  307. </a>
  308. ` : ''}
  309. <div class="dropdown-divider"></div>
  310. <button class="dropdown-item text-danger" onclick="deleteTransaction(${item.id})">删除</button>
  311. </div>
  312. </div>
  313. </td>
  314. </tr>
  315. `;
  316. });
  317. } else {
  318. html = `<tr><td colspan="19" class="text-center">暂无数据</td></tr>`;
  319. }
  320. $('#transactionList').html(html);
  321. renderPagination(response.count, page, currentItemsPerPage);
  322. } else {
  323. $('#transactionList').html(`<tr><td colspan="19" class="text-center text-danger">加载失败:${response.msg}</td></tr>`);
  324. }
  325. }).fail(function(jqXHR, textStatus, errorThrown) {
  326. $('#transactionList').html(`<tr><td colspan="19" class="text-center text-danger">加载失败:${textStatus}</td></tr>`);
  327. });
  328. }
  329. function renderPagination(totalItems, currentPage, itemsPerPage) {
  330. const totalPages = Math.ceil(totalItems / itemsPerPage);
  331. let paginationHtml = '';
  332. if (totalPages <= 1) {
  333. $('#pagination').empty();
  334. $('#itemsPerPageSelect').closest('div').hide();
  335. return;
  336. } else {
  337. $('#itemsPerPageSelect').closest('div').show();
  338. }
  339. paginationHtml += `<li class="page-item ${currentPage === 1 ? 'disabled' : ''}">
  340. <a class="page-link" href="#" data-page="${currentPage - 1}" aria-label="Previous">
  341. <span aria-hidden="true">&laquo;</span>
  342. </a>
  343. </li>`;
  344. const maxPagesToShow = 5;
  345. let startPage = Math.max(1, currentPage - Math.floor(maxPagesToShow / 2));
  346. let endPage = Math.min(totalPages, startPage + maxPagesToShow - 1);
  347. if (endPage - startPage + 1 < maxPagesToShow) {
  348. startPage = Math.max(1, endPage - maxPagesToShow + 1);
  349. }
  350. if (startPage > 1) {
  351. paginationHtml += `<li class="page-item"><a class="page-link" href="#" data-page="1">1</a></li>`;
  352. if (startPage > 2) {
  353. paginationHtml += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
  354. }
  355. }
  356. for (let i = startPage; i <= endPage; i++) {
  357. paginationHtml += `<li class="page-item ${i === currentPage ? 'active' : ''}">
  358. <a class="page-link" href="#" data-page="${i}">${i}</a>
  359. </li>`;
  360. }
  361. if (endPage < totalPages) {
  362. if (endPage < totalPages - 1) {
  363. paginationHtml += `<li class="page-item disabled"><span class="page-link">...</span></li>`;
  364. }
  365. paginationHtml += `<li class="page-item"><a class="page-link" href="#" data-page="${totalPages}">${totalPages}</a></li>`;
  366. }
  367. paginationHtml += `<li class="page-item ${currentPage === totalPages ? 'disabled' : ''}">
  368. <a class="page-link" href="#" data-page="${currentPage + 1}" aria-label="Next">
  369. <span aria-hidden="true">&raquo;</span>
  370. </a>
  371. </li>`;
  372. $('#pagination').html(paginationHtml);
  373. $('#pagination .page-link').on('click', function(e) {
  374. e.preventDefault();
  375. const page = $(this).data('page');
  376. if (page && page !== currentPage) {
  377. const filters = getFilters();
  378. loadTransactions(page, filters);
  379. }
  380. });
  381. }
  382. function getFilters() {
  383. const filters = {};
  384. $('#filterForm').serializeArray().forEach(function(item) {
  385. if (item.value) {
  386. if (item.name === 'trade_status[]') {
  387. if (!filters['trade_status']) {
  388. filters['trade_status'] = [];
  389. }
  390. filters['trade_status'].push(item.value);
  391. } else {
  392. filters[item.name] = item.value;
  393. }
  394. }
  395. });
  396. return filters;
  397. }
  398. function getPositionTypeText(type) {
  399. const types = {
  400. 0: '开多',
  401. 1: '平多',
  402. 2: '开空',
  403. 3: '平空'
  404. };
  405. return types[type] || '未知';
  406. }
  407. function getTradeTypeText(type) {
  408. return type === 1 ? '实盘' : (type === 0 ? '模拟' : '未知');
  409. }
  410. function getProfitLossClass(value) {
  411. if (value === null || value === undefined || isNaN(value)) {
  412. return '';
  413. }
  414. const num = parseFloat(value);
  415. if (num > 0) return 'text-danger';
  416. if (num < 0) return 'text-success';
  417. return '';
  418. }
  419. window.deleteTransaction = function(id) {
  420. if (!id) return;
  421. if (confirm('确认要删除这条交易记录吗?(将同时删除关联汇总记录)')) {
  422. $.ajax({
  423. url: "/transaction/api/delete/" + id,
  424. type: 'DELETE',
  425. success: function(response) {
  426. if (response.code === 0) {
  427. alert('删除成功');
  428. const filters = getFilters();
  429. loadTransactions(currentPage, filters);
  430. } else {
  431. alert('删除失败:' + response.msg);
  432. }
  433. },
  434. error: function(jqXHR, textStatus, errorThrown) {
  435. alert('删除失败:' + textStatus);
  436. }
  437. });
  438. }
  439. }
  440. $('#filterForm').on('submit', function(e) {
  441. e.preventDefault();
  442. const filters = getFilters();
  443. loadTransactions(1, filters);
  444. });
  445. $('#itemsPerPageSelect').on('change', function() {
  446. currentItemsPerPage = parseInt($(this).val());
  447. const filters = getFilters();
  448. loadTransactions(1, filters);
  449. });
  450. // Initial load with restored state
  451. const initialFilters = getFilters();
  452. loadTransactions(currentPage, initialFilters);
  453. });
  454. </script>
  455. {% endblock %}