main.js 16 KB

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