edit.html 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. {% extends 'base.html' %}
  2. {% block title %}编辑期货信息 - 期货数据管理系统{% endblock %}
  3. {% block content %}
  4. <div class="container-fluid">
  5. <div class="row mb-3">
  6. <div class="col">
  7. <h2>编辑期货信息</h2>
  8. </div>
  9. <div class="col-auto">
  10. <a href="{{ url_for('future_info.index') }}" class="btn btn-secondary">
  11. <i class="fas fa-arrow-left"></i> 返回列表
  12. </a>
  13. </div>
  14. </div>
  15. <div class="card">
  16. <div class="card-body">
  17. <form id="editForm" class="needs-validation" novalidate>
  18. <div class="row">
  19. <!-- 基本信息 -->
  20. <div class="col-md-6">
  21. <h5 class="mb-3">基本信息</h5>
  22. <div class="mb-3">
  23. <label for="contract_letter" class="form-label">合约字母 <span class="text-danger">*</span></label>
  24. <input type="text" class="form-control" id="contract_letter" name="contract_letter" required>
  25. <div class="invalid-feedback">请输入合约字母</div>
  26. </div>
  27. <div class="mb-3">
  28. <label for="name" class="form-label">期货名称 <span class="text-danger">*</span></label>
  29. <input type="text" class="form-control" id="name" name="name" required>
  30. <div class="invalid-feedback">请输入期货名称</div>
  31. </div>
  32. <div class="mb-3">
  33. <label for="market" class="form-label">市场 <span class="text-danger">*</span></label>
  34. <select class="form-select" id="market" name="market" required>
  35. <option value="0">国内</option>
  36. <option value="1">国外</option>
  37. </select>
  38. <div class="invalid-feedback">请选择市场</div>
  39. </div>
  40. <div class="mb-3">
  41. <label for="exchange" class="form-label">交易所</label>
  42. <input type="text" class="form-control" id="exchange" name="exchange">
  43. </div>
  44. <div class="mb-3">
  45. <label for="contract_multiplier" class="form-label">合约乘数</label>
  46. <input type="number" class="form-control" id="contract_multiplier" name="contract_multiplier" step="1">
  47. </div>
  48. <div class="mb-3">
  49. <label for="th_main_contract" class="form-label">同花主力合约</label>
  50. <input type="text" class="form-control" id="th_main_contract" name="th_main_contract">
  51. </div>
  52. <div class="mb-3">
  53. <label for="current_main_contract" class="form-label">当前主力合约</label>
  54. <input type="text" class="form-control" id="current_main_contract" name="current_main_contract">
  55. </div>
  56. <div class="mb-3">
  57. <label for="th_order" class="form-label">同花顺顺序</label>
  58. <input type="number" class="form-control" id="th_order" name="th_order" step="1">
  59. </div>
  60. </div>
  61. <!-- 交易参数和趋势信息 -->
  62. <div class="col-md-6">
  63. <h5 class="mb-3">交易参数</h5>
  64. <div class="mb-3">
  65. <label for="long_margin_rate" class="form-label">做多保证金率</label>
  66. <div class="input-group">
  67. <input type="number" class="form-control" id="long_margin_rate" name="long_margin_rate" step="0.01" min="0" max="1">
  68. <span class="input-group-text">%</span>
  69. </div>
  70. </div>
  71. <div class="mb-3">
  72. <label for="short_margin_rate" class="form-label">做空保证金率</label>
  73. <div class="input-group">
  74. <input type="number" class="form-control" id="short_margin_rate" name="short_margin_rate" step="0.01" min="0" max="1">
  75. <span class="input-group-text">%</span>
  76. </div>
  77. </div>
  78. <div class="mb-3">
  79. <label for="open_fee" class="form-label">开仓费用(按手)</label>
  80. <input type="number" class="form-control" id="open_fee" name="open_fee" step="0.01">
  81. </div>
  82. <div class="mb-3">
  83. <label for="close_fee" class="form-label">平仓费用(按手)</label>
  84. <input type="number" class="form-control" id="close_fee" name="close_fee" step="0.01">
  85. </div>
  86. <div class="mb-3">
  87. <label for="close_today_rate" class="form-label">平今费率(按金额)</label>
  88. <div class="input-group">
  89. <input type="number" class="form-control" id="close_today_rate" name="close_today_rate" step="0.000001" min="0" max="1">
  90. <span class="input-group-text">%</span>
  91. </div>
  92. </div>
  93. <div class="mb-3">
  94. <label for="close_today_fee" class="form-label">平今费用(按手)</label>
  95. <input type="number" class="form-control" id="close_today_fee" name="close_today_fee" step="0.01">
  96. </div>
  97. <div class="mb-3">
  98. <label for="long_margin_amount" class="form-label">做多1手保证金</label>
  99. <input type="number" class="form-control" id="long_margin_amount" name="long_margin_amount" step="0.01">
  100. </div>
  101. <div class="mb-3">
  102. <label for="short_margin_amount" class="form-label">做空1手保证金</label>
  103. <input type="number" class="form-control" id="short_margin_amount" name="short_margin_amount" step="0.01">
  104. </div>
  105. <div class="mb-3">
  106. <label for="core_ratio" class="form-label">核心比率</label>
  107. <input type="number" class="form-control" id="core_ratio" name="core_ratio" step="0.01" min="0" max="1">
  108. </div>
  109. <h5 class="mb-3 mt-4">趋势信息</h5>
  110. <div class="mb-3">
  111. <label for="long_term_trend" class="form-label">长期趋势特征</label>
  112. <!-- 隐藏的输入框,用于存储最终的数据 -->
  113. <input type="hidden" id="long_term_trend" name="long_term_trend">
  114. <!-- 趋势特征搜索和选择区域 -->
  115. <div class="trend-selection-area">
  116. <!-- 已选择的趋势特征标签 -->
  117. <div id="selected_trends" class="selected-trends mb-2">
  118. <!-- 已选择的趋势特征标签将在这里显示 -->
  119. </div>
  120. <!-- 趋势特征搜索输入框 -->
  121. <div class="position-relative">
  122. <input type="text" class="form-control" id="trend_search_input"
  123. placeholder="搜索并添加趋势特征..." autocomplete="off">
  124. <div id="trend_search_results" class="dropdown-menu w-100" style="display: none; max-height: 200px; overflow-y: auto;">
  125. <!-- 搜索结果将在这里显示 -->
  126. </div>
  127. </div>
  128. </div>
  129. <div class="form-text mt-2">搜索并选择趋势特征,支持添加多个</div>
  130. <div id="trend_validation_feedback" class="invalid-feedback"></div>
  131. </div>
  132. </div>
  133. </div>
  134. <div class="row mt-4">
  135. <div class="col">
  136. <button type="submit" class="btn btn-primary">保存更改</button>
  137. <button type="button" class="btn btn-secondary" onclick="window.history.back()">取消</button>
  138. </div>
  139. </div>
  140. </form>
  141. </div>
  142. </div>
  143. </div>
  144. {% endblock %}
  145. {% block scripts %}
  146. <script>
  147. document.addEventListener('DOMContentLoaded', function() {
  148. // 从Flask传递的ID,转换为数字
  149. const futureId = parseInt("{{ future_id }}");
  150. const form = document.getElementById('editForm');
  151. const longTermTrendInput = document.getElementById('long_term_trend');
  152. const trendSearchInput = document.getElementById('trend_search_input');
  153. const trendSearchResults = document.getElementById('trend_search_results');
  154. const selectedTrendsContainer = document.getElementById('selected_trends');
  155. let allTrends = []; // 存储所有趋势特征数据
  156. let selectedTrends = []; // 存储已选择的趋势特征
  157. let searchTimeout;
  158. // 加载趋势信息数据
  159. fetch('/api/future_info/trends')
  160. .then(response => response.json())
  161. .then(data => {
  162. if (data.code === 0) {
  163. allTrends = data.data;
  164. console.log('加载趋势特征列表:', allTrends.length, '个');
  165. // 加载期货信息
  166. loadFutureInfo();
  167. }
  168. })
  169. .catch(error => {
  170. console.error('加载趋势信息失败:', error);
  171. // 即使趋势信息加载失败,也加载期货信息
  172. loadFutureInfo();
  173. });
  174. // 加载期货信息
  175. function loadFutureInfo() {
  176. fetch(`/api/future_info/get/${futureId}`)
  177. .then(response => response.json())
  178. .then(data => {
  179. if (data.code === 0 && data.data) {
  180. const future = data.data;
  181. console.log('从服务器获取的数据:', future);
  182. // 填充表单数据
  183. document.getElementById('contract_letter').value = future.contract_letter || '';
  184. document.getElementById('name').value = future.name || '';
  185. document.getElementById('market').value = future.market;
  186. document.getElementById('exchange').value = future.exchange || '';
  187. document.getElementById('contract_multiplier').value = future.contract_multiplier || '';
  188. document.getElementById('long_margin_rate').value = future.long_margin_rate || '';
  189. document.getElementById('short_margin_rate').value = future.short_margin_rate || '';
  190. document.getElementById('open_fee').value = future.open_fee || '';
  191. document.getElementById('close_fee').value = future.close_fee || '';
  192. // 特别处理平今费率
  193. const closeTodayRateInput = document.getElementById('close_today_rate');
  194. closeTodayRateInput.value = future.close_today_rate || '';
  195. console.log('平今费率值:', {
  196. original: future.close_today_rate,
  197. formatted: closeTodayRateInput.value
  198. });
  199. document.getElementById('close_today_fee').value = future.close_today_fee || '';
  200. document.getElementById('long_margin_amount').value = future.long_margin_amount || '';
  201. document.getElementById('short_margin_amount').value = future.short_margin_amount || '';
  202. document.getElementById('th_main_contract').value = future.th_main_contract || '';
  203. document.getElementById('current_main_contract').value = future.current_main_contract || '';
  204. document.getElementById('th_order').value = future.th_order || '';
  205. document.getElementById('core_ratio').value = future.core_ratio || '';
  206. // 设置长期趋势特征
  207. const trendValue = future.long_term_trend || '';
  208. longTermTrendInput.value = trendValue;
  209. // 解析已有的趋势特征并设置到选择组件中
  210. if (trendValue.trim()) {
  211. const trendNames = trendValue.split('+').map(name => name.trim()).filter(name => name);
  212. selectedTrends = [];
  213. trendNames.forEach(trendName => {
  214. const trend = allTrends.find(t => t.name === trendName);
  215. if (trend) {
  216. selectedTrends.push(trend);
  217. }
  218. });
  219. updateSelectedTrendsDisplay();
  220. }
  221. } else {
  222. alert('加载期货信息失败:未找到对应的期货信息');
  223. }
  224. })
  225. .catch(error => {
  226. console.error('Error:', error);
  227. alert('加载期货信息失败');
  228. });
  229. }
  230. // 监听平今费率输入变化
  231. document.getElementById('close_today_rate').addEventListener('input', function(e) {
  232. console.log('平今费率输入值变化:', {
  233. value: e.target.value,
  234. valueAsNumber: e.target.valueAsNumber
  235. });
  236. });
  237. // 趋势特征搜索和选择功能
  238. // 搜索趋势特征
  239. function searchTrends(query) {
  240. if (!query.trim()) {
  241. hideTrendSearchResults();
  242. return;
  243. }
  244. // 过滤已选择的趋势特征
  245. const selectedTrendNames = selectedTrends.map(trend => trend.name);
  246. const filteredTrends = allTrends.filter(trend =>
  247. trend.name.toLowerCase().includes(query.toLowerCase()) &&
  248. !selectedTrendNames.includes(trend.name)
  249. );
  250. showTrendSearchResults(filteredTrends);
  251. }
  252. // 显示搜索结果
  253. function showTrendSearchResults(results) {
  254. trendSearchResults.innerHTML = '';
  255. if (results.length === 0) {
  256. trendSearchResults.innerHTML = '<div class="dropdown-item-text text-muted">未找到匹配的趋势特征</div>';
  257. } else {
  258. results.slice(0, 10).forEach(trend => { // 限制显示前10个结果
  259. const item = document.createElement('div');
  260. item.className = 'dropdown-item cursor-pointer';
  261. item.style.cursor = 'pointer';
  262. item.textContent = trend.name;
  263. item.addEventListener('click', () => selectTrend(trend));
  264. trendSearchResults.appendChild(item);
  265. });
  266. }
  267. trendSearchResults.style.display = 'block';
  268. }
  269. // 隐藏搜索结果
  270. function hideTrendSearchResults() {
  271. trendSearchResults.style.display = 'none';
  272. }
  273. // 选择趋势特征
  274. function selectTrend(trend) {
  275. // 检查是否已经选择
  276. if (selectedTrends.find(t => t.name === trend.name)) {
  277. return;
  278. }
  279. selectedTrends.push(trend);
  280. updateSelectedTrendsDisplay();
  281. updateHiddenInput();
  282. // 清空搜索框
  283. trendSearchInput.value = '';
  284. hideTrendSearchResults();
  285. console.log('选择了趋势特征:', trend.name);
  286. }
  287. // 删除趋势特征
  288. function removeTrend(index) {
  289. selectedTrends.splice(index, 1);
  290. updateSelectedTrendsDisplay();
  291. updateHiddenInput();
  292. console.log('删除趋势特征,剩余:', selectedTrends.length, '个');
  293. }
  294. // 更新已选择趋势特征的显示
  295. function updateSelectedTrendsDisplay() {
  296. selectedTrendsContainer.innerHTML = '';
  297. selectedTrends.forEach((trend, index) => {
  298. const badge = document.createElement('span');
  299. badge.className = 'badge bg-primary me-2 mb-2 d-inline-flex align-items-center';
  300. badge.innerHTML = `
  301. ${trend.name}
  302. <button type="button" class="btn-close btn-close-white ms-2" style="font-size: 0.7em;" aria-label="Remove"></button>
  303. `;
  304. // 添加删除事件
  305. const closeBtn = badge.querySelector('.btn-close');
  306. closeBtn.addEventListener('click', () => removeTrend(index));
  307. selectedTrendsContainer.appendChild(badge);
  308. });
  309. }
  310. // 更新隐藏输入框的值
  311. function updateHiddenInput() {
  312. const trendNames = selectedTrends.map(trend => trend.name);
  313. longTermTrendInput.value = trendNames.join('+');
  314. console.log('更新隐藏输入框值:', longTermTrendInput.value);
  315. }
  316. // 监听趋势特征搜索输入
  317. trendSearchInput.addEventListener('input', function() {
  318. const query = this.value.trim();
  319. // 使用防抖进行搜索
  320. clearTimeout(searchTimeout);
  321. searchTimeout = setTimeout(() => {
  322. searchTrends(query);
  323. }, 300);
  324. });
  325. // 监听焦点离开事件,隐藏搜索结果
  326. trendSearchInput.addEventListener('blur', function() {
  327. // 延迟隐藏,允许用户点击搜索结果
  328. setTimeout(() => {
  329. hideTrendSearchResults();
  330. }, 200);
  331. });
  332. // 监听点击外部区域隐藏搜索结果
  333. document.addEventListener('click', function(event) {
  334. if (!event.target.closest('.trend-selection-area')) {
  335. hideTrendSearchResults();
  336. }
  337. });
  338. // 表单提交处理
  339. form.addEventListener('submit', function(event) {
  340. event.preventDefault();
  341. if (!form.checkValidity()) {
  342. event.stopPropagation();
  343. form.classList.add('was-validated');
  344. return;
  345. }
  346. // 直接提交,因为趋势特征已通过多选组件验证
  347. // 获取平今费率的值
  348. const closeTodayRateInput = document.getElementById('close_today_rate');
  349. const closeTodayRateValue = closeTodayRateInput.value;
  350. console.log('提交前平今费率值:', {
  351. inputValue: closeTodayRateValue,
  352. inputValueAsNumber: closeTodayRateInput.valueAsNumber,
  353. inputStep: closeTodayRateInput.step
  354. });
  355. // 收集表单数据
  356. const formData = {
  357. contract_letter: document.getElementById('contract_letter').value,
  358. name: document.getElementById('name').value,
  359. market: parseInt(document.getElementById('market').value),
  360. exchange: document.getElementById('exchange').value,
  361. contract_multiplier: document.getElementById('contract_multiplier').value ? Number(document.getElementById('contract_multiplier').value) : null,
  362. long_margin_rate: document.getElementById('long_margin_rate').value !== '' ? Number(document.getElementById('long_margin_rate').value) : null,
  363. short_margin_rate: document.getElementById('short_margin_rate').value !== '' ? Number(document.getElementById('short_margin_rate').value) : null,
  364. open_fee: document.getElementById('open_fee').value !== '' ? Number(document.getElementById('open_fee').value) : null,
  365. close_fee: document.getElementById('close_fee').value !== '' ? Number(document.getElementById('close_fee').value) : null,
  366. close_today_rate: closeTodayRateValue !== '' ? Number(closeTodayRateValue) : null,
  367. close_today_fee: document.getElementById('close_today_fee').value !== '' ? Number(document.getElementById('close_today_fee').value) : null,
  368. long_margin_amount: document.getElementById('long_margin_amount').value !== '' ? Number(document.getElementById('long_margin_amount').value) : null,
  369. short_margin_amount: document.getElementById('short_margin_amount').value !== '' ? Number(document.getElementById('short_margin_amount').value) : null,
  370. th_main_contract: document.getElementById('th_main_contract').value,
  371. current_main_contract: document.getElementById('current_main_contract').value,
  372. th_order: document.getElementById('th_order').value ? parseInt(document.getElementById('th_order').value) : null,
  373. long_term_trend: document.getElementById('long_term_trend').value,
  374. core_ratio: document.getElementById('core_ratio').value ? Number(document.getElementById('core_ratio').value) : null
  375. };
  376. console.log('准备提交的数据:', formData);
  377. // 发送更新请求
  378. fetch(`/api/future_info/update/${futureId}`, {
  379. method: 'PUT',
  380. headers: {
  381. 'Content-Type': 'application/json'
  382. },
  383. body: JSON.stringify(formData)
  384. })
  385. .then(response => response.json())
  386. .then(data => {
  387. console.log('服务器响应:', data);
  388. if (data.code === 0) {
  389. alert('更新成功');
  390. window.location.href = "{{ url_for('future_info.index') }}";
  391. } else {
  392. alert('更新失败:' + data.msg);
  393. }
  394. })
  395. .catch(error => {
  396. console.error('Error:', error);
  397. alert('更新失败');
  398. });
  399. });
  400. });
  401. </script>
  402. {% endblock %}