main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. if (libName.trim().length === 0) {
  20. alert('名称不可为空!');
  21. $('#libNameTxt').val('')
  22. }
  23. else if (allNames.indexOf(libName) !== -1) {
  24. alert('此定额库已存在!');
  25. $('#libNameTxt').val('')
  26. }
  27. else if (compilationName.trim().length === 0) {
  28. alert('编办不可为空!');
  29. }
  30. else if (gljLibName.trim().length === 0) {
  31. alert("请选择工料机库!");
  32. }
  33. else {
  34. let newRationLib = {};
  35. newRationLib.dispName = libName;
  36. newRationLib.code = $('#libCodeTxt').val() || '';
  37. newRationLib.shortName = $('#libShortNameTxt').val() || '';
  38. newRationLib.compilationId = compilationId;
  39. newRationLib.compilationName = compilationName;
  40. newRationLib.gljLib = gljLibId;
  41. newRationLib.creator = userAccount;
  42. newRationLib.appType = "建筑";
  43. $('#libNameTxt').val('');
  44. $('#libCodeTxt').val('');
  45. $('#libShortNameTxt').val('');
  46. createRationLib(newRationLib);
  47. }
  48. });
  49. //重命名
  50. $("#showArea").on("click", "[data-target = '#edit']", function () {
  51. const $tr = $(this).parent().parent();
  52. let renameId = $tr.attr("id");
  53. $('#renameText').val($tr.find('td:first-child').text());
  54. $('#reCodeText').val($tr.find('td:nth-child(2)').text());
  55. $('#reShortNameText').val($tr.find('td:nth-child(3)').text());
  56. $("#renameA").attr("renameId", renameId);
  57. });
  58. $("#renameA").click(function () {
  59. let newName = $("#renameText").val();
  60. let libId = $(this).attr("renameId");
  61. let jqSel = "#" + libId + " td:first" + " a";
  62. let orgName = $(jqSel).text();
  63. if (newName.trim().length === 0) {
  64. alert("名称不可为空!");
  65. $("#renameText").val('');
  66. }
  67. else if (allNames.indexOf(newName) !== -1 && orgName !== newName) {
  68. alert("该定额库已存在!");
  69. $("#renameText").val('');
  70. }
  71. else {
  72. const code = $('#reCodeText').val();
  73. const shortName = $('#reShortNameText').val();
  74. renameRationLib({ ID: libId, newName: newName, orgName: orgName, code, shortName });
  75. }
  76. });
  77. $('#edit').on('shown.bs.modal', function () {
  78. setTimeout(function () {
  79. $('#renameText').focus();
  80. }, 100);
  81. });
  82. $('#add').on('shown.bs.modal', function () {
  83. setTimeout(function () {
  84. $('#libNameTxt').focus();
  85. }, 100);
  86. });
  87. $('#add').on('hidden.bs.modal', function () {
  88. $('#libNameTxt').val('');
  89. $('#libCodeTxt').val('');
  90. $('#libShortNameTxt').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. $("#source-import,#data-import").click(function () {
  163. $.bootstrapLoading.start();
  164. const self = $(this);
  165. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  166. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  167. try {
  168. let formData = new FormData();
  169. let file = $("input[name='" + type + "']")[0];
  170. if (file.files.length <= 0) {
  171. throw '请选择文件!';
  172. }
  173. formData.append('file', file.files[0]);
  174. // 获取定额库id
  175. if (rationRepId <= 0) {
  176. return false;
  177. }
  178. formData.append('rationRepId', rationRepId);
  179. formData.append('type', type);
  180. $.ajax({
  181. url: '/rationRepository/api/upload',
  182. type: 'POST',
  183. data: formData,
  184. cache: false,
  185. contentType: false,
  186. processData: false,
  187. beforeSend: function () {
  188. self.attr('disabled', 'disabled');
  189. self.text('上传中...');
  190. },
  191. success: function (response) {
  192. self.removeAttr('disabled');
  193. self.text('确定导入');
  194. if (response.err === 0) {
  195. $.bootstrapLoading.end();
  196. const message = response.msg !== undefined ? response.msg : '';
  197. if (message !== '') {
  198. alert(message);
  199. }
  200. // 成功则关闭窗体
  201. dialog.modal("hide");
  202. } else {
  203. $.bootstrapLoading.end();
  204. const message = response.msg !== undefined ? response.msg : '上传失败!';
  205. alert(message);
  206. }
  207. },
  208. error: function () {
  209. $.bootstrapLoading.end();
  210. alert("与服务器通信发生错误");
  211. self.removeAttr('disabled');
  212. self.text('确定导入');
  213. }
  214. });
  215. } catch (error) {
  216. alert(error);
  217. }
  218. });
  219. // 导出数据
  220. $("#showArea").on("click", ".export", function () {
  221. let id = $(this).data("id");
  222. id = parseInt(id);
  223. if (isNaN(id) || id <= 0) {
  224. return false;
  225. }
  226. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  227. });
  228. //设置补充定额库章节树模板
  229. $("#showArea").on("click", ".set-comple", function () {
  230. let id = $(this).data("id");
  231. id = parseInt(id);
  232. if (isNaN(id) || id <= 0) {
  233. return false;
  234. }
  235. rationRepId = id;
  236. $('#templateA').addClass('disabled');
  237. $('#template').modal('show');
  238. $('#compilations').empty();
  239. for (let data of compilationsArr) {
  240. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  241. $('#compilations').append($opt);
  242. }
  243. $('#compilations').change();
  244. });
  245. $('#compilations').change(function () {
  246. selCompilationId = $(this).select().val();
  247. CommonAjax.get(`/rationRepository/api/sectionTemplateCount/${selCompilationId}`, function (rstData) {
  248. rstData.data.count > 0 ?
  249. $('#templateText').text('该费用定额下已有定额章节树模板数据,是否确认覆盖数据?') :
  250. $('#templateText').text('确认是否将此库的章节树设置成该费用定额下补充定额章节树模板?');
  251. $('#templateA').removeClass('disabled');
  252. });
  253. });
  254. $('#templateA').click(function () {
  255. if (rationRepId <= 0 && selCompilationId) {
  256. return false;
  257. }
  258. $.bootstrapLoading.start();
  259. CommonAjax.post('/rationRepository/api/initSectionTemplate', { rationLibId: rationRepId, compilationId: selCompilationId }, function () {
  260. $.bootstrapLoading.end();
  261. $('#template').modal('hide');
  262. }, function () {
  263. $.bootstrapLoading.end();
  264. $('#template').modal('hide');
  265. });
  266. });
  267. });
  268. function getCompilationList(callback) {
  269. $.ajax({
  270. type: 'post',
  271. url: '/rationRepository/api/getCompilationList',
  272. dataType: 'json',
  273. success: function (result) {
  274. //addoptions
  275. for (let i = 0; i < result.data.compilation.length; i++) {
  276. let $option = $("<option >" + result.data.compilation[i].name + "</option>");
  277. $option.val(result.data.compilation[i]._id);
  278. $('#compilationSels').append($option);
  279. }
  280. //初始工料机库选项
  281. if (result.data.compilation.length > 0 && result.data.gljLibs.length > 0) {
  282. let compilationId = result.data.compilation[0]._id;
  283. //console.log(compilationId);
  284. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  285. for (let i = 0; i < gljLibOps.length; i++) {
  286. let $option = $("<option >" + gljLibOps[i].dispName + "</option>");
  287. $option.val(gljLibOps[i].ID);
  288. $('#gljLibSels').append($option);
  289. }
  290. }
  291. $('#compilationSels').on("change", function () {
  292. //刷新工料机库选项
  293. $('#gljLibSels').children().remove();
  294. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  295. for (let i = 0; i < newGljLibOps.length; i++) {
  296. let $option = $("<option >" + newGljLibOps[i].dispName + "</option>");
  297. $option.val(newGljLibOps[i].ID);
  298. $('#gljLibSels').append($option);
  299. }
  300. });
  301. callback(result.data);
  302. }
  303. });
  304. }
  305. function getGljLibOps(compilationId, gljLibs) {
  306. let rst = [];
  307. for (let i = 0; i < gljLibs.length; i++) {
  308. if (gljLibs[i]) {
  309. if (compilationId === gljLibs[i].compilationId) {
  310. rst.push(gljLibs[i]);
  311. }
  312. }
  313. }
  314. return rst;
  315. }
  316. function createRationLib(rationObj) {
  317. $.ajax({
  318. type: 'post',
  319. url: '/rationRepository/api/addRationRepository',
  320. data: { rationRepObj: JSON.stringify(rationObj) },
  321. dataType: 'json',
  322. success: function (result) {
  323. window.location.href = location.href;
  324. }
  325. })
  326. }
  327. function renameRationLib(renameObj) {
  328. $.ajax({
  329. type: 'post',
  330. url: '/rationRepository/api/editRationLibs',
  331. data: { oprtor: userAccount, renameObj: JSON.stringify(renameObj) },
  332. dataType: 'json',
  333. success: function (result) {
  334. if (!result.error) {
  335. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  336. $(jqSel).text(renameObj.newName);
  337. const $code = $(`#${renameObj.ID} td:nth-child(2)`);
  338. $code.text(renameObj.code);
  339. const $shortName = $(`#${renameObj.ID} td:nth-child(3)`);
  340. $shortName.text(renameObj.shortName);
  341. let index = allNames.indexOf(renameObj.orgName);
  342. allNames.splice(index, 1);
  343. allNames.splice(index, 0, renameObj.newName);
  344. }
  345. $('#editCancelBtn').click();
  346. $('#renameText').val('');
  347. }
  348. })
  349. }
  350. function removeRationLib(delObj) {
  351. $.bootstrapLoading.start();
  352. $.ajax({
  353. type: 'post',
  354. url: '/rationRepository/api/deleteRationLibs',
  355. data: { oprtor: userAccount, libId: delObj.libId },
  356. dataType: 'json',
  357. success: function (result) {
  358. if (!result.error) {
  359. var jqSel = "#" + delObj.libId;
  360. $(jqSel).remove();
  361. let index = allNames.indexOf(delObj.libName);
  362. allNames.splice(index, 1);
  363. $('#delCancelBtn').click();
  364. }
  365. $.bootstrapLoading.end();
  366. },
  367. error: function () {
  368. alert('删除失败');
  369. $.bootstrapLoading.end();
  370. }
  371. })
  372. }