index.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. max-height: 70vh; /* 限制最大高度,让表头可以固定 */
  11. }
  12. .table-responsive::-webkit-scrollbar { height: 8px; }
  13. .table-responsive::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
  14. .table-responsive::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 4px; }
  15. .table-responsive::-webkit-scrollbar-thumb:hover { background: #a8a8a8; }
  16. /* 列冻结样式 */
  17. .sticky-table { position: relative; width: 100%; }
  18. .sticky-table thead th { position: sticky; top: 0; z-index: 1; background-color: #f8f9fa; }
  19. .sticky-table td, .sticky-table th { white-space: nowrap; }
  20. /* 冻结列的 z-index 和 position */
  21. .sticky-table .sticky-left,
  22. .sticky-table .sticky-right {
  23. position: sticky;
  24. z-index: 2; /* 高于普通表头 */
  25. }
  26. /* 冻结列和表头的不透明背景 */
  27. .sticky-table th.sticky-left,
  28. .sticky-table th.sticky-right {
  29. background-color: #f8f9fa; /* 表头背景色 */
  30. }
  31. .sticky-table td.sticky-left,
  32. .sticky-table td.sticky-right {
  33. background-color: #ffffff; /* 单元格背景色 */
  34. }
  35. /* 修复斑马纹表格下的背景色问题 */
  36. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-left,
  37. .table-striped > tbody > tr:nth-of-type(odd) > td.sticky-right {
  38. background-color: #f9f9f9; /* 斑马纹行背景色 */
  39. }
  40. /* 左侧冻结列位置 */
  41. .sticky-left:nth-child(1) { left: 0; } /* 合约 */
  42. .sticky-left:nth-child(2) { left: 120px; } /* 名称 */
  43. /* 右侧冻结列 */
  44. .sticky-right {
  45. right: 0;
  46. border-left: 2px solid #dee2e6;
  47. }
  48. /* 左侧冻结列分隔线 */
  49. th.sticky-left:nth-child(2),
  50. td.sticky-left:nth-child(2) {
  51. border-right: 2px solid #dee2e6;
  52. }
  53. /* 开仓模式行背景色 */
  54. .row-virtual {
  55. background-color: #e3f2fd !important; /* 浅蓝色 */
  56. }
  57. .row-virtual td {
  58. background-color: #e3f2fd !important;
  59. }
  60. .row-execute {
  61. background-color: #e8f5e8 !important; /* 中等浅绿色 */
  62. }
  63. .row-execute td {
  64. background-color: #e8f5e8 !important;
  65. }
  66. /* 确保固定列也应用背景色 */
  67. .row-virtual .sticky-left,
  68. .row-virtual .sticky-right {
  69. background-color: #e3f2fd !important;
  70. }
  71. .row-execute .sticky-left,
  72. .row-execute .sticky-right {
  73. background-color: #e8f5e8 !important;
  74. }
  75. /* 确保虚拟和执行样式始终生效,不被斑马纹覆盖 */
  76. .table-striped > tbody > .row-virtual,
  77. .table-striped > tbody > .row-virtual:nth-of-type(odd) {
  78. background-color: #e3f2fd !important;
  79. }
  80. .table-striped > tbody > .row-virtual > td,
  81. .table-striped > tbody > .row-virtual:nth-of-type(odd) > td {
  82. background-color: #e3f2fd !important;
  83. }
  84. .table-striped > tbody > .row-execute,
  85. .table-striped > tbody > .row-execute:nth-of-type(odd) {
  86. background-color: #e8f5e8 !important;
  87. }
  88. .table-striped > tbody > .row-execute > td,
  89. .table-striped > tbody > .row-execute:nth-of-type(odd) > td {
  90. background-color: #e8f5e8 !important;
  91. }
  92. </style>
  93. {% endblock %}
  94. {% block content %}
  95. <div class="container-fluid">
  96. <div class="card">
  97. <div class="card-header">
  98. <div class="row mb-0 align-items-center">
  99. <div class="col-md-6">
  100. <h2>监控记录</h2>
  101. </div>
  102. <div class="col-md-6 text-end">
  103. <a href="{{ url_for('monitor.add') }}" class="btn btn-primary btn-sm me-1">添加监控记录</a>
  104. <a href="{{ url_for('monitor.import_view') }}" class="btn btn-success btn-sm">导入监控记录</a>
  105. </div>
  106. </div>
  107. </div>
  108. <div class="card-body">
  109. <form method="get" class="mb-3">
  110. <div class="row justify-content-start">
  111. <div class="col-md-auto">
  112. <div class="dropdown">
  113. <button class="btn btn-sm btn-outline-secondary dropdown-toggle" type="button" id="statusFilter" data-bs-toggle="dropdown" aria-expanded="false">
  114. 状态筛选
  115. </button>
  116. <div class="dropdown-menu p-3" aria-labelledby="statusFilter" style="min-width: 200px;" onclick="event.stopPropagation();">
  117. <div class="form-check">
  118. <input class="form-check-input" type="checkbox" value="0" id="status0" name="status" checked>
  119. <label class="form-check-label" for="status0">观察中</label>
  120. </div>
  121. <div class="form-check">
  122. <input class="form-check-input" type="checkbox" value="1" id="status1" name="status" checked>
  123. <label class="form-check-label" for="status1">重点关注</label>
  124. </div>
  125. <div class="form-check">
  126. <input class="form-check-input" type="checkbox" value="2" id="status2" name="status" checked>
  127. <label class="form-check-label" for="status2">已触发</label>
  128. </div>
  129. <div class="form-check">
  130. <input class="form-check-input" type="checkbox" value="3" id="status3" name="status">
  131. <label class="form-check-label" for="status3">已失效</label>
  132. </div>
  133. <hr class="my-2">
  134. <button type="button" class="btn btn-sm btn-primary" onclick="applyStatusFilter()">应用筛选</button>
  135. <button type="button" class="btn btn-sm btn-secondary ms-1" onclick="resetStatusFilter()">重置</button>
  136. </div>
  137. </div>
  138. </div>
  139. <div class="col-md-auto">
  140. <select name="market" class="form-select form-select-sm">
  141. <option value="">所有市场</option>
  142. <option value="0" {% if request.args.get('market') == '0' %}selected{% endif %}>国内</option>
  143. <option value="1" {% if request.args.get('market') == '1' %}selected{% endif %}>国际</option>
  144. </select>
  145. </div>
  146. <div class="col-md-auto">
  147. <button type="submit" class="btn btn-secondary btn-sm">筛选</button>
  148. </div>
  149. </div>
  150. </form>
  151. <div class="table-responsive">
  152. <table class="table table-striped table-bordered table-sm sticky-table" style="min-width: 1400px;">
  153. <thead>
  154. <tr>
  155. <th class="sticky-left" style="min-width: 120px;">合约</th>
  156. <th class="sticky-left" style="min-width: 150px;">名称</th>
  157. <th style="min-width: 120px;">开仓模式</th>
  158. <th style="min-width: 150px;">K线形态</th>
  159. <th style="min-width: 100px;">开仓价格</th>
  160. <th style="min-width: 100px;">关键价格</th>
  161. <th style="min-width: 120px;">做多保证金</th>
  162. <th style="min-width: 120px;">做空保证金</th>
  163. <th style="min-width: 100px;">状态</th>
  164. <th class="sticky-right" style="min-width: 120px;">操作</th>
  165. </tr>
  166. </thead>
  167. <tbody id="monitor-table-body">
  168. <tr>
  169. <td colspan="10" class="text-center">正在加载...</td>
  170. </tr>
  171. </tbody>
  172. </table>
  173. </div>
  174. </div>
  175. </div>
  176. </div>
  177. <!-- 自定义确认对话框 -->
  178. <div class="modal fade" id="confirmModal" tabindex="-1" aria-labelledby="confirmModalLabel" aria-hidden="true">
  179. <div class="modal-dialog modal-dialog-centered">
  180. <div class="modal-content">
  181. <div class="modal-header">
  182. <h5 class="modal-title" id="confirmModalLabel">确认操作</h5>
  183. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  184. </div>
  185. <div class="modal-body" id="confirmModalBody">
  186. 确定要执行此操作吗?
  187. </div>
  188. <div class="modal-footer">
  189. <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
  190. <button type="button" class="btn btn-primary" id="confirmModalOK">确定</button>
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. <!-- 错误提示模态框 -->
  196. <div class="modal fade" id="errorModal" tabindex="-1" aria-labelledby="errorModalLabel" aria-hidden="true">
  197. <div class="modal-dialog modal-dialog-centered">
  198. <div class="modal-content">
  199. <div class="modal-header">
  200. <h5 class="modal-title text-danger" id="errorModalLabel">操作失败</h5>
  201. <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
  202. </div>
  203. <div class="modal-body" id="errorModalBody">
  204. 操作失败,请稍后重试
  205. </div>
  206. <div class="modal-footer">
  207. <button type="button" class="btn btn-primary" data-bs-dismiss="modal">确定</button>
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. {% endblock %}
  213. {% block scripts %}
  214. <script>
  215. document.addEventListener('DOMContentLoaded', function() {
  216. const tableBody = document.getElementById('monitor-table-body');
  217. // 自定义确认对话框函数
  218. function showConfirmDialog(message, onConfirm) {
  219. const modal = new bootstrap.Modal(document.getElementById('confirmModal'));
  220. const body = document.getElementById('confirmModalBody');
  221. const okBtn = document.getElementById('confirmModalOK');
  222. body.textContent = message;
  223. // 移除旧的事件监听器
  224. okBtn.replaceWith(okBtn.cloneNode(true));
  225. const newOkBtn = document.getElementById('confirmModalOK');
  226. // 添加确认事件
  227. newOkBtn.addEventListener('click', function() {
  228. modal.hide();
  229. onConfirm();
  230. });
  231. // 监听回车键
  232. const keyHandler = function(e) {
  233. if (e.key === 'Enter') {
  234. e.preventDefault();
  235. modal.hide();
  236. document.removeEventListener('keydown', keyHandler);
  237. onConfirm();
  238. }
  239. };
  240. document.addEventListener('keydown', keyHandler);
  241. // 模态框关闭时清理事件监听器
  242. document.getElementById('confirmModal').addEventListener('hidden.bs.modal', function() {
  243. document.removeEventListener('keydown', keyHandler);
  244. }, { once: true });
  245. modal.show();
  246. // 聚焦到确定按钮,以便回车键生效
  247. setTimeout(() => {
  248. newOkBtn.focus();
  249. }, 150);
  250. }
  251. // 显示错误对话框
  252. function showErrorDialog(message) {
  253. const modal = new bootstrap.Modal(document.getElementById('errorModal'));
  254. const body = document.getElementById('errorModalBody');
  255. const okBtn = document.querySelector('#errorModal .btn-primary');
  256. body.textContent = message;
  257. // 监听回车键
  258. const keyHandler = function(e) {
  259. if (e.key === 'Enter') {
  260. e.preventDefault();
  261. modal.hide();
  262. document.removeEventListener('keydown', keyHandler);
  263. }
  264. };
  265. document.addEventListener('keydown', keyHandler);
  266. // 模态框关闭时清理事件监听器
  267. document.getElementById('errorModal').addEventListener('hidden.bs.modal', function() {
  268. document.removeEventListener('keydown', keyHandler);
  269. }, { once: true });
  270. modal.show();
  271. // 聚焦到确定按钮,以便回车键生效
  272. setTimeout(() => {
  273. okBtn.focus();
  274. }, 150);
  275. }
  276. // 将函数附加到window对象,以便在其他函数中使用
  277. window.showConfirmDialog = showConfirmDialog;
  278. window.showErrorDialog = showErrorDialog;
  279. // 状态映射
  280. const statusMap = {
  281. 0: '观察中',
  282. 1: '重点关注',
  283. 2: '已触发',
  284. 3: '已失效'
  285. };
  286. // 市场映射
  287. const marketMap = {
  288. 0: '国内',
  289. 1: '国外'
  290. };
  291. // 开仓模式数据缓存
  292. let positionModes = [];
  293. // 加载开仓模式数据
  294. function loadPositionModes() {
  295. return fetch('/monitor/api/position_modes')
  296. .then(response => response.json())
  297. .then(data => {
  298. if (data.code === 0) {
  299. positionModes = data.data;
  300. console.log('开仓模式数据加载成功:', positionModes);
  301. } else {
  302. console.error('加载开仓模式失败:', data.msg);
  303. }
  304. })
  305. .catch(error => {
  306. console.error('加载开仓模式出错:', error);
  307. });
  308. }
  309. // 获取开仓模式名称
  310. function getPositionModeName(modeId) {
  311. if (!modeId) return '-';
  312. const mode = positionModes.find(m => m.id === modeId);
  313. return mode ? mode.name : '-';
  314. }
  315. // 加载监控记录数据
  316. function loadMonitorRecords() {
  317. // 获取筛选参数
  318. const selectedStatuses = getSelectedStatuses();
  319. const urlParams = new URLSearchParams(window.location.search);
  320. const market = urlParams.get('market') || '';
  321. // 构建查询参数
  322. const params = new URLSearchParams();
  323. selectedStatuses.forEach(status => params.append('status', status));
  324. if (market) params.append('market', market);
  325. console.log('正在加载监控记录,参数:', params.toString());
  326. // 如果开仓模式数据未加载,先加载
  327. const loadPromise = positionModes.length > 0 ? Promise.resolve() : loadPositionModes();
  328. loadPromise.then(() => {
  329. return fetch(`/monitor/list?${params.toString()}`);
  330. })
  331. .then(response => response.json())
  332. .then(data => {
  333. console.log('监控记录API响应:', data);
  334. if (data.code === 0) {
  335. renderMonitorTable(data.data);
  336. } else {
  337. console.error('获取监控记录失败:', data.msg);
  338. tableBody.innerHTML = `<tr><td colspan="10" class="text-center text-danger">加载失败: ${data.msg}</td></tr>`;
  339. }
  340. })
  341. .catch(error => {
  342. console.error('获取监控记录出错:', error);
  343. tableBody.innerHTML = '<tr><td colspan="10" class="text-center text-danger">网络错误,请刷新重试</td></tr>';
  344. });
  345. }
  346. // 渲染监控记录表格
  347. function renderMonitorTable(monitors) {
  348. console.log('渲染监控记录数据:', monitors);
  349. if (!monitors || monitors.length === 0) {
  350. tableBody.innerHTML = '<tr><td colspan="10" class="text-center text-muted">暂无监控记录</td></tr>';
  351. return;
  352. }
  353. const rows = monitors.map(monitor => {
  354. const statusText = statusMap[monitor.status] || '未知状态';
  355. const positionModeText = getPositionModeName(monitor.position_mode_id);
  356. // 根据开仓模式确定行样式类
  357. let rowClass = '';
  358. if (positionModeText && positionModeText !== '无' && positionModeText.includes('虚拟')) {
  359. rowClass = 'row-virtual';
  360. } else if (positionModeText && positionModeText !== '无' && positionModeText.includes('执行')) {
  361. rowClass = 'row-execute';
  362. }
  363. return `
  364. <tr${rowClass ? ' class="' + rowClass + '"' : ''}>
  365. <td class="sticky-left">${monitor.contract || '-'}</td>
  366. <td class="sticky-left">${monitor.name || '-'}</td>
  367. <td>${positionModeText}</td>
  368. <td title="${monitor.candle_pattern || ''}">${truncateText(monitor.candle_pattern, 15)}</td>
  369. <td>${formatPrice(monitor.open_price)}</td>
  370. <td>${formatPrice(monitor.key_price)}</td>
  371. <td>${formatPrice(monitor.open_long_margin_per_unit)}</td>
  372. <td>${formatPrice(monitor.open_short_margin_per_unit)}</td>
  373. <td>
  374. <span class="badge ${getStatusBadgeClass(monitor.status)}">${statusText}</span>
  375. </td>
  376. <td class="sticky-right">
  377. <div class="btn-group dropstart">
  378. <button type="button" class="btn btn-secondary btn-sm dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
  379. 操作
  380. </button>
  381. <ul class="dropdown-menu">
  382. <li><button class="dropdown-item" onclick="viewDetail(${monitor.id})">查看详情</button></li>
  383. <li><button class="dropdown-item" onclick="editMonitor(${monitor.id})">编辑</button></li>
  384. ${monitor.status !== 3 ? '<li><button class="dropdown-item text-warning" onclick="invalidateMonitor(' + monitor.id + ')">失效</button></li>' : ''}
  385. <li><hr class="dropdown-divider"></li>
  386. <li><button class="dropdown-item text-danger" onclick="deleteMonitor(${monitor.id})">删除</button></li>
  387. </ul>
  388. </div>
  389. </td>
  390. </tr>
  391. `;
  392. }).join('');
  393. tableBody.innerHTML = rows;
  394. }
  395. // 格式化价格显示
  396. function formatPrice(price) {
  397. if (price === null || price === undefined || price === '') {
  398. return '-';
  399. }
  400. return parseFloat(price).toFixed(2);
  401. }
  402. // 截断长文本
  403. function truncateText(text, maxLength) {
  404. if (!text) return '-';
  405. return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
  406. }
  407. // 获取状态徽章的CSS类
  408. function getStatusBadgeClass(status) {
  409. const classes = {
  410. 0: 'bg-secondary', // 观察中
  411. 1: 'bg-warning', // 重点关注
  412. 2: 'bg-success', // 已触发
  413. 3: 'bg-danger' // 已失效
  414. };
  415. return classes[status] || 'bg-light';
  416. }
  417. // 获取选中的状态值
  418. function getSelectedStatuses() {
  419. const checkboxes = document.querySelectorAll('input[name="status"]:checked');
  420. return Array.from(checkboxes).map(cb => cb.value);
  421. }
  422. // 应用状态筛选
  423. window.applyStatusFilter = function() {
  424. loadMonitorRecords();
  425. // 关闭dropdown
  426. const dropdown = bootstrap.Dropdown.getInstance(document.getElementById('statusFilter'));
  427. if (dropdown) dropdown.hide();
  428. };
  429. // 重置状态筛选(默认选中除"已失效"外的所有状态)
  430. window.resetStatusFilter = function() {
  431. document.getElementById('status0').checked = true; // 观察中
  432. document.getElementById('status1').checked = true; // 重点关注
  433. document.getElementById('status2').checked = true; // 已触发
  434. document.getElementById('status3').checked = false; // 已失效
  435. loadMonitorRecords();
  436. };
  437. // 查看详情
  438. window.viewDetail = function(id) {
  439. // 实现查看详情逻辑
  440. console.log('查看监控详情:', id);
  441. // 这里可以跳转到详情页面或打开模态框
  442. window.location.href = `/monitor/detail/${id}`;
  443. };
  444. // 编辑监控
  445. window.editMonitor = function(id) {
  446. console.log('编辑监控记录:', id);
  447. // 跳转到编辑页面
  448. window.location.href = `/monitor/edit/${id}`;
  449. };
  450. // 失效监控记录
  451. window.invalidateMonitor = function(id) {
  452. showConfirmDialog('确定要将这条监控记录标记为失效吗?', function() {
  453. fetch(`/monitor/invalidate/${id}`, {
  454. method: 'PUT',
  455. headers: {
  456. 'Content-Type': 'application/json'
  457. }
  458. })
  459. .then(response => response.json())
  460. .then(data => {
  461. if (data.code === 0) {
  462. // 成功时不显示提示,直接刷新数据
  463. loadMonitorRecords();
  464. } else {
  465. // 失败时显示错误对话框
  466. showErrorDialog('标记失效失败:' + data.msg);
  467. }
  468. })
  469. .catch(error => {
  470. console.error('标记失效失败:', error);
  471. showErrorDialog('标记失效失败,请稍后重试');
  472. });
  473. });
  474. };
  475. // 删除监控
  476. window.deleteMonitor = function(id) {
  477. showConfirmDialog('确定要删除这条监控记录吗?此操作不可撤销!', function() {
  478. fetch(`/monitor/delete/${id}`, {
  479. method: 'DELETE'
  480. })
  481. .then(response => response.json())
  482. .then(data => {
  483. if (data.code === 0) {
  484. // 成功时不显示提示,直接刷新数据
  485. loadMonitorRecords();
  486. } else {
  487. // 失败时显示错误对话框
  488. showErrorDialog('删除失败:' + data.msg);
  489. }
  490. })
  491. .catch(error => {
  492. console.error('删除失败:', error);
  493. showErrorDialog('删除失败,请稍后重试');
  494. });
  495. });
  496. };
  497. // 初始化时先加载开仓模式数据,再加载监控记录
  498. loadPositionModes().then(() => {
  499. loadMonitorRecords();
  500. });
  501. });
  502. </script>
  503. {% endblock %}