priceEmpty.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // 空数据表
  2. const EMPTY_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: 100, dataCode: 'expString', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  8. { headerName: '材料名称', headerWidth: 275, dataCode: 'name', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  9. { headerName: '规格型号', headerWidth: 180, dataCode: 'specs', dataType: 'String', hAlign: 'left', vAlign: 'center' },
  10. { headerName: '单位', headerWidth: 80, dataCode: 'unit', dataType: 'String', hAlign: 'center', vAlign: 'center' },
  11. ],
  12. };
  13. // 初始化表格
  14. const workBookObj = {
  15. book: null,
  16. sheet: null,
  17. };
  18. const buildWorkBook = () => {
  19. workBookObj.book = initSheet($('.empty-spread')[0], setting);
  20. workBookObj.book.options.allowUserDragDrop = true;
  21. workBookObj.book.options.allowUserDragFill = true;
  22. workBookObj.book.options.allowExtendPasteRange = false;
  23. lockUtil.lockSpreads([workBookObj.book], locked);
  24. workBookObj.sheet = workBookObj.book.getSheet(0);
  25. }
  26. // 总数据(数据没合并前)
  27. let totalData = [];
  28. let totalMap = {};
  29. // 表格显示的数据(合并后)
  30. const cache = [];
  31. const getCompareKey = (item) => {
  32. const props = ['name', 'specs', 'unit'];
  33. return props.map(prop => item[prop] ? item[prop].trim() : '').join('@');
  34. }
  35. const setTotalMap = (items) => {
  36. totalMap = {};
  37. items.forEach(item => {
  38. const key = getCompareKey(item);
  39. if (totalMap[key]) {
  40. totalMap[key].push(item);
  41. } else {
  42. totalMap[key] = [item];
  43. }
  44. })
  45. }
  46. // 获取表格数据,汇总空数据,多地区可能存在相同材料,按名称规格单位做筛选,重复材料仅显示一条即可
  47. const getTableData = (items) => {
  48. const map = {};
  49. items.forEach(item => {
  50. const key = getCompareKey(item);
  51. if (!map[key]) {
  52. map[key] = { ...item };
  53. }
  54. });
  55. return Object.values(map);
  56. }
  57. // 根据表格数据,获取实际信息价数据(一对多)
  58. const getItemsFromTableItem = (item) => {
  59. const key = getCompareKey(item);
  60. return totalMap[key] || [];
  61. }
  62. // 获取材料关键字: 名称 规格
  63. const getKeyword = (item) => {
  64. return item ? `${item.name} ${item.specs}` : '';
  65. }
  66. // 改变关键字
  67. const changeKeyword = (item) => {
  68. const keyword = getKeyword(item);
  69. $('#recommend-search').val(keyword);
  70. if (!keyword) {
  71. RECOMMEND_BOOK.clear();
  72. } else {
  73. RECOMMEND_BOOK.loadRecommendData(keyword);
  74. }
  75. }
  76. // 清空
  77. function clear() {
  78. cache.length = 0;
  79. workBookObj.sheet.setRowCount(0);
  80. }
  81. let curRow = 0;
  82. // 初始化数据
  83. async function initData() {
  84. clear();
  85. curRow = 0;
  86. $.bootstrapLoading.start();
  87. try {
  88. totalData = await ajaxPost('/priceInfo/getPriceEmptyData', { libID, compilationID }, 1000 * 60 * 10);
  89. setTotalMap(totalData);
  90. const tableData = getTableData(totalData);
  91. cache.push(...tableData)
  92. showData(workBookObj.sheet, cache, setting.header);
  93. changeKeyword(cache[0]);
  94. } catch (err) {
  95. clear();
  96. alert(err);
  97. } finally {
  98. $.bootstrapLoading.end();
  99. }
  100. }
  101. // 编辑处理
  102. async function handleEdit(changedCells, diffMap, needRefresh) {
  103. const postData = []; // 请求用
  104. // 更新缓存用
  105. const updateData = [];
  106. const deleteData = [];
  107. const insertData = [];
  108. try {
  109. changedCells.forEach(({ row }) => {
  110. if (cache[row]) {
  111. const rowData = getRowData(workBookObj.sheet, row, setting.header);
  112. if (Object.keys(rowData).length) { // 还有数据,更新
  113. let diffData;
  114. if (diffMap) {
  115. diffData = diffMap[row];
  116. } else {
  117. diffData = getRowDiffData(rowData, cache[row], setting.header);
  118. }
  119. if (diffData) {
  120. // 改一行, 实际可能是改多行,表格一行数据是多行合并显示的
  121. const items = getItemsFromTableItem(cache[row]);
  122. items.forEach(item => {
  123. // 只有珠海才更新计算式
  124. const updateObj = { ...diffData };
  125. const area = AREA_BOOK.cache.find(areaItem => areaItem.ID === item.areaID);
  126. if (!area || !area.name || !/珠海/.test(area.name)) {
  127. delete updateObj.expString;
  128. }
  129. postData.push({ type: UpdateType.UPDATE, ID: item.ID, areaID: area.ID, compilationID, period: curLibPeriod, data: updateObj });
  130. });
  131. updateData.push({ row, data: diffData });
  132. }
  133. } else { // 该行无数据了,删除
  134. const items = getItemsFromTableItem(cache[row]);
  135. items.forEach(item => {
  136. const area = AREA_BOOK.cache.find(areaItem => areaItem.ID === item.areaID);
  137. postData.push({ type: UpdateType.DELETE, areaID: area.ID, compilationID, period: curLibPeriod, ID: item.ID });
  138. });
  139. deleteData.push(cache[row]);
  140. }
  141. }
  142. });
  143. if (postData.length) {
  144. $.bootstrapLoading.start();
  145. await ajaxPost('/priceInfo/editPriceData', { postData }, TIME_OUT);
  146. // 更新缓存,先更新然后删除,最后再新增,防止先新增后缓存数据的下标与更新、删除数据的下标对应不上
  147. updateData.forEach(item => {
  148. // 更新总表
  149. const curItem = cache[item.row];
  150. const compareKey = getCompareKey(curItem);
  151. const totalItems = totalMap[compareKey];
  152. if (totalItems) {
  153. const newCompareKey = getCompareKey({ ...curItem, ...item.data });
  154. totalItems.forEach(totalItem => {
  155. Object.assign(totalItem, item.data);
  156. });
  157. if (newCompareKey !== compareKey) {
  158. totalMap[newCompareKey] = totalItems;
  159. delete totalMap[compareKey];
  160. }
  161. }
  162. // 更新表格缓存
  163. Object.assign(cache[item.row], item.data);
  164. });
  165. deleteData.forEach(item => {
  166. // 更新总表
  167. const compareKey = getCompareKey(item);
  168. const totalItems = totalMap[compareKey];
  169. if (totalItems) {
  170. const totalItemIDs = totalItems.map(item => item.ID);
  171. totalData = totalData.filter(totalItem => !totalItemIDs.includes(totalItem));
  172. delete totalMap[compareKey];
  173. }
  174. // 更新表格缓存
  175. const index = cache.indexOf(item);
  176. if (index >= 0) {
  177. cache.splice(index, 1);
  178. }
  179. });
  180. insertData.forEach(item => cache.push(item));
  181. if (deleteData.length || insertData.length || needRefresh) {
  182. showData(workBookObj.sheet, cache, setting.header);
  183. }
  184. $.bootstrapLoading.end();
  185. CLASS_BOOK.reload();
  186. }
  187. } catch (err) {
  188. // 恢复各单元格数据
  189. showData(workBookObj.sheet, cache, setting.header);
  190. $.bootstrapLoading.end();
  191. }
  192. }
  193. // 跟新行的编号、编码编码
  194. async function updateRowCode(code, classCode, expString) {
  195. const item = cache[curRow];
  196. if (!item) {
  197. return;
  198. }
  199. const diffData = { code, classCode, expString };
  200. await handleEdit([{ row: curRow }], { [curRow]: diffData }, true);
  201. }
  202. const bindEvent = () => {
  203. workBookObj.sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function (e, info) {
  204. const changedCells = [{ row: info.row }];
  205. handleEdit(changedCells);
  206. });
  207. workBookObj.sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function (e, info) {
  208. const row = info.newSelections && info.newSelections[0] ? info.newSelections[0].row : 0;
  209. if (curRow !== row) {
  210. const item = cache[row];
  211. changeKeyword(item);
  212. }
  213. curRow = row;
  214. });
  215. workBookObj.sheet.bind(GC.Spread.Sheets.Events.RangeChanged, function (e, info) {
  216. const changedRows = [];
  217. let preRow;
  218. info.changedCells.forEach(({ row }) => {
  219. if (row !== preRow) {
  220. changedRows.push({ row });
  221. }
  222. preRow = row;
  223. });
  224. handleEdit(changedRows);
  225. });
  226. }
  227. // 将空表的表格保存至总表
  228. const saveInSummary = async () => {
  229. const documents = [];
  230. const removeIDs = [];
  231. cache.filter(item => item.code && item.classCode).forEach(item => {
  232. removeIDs.push(item.ID);
  233. documents.push({
  234. ID: uuid.v1(),
  235. code: item.code,
  236. classCode: item.classCode,
  237. expString: item.expString,
  238. name: item.name,
  239. specs: item.specs,
  240. unit: item.unit,
  241. });
  242. });
  243. if (!documents.length) {
  244. alert('不存在可保存数据');
  245. return;
  246. }
  247. console.log(documents);
  248. try {
  249. $.bootstrapLoading.progressStart('保存至总表', true);
  250. $("#progress_modal_body").text('正在保存至总表,请稍后...');
  251. await ajaxPost('/priceInfoSummary/saveInSummary', { documents }, 1000 * 60 * 5);
  252. setTimeout(() => {
  253. $.bootstrapLoading.progressEnd();
  254. alert('保存成功');
  255. const filterCache = cache.filter(item => !removeIDs.includes(item.ID));
  256. cache.length = 0;
  257. cache.push(...filterCache);
  258. showData(workBookObj.sheet, cache, setting.header);
  259. }, 1000);
  260. } catch (error) {
  261. setTimeout(() => {
  262. $.bootstrapLoading.progressEnd();
  263. }, 500);
  264. console.log(error);
  265. alert(error);
  266. }
  267. }
  268. return {
  269. buildWorkBook,
  270. bindEvent,
  271. clear,
  272. initData,
  273. workBookObj,
  274. updateRowCode,
  275. saveInSummary,
  276. }
  277. })();
  278. $(document).ready(() => {
  279. $('#empty-area').on('shown.bs.modal', function () {
  280. if (!EMPTY_BOOK.workBookObj.book) {
  281. EMPTY_BOOK.buildWorkBook();
  282. EMPTY_BOOK.bindEvent();
  283. }
  284. EMPTY_BOOK.initData();
  285. });
  286. $('#empty-area').on('hidden.bs.modal', function () {
  287. EMPTY_BOOK.clear();
  288. });
  289. // 保存至总表
  290. $('#save-in-summary').click(() => {
  291. EMPTY_BOOK.saveInSummary();
  292. });
  293. });