main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /**
  2. * Created by Syusuke on 2017/3/17.
  3. */
  4. $(function () {
  5. let preDeleteId = null;
  6. let deleteCount = 0;
  7. let selCompilationId,
  8. compilationsArr = [];
  9. $('#del').on('hidden.bs.modal', function () {
  10. deleteCount = 0;
  11. });
  12. //添加
  13. $('#addBtn').click(function () {
  14. let compilationName = $('#compilationSels option:selected').text();
  15. let compilationId = $('#compilationSels option:selected').val();
  16. let gljLibName = $('#gljLibSels option:selected').text();
  17. let gljLibId = $('#gljLibSels option:selected').val();
  18. let libName = $('#libNameTxt').val();
  19. let libCode = $('#libCode').val().trim();
  20. const serialNo = +$('#serialNo').val();
  21. if (libName.trim().length === 0) {
  22. alert('名称不可为空!');
  23. $('#libNameTxt').val('')
  24. } else if (!libCode) {
  25. alert('定额库编码不可为空');
  26. $('#libCode').val('');
  27. } else if (allNames.indexOf(libName) !== -1) {
  28. alert('此定额库已存在!');
  29. $('#libNameTxt').val('')
  30. } else if (compilationName.trim().length === 0) {
  31. alert('编办不可为空!');
  32. } else if (gljLibName.trim().length === 0) {
  33. alert("请选择工料机库!");
  34. } else {
  35. let newRationLib = {};
  36. newRationLib.dispName = libName;
  37. newRationLib.libCode = libCode; //定额编号,标准数据导出xml需要
  38. newRationLib.compilationId = compilationId;
  39. newRationLib.compilationName = compilationName;
  40. newRationLib.gljLib = gljLibId;
  41. newRationLib.creator = userAccount;
  42. newRationLib.appType = "建筑";
  43. newRationLib.serialNo = serialNo;
  44. $('#libNameTxt').val('');
  45. createRationLib(newRationLib);
  46. }
  47. });
  48. //重命名
  49. $("#showArea").on("click", "[data-target = '#edit']", function () {
  50. let renameId = $(this).parent().parent().attr("id");
  51. $('#renameText').val($(this).parent().parent().find('td:first-child').text());
  52. $('#renameSerialNo').val($(this).parent().parent().find('td:eq(1)').text());
  53. $('#renameCode').val($(this).parent().parent().find('td:eq(2)').text());
  54. $("#renameA").attr("renameId", renameId);
  55. });
  56. $("#renameA").click(function () {
  57. let newName = $("#renameText").val();
  58. let newLibCode = $('#renameCode').val().trim();
  59. let newSerialNo = +$('#renameSerialNo').val();
  60. let libId = $(this).attr("renameId");
  61. let jqSel = "#" + libId + " td:first" + " a";
  62. let orgName = $(jqSel).text();
  63. let filterName = allNames.filter(function (v) {
  64. return v === newName;
  65. });
  66. if (newName.trim().length === 0) {
  67. alert("名称不可为空!");
  68. $("#renameText").val('');
  69. } else if (!newLibCode) {
  70. alert('定额库编码不可为空!');
  71. $('#renameCode').val('');
  72. } else if (filterName.length > 0 && newName !== orgName) {
  73. alert("该定额库已存在!");
  74. $("#renameText").val('');
  75. } else {
  76. renameRationLib({ ID: libId, newName: newName, orgName: orgName, newLibCode: newLibCode, newSerialNo });
  77. }
  78. });
  79. $('#edit').on('shown.bs.modal', function () {
  80. setTimeout(function () {
  81. $('#renameText').focus();
  82. }, 100);
  83. });
  84. $('#add').on('shown.bs.modal', function () {
  85. setTimeout(function () {
  86. $('#libNameTxt').focus();
  87. }, 100);
  88. });
  89. $('#add').on('hidden.bs.modal', function () {
  90. $('#libNameTxt').val('');
  91. });
  92. //删除
  93. $("#showArea").on("click", "[data-target = '#del']", function () {
  94. let deleteId = $(this).parent().parent().attr("id");
  95. $("#deleteA").attr("deleteId", deleteId);
  96. let delLibName = $(`#${deleteId}`).find('td:first').text();
  97. $('#del').find('.modal-body h5').text(`准备删除 “${delLibName}”,会导致已引用此库的地方出错,确定要删除吗?`);
  98. });
  99. $("#deleteA").click(function () {
  100. let deleteId = $(this).attr("deleteId");
  101. if (preDeleteId && preDeleteId !== deleteId) {
  102. deleteCount = 0;
  103. }
  104. preDeleteId = deleteId;
  105. deleteCount++;
  106. let jqSel = "#" + deleteId + " td:first" + " a";
  107. let libName = $(jqSel).text();
  108. if (deleteCount === 3) {
  109. deleteCount = 0;
  110. removeRationLib({ libId: deleteId, libName: libName });
  111. $('#del').modal('hide');
  112. }
  113. });
  114. //全部计算
  115. $("#showArea").on("click", "[data-target = '#reCalcAll']", function () {
  116. let recalcId = $(this).parent().parent().attr("id");
  117. $("#reCalcConfirm").attr("recalcId", recalcId);
  118. });
  119. $("#reCalcConfirm").click(function () {
  120. $('#reCalcConfirm').addClass('disabled');
  121. $.bootstrapLoading.start();
  122. let recalcId = $(this).attr("recalcId");
  123. CommonAjax.post('/rationRepository/api/reCalcAll', { rationRepId: recalcId }, function (rstData) {
  124. $.bootstrapLoading.end();
  125. $('#reCalcAll').modal('hide');
  126. $('#reCalcConfirm').removeClass('disabled');
  127. }, function () {
  128. $.bootstrapLoading.end();
  129. $('#reCalcAll').modal('hide');
  130. $('#reCalcConfirm').removeClass('disabled')
  131. });
  132. });
  133. // 锁定、解锁
  134. $('#showArea').on('click', '.lock', function () {
  135. lockUtil.handleLockClick($(this));
  136. });
  137. getCompilationList(function (data) {
  138. compilationsArr = data.compilation;
  139. });
  140. // 导入原始数据按钮
  141. let rationRepId = 0;
  142. $("#showArea").on("click", ".import-source", function () {
  143. let id = $(this).data("id");
  144. id = parseInt(id);
  145. if (isNaN(id) || id <= 0) {
  146. return false;
  147. }
  148. rationRepId = id;
  149. $("#import").modal("show");
  150. });
  151. // 导入内部数据
  152. $("#showArea").on("click", ".import-data", function () {
  153. let id = $(this).data("id");
  154. id = parseInt(id);
  155. if (isNaN(id) || id <= 0) {
  156. return false;
  157. }
  158. rationRepId = id;
  159. $("#import2").modal("show");
  160. });
  161. // 导入章节数据
  162. $('#showArea').on('click', '.import-section', function () {
  163. let id = $(this).data('id');
  164. id = parseInt(id);
  165. if (isNaN(id) || id <= 0) {
  166. return false;
  167. }
  168. rationRepId = id;
  169. });
  170. // 导入原始数据确认
  171. $("#source-import,#data-import").click(function () {
  172. $.bootstrapLoading.start();
  173. const self = $(this);
  174. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  175. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  176. try {
  177. let formData = new FormData();
  178. let file = $("input[name='" + type + "']")[0];
  179. if (file.files.length <= 0) {
  180. throw '请选择文件!';
  181. }
  182. formData.append('file', file.files[0]);
  183. // 获取定额库id
  184. if (rationRepId <= 0) {
  185. return false;
  186. }
  187. formData.append('rationRepId', rationRepId);
  188. formData.append('type', type);
  189. $.ajax({
  190. url: '/rationRepository/api/upload',
  191. type: 'POST',
  192. data: formData,
  193. cache: false,
  194. contentType: false,
  195. processData: false,
  196. beforeSend: function () {
  197. self.attr('disabled', 'disabled');
  198. self.text('上传中...');
  199. },
  200. success: function (response) {
  201. self.removeAttr('disabled');
  202. self.text('确定导入');
  203. if (response.err === 0) {
  204. $.bootstrapLoading.end();
  205. const message = response.msg !== undefined ? response.msg : '';
  206. if (message !== '') {
  207. alert(message);
  208. }
  209. // 成功则关闭窗体
  210. dialog.modal("hide");
  211. } else {
  212. $.bootstrapLoading.end();
  213. const message = response.msg !== undefined ? response.msg : '上传失败!';
  214. alert(message);
  215. }
  216. },
  217. error: function () {
  218. $.bootstrapLoading.end();
  219. alert("与服务器通信发生错误");
  220. self.removeAttr('disabled');
  221. self.text('确定导入');
  222. }
  223. });
  224. } catch (error) {
  225. alert(error);
  226. }
  227. });
  228. // 导入章节数据确认
  229. $('#import-section-confirm').click(function () {
  230. $.bootstrapLoading.start();
  231. const self = $(this);
  232. const dialog = $('#section');
  233. try {
  234. const formData = new FormData();
  235. const file = $('input[name=section_file]')[0];
  236. if (file.files.length <= 0) {
  237. throw '请选择文件!';
  238. }
  239. formData.append('file', file.files[0]);
  240. formData.append('type', 'section_file');
  241. // 获取定额库id
  242. if (rationRepId <= 0) {
  243. return false;
  244. }
  245. formData.append('rationRepId', rationRepId);
  246. $.ajax({
  247. url: '/rationRepository/api/upload',
  248. type: 'POST',
  249. data: formData,
  250. cache: false,
  251. contentType: false,
  252. processData: false,
  253. beforeSend: function () {
  254. self.attr('disabled', 'disabled');
  255. self.text('上传中...');
  256. },
  257. success: function (response) {
  258. self.removeAttr('disabled');
  259. self.text('确定导入');
  260. if (response.err === 0) {
  261. $.bootstrapLoading.end();
  262. const message = response.msg !== undefined ? response.msg : '';
  263. if (message !== '') {
  264. alert(message);
  265. }
  266. // 成功则关闭窗体
  267. dialog.modal("hide");
  268. } else {
  269. $.bootstrapLoading.end();
  270. const message = response.msg !== undefined ? response.msg : '上传失败!';
  271. alert(message);
  272. }
  273. },
  274. error: function () {
  275. $.bootstrapLoading.end();
  276. alert("与服务器通信发生错误");
  277. self.removeAttr('disabled');
  278. self.text('确定导入');
  279. }
  280. });
  281. } catch (error) {
  282. alert(error);
  283. }
  284. });
  285. // 导出数据
  286. $("#showArea").on("click", ".export", function () {
  287. let id = $(this).data("id");
  288. id = parseInt(id);
  289. if (isNaN(id) || id <= 0) {
  290. return false;
  291. }
  292. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  293. });
  294. //设置补充定额库章节树模板
  295. $("#showArea").on("click", ".set-comple", function () {
  296. let id = $(this).data("id");
  297. id = parseInt(id);
  298. if (isNaN(id) || id <= 0) {
  299. return false;
  300. }
  301. rationRepId = id;
  302. $('#templateA').addClass('disabled');
  303. $('#template').modal('show');
  304. $('#compilations').empty();
  305. for (let data of compilationsArr) {
  306. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  307. $('#compilations').append($opt);
  308. }
  309. $('#compilations').change();
  310. });
  311. $('#compilations').change(function () {
  312. selCompilationId = $(this).select().val();
  313. CommonAjax.get(`/rationRepository/api/sectionTemplateCount/${selCompilationId}`, function (rstData) {
  314. rstData.data.count > 0 ?
  315. $('#templateText').text('该费用定额下已有定额章节树模板数据,是否确认覆盖数据?') :
  316. $('#templateText').text('确认是否将此库的章节树设置成该费用定额下补充定额章节树模板?');
  317. $('#templateA').removeClass('disabled');
  318. });
  319. });
  320. $('#templateA').click(function () {
  321. if (rationRepId <= 0 && selCompilationId) {
  322. return false;
  323. }
  324. $.bootstrapLoading.start();
  325. CommonAjax.post('/rationRepository/api/initSectionTemplate', { rationLibId: rationRepId, compilationId: selCompilationId }, function () {
  326. $.bootstrapLoading.end();
  327. $('#template').modal('hide');
  328. }, function () {
  329. $.bootstrapLoading.end();
  330. $('#template').modal('hide');
  331. });
  332. });
  333. });
  334. function getCompilationList(callback) {
  335. $.ajax({
  336. type: 'post',
  337. url: '/rationRepository/api/getCompilationList',
  338. dataType: 'json',
  339. success: function (result) {
  340. //addoptions
  341. for (let i = 0; i < result.data.compilation.length; i++) {
  342. let $option = $("<option >" + result.data.compilation[i].name + "</option>");
  343. $option.val(result.data.compilation[i]._id);
  344. $('#compilationSels').append($option);
  345. }
  346. //初始工料机库选项
  347. if (result.data.compilation.length > 0 && result.data.gljLibs.length > 0) {
  348. let compilationId = result.data.compilation[0]._id;
  349. //console.log(compilationId);
  350. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  351. for (let i = 0; i < gljLibOps.length; i++) {
  352. let $option = $("<option >" + gljLibOps[i].dispName + "</option>");
  353. $option.val(gljLibOps[i].ID);
  354. $('#gljLibSels').append($option);
  355. }
  356. }
  357. $('#compilationSels').on("change", function () {
  358. //刷新工料机库选项
  359. $('#gljLibSels').children().remove();
  360. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  361. for (let i = 0; i < newGljLibOps.length; i++) {
  362. let $option = $("<option >" + newGljLibOps[i].dispName + "</option>");
  363. $option.val(newGljLibOps[i].ID);
  364. $('#gljLibSels').append($option);
  365. }
  366. });
  367. callback(result.data);
  368. }
  369. });
  370. }
  371. function getGljLibOps(compilationId, gljLibs) {
  372. let rst = [];
  373. for (let i = 0; i < gljLibs.length; i++) {
  374. if (gljLibs[i]) {
  375. if (compilationId === gljLibs[i].compilationId) {
  376. rst.push(gljLibs[i]);
  377. }
  378. }
  379. }
  380. return rst;
  381. }
  382. function createRationLib(rationObj) {
  383. $.ajax({
  384. type: 'post',
  385. url: '/rationRepository/api/addRationRepository',
  386. data: { rationRepObj: JSON.stringify(rationObj) },
  387. dataType: 'json',
  388. success: function (result) {
  389. window.location.href = location.href;
  390. $('#cancelBtn').click();
  391. }
  392. })
  393. }
  394. function renameRationLib(renameObj) {
  395. $.ajax({
  396. type: 'post',
  397. url: '/rationRepository/api/editRationLibs',
  398. data: { oprtor: userAccount, renameObj: JSON.stringify(renameObj) },
  399. dataType: 'json',
  400. success: function (result) {
  401. if (!result.error) {
  402. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  403. $(jqSel).text(renameObj.newName);
  404. $(`#${renameObj.ID} td:eq(1)`).text(renameObj.newSerialNo || '');
  405. $(`#${renameObj.ID} td:eq(2)`).text(renameObj.newLibCode);
  406. let index = allNames.indexOf(renameObj.orgName);
  407. allNames.splice(index, 1);
  408. allNames.splice(index, 0, renameObj.newName);
  409. }
  410. $('#editCancelBtn').click();
  411. $('#renameText').val('');
  412. $('#renameCode').val('');
  413. $('#renameSerialNo').val('');
  414. }
  415. })
  416. }
  417. function removeRationLib(delObj) {
  418. $.bootstrapLoading.start();
  419. $.ajax({
  420. type: 'post',
  421. url: '/rationRepository/api/deleteRationLibs',
  422. data: { oprtor: userAccount, libId: delObj.libId },
  423. dataType: 'json',
  424. success: function (result) {
  425. if (!result.error) {
  426. var jqSel = "#" + delObj.libId;
  427. $(jqSel).remove();
  428. let index = allNames.indexOf(delObj.libName);
  429. allNames.splice(index, 1);
  430. $('#delCancelBtn').click();
  431. }
  432. $.bootstrapLoading.end();
  433. },
  434. error: function () {
  435. alert('删除失败');
  436. $.bootstrapLoading.end();
  437. }
  438. })
  439. }