priceItem.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. const handleSelectionChange = (row) => {
  119. // 显示关键字数据
  120. const keywordList = cache[row] && cache[row].keywordList || [];
  121. KEYWORD_BOOK.showKeywordData(keywordList);
  122. }
  123. sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  124. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  125. handleSelectionChange(row);
  126. });
  127. sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  128. const changedRows = [];
  129. let preRow;
  130. info.changedCells.forEach(({ row }) => {
  131. if (row !== preRow) {
  132. changedRows.push({ row });
  133. }
  134. preRow = row;
  135. });
  136. handleEdit(changedRows);
  137. });
  138. // 显示重复别名编码数据
  139. let orgCache = [...cache];
  140. const showRepeatData = () => {
  141. if (isCheckingRepeat) {
  142. $('#check-repeat').text('检测重复别名编码');
  143. isCheckingRepeat = false;
  144. cache = orgCache;
  145. showData(sheet, cache, setting.header, 5);
  146. return;
  147. }
  148. $('#check-repeat').text('检测重复别名编码(检测中)');
  149. isCheckingRepeat = true;
  150. const tableData = [];
  151. const classCodeMap = {};
  152. cache.forEach(item => {
  153. const classCode = item.classCode || '';
  154. if (!classCodeMap[classCode]) {
  155. classCodeMap[classCode] = 1;
  156. } else {
  157. classCodeMap[classCode] = classCodeMap[classCode] + 1;
  158. }
  159. });
  160. cache.forEach(item => {
  161. const classCode = item.classCode || '';
  162. if (classCodeMap[classCode] > 1) {
  163. tableData.push(item);
  164. }
  165. });
  166. orgCache = [...cache];
  167. cache = tableData;
  168. showData(sheet, cache, setting.header);
  169. }
  170. const getKey = (item) => {
  171. return `${item.code || ''}@${item.name || ''}@${item.specs || ''}@${item.unit || ''}`;
  172. }
  173. // 批量修改
  174. const batchEdit = async () => {
  175. try {
  176. $.bootstrapLoading.start();
  177. const row = sheet.getActiveRowIndex();
  178. const col = sheet.getActiveColumnIndex();
  179. const colHeader = setting.header[col];
  180. const priceItem = cache[row];
  181. if (!priceItem || !colHeader) {
  182. return;
  183. }
  184. const prop = colHeader.dataCode;
  185. const val = $('#edit-text').val();
  186. if (['noTaxPrice', 'taxPrice'].includes(prop) && (!val || isNaN(val))) {
  187. throw new Error('请输入数值');
  188. }
  189. await ajaxPost('/priceInfo/batchUpdate', { priceItem, prop, val });
  190. const key = getKey(priceItem);
  191. cache.forEach(item => {
  192. if (key === getKey(item)) {
  193. item[prop] = val;
  194. }
  195. });
  196. showData(sheet, cache, setting.header, 5);
  197. $('#batch-edit').modal('hide');
  198. } catch (error) {
  199. alert(error.message);
  200. }
  201. $.bootstrapLoading.end();
  202. }
  203. // 右键功能
  204. function buildContextMenu() {
  205. $.contextMenu({
  206. selector: '#price-spread',
  207. build: function ($triggerElement, e) {
  208. // 控制允许右键菜单在哪个位置出现
  209. const offset = $('#price-spread').offset();
  210. const x = e.pageX - offset.left;
  211. const y = e.pageY - offset.top;
  212. const target = sheet.hitTest(x, y);
  213. if (target.hitTestType === 3) { // 在表格内
  214. const sel = sheet.getSelections()[0];
  215. if (sel && sel.rowCount === 1 && typeof target.row !== 'undefined') {
  216. const orgRow = sheet.getActiveRowIndex();
  217. sheet.setActiveCell(target.row, target.col);
  218. if (orgRow !== target.row) {
  219. handleSelectionChange(target.row);
  220. }
  221. }
  222. return {
  223. items: {
  224. batchUpdate: {
  225. name: '批量修改',
  226. icon: "fa-edit",
  227. disabled: function () {
  228. return locked || !cache[target.row];
  229. },
  230. callback: function (key, opt) {
  231. const colHeader = setting.header[target.col];
  232. const item = cache[target.row];
  233. if (item) {
  234. const info = `材料:${item.code || ''} ${item.name || ''} ${item.specs || ''} ${item.unit}`;
  235. $('#edit-item').text(info);
  236. $('#edit-label').text(colHeader.headerName);
  237. $('#edit-text').attr('placeholder', `请输入${colHeader.headerName}`);
  238. $('#edit-text').val(item[colHeader.dataCode] || '');
  239. $('#batch-edit').modal('show');
  240. }
  241. }
  242. },
  243. }
  244. };
  245. }
  246. else {
  247. return false;
  248. }
  249. }
  250. });
  251. }
  252. buildContextMenu();
  253. return {
  254. clear,
  255. initData,
  256. showRepeatData,
  257. batchEdit,
  258. }
  259. })();
  260. $(document).ready(() => {
  261. // 检测重复别名编码
  262. $('#check-repeat').click(() => {
  263. PRICE_BOOK.showRepeatData();
  264. });
  265. // 批量修改
  266. $('#batch-edit-confirm').click(() => {
  267. PRICE_BOOK.batchEdit();
  268. });
  269. $('#batch-edit').on('shown.bs.modal', function () {
  270. $('#edit-text').focus();
  271. });
  272. });