rpt_archive.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /**
  2. * Created by Tony on 2021/4/2.
  3. */
  4. let rptArchiveObj = {
  5. treeObj: null,
  6. currentNode: null,
  7. currentArchiveUuid: null,
  8. currentArchiveDateStr: null,
  9. iniPage: function() {
  10. //初始化页面的归档信息
  11. let me = rptArchiveObj;
  12. me.currentNode = null;
  13. me.currentArchiveUuid = null;
  14. me.currentArchiveDateStr = null;
  15. const archivedRptIds = [];
  16. for (let aItem of ARCHIVE_LIST) {
  17. archivedRptIds.push(parseInt(aItem.rpt_id));
  18. }
  19. let private_remove_hide_item = function (items, nlv) {
  20. if (items && items.length > 0) {
  21. for (let i = items.length - 1; i >= 0; i--) {
  22. if (!(items[i].released) && items[i].nodeType === 2) {
  23. items.splice(i, 1);
  24. } else {
  25. if (items[i].items && items[i].items.length > 0) {
  26. private_remove_hide_item(items[i].items, nlv + 1);
  27. if (items[i].items.length === 0 && nlv > 0) {
  28. items.splice(i, 1);
  29. }
  30. }
  31. }
  32. }
  33. }
  34. };
  35. const private_remove_un_archive_item = function(items, nlv) {
  36. if (items && items.length > 0) {
  37. for (let i = items.length - 1; i >= 0; i--) {
  38. if (items[i].nodeType === 2) {
  39. if (archivedRptIds.indexOf(items[i].refId) < 0) {
  40. items.splice(i, 1);
  41. }
  42. } else {
  43. if (items[i].items && items[i].items.length > 0) {
  44. private_remove_un_archive_item(items[i].items, nlv + 1);
  45. if (items[i].items.length === 0 && nlv > 0) {
  46. items.splice(i, 1);
  47. }
  48. } else {
  49. //items[i]是目录,但又没有items子项,
  50. items.splice(i, 1);
  51. }
  52. }
  53. }
  54. }
  55. };
  56. let nodeLv = 0;
  57. private_remove_hide_item(TOP_TREE_NODES, nodeLv);
  58. private_remove_un_archive_item(TOP_TREE_NODES, nodeLv);
  59. zTreeHelper.createTreeDirectly(TOP_TREE_NODES, rpt_prj_folder_setting, "rptTplTree", me);
  60. me.treeObj.expandAll(true);
  61. me.refreshNodes();
  62. rptArchiveObj._countChkedRptTpl();
  63. rptArchiveObj._buildeArchiveDateSelect();
  64. },
  65. toggleBtn: function (enabled) {
  66. if (current_stage_status === 3 && enabled) {
  67. $('#btnArchiveRpt').removeAttr('disabled');
  68. $('#btnArchiveList').removeAttr('disabled');
  69. } else {
  70. $('#btnArchiveRpt').attr('disabled', '');
  71. $('#btnArchiveList').attr('disabled', '');
  72. }
  73. },
  74. refreshNodes: function() {
  75. let me = this;
  76. let private_setupIsParent = function(node){
  77. node.isParent = (node.nodeType === RT.NodeType.NODE || node.level === 0);
  78. if (node.items && node.items.length) {
  79. for (let i = 0; i < node.items.length; i++) {
  80. private_setupIsParent(node.items[i]);
  81. }
  82. }
  83. };
  84. let topNodes = me.treeObj.getNodes();
  85. for (let i = 0; i < topNodes.length; i++) {
  86. private_setupIsParent(topNodes[i]);
  87. }
  88. me.treeObj.refresh();
  89. },
  90. onCheck: function(event, treeId, treeNode) {
  91. rptArchiveObj._countChkedRptTpl();
  92. if (treeNode.isParent) {
  93. rptArchiveObj.treeObj.expandNode(treeNode, true, true, false);
  94. }
  95. },
  96. onClick: function(event,treeId,treeNode) {
  97. let me = rptArchiveObj;
  98. if (treeNode && treeNode.nodeType === TPL_TYPE_TEMPLATE && treeNode.refId > 0) {
  99. me.currentNode = treeNode;
  100. for (let aItem of ARCHIVE_LIST) {
  101. if (treeNode.refId === parseInt(aItem.rpt_id)) {
  102. me.currentArchiveUuid = null;
  103. me.currentArchiveDateStr = null;
  104. if (aItem.items && aItem.items.length > 0) {
  105. // me.currentArchiveUuid = aItem.items[0].uuid;
  106. me.currentArchiveUuid = aItem.items[aItem.items.length - 1].uuid;
  107. // me.currentArchiveDateStr = aItem.items[0].updateDate_time;
  108. me.currentArchiveDateStr = '#' + (aItem.items.length) + ' ' + aItem.items[aItem.items.length - 1].updateDate_time;
  109. }
  110. break;
  111. }
  112. }
  113. me._countChkedRptTpl();
  114. me._buildeArchiveDateSelect();
  115. me._requestArchiveReport();
  116. }
  117. },
  118. _requestArchiveReport: function () {
  119. let me = rptArchiveObj;
  120. if (me.currentNode && me.currentArchiveUuid) {
  121. try {
  122. // let uuIdUrl = "/getArchivedFileByUUID/" + me.currentArchiveUuid + "/" + stringUtil.replaceAll(me.currentNode.name, "#", "_");
  123. // console.log(uuIdUrl);
  124. $('#iframe_made').html('<iframe src="/archive/pdf/show?file=https://measure-sign-pdf.oss-cn-shenzhen.aliyuncs.com/archive/'+ me.currentArchiveUuid +'.PDF" height="750px" width="100%" style="border: none;"></iframe>');
  125. // NetcaPDFSeal.openPDFWithUrl(window.location.href);
  126. // window.location = uuIdUrl;
  127. } catch (ex) {
  128. console.log(ex.toString());
  129. }
  130. }
  131. },
  132. _changeArchiveDateSelect: function (dom) {
  133. let me = rptArchiveObj;
  134. // me.currentArchiveUuid = dom.uuid; //在dom的onclick时已经设置过了
  135. me.currentArchiveDateStr = dom.innerHTML;
  136. $('#iframe_made').html('<iframe src="/archive/pdf/show?file=https://measure-sign-pdf.oss-cn-shenzhen.aliyuncs.com/archive/'+ me.currentArchiveUuid +'.PDF" height="750px" width="100%" style="border: none;"></iframe>');
  137. me._buildeArchiveDateSelect();
  138. },
  139. _buildeArchiveDateSelect: function () {
  140. let me = rptArchiveObj;
  141. let targetDom = document.getElementById("currentDrpArchiveSelect");
  142. targetDom.innerHTML = me.currentArchiveDateStr;
  143. let drpDom = $("#drpArchiveSelect");
  144. drpDom.empty();
  145. if (me.currentNode && me.currentArchiveUuid && me.currentArchiveDateStr) {
  146. for (let aItem of ARCHIVE_LIST) {
  147. if (me.currentNode.refId === parseInt(aItem.rpt_id)) {
  148. for (let [index,item] of aItem.items.entries()) {
  149. if (item.uuid !== me.currentArchiveUuid) {
  150. const str = '<a class="dropdown-item" href="javascript: void(0);" onclick="rptArchiveObj.currentArchiveUuid = \'' + item.uuid + '\'; rptArchiveObj._changeArchiveDateSelect(this)">' + '#' + (index+1) + ' ' + item.updateDate_time + '</a>';
  151. drpDom.append(str);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. },
  158. _countChkedRptTpl: function () {
  159. let me = rptArchiveObj;
  160. if (me.treeObj) {
  161. me.checkedRptTplNodes = [];
  162. let chkNodes = me.treeObj.getCheckedNodes(true), cnt = 0, hasCurrentNode = false;
  163. for (let node of chkNodes) {
  164. if (node.nodeType === TPL_TYPE_TEMPLATE) {
  165. cnt++;
  166. me.checkedRptTplNodes.push(node);
  167. if (me.currentNode === node) hasCurrentNode = true;
  168. }
  169. }
  170. if (!hasCurrentNode && cnt === 0 && me.currentNode !== null) {
  171. //这里根据实际需求再做处理
  172. cnt++;
  173. me.checkedRptTplNodes.push(me.currentNode);
  174. }
  175. $("#print_div").find("span").each(function(cIdx,elementSpan){
  176. elementSpan.innerText = cnt;
  177. });
  178. $("#export_div").find("span").each(function(cIdx,elementSpan){
  179. elementSpan.innerText = cnt;
  180. });
  181. }
  182. },
  183. showArchivedItem: function(currentNode) {
  184. //初始化当前报表已经归档的信息
  185. //ARCHIVE_LIST结构:[{rpt_id, items: [{uuid, update_time, is_common}...最多3个]}...] (当前项目、当前期的所有报表归档信息)
  186. if (currentNode) {
  187. //1. cardArchiveInfo
  188. let cardArchiveInfo = $('#cardArchiveInfo');
  189. cardArchiveInfo.empty();
  190. let auditDate = null;
  191. let achivedAmt = 0;
  192. let achivedItem = null;
  193. for (let item of ARCHIVE_LIST) {
  194. if (parseInt(item.rpt_id) === currentNode.refId) {
  195. auditDate = new Date(LAST_AUDITOR.begin_time);
  196. achivedAmt = item.items?item.items.length:0;
  197. achivedItem = item;
  198. break;
  199. }
  200. }
  201. if (auditDate) {
  202. cardArchiveInfo.append('<h6>第' + current_stage_order + '期,审批通过时间:' + auditDate.getFullYear() + '-' + (auditDate.getMonth() + 1) + '-' + auditDate.getDate() + '。</h6>');
  203. } else {
  204. cardArchiveInfo.append('<h6>第' + current_stage_order + '期');
  205. }
  206. cardArchiveInfo.append('<h6>本张报表第' + current_stage_order + '期,已归档' + achivedAmt + '份文件。</h6>');
  207. if (achivedItem && achivedItem.items && achivedItem.items.length === 3) {
  208. cardArchiveInfo.append('<h6>本次归档将会覆盖最旧那次归档。</h6>');
  209. }
  210. cardArchiveInfo.append('<div class="form-group" id="archived_frm_grp">');
  211. if (achivedAmt > 0) {
  212. for (let idx = 0; idx < achivedItem.items.length; idx++) {
  213. cardArchiveInfo.append('<div class="form-check">');
  214. // if (achivedAmt === 3) {
  215. // cardArchiveInfo.append('<input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios' + idx + '" value="option' + idx + '" ' + ((idx === 0)?'checked':'') + '>');
  216. // }
  217. cardArchiveInfo.append('<label class="form-check-label" for="exampleRadios' + idx + '">');
  218. // let ad = new Date(achivedItem.items[idx].update_time);
  219. // cardArchiveInfo.append('#' + (idx + 1) + ' ' + ad.getFullYear() + '-' + (ad.getMonth() + 1) + '-' + ad.getDate() + ' 归档');
  220. cardArchiveInfo.append('#' + (idx + 1) + ' ' + achivedItem.items[idx].updateDate_time + ' 归档');
  221. cardArchiveInfo.append('</label>');
  222. cardArchiveInfo.append('</div>');
  223. }
  224. }
  225. cardArchiveInfo.append('</div>');
  226. //2. selectionArchiveInfo
  227. let selectionArchiveInfo = $('#selectionArchiveInfo');
  228. selectionArchiveInfo.empty();
  229. if (achivedAmt > 0) {
  230. for (let idx = 0; idx < achivedItem.items.length; idx++) {
  231. selectionArchiveInfo.append('<a class="dropdown-item" href="javascript: void(0);">#' + (idx + 1) + ' ' + achivedItem.items[idx].updateDate_time + '</a>');
  232. }
  233. }
  234. }
  235. },
  236. _getCurrentArchives: function (currentNode) {
  237. let rst = null;
  238. if (ARCHIVE_LIST.length > 0 && currentNode) {
  239. for (let aItem of ARCHIVE_LIST) {
  240. if (parseInt(aItem.rpt_id) === currentNode.refId) {
  241. rst = aItem;
  242. break;
  243. }
  244. }
  245. }
  246. return rst;
  247. },
  248. _chkIfFullArchives: function(currentNode) {
  249. let aItem = this._getCurrentArchives(currentNode);
  250. let rst = (aItem && aItem.items && aItem.items.length === 3);
  251. return rst;
  252. },
  253. archiveCurrentReport: function(currentRptPageRst, currentNode) {
  254. // 归档当前报表
  255. if (currentRptPageRst !== null) {
  256. try {
  257. let doc = JpcJsPDFHelper._createPdf(currentRptPageRst, rptControlObj.getCurrentPageSize(), ROLE_REL_LIST, STAGE_AUDIT);
  258. let formData = new FormData();
  259. formData.append('file', doc.output('blob'), 'upload.pdf'); //上传单个文件的添加方式
  260. if (!rptArchiveObj._chkIfFullArchives(currentNode)) {
  261. postDataWithFile('/tender/report_api/addArchive/' + PROJECT_ID + '/' + current_stage_id + '/' + currentNode.refId, formData, function (result) {
  262. // 成功后,更新当前页面
  263. if (result.addedRst) {
  264. // console.log(result);
  265. ARCHIVE_LIST = result.addedRst;
  266. rptArchiveObj.showArchivedItem(currentNode);
  267. } else {
  268. // 有冲突,需要删除
  269. CommonAjax.postXsrfEx('/tender/report_api/removeArchive/' + PROJECT_ID + '/' + current_stage_id + '/' + currentNode.refId + result.fileName, '', 3000, true, getCookie('csrfToken'),
  270. function(result){
  271. //
  272. }
  273. );
  274. }
  275. }, function (error){
  276. // alert(error);
  277. });
  278. } else {
  279. let aItem = this._getCurrentArchives(currentNode);
  280. if (aItem && aItem.items && aItem.items.length > 0) {
  281. let orgName = aItem.items[0].uuid;
  282. let compStr = aItem.items[0].updateDate_time;
  283. for (let idx = 1; idx < aItem.items.length; idx++) {
  284. if (aItem.items[idx].updateDate_time < compStr) {
  285. compStr = aItem.items[idx].updateDate_time;
  286. orgName = aItem.items[idx].uuid;
  287. }
  288. }
  289. postDataWithFile('/tender/report_api/updateArchive/' + PROJECT_ID + '/' + current_stage_id + '/' + currentNode.refId + '/' + orgName, formData, function (result) {
  290. // 成功后,更新当前页面
  291. if (result.updatedRst) {
  292. // console.log(result);
  293. ARCHIVE_LIST = result.updatedRst;
  294. rptArchiveObj.showArchivedItem();
  295. } else {
  296. // 有冲突,需要删除
  297. CommonAjax.postXsrfEx('/tender/report_api/removeArchive/' + PROJECT_ID + '/' + current_stage_id + '/' + currentNode.refId + result.fileName, '', 3000, true, getCookie('csrfToken'),
  298. function(result){
  299. //
  300. }
  301. );
  302. }
  303. }, function (error){
  304. // alert(error);
  305. });
  306. }
  307. }
  308. } catch (ex) {
  309. console.log(ex.toString());
  310. }
  311. } else {
  312. alert('请选择打开一个报表!');
  313. }
  314. }
  315. };
  316. function _dataURLtoFile(dataurl, filename) {
  317. var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
  318. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  319. while(n--){
  320. u8arr[n] = bstr.charCodeAt(n);
  321. }
  322. return new File([u8arr], filename, {type:mime});
  323. };
  324. /**
  325. * 获取 blob
  326. * @param {String} url 目标文件地址
  327. * @return {Promise}
  328. */
  329. function getBlob(url) {
  330. return new Promise(resolve => {
  331. const xhr = new XMLHttpRequest();
  332. xhr.open('GET', url, true);
  333. xhr.responseType = 'blob';
  334. xhr.onload = () => {
  335. if (xhr.status === 200) {
  336. resolve(xhr.response);
  337. }
  338. };
  339. xhr.send();
  340. });
  341. }
  342. /**
  343. * 保存
  344. * @param {Blob} blob
  345. * @param {String} filename 想要保存的文件名称
  346. */
  347. function saveAs(blob, filename) {
  348. if (window.navigator.msSaveOrOpenBlob) {
  349. navigator.msSaveBlob(blob, filename);
  350. } else {
  351. const link = document.createElement('a');
  352. const body = document.querySelector('body');
  353. link.href = window.URL.createObjectURL(blob);
  354. link.download = filename;
  355. // fix Firefox
  356. link.style.display = 'none';
  357. body.appendChild(link);
  358. link.click();
  359. body.removeChild(link);
  360. window.URL.revokeObjectURL(link.href);
  361. }
  362. }
  363. /**
  364. * 下载
  365. * @param {String} url 目标文件地址
  366. * @param {String} filename 想要保存的文件名称
  367. */
  368. function download(url, filename) {
  369. getBlob(url).then(blob => {
  370. saveAs(blob, filename);
  371. });
  372. }
  373. $(function () {
  374. $('#download_file').click(function () {
  375. if (rptArchiveObj.currentArchiveUuid) {
  376. download(`https://measure-sign-pdf.oss-cn-shenzhen.aliyuncs.com/archive/${rptArchiveObj.currentArchiveUuid}.PDF`, `${rptArchiveObj.currentNode.name} ${rptArchiveObj.currentArchiveDateStr}.pdf`);
  377. } else {
  378. alert('请选择打开一个报表!');
  379. }
  380. });
  381. $('#print_file').click(function () {
  382. if (rptArchiveObj.currentArchiveUuid) {
  383. $('#iframe_made iframe')[0].contentWindow.print();
  384. } else {
  385. alert('请选择打开一个报表!');
  386. }
  387. });
  388. })