batch_import.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const BatchImportStageGcl = function (setting) {
  2. const biObj = {
  3. setting,
  4. spread: null,
  5. sheet: null,
  6. tenderSourceTree: null,
  7. history: [],
  8. batchTree: null,
  9. rebuildStageSelect: function () {
  10. const getItems = function (data) {
  11. const items = [];
  12. if (data) {
  13. for (let i = 1; i <= data.stageCount; i++) {
  14. items.push({value: i, text: `第${i}期`});
  15. }
  16. }
  17. return items;
  18. };
  19. for (let i = 0; i < biObj.sheet.getRowCount(); i++) {
  20. const data = biObj.batchTree.nodes[i];
  21. if (!data.tid) continue;
  22. const items = getItems(data);
  23. const cellType = new spreadNS.CellTypes.ComboBox().itemHeight(10).editorValueType(spreadNS.CellTypes.EditorValueType.value).items(items);
  24. biObj.sheet.getCell(i, 2).cellType(cellType);
  25. }
  26. },
  27. trEditEnded: function (e, info) {
  28. const data = SpreadJsObj.getSelectObject(info.sheet);
  29. const col = info.sheet.zh_setting.cols[info.col];
  30. data[col.field] = info.sheet.getValue(info.row, info.col);
  31. },
  32. reloadBatchTree() {
  33. this.batchTree.clearDatas();
  34. for (const h of this.history) {
  35. if (!h.ledger_node) continue;
  36. const ledgerData = { lid: h.lid, ledger_id: h.ledger_node.ledger_id, code: h.ledger_node.code, name: h.ledger_node.name, ledger_node: h.ledger_node };
  37. const batchNode = this.batchTree.addNode(ledgerData, null);
  38. for (const t of h.tenders) {
  39. const tenderData = JSON.parse(JSON.stringify(t));
  40. const tender = this.tenderSourceTree.nodes.find(y => { return y.tid === t.tid });
  41. tenderData.stageCount = tender.stageCount;
  42. this.batchTree.addNode(tenderData, batchNode);
  43. }
  44. }
  45. this.batchTree.sortTreeNode(true);
  46. },
  47. loadHistory: function () {
  48. if (biObj.batching) return;
  49. biObj.tender_id = biObj.setting.stageTree.nodes[0].tender_id;
  50. postData('/list/load', {type: 'stageBatch', tid: biObj.tender_id}, data => {
  51. biObj.history = data.history || [];
  52. // 屏蔽自己
  53. const curIndex = data.tenders.findIndex(x => { return x.id === biObj.tender_id });
  54. if (curIndex >= 0) data.tenders.splice(curIndex, 1);
  55. biObj.tenderSourceTree = Tender2Tree.convert(data.category, data.tenders, data.ledgerAuditConst, data.stageAuditConst);
  56. for (const h of biObj.history) {
  57. h.ledger_order = biObj.setting.stageTree.nodes.findIndex(x => { return x.id === h.lid; });
  58. h.ledger_node = h.ledger_order >= 0 ? biObj.setting.stageTree.nodes[h.ledger_order] : null;
  59. if (h.tenders) h.tenders = h.tenders.filter(x => { return biObj.tenderSourceTree.nodes.find(y => { return x.tid === y.tid; })});
  60. }
  61. biObj.history.sort((x, y) => { return x.ledger_order - y.ledger_order; });
  62. biObj.reloadBatchTree();
  63. SpreadJsObj.loadSheetData(biObj.sheet, SpreadJsObj.DataType.Tree, biObj.batchTree);
  64. biObj.rebuildStageSelect();
  65. });
  66. },
  67. initBatchImport: function () {
  68. if (this.spread) return;
  69. this.spread = SpreadJsObj.createNewSpread($('#bi-spread')[0]);
  70. this.sheet = this.spread.getActiveSheet();
  71. SpreadJsObj.initSheet(this.sheet, {
  72. cols: [
  73. // {title: '选择', field: 'selected', hAlign: 1, width: 40, formatter: '@', cellType: 'checkbox'},
  74. {title: '编号', field: 'code', hAlign: 0, width: 180, formatter: '@', cellType: 'tree'},
  75. {title: '名称/引用标段', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  76. {title: '可选期', field: 'stage', hAlign: 1, width: 60, formatter: '@'},
  77. // {title: '覆盖数据', field: 'is_cover', hAlign: 1, width: 60, cellType: 'checkbox'},
  78. {title: '状态', field: 'status', hAlign: 1, width: 60, formatter: '@'},
  79. {title: '错误信息', field: 'error', hAlign: 1, width: 60, formatter: '@'},
  80. ],
  81. emptyRows: 0,
  82. headRows: 1,
  83. headRowHeight: [32],
  84. defaultRowHeight: 21,
  85. headerFont: '12px 微软雅黑',
  86. font: '12px 微软雅黑',
  87. selectedBackColor: '#fffacd',
  88. });
  89. this.spread.bind(spreadNS.Events.EditEnded, biObj.trEditEnded);
  90. this.batchTree = createNewPathTree('gather', {
  91. id: 'id',
  92. pid: 'pid',
  93. order: 'order',
  94. level: 'level',
  95. rootId: -1,
  96. fullPath: 'full_path',
  97. });
  98. },
  99. checkErrors: function () {
  100. const hasError = this.batchTree.children.findIndex(x => { return x.error > 0; }) >= 0;
  101. if (hasError) {
  102. $('#bi-download-error').show();
  103. } else {
  104. $('#bi-download-error').hide();
  105. }
  106. },
  107. importStageGcl: async function (node, cover) {
  108. const updateData = { lid: node.lid, type: 'stage', cover, tenders: [] };
  109. for (const tender of node.children) {
  110. updateData.tenders.push({ tid: tender.tid, name: tender.name, stageCount: tender.stageCount, stage: tender.stage });
  111. }
  112. const result = await postDataAsync(window.location.pathname + '/sumLoad', updateData);
  113. biObj.setting.afterLoad(result, node.ledger_node);
  114. node.errors = result.sumLoadHis.errors;
  115. node.error = node.errors ? node.errors.length : 0;
  116. },
  117. batchImport: async function () {
  118. $('#bi-start')[0].disabled = true;
  119. biObj.batching = true;
  120. const cover = $('#bi-cover')[0].checked;
  121. for (const node of this.batchTree.children) {
  122. if (!node.children || node.children.length === 0) continue;
  123. const row = this.batchTree.getNodeIndex(node);
  124. try {
  125. node.status = '开始导入';
  126. SpreadJsObj.reLoadRowData(biObj.sheet, row);
  127. await biObj.importStageGcl(node, cover);
  128. node.status = '导入完成';
  129. SpreadJsObj.reLoadRowData(biObj.sheet, row);
  130. } catch(err) {
  131. console.log(err);
  132. node.status = '导入失败';
  133. SpreadJsObj.reLoadRowData(biObj.sheet, row);
  134. }
  135. }
  136. biObj.batching = false;
  137. $('#bi-start')[0].disabled = false;
  138. biObj.checkErrors();
  139. },
  140. downloadErrors: function () {
  141. const errorType = {
  142. less: '数量变少',
  143. miss: '找不到清单',
  144. 'qc-conflict': '变更冲突(已调用变更令)'
  145. };
  146. // const setting = {
  147. // header: ['清单编号', '清单名称','单位', '合同数量', '变更数量', '错误类型'],
  148. // width: [80, 200, 60, 80, 80, 180],
  149. // hAlign: ['left', 'left', 'center', 'right', 'right', 'left'],
  150. // };
  151. // const sheets = [];
  152. // for (const node of this.batchTree.children) {
  153. // if (node.error > 0) {
  154. // sheets.push({ name: node.code, setting, data: node.errors.map(x => {
  155. // return [x.b_code, x.name, x.unit, x.qty, x.qc_qty, errorType[x.type]]; })
  156. // });
  157. // }
  158. // }
  159. // XLSXObj.exportXlsxSheets(sheets, '批量导入错误.xlsx');
  160. const setting = {
  161. cols: [
  162. {title: '清单编号', field: 'b_code', hAlign: 0, width: 80, formatter: '@'},
  163. {title: '清单名称', field: 'name', hAlign: 0, width: 180, formatter: '@'},
  164. {title: '单位', field: 'unit', hAlign: 1, width: 60, formatter: '@'},
  165. {title: '合同数量', field: 'qty', hAlign: 2, width: 80, formatter: '@'},
  166. {title: '变更数量', field: 'qc_qty', hAlign: 2, width: 80, formatter: '@'},
  167. {title: '错误类型', field: 'type', hAlign: 0, width: 150, formatter: '@', getValue(data) { return errorType[data.type]; }},
  168. ],
  169. emptyRows: 0,
  170. headRows: 1,
  171. headRowHeight: [32],
  172. defaultRowHeight: 21,
  173. headerFont: '12px 微软雅黑',
  174. font: '12px 微软雅黑',
  175. };
  176. const sheets = [];
  177. for (const node of this.batchTree.children) {
  178. if (node.error > 0) {
  179. sheets.push({ name: node.code, setting, data: node.errors });
  180. }
  181. }
  182. SpreadExcelObj.exportSimpleXlsxSheets(sheets, '批量导入错误.xlsx');
  183. }
  184. };
  185. $('#batch-import').on('shown.bs.modal', () => {
  186. biObj.initBatchImport();
  187. biObj.loadHistory();
  188. });
  189. $('#bi-start').click(function () {
  190. biObj.batchImport();
  191. });
  192. $('#bi-download-error').click(function () {
  193. biObj.downloadErrors();
  194. });
  195. const show = function () {
  196. $('#batch-import').modal('show');
  197. };
  198. return { show }
  199. };