index.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. function setAlign(sheet, headers) {
  2. const fuc = () => {
  3. headers.forEach(({ hAlign, vAlign }, index) => {
  4. sheetCommonObj.setAreaAlign(sheet.getRange(-1, index, -1, 1), hAlign, vAlign)
  5. });
  6. };
  7. sheetCommonObj.renderSheetFunc(sheet, fuc);
  8. }
  9. function initSheet(dom, setting) {
  10. const workBook = sheetCommonObj.buildSheet(dom, setting);
  11. const sheet = workBook.getSheet(0);
  12. setAlign(sheet, setting.header);
  13. return workBook;
  14. }
  15. function showData(sheet, data, headers, emptyRows) {
  16. const fuc = () => {
  17. sheet.setRowCount(data.length);
  18. data.forEach((item, row) => {
  19. headers.forEach(({ dataCode }, col) => {
  20. sheet.setValue(row, col, item[dataCode] || '');
  21. });
  22. });
  23. if (emptyRows) {
  24. sheet.addRows(data.length, emptyRows);
  25. }
  26. };
  27. sheetCommonObj.renderSheetFunc(sheet, fuc);
  28. }
  29. const TIME_OUT = 10000;
  30. const libID = window.location.search.match(/libID=([^&]+)/)[1];
  31. const UpdateType = {
  32. UPDATE: 'update',
  33. DELETE: 'delete',
  34. CREATE: 'create',
  35. };
  36. const DEBOUNCE_TIME = 200;
  37. const locked = lockUtil.getLocked();
  38. // 地区表
  39. const AREA_BOOK = (() => {
  40. const cache = areaList;
  41. const setting = {
  42. header: [{ headerName: '地区', headerWidth: $('#area-spread').width(), dataCode: 'name', dataType: 'String', hAlign: 'center', vAlign: 'center' }]
  43. };
  44. // 初始化表格
  45. const workBook = initSheet($('#area-spread')[0], setting);
  46. lockUtil.lockSpreads([workBook], locked);
  47. workBook.options.allowExtendPasteRange = false;
  48. workBook.options.allowUserDragDrop = true;
  49. workBook.options.allowUserDragFill = true;
  50. const sheet = workBook.getSheet(0);
  51. // 显示数据
  52. showData(sheet, cache, setting.header);
  53. // 编辑处理
  54. async function handleEdit(changedCells) {
  55. const updateData = [];
  56. changedCells.forEach(({ row, col }) => {
  57. updateData.push({
  58. row,
  59. ID: cache[row].ID,
  60. name: sheet.getValue(row, col)
  61. });
  62. });
  63. try {
  64. await ajaxPost('/priceInfo/editArea', { updateData }, TIME_OUT);
  65. updateData.forEach(({ row, name }) => cache[row].name = name);
  66. } catch (err) {
  67. // 恢复各单元格数据
  68. sheetCommonObj.renderSheetFunc(sheet, () => {
  69. changedCells.forEach(({ row }) => {
  70. sheet.setValue(cache[row].name);
  71. });
  72. });
  73. }
  74. }
  75. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  76. const changedCells = [{ row: info.row, col: info.col }];
  77. handleEdit(changedCells);
  78. });
  79. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  80. handleEdit(info.changedCells);
  81. });
  82. const curArea = { ID: null };
  83. // 焦点变更处理
  84. const debounceSelectionChanged = _.debounce(function (e, info) {
  85. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  86. handleSelectionChanged(row);
  87. }, DEBOUNCE_TIME, { leading: true }); // leading = true : 先触发再延迟
  88. function handleSelectionChanged(row) {
  89. const areaItem = cache[row];
  90. curArea.ID = areaItem && areaItem.ID || null;
  91. CLASS_BOOK.initData(libID, curArea.ID);
  92. }
  93. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, debounceSelectionChanged);
  94. // 新增
  95. async function insert() {
  96. const data = {
  97. compilationID,
  98. ID: uuid.v1(),
  99. name: '',
  100. };
  101. try {
  102. $.bootstrapLoading.start();
  103. await ajaxPost('/priceInfo/insertArea', { insertData: [data] });
  104. // 新增的数据总是添加在最后
  105. sheet.addRows(cache.length, 1);
  106. cache.push(data);
  107. const lastRow = cache.length - 1;
  108. sheet.setSelection(lastRow, 0, 1, 1);
  109. sheet.showRow(lastRow, GC.Spread.Sheets.VerticalPosition.top);
  110. handleSelectionChanged(lastRow);
  111. } catch (err) {
  112. alert(err);
  113. } finally {
  114. $.bootstrapLoading.end();
  115. }
  116. }
  117. // 删除
  118. async function del() {
  119. try {
  120. $.bootstrapLoading.start();
  121. await ajaxPost('/priceInfo/deleteArea', { deleteData: [curArea.ID] });
  122. const index = cache.findIndex(item => item.ID === curArea.ID);
  123. sheet.deleteRows(index, 1);
  124. cache.splice(index, 1);
  125. const row = sheet.getActiveRowIndex();
  126. handleSelectionChanged(row);
  127. } catch (err) {
  128. alert(err);
  129. } finally {
  130. $.bootstrapLoading.end();
  131. }
  132. }
  133. // 右键功能
  134. function buildContextMenu() {
  135. $.contextMenu({
  136. selector: '#area-spread',
  137. build: function ($triggerElement, e) {
  138. // 控制允许右键菜单在哪个位置出现
  139. const offset = $('#area-spread').offset();
  140. const x = e.pageX - offset.left;
  141. const y = e.pageY - offset.top;
  142. const target = sheet.hitTest(x, y);
  143. if (target.hitTestType === 3) { // 在表格内
  144. const sel = sheet.getSelections()[0];
  145. if (sel && sel.rowCount === 1 && typeof target.row !== 'undefined') {
  146. const orgRow = sheet.getActiveRowIndex();
  147. if (orgRow !== target.row) {
  148. sheet.setActiveCell(target.row, target.col);
  149. handleSelectionChanged(target.row);
  150. }
  151. }
  152. return {
  153. items: {
  154. insert: {
  155. name: '新增',
  156. icon: "fa-arrow-left",
  157. disabled: function () {
  158. return locked;
  159. },
  160. callback: function (key, opt) {
  161. insert();
  162. }
  163. },
  164. del: {
  165. name: '删除',
  166. icon: "fa-arrow-left",
  167. disabled: function () {
  168. return locked || !cache[target.row];
  169. },
  170. callback: function (key, opt) {
  171. del();
  172. }
  173. },
  174. }
  175. };
  176. }
  177. else {
  178. return false;
  179. }
  180. }
  181. });
  182. }
  183. buildContextMenu();
  184. return {
  185. handleSelectionChanged,
  186. curArea,
  187. }
  188. })();
  189. // 分类表
  190. const CLASS_BOOK = (() => {
  191. const setting = {
  192. header: [{ headerName: '分类', headerWidth: $('#area-spread').width(), dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' }],
  193. controller: {
  194. cols: [
  195. {
  196. data: {
  197. field: 'name',
  198. vAlign: 1,
  199. hAlign: 0,
  200. font: 'Arial'
  201. },
  202. }
  203. ],
  204. headRows: 1,
  205. headRowHeight: [30],
  206. emptyRows: 0,
  207. treeCol: 0
  208. },
  209. tree: {
  210. id: 'ID',
  211. pid: 'ParentID',
  212. nid: 'NextSiblingID',
  213. rootId: -1
  214. }
  215. };
  216. // 初始化表格
  217. const workBook = initSheet($('#class-spread')[0], setting);
  218. workBook.options.allowExtendPasteRange = false;
  219. workBook.options.allowUserDragDrop = true;
  220. workBook.options.allowUserDragFill = true;
  221. const sheet = workBook.getSheet(0);
  222. let tree;
  223. let controller;
  224. // 初始化数据
  225. async function initData(libID, areaID) {
  226. if (!areaID) {
  227. tree = null;
  228. controller = null;
  229. sheet.setRowCount(0);
  230. PRICE_BOOK.clear();
  231. return;
  232. }
  233. $.bootstrapLoading.start();
  234. try {
  235. const data = await ajaxPost('/priceInfo/getClassData', { libID, areaID }, TIME_OUT);
  236. tree = idTree.createNew(setting.tree);
  237. tree.loadDatas(data);
  238. tree.selected = tree.items.length > 0 ? tree.items[0] : null;
  239. controller = TREE_SHEET_CONTROLLER.createNew(tree, sheet, setting.controller, false);
  240. controller.showTreeData();
  241. handleSelectionChanged(0);
  242. sheet.setSelection(0, 0, 1, 1);
  243. lockUtil.lockSpreads([workBook], locked);
  244. } catch (err) {
  245. tree = null;
  246. controller = null;
  247. sheet.setRowCount(0);
  248. alert(err);
  249. } finally {
  250. $.bootstrapLoading.end();
  251. }
  252. }
  253. // 编辑处理
  254. async function handleEdit(changedCells) {
  255. const updateData = [];
  256. changedCells.forEach(({ row, col }) => {
  257. updateData.push({
  258. row,
  259. type: UpdateType.UPDATE,
  260. filter: { ID: tree.items[row].data.ID },
  261. update: { name: sheet.getValue(row, col) }
  262. });
  263. });
  264. try {
  265. await ajaxPost('/priceInfo/editClassData', { updateData }, TIME_OUT);
  266. updateData.forEach(({ row, update: { name } }) => tree.items[row].data.name = name);
  267. } catch (err) {
  268. // 恢复各单元格数据
  269. sheetCommonObj.renderSheetFunc(sheet, () => {
  270. changedCells.forEach(({ row }) => {
  271. sheet.setValue(tree.items[row].data.name);
  272. });
  273. });
  274. }
  275. }
  276. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  277. const changedCells = [{ row: info.row, col: info.col }];
  278. handleEdit(changedCells);
  279. });
  280. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  281. handleEdit(info.changedCells);
  282. });
  283. // 树操作相关
  284. const $insert = $('#tree-insert');
  285. const $remove = $('#tree-remove');
  286. const $upLevel = $('#tree-up-level');
  287. const $downLevel = $('#tree-down-level');
  288. const $downMove = $('#tree-down-move');
  289. const $upMove = $('#tree-up-move');
  290. // 插入
  291. let canInsert = true;
  292. async function insert() {
  293. try {
  294. if (!canInsert) {
  295. return false;
  296. }
  297. canInsert = false;
  298. $.bootstrapLoading.start();
  299. const updateData = [];
  300. const selected = tree.selected;
  301. const newItem = {
  302. libID,
  303. areaID: AREA_BOOK.curArea.ID,
  304. ID: uuid.v1(),
  305. name: '',
  306. ParentID: '-1',
  307. NextSiblingID: '-1'
  308. };
  309. if (selected) {
  310. newItem.ParentID = selected.data.ParentID;
  311. updateData.push({
  312. type: UpdateType.UPDATE,
  313. filter: { ID: selected.data.ID },
  314. update: { NextSiblingID: newItem.ID }
  315. });
  316. if (selected.nextSibling) {
  317. newItem.NextSiblingID = selected.nextSibling.data.ID;
  318. }
  319. }
  320. updateData.push({
  321. type: UpdateType.CREATE,
  322. document: newItem
  323. });
  324. await ajaxPost('/priceInfo/editClassData', { updateData });
  325. controller.insertByID(newItem.ID);
  326. handleSelectionChanged(sheet.getActiveRowIndex());
  327. } catch (err) {
  328. alert(err);
  329. } finally {
  330. canInsert = true;
  331. $.bootstrapLoading.end();
  332. }
  333. }
  334. $insert.click(_.debounce(insert, DEBOUNCE_TIME, { leading: true }));
  335. // 删除
  336. let canRemove = true;
  337. async function remove() {
  338. try {
  339. if (!canRemove) {
  340. return false;
  341. }
  342. canRemove = false;
  343. $.bootstrapLoading.start();
  344. const updateData = [];
  345. const selected = tree.selected;
  346. const children = selected.getPosterity();
  347. [selected, ...children].forEach(node => updateData.push({
  348. type: UpdateType.DELETE,
  349. filter: { ID: node.data.ID }
  350. }));
  351. if (selected.preSibling) {
  352. updateData.push({
  353. type: UpdateType.UPDATE,
  354. filter: { ID: selected.preSibling.data.ID },
  355. update: { NextSiblingID: selected.data.NextSiblingID }
  356. });
  357. }
  358. await ajaxPost('/priceInfo/editClassData', { updateData });
  359. controller.delete();
  360. handleSelectionChanged(sheet.getActiveRowIndex());
  361. } catch (err) {
  362. alert(err);
  363. } finally {
  364. canRemove = true;
  365. $.bootstrapLoading.end();
  366. }
  367. }
  368. $remove.click(_.debounce(remove, DEBOUNCE_TIME, { leading: true }));
  369. // 升级
  370. let canUpLevel = true;
  371. async function upLevel() {
  372. try {
  373. if (!canUpLevel) {
  374. return false;
  375. }
  376. canUpLevel = false;
  377. $.bootstrapLoading.start();
  378. const updateData = [];
  379. const selected = tree.selected;
  380. if (selected.preSibling) {
  381. updateData.push({
  382. type: UpdateType.UPDATE,
  383. filter: { ID: selected.preSibling.data.ID },
  384. update: { NextSiblingID: -1 }
  385. });
  386. }
  387. if (selected.parent) {
  388. updateData.push({
  389. type: UpdateType.UPDATE,
  390. filter: { ID: selected.parent.data.ID },
  391. update: { NextSiblingID: selected.data.ID }
  392. });
  393. }
  394. updateData.push({
  395. type: UpdateType.UPDATE,
  396. filter: { ID: selected.data.ID },
  397. update: { ParentID: selected.parent.data.ParentID, NextSiblingID: selected.parent.data.NextSiblingID }
  398. });
  399. let curNode = selected.nextSibling;
  400. while (curNode) {
  401. updateData.push({
  402. type: UpdateType.UPDATE,
  403. filter: { ID: curNode.data.ID },
  404. update: { ParentID: selected.data.ID }
  405. });
  406. curNode = curNode.nextSibling;
  407. }
  408. await ajaxPost('/priceInfo/editClassData', { updateData });
  409. controller.upLevel();
  410. refreshTreeButton(tree.selected);
  411. } catch (err) {
  412. alert(err);
  413. } finally {
  414. canUpLevel = true;
  415. $.bootstrapLoading.end();
  416. }
  417. }
  418. $upLevel.click(_.debounce(upLevel, DEBOUNCE_TIME, { leading: true }));
  419. // 降级
  420. let canDownLevel = true;
  421. async function downLevel() {
  422. try {
  423. if (!canDownLevel) {
  424. return false;
  425. }
  426. canDownLevel = false;
  427. $.bootstrapLoading.start();
  428. const updateData = [];
  429. const selected = tree.selected;
  430. if (selected.preSibling) {
  431. updateData.push({
  432. type: UpdateType.UPDATE,
  433. filter: { ID: selected.preSibling.data.ID },
  434. update: { NextSiblingID: selected.data.NextSiblingID }
  435. });
  436. const preSiblingLastChild = selected.preSibling.children[selected.preSibling.children.length - 1];
  437. if (preSiblingLastChild) {
  438. updateData.push({
  439. type: UpdateType.UPDATE,
  440. filter: { ID: preSiblingLastChild.data.ID },
  441. update: { NextSiblingID: selected.data.ID }
  442. });
  443. }
  444. updateData.push({
  445. type: UpdateType.UPDATE,
  446. filter: { ID: selected.data.ID },
  447. update: { ParentID: selected.preSibling.data.ID, NextSiblingID: -1 }
  448. });
  449. }
  450. await ajaxPost('/priceInfo/editClassData', { updateData });
  451. controller.downLevel();
  452. refreshTreeButton(tree.selected);
  453. } catch (err) {
  454. alert(err);
  455. } finally {
  456. canDownLevel = true;
  457. $.bootstrapLoading.end();
  458. }
  459. }
  460. $downLevel.click(_.debounce(downLevel, DEBOUNCE_TIME, { leading: true }));
  461. // 下移
  462. let canDownMove = true;
  463. async function downMove() {
  464. try {
  465. if (!canDownMove) {
  466. return false;
  467. }
  468. canDownMove = false;
  469. $.bootstrapLoading.start();
  470. const updateData = [];
  471. const selected = tree.selected;
  472. if (selected.preSibling) {
  473. updateData.push({
  474. type: UpdateType.UPDATE,
  475. filter: { ID: selected.preSibling.data.ID },
  476. update: { NextSiblingID: selected.data.NextSiblingID }
  477. });
  478. }
  479. updateData.push({
  480. type: UpdateType.UPDATE,
  481. filter: { ID: selected.data.ID },
  482. update: { NextSiblingID: selected.nextSibling.data.NextSiblingID }
  483. });
  484. updateData.push({
  485. type: UpdateType.UPDATE,
  486. filter: { ID: selected.nextSibling.data.ID },
  487. update: { NextSiblingID: selected.data.ID }
  488. });
  489. await ajaxPost('/priceInfo/editClassData', { updateData });
  490. controller.downMove();
  491. refreshTreeButton(tree.selected);
  492. } catch (err) {
  493. alert(err);
  494. } finally {
  495. canDownMove = true;
  496. $.bootstrapLoading.end();
  497. }
  498. }
  499. $downMove.click(_.debounce(downMove, DEBOUNCE_TIME, { leading: true }));
  500. // 上移
  501. let canUpMove = true;
  502. async function upMove() {
  503. try {
  504. if (!canUpMove) {
  505. return false;
  506. }
  507. canUpMove = false;
  508. $.bootstrapLoading.start();
  509. const updateData = [];
  510. const selected = tree.selected;
  511. if (selected.preSibling) {
  512. updateData.push({
  513. type: UpdateType.UPDATE,
  514. filter: { ID: selected.preSibling.data.ID },
  515. update: { NextSiblingID: selected.data.NextSiblingID }
  516. });
  517. }
  518. const prePreSibling = selected.preSibling.preSibling;
  519. if (prePreSibling) {
  520. updateData.push({
  521. type: UpdateType.UPDATE,
  522. filter: { ID: prePreSibling.data.ID },
  523. update: { NextSiblingID: selected.data.ID }
  524. });
  525. }
  526. updateData.push({
  527. type: UpdateType.UPDATE,
  528. filter: { ID: selected.data.ID },
  529. update: { NextSiblingID: selected.preSibling.data.ID }
  530. });
  531. await ajaxPost('/priceInfo/editClassData', { updateData });
  532. controller.upMove();
  533. refreshTreeButton(tree.selected);
  534. } catch (err) {
  535. alert(err);
  536. } finally {
  537. canUpMove = true;
  538. $.bootstrapLoading.end();
  539. }
  540. }
  541. $upMove.click(_.debounce(upMove, DEBOUNCE_TIME, { leading: true }));
  542. // 刷新树操作按钮有效性
  543. function refreshTreeButton(selected) {
  544. if (locked) {
  545. return;
  546. }
  547. $insert.removeClass('disabled');
  548. $remove.removeClass('disabled');
  549. $upLevel.removeClass('disabled');
  550. $downLevel.removeClass('disabled');
  551. $downMove.removeClass('disabled');
  552. $upMove.removeClass('disabled');
  553. if (!selected) {
  554. $remove.addClass('disabled');
  555. $upLevel.addClass('disabled');
  556. $downLevel.addClass('disabled');
  557. $downMove.addClass('disabled');
  558. $upMove.addClass('disabled');
  559. } else {
  560. if (!selected.preSibling) {
  561. $downLevel.addClass('disabled');
  562. $upMove.addClass('disabled');
  563. }
  564. if (!selected.nextSibling) {
  565. $downMove.addClass('disabled');
  566. }
  567. if (!selected.parent) {
  568. $upLevel.addClass('disabled');
  569. }
  570. }
  571. }
  572. // 焦点变更处理
  573. const curClass = { ID: null };
  574. function handleSelectionChanged(row) {
  575. const classNode = tree.items[row] || null;
  576. tree.selected = classNode;
  577. refreshTreeButton(classNode);
  578. curClass.ID = classNode && classNode.data && classNode.data.ID || null;
  579. const classIDList = []
  580. if (classNode) {
  581. classIDList.push(classNode.data.ID);
  582. const children = classNode.getPosterity();
  583. children.forEach(child => classIDList.push(child.data.ID));
  584. }
  585. PRICE_BOOK.initData(classIDList);
  586. }
  587. const debounceSelectionChanged = _.debounce(function (e, info) {
  588. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  589. handleSelectionChanged(row);
  590. }, DEBOUNCE_TIME, { leading: true });
  591. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, debounceSelectionChanged);
  592. return {
  593. initData,
  594. handleSelectionChanged,
  595. curClass,
  596. }
  597. })();
  598. // 价格信息表
  599. const PRICE_BOOK = (() => {
  600. const setting = {
  601. header: [
  602. { headerName: '编码', headerWidth: 100, dataCode: 'code', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  603. { headerName: '名称', headerWidth: 200, dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  604. { headerName: '规格型号', headerWidth: 120, dataCode: 'specs', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  605. { headerName: '单位', headerWidth: 80, dataCode: 'unit', dataType: 'String', hAlign: 'center', vAlign: 'center' },
  606. { headerName: '不含税价', headerWidth: 80, dataCode: 'noTaxPrice', dataType: 'String', hAlign: 'right', vAlign: 'center' },
  607. { headerName: '含税价', headerWidth: 80, dataCode: 'taxPrice', dataType: 'String', hAlign: 'right', vAlign: 'center' },
  608. ],
  609. };
  610. // 初始化表格
  611. const workBook = initSheet($('#price-spread')[0], setting);
  612. workBook.options.allowUserDragDrop = true;
  613. workBook.options.allowUserDragFill = true;
  614. lockUtil.lockSpreads([workBook], locked);
  615. const sheet = workBook.getSheet(0);
  616. let cache = [];
  617. // 清空
  618. function clear() {
  619. cache = [];
  620. sheet.setRowCount(0);
  621. }
  622. // 初始化数据
  623. async function initData(classIDList) {
  624. if (!classIDList || !classIDList.length) {
  625. return clear();
  626. }
  627. $.bootstrapLoading.start();
  628. try {
  629. cache = await ajaxPost('/priceInfo/getPriceData', { classIDList }, TIME_OUT);
  630. showData(sheet, cache, setting.header, 5);
  631. } catch (err) {
  632. cache = [];
  633. sheet.setRowCount(0);
  634. alert(err);
  635. } finally {
  636. $.bootstrapLoading.end();
  637. }
  638. }
  639. // 获取当前表中行数据
  640. function getRowData(sheet, row, headers) {
  641. const item = {};
  642. headers.forEach(({ dataCode }, index) => {
  643. const value = sheet.getValue(row, index) || '';
  644. if (value) {
  645. item[dataCode] = value;
  646. }
  647. });
  648. return item;
  649. }
  650. // 获取表数据和缓存数据的不同数据
  651. function getRowDiffData(curRowData, cacheRowData, headers) {
  652. let item = null;
  653. headers.forEach(({ dataCode }) => {
  654. const curValue = curRowData[dataCode];
  655. const cacheValue = cacheRowData[dataCode];
  656. if (!cacheValue && !curValue) {
  657. return;
  658. }
  659. if (cacheValue !== curValue) {
  660. if (!item) {
  661. item = {};
  662. }
  663. item[dataCode] = curValue || '';
  664. }
  665. });
  666. return item;
  667. }
  668. // 编辑处理
  669. async function handleEdit(changedCells) {
  670. const postData = []; // 请求用
  671. // 更新缓存用
  672. const updateData = [];
  673. const deleteData = [];
  674. const insertData = [];
  675. try {
  676. changedCells.forEach(({ row }) => {
  677. if (cache[row]) {
  678. const rowData = getRowData(sheet, row, setting.header);
  679. if (Object.keys(rowData).length) { // 还有数据,更新
  680. const diffData = getRowDiffData(rowData, cache[row], setting.header);
  681. if (diffData) {
  682. postData.push({ type: UpdateType.UPDATE, ID: cache[row].ID, data: diffData });
  683. updateData.push({ row, data: diffData });
  684. }
  685. } else { // 该行无数据了,删除
  686. postData.push({ type: UpdateType.DELETE, ID: cache[row].ID });
  687. deleteData.push(cache[row]);
  688. }
  689. } else { // 新增
  690. const rowData = getRowData(sheet, row, setting.header);
  691. if (Object.keys(rowData).length) {
  692. rowData.ID = uuid.v1();
  693. rowData.libID = libID;
  694. rowData.compilationID = compilationID;
  695. rowData.areaID = AREA_BOOK.curArea.ID;
  696. rowData.classID = CLASS_BOOK.curClass.ID;
  697. rowData.period = curLibPeriod;
  698. postData.push({ type: UpdateType.CREATE, data: rowData });
  699. insertData.push(rowData);
  700. }
  701. }
  702. });
  703. if (postData.length) {
  704. await ajaxPost('/priceInfo/editPriceData', { postData }, TIME_OUT);
  705. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  706. updateData.forEach(item => {
  707. Object.assign(cache[item.row], item.data);
  708. });
  709. deleteData.forEach(item => {
  710. const index = cache.indexOf(item);
  711. if (index >= 0) {
  712. cache.splice(index, 1);
  713. }
  714. });
  715. insertData.forEach(item => cache.push(item));
  716. if (deleteData.length || insertData.length) {
  717. showData(sheet, cache, setting.header, 5);
  718. }
  719. }
  720. } catch (err) {
  721. // 恢复各单元格数据
  722. showData(sheet, cache, setting.header, 5);
  723. }
  724. }
  725. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  726. const changedCells = [{ row: info.row }];
  727. handleEdit(changedCells);
  728. });
  729. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  730. const changedRows = [];
  731. let preRow;
  732. info.changedCells.forEach(({ row }) => {
  733. if (row !== preRow) {
  734. changedRows.push({ row });
  735. }
  736. preRow = row;
  737. });
  738. handleEdit(changedRows);
  739. });
  740. return {
  741. clear,
  742. initData,
  743. }
  744. })();
  745. $(document).ready(() => {
  746. $('[data-toggle="tooltip"]').tooltip();
  747. AREA_BOOK.handleSelectionChanged(0);
  748. const $range = $(document.body);
  749. lockUtil.lockTools($range, locked);
  750. });