priceItem.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. showData(sheet, cache, setting.header, 5);
  42. const row = sheet.getActiveRowIndex();
  43. const keywordList = cache[row] && cache[row].keywordList || [];
  44. KEYWORD_BOOK.showKeywordData(keywordList);
  45. } catch (err) {
  46. cache = [];
  47. sheet.setRowCount(0);
  48. alert(err);
  49. } finally {
  50. $.bootstrapLoading.end();
  51. }
  52. }
  53. // 编辑处理
  54. async function handleEdit(changedCells, needLoading = true) {
  55. $.bootstrapLoading.start();
  56. const areaID = AREA_BOOK.curArea.ID;
  57. const postData = []; // 请求用
  58. // 更新缓存用
  59. const updateData = [];
  60. const deleteData = [];
  61. const insertData = [];
  62. try {
  63. changedCells.forEach(({ row }) => {
  64. if (cache[row]) {
  65. const rowData = getRowData(sheet, row, setting.header);
  66. if (Object.keys(rowData).length) { // 还有数据,更新
  67. const diffData = getRowDiffData(rowData, cache[row], setting.header);
  68. if (diffData) {
  69. postData.push({ type: UpdateType.UPDATE, ID: cache[row].ID, areaID, compilationID, period: curLibPeriod, data: diffData });
  70. updateData.push({ row, data: diffData });
  71. }
  72. } else { // 该行无数据了,删除
  73. postData.push({ type: UpdateType.DELETE, ID: cache[row].ID, areaID, compilationID, period: curLibPeriod });
  74. deleteData.push(cache[row]);
  75. }
  76. } else { // 新增
  77. const rowData = getRowData(sheet, row, setting.header);
  78. if (Object.keys(rowData).length) {
  79. rowData.ID = uuid.v1();
  80. rowData.libID = libID;
  81. rowData.compilationID = compilationID;
  82. rowData.areaID = AREA_BOOK.curArea.ID;
  83. rowData.classID = CLASS_BOOK.curClass.ID;
  84. rowData.period = curLibPeriod;
  85. postData.push({ type: UpdateType.CREATE, data: rowData });
  86. insertData.push(rowData);
  87. }
  88. }
  89. });
  90. if (postData.length) {
  91. await ajaxPost('/priceInfo/editPriceData', { postData }, TIME_OUT);
  92. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  93. updateData.forEach(item => {
  94. Object.assign(cache[item.row], item.data);
  95. });
  96. deleteData.forEach(item => {
  97. const index = cache.indexOf(item);
  98. if (index >= 0) {
  99. cache.splice(index, 1);
  100. }
  101. });
  102. insertData.forEach(item => cache.push(item));
  103. if (deleteData.length || insertData.length) {
  104. showData(sheet, cache, setting.header, isCheckingRepeat ? 0 : 5);
  105. }
  106. }
  107. } catch (err) {
  108. // 恢复各单元格数据
  109. showData(sheet, cache, setting.header, 5);
  110. }
  111. $.bootstrapLoading.end();
  112. }
  113. sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  114. const changedCells = [{ row: info.row }];
  115. handleEdit(changedCells);
  116. });
  117. const handleSelectionChange = (row) => {
  118. // 显示关键字数据
  119. const keywordList = cache[row] && cache[row].keywordList || [];
  120. KEYWORD_BOOK.showKeywordData(keywordList);
  121. }
  122. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  123. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  124. handleSelectionChange(row);
  125. });
  126. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  127. const changedRows = [];
  128. let preRow;
  129. info.changedCells.forEach(({ row }) => {
  130. if (row !== preRow) {
  131. changedRows.push({ row });
  132. }
  133. preRow = row;
  134. });
  135. handleEdit(changedRows);
  136. });
  137. // 显示重复别名编码数据
  138. let orgCache = [...cache];
  139. const showRepeatData = () => {
  140. if (isCheckingRepeat) {
  141. $('#check-repeat').text('检测重复别名编码');
  142. isCheckingRepeat = false;
  143. cache = orgCache;
  144. showData(sheet, cache, setting.header, 5);
  145. return;
  146. }
  147. $('#check-repeat').text('检测重复别名编码(检测中)');
  148. isCheckingRepeat = true;
  149. const tableData = [];
  150. const classCodeMap = {};
  151. cache.forEach(item => {
  152. const classCode = item.classCode || '';
  153. if (!classCodeMap[classCode]) {
  154. classCodeMap[classCode] = 1;
  155. } else {
  156. classCodeMap[classCode] = classCodeMap[classCode] + 1;
  157. }
  158. });
  159. cache.forEach(item => {
  160. const classCode = item.classCode || '';
  161. if (classCodeMap[classCode] > 1) {
  162. tableData.push(item);
  163. }
  164. });
  165. orgCache = [...cache];
  166. cache = tableData;
  167. showData(sheet, cache, setting.header);
  168. }
  169. const getKey = (item) => {
  170. return `${item.code || ''}@${item.name || ''}@${item.specs || ''}@${item.unit || ''}`;
  171. }
  172. // 批量修改
  173. const batchEdit = async () => {
  174. try {
  175. $.bootstrapLoading.start();
  176. const row = sheet.getActiveRowIndex();
  177. const col = sheet.getActiveColumnIndex();
  178. const colHeader = setting.header[col];
  179. const priceItem = cache[row];
  180. if (!priceItem || !colHeader) {
  181. return;
  182. }
  183. const prop = colHeader.dataCode;
  184. const val = $('#edit-text').val();
  185. if (['noTaxPrice', 'taxPrice'].includes(prop) && (!val || isNaN(val))) {
  186. throw new Error('请输入数值');
  187. }
  188. await ajaxPost('/priceInfo/batchUpdate', { priceItem, prop, val });
  189. const key = getKey(priceItem);
  190. cache.forEach(item => {
  191. if (key === getKey(item)) {
  192. item[prop] = val;
  193. }
  194. });
  195. showData(sheet, cache, setting.header, 5);
  196. $('#batch-edit').modal('hide');
  197. } catch (error) {
  198. alert(error.message);
  199. }
  200. $.bootstrapLoading.end();
  201. }
  202. // 右键功能
  203. function buildContextMenu() {
  204. $.contextMenu({
  205. selector: '#price-spread',
  206. build: function ($triggerElement, e) {
  207. // 控制允许右键菜单在哪个位置出现
  208. const offset = $('#price-spread').offset();
  209. const x = e.pageX - offset.left;
  210. const y = e.pageY - offset.top;
  211. const target = sheet.hitTest(x, y);
  212. if (target.hitTestType === 3) { // 在表格内
  213. const sel = sheet.getSelections()[0];
  214. if (sel && sel.rowCount === 1 && typeof target.row !== 'undefined') {
  215. const orgRow = sheet.getActiveRowIndex();
  216. sheet.setActiveCell(target.row, target.col);
  217. if (orgRow !== target.row) {
  218. handleSelectionChange(target.row);
  219. }
  220. }
  221. return {
  222. items: {
  223. batchUpdate: {
  224. name: '批量修改',
  225. icon: "fa-edit",
  226. disabled: function () {
  227. return locked || !cache[target.row];
  228. },
  229. callback: function (key, opt) {
  230. const colHeader = setting.header[target.col];
  231. const item = cache[target.row];
  232. if (item) {
  233. const info = `材料:${item.code || ''} ${item.name || ''} ${item.specs || ''} ${item.unit}`;
  234. $('#edit-item').text(info);
  235. $('#edit-label').text(colHeader.headerName);
  236. $('#edit-text').attr('placeholder', `请输入${colHeader.headerName}`);
  237. $('#edit-text').val(item[colHeader.dataCode] || '');
  238. $('#batch-edit').modal('show');
  239. }
  240. }
  241. },
  242. }
  243. };
  244. }
  245. else {
  246. return false;
  247. }
  248. }
  249. });
  250. }
  251. buildContextMenu();
  252. return {
  253. clear,
  254. initData,
  255. showRepeatData,
  256. batchEdit,
  257. }
  258. })();
  259. $(document).ready(() => {
  260. // 检测重复别名编码
  261. $('#check-repeat').click(() => {
  262. PRICE_BOOK.showRepeatData();
  263. });
  264. // 批量修改
  265. $('#batch-edit-confirm').click(() => {
  266. PRICE_BOOK.batchEdit();
  267. });
  268. $('#batch-edit').on('shown.bs.modal', function () {
  269. $('#edit-text').focus();
  270. });
  271. });