priceItem.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // 价格信息表
  2. const PRICE_BOOK = (() => {
  3. const setting = {
  4. header: [
  5. { headerName: '编码', headerWidth: 100, dataCode: 'code', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
  6. { headerName: '别名编码', headerWidth: 70, dataCode: 'classCode', dataType: 'String', hAlign: 'left', vAlign: 'center', formatter: "@" },
  7. { headerName: '名称', headerWidth: 200, dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  8. { headerName: '规格型号', headerWidth: 120, dataCode: 'specs', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  9. { headerName: '单位', headerWidth: 80, dataCode: 'unit', dataType: 'String', hAlign: 'center', vAlign: 'center' },
  10. { headerName: '不含税价', headerWidth: 80, dataCode: 'noTaxPrice', dataType: 'String', hAlign: 'right', vAlign: 'center' },
  11. { headerName: '含税价', headerWidth: 80, dataCode: 'taxPrice', dataType: 'String', hAlign: 'right', vAlign: 'center' },
  12. { headerName: '月份备注', headerWidth: 140, dataCode: 'dateRemark', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  13. { headerName: '计算式', headerWidth: 100, dataCode: 'expString', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  14. ],
  15. };
  16. // 初始化表格
  17. const workBook = initSheet($('#price-spread')[0], setting);
  18. workBook.options.allowUserDragDrop = true;
  19. workBook.options.allowUserDragFill = true;
  20. lockUtil.lockSpreads([workBook], locked);
  21. const sheet = workBook.getSheet(0);
  22. let cache = [];
  23. // 清空
  24. function clear() {
  25. cache = [];
  26. sheet.setRowCount(0);
  27. }
  28. // 是否正在检测重复数据
  29. let isCheckingRepeat = false;
  30. // 初始化数据
  31. async function initData(classIDList) {
  32. isCheckingRepeat = false;
  33. $('#check-repeat').text('检测重复别名编码');
  34. if (!classIDList || !classIDList.length) {
  35. return clear();
  36. }
  37. $.bootstrapLoading.start();
  38. try {
  39. cache = await ajaxPost('/priceInfo/getPriceData', { classIDList }, 1000 * 60 * 10);
  40. // cache = _.sortBy(cache, 'classCode');
  41. // cache = _.sortBy(cache, 'code');
  42. showData(sheet, cache, setting.header, 5);
  43. const row = sheet.getActiveRowIndex();
  44. const keywordList = cache[row] && cache[row].keywordList || [];
  45. KEYWORD_BOOK.showKeywordData(keywordList);
  46. } catch (err) {
  47. cache = [];
  48. sheet.setRowCount(0);
  49. alert(err);
  50. } finally {
  51. $.bootstrapLoading.end();
  52. }
  53. }
  54. // 编辑处理
  55. async function handleEdit(changedCells, needLoading = true) {
  56. $.bootstrapLoading.start();
  57. const areaID = AREA_BOOK.curArea.ID;
  58. const postData = []; // 请求用
  59. // 更新缓存用
  60. const updateData = [];
  61. const deleteData = [];
  62. const insertData = [];
  63. try {
  64. changedCells.forEach(({ row }) => {
  65. if (cache[row]) {
  66. const rowData = getRowData(sheet, row, setting.header);
  67. if (Object.keys(rowData).length) { // 还有数据,更新
  68. const diffData = getRowDiffData(rowData, cache[row], setting.header);
  69. if (diffData) {
  70. postData.push({ type: UpdateType.UPDATE, ID: cache[row].ID, areaID, compilationID, period: curLibPeriod, data: diffData });
  71. updateData.push({ row, data: diffData });
  72. }
  73. } else { // 该行无数据了,删除
  74. postData.push({ type: UpdateType.DELETE, ID: cache[row].ID, areaID, compilationID, period: curLibPeriod });
  75. deleteData.push(cache[row]);
  76. }
  77. } else { // 新增
  78. const rowData = getRowData(sheet, row, setting.header);
  79. if (Object.keys(rowData).length) {
  80. rowData.ID = uuid.v1();
  81. rowData.libID = libID;
  82. rowData.compilationID = compilationID;
  83. rowData.areaID = AREA_BOOK.curArea.ID;
  84. rowData.classID = CLASS_BOOK.curClass.ID;
  85. rowData.period = curLibPeriod;
  86. postData.push({ type: UpdateType.CREATE, data: rowData });
  87. insertData.push(rowData);
  88. }
  89. }
  90. });
  91. if (postData.length) {
  92. await ajaxPost('/priceInfo/editPriceData', { postData }, TIME_OUT);
  93. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  94. updateData.forEach(item => {
  95. Object.assign(cache[item.row], item.data);
  96. });
  97. deleteData.forEach(item => {
  98. const index = cache.indexOf(item);
  99. if (index >= 0) {
  100. cache.splice(index, 1);
  101. }
  102. });
  103. insertData.forEach(item => cache.push(item));
  104. if (deleteData.length || insertData.length) {
  105. showData(sheet, cache, setting.header, isCheckingRepeat ? 0 : 5);
  106. }
  107. }
  108. } catch (err) {
  109. // 恢复各单元格数据
  110. showData(sheet, cache, setting.header, 5);
  111. }
  112. $.bootstrapLoading.end();
  113. }
  114. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  115. const changedCells = [{ row: info.row }];
  116. handleEdit(changedCells);
  117. });
  118. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  119. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  120. // 显示关键字数据
  121. const keywordList = cache[row] && cache[row].keywordList || [];
  122. KEYWORD_BOOK.showKeywordData(keywordList);
  123. });
  124. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  125. const changedRows = [];
  126. let preRow;
  127. info.changedCells.forEach(({ row }) => {
  128. if (row !== preRow) {
  129. changedRows.push({ row });
  130. }
  131. preRow = row;
  132. });
  133. handleEdit(changedRows);
  134. });
  135. // 显示重复别名编码数据
  136. let orgCache = [...cache];
  137. const showRepeatData = () => {
  138. if (isCheckingRepeat) {
  139. $('#check-repeat').text('检测重复别名编码');
  140. isCheckingRepeat = false;
  141. cache = orgCache;
  142. showData(sheet, cache, setting.header, 5);
  143. return;
  144. }
  145. $('#check-repeat').text('检测重复别名编码(检测中)');
  146. isCheckingRepeat = true;
  147. const tableData = [];
  148. const classCodeMap = {};
  149. cache.forEach(item => {
  150. const classCode = item.classCode || '';
  151. if (!classCodeMap[classCode]) {
  152. classCodeMap[classCode] = 1;
  153. } else {
  154. classCodeMap[classCode] = classCodeMap[classCode] + 1;
  155. }
  156. });
  157. cache.forEach(item => {
  158. const classCode = item.classCode || '';
  159. if (classCodeMap[classCode] > 1) {
  160. tableData.push(item);
  161. }
  162. });
  163. orgCache = [...cache];
  164. cache = tableData;
  165. showData(sheet, cache, setting.header);
  166. }
  167. return {
  168. clear,
  169. initData,
  170. showRepeatData,
  171. }
  172. })();
  173. $(document).ready(() => {
  174. // 检测重复别名编码
  175. $('#check-repeat').click(() => {
  176. PRICE_BOOK.showRepeatData();
  177. });
  178. });