main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // 锁定、解锁
  130. $('#showArea').on('click', '.lock', function () {
  131. lockUtil.handleLockClick($(this));
  132. });
  133. getCompilationList(function (data) {
  134. compilationsArr = data.compilation;
  135. });
  136. // 导入原始数据按钮
  137. let rationRepId = 0;
  138. $("#showArea").on("click", ".import-source", function () {
  139. let id = $(this).data("id");
  140. id = parseInt(id);
  141. if (isNaN(id) || id <= 0) {
  142. return false;
  143. }
  144. rationRepId = id;
  145. $("#import").modal("show");
  146. });
  147. // 导入内部数据
  148. $("#showArea").on("click", ".import-data", function () {
  149. let id = $(this).data("id");
  150. id = parseInt(id);
  151. if (isNaN(id) || id <= 0) {
  152. return false;
  153. }
  154. rationRepId = id;
  155. $("#import2").modal("show");
  156. });
  157. // 导入章节数据
  158. $('#showArea').on('click', '.import-section', function () {
  159. let id = $(this).data('id');
  160. id = parseInt(id);
  161. if (isNaN(id) || id <= 0) {
  162. return false;
  163. }
  164. rationRepId = id;
  165. });
  166. // 导入原始数据确认
  167. $("#source-import,#data-import").click(function() {
  168. $.bootstrapLoading.start();
  169. const self = $(this);
  170. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  171. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  172. try {
  173. let formData = new FormData();
  174. let file = $("input[name='"+ type +"']")[0];
  175. if (file.files.length <= 0) {
  176. throw '请选择文件!';
  177. }
  178. formData.append('file', file.files[0]);
  179. // 获取定额库id
  180. if (rationRepId <= 0) {
  181. return false;
  182. }
  183. formData.append('rationRepId', rationRepId);
  184. formData.append('type', type);
  185. $.ajax({
  186. url: '/rationRepository/api/upload',
  187. type: 'POST',
  188. data: formData,
  189. cache: false,
  190. contentType: false,
  191. processData: false,
  192. beforeSend: function() {
  193. self.attr('disabled', 'disabled');
  194. self.text('上传中...');
  195. },
  196. success: function(response){
  197. self.removeAttr('disabled');
  198. self.text('确定导入');
  199. if (response.err === 0) {
  200. $.bootstrapLoading.end();
  201. const message = response.msg !== undefined ? response.msg : '';
  202. if (message !== '') {
  203. alert(message);
  204. }
  205. // 成功则关闭窗体
  206. dialog.modal("hide");
  207. } else {
  208. $.bootstrapLoading.end();
  209. const message = response.msg !== undefined ? response.msg : '上传失败!';
  210. alert(message);
  211. }
  212. },
  213. error: function(){
  214. $.bootstrapLoading.end();
  215. alert("与服务器通信发生错误");
  216. self.removeAttr('disabled');
  217. self.text('确定导入');
  218. }
  219. });
  220. } catch(error) {
  221. alert(error);
  222. }
  223. });
  224. // 导入章节数据确认
  225. $('#import-section-confirm').click(function() {
  226. $.bootstrapLoading.start();
  227. const self = $(this);
  228. const dialog = $('#section');
  229. try {
  230. const formData = new FormData();
  231. const file = $('input[name=section_file]')[0];
  232. if (file.files.length <= 0) {
  233. throw '请选择文件!';
  234. }
  235. formData.append('file', file.files[0]);
  236. formData.append('type', 'section_file');
  237. // 获取定额库id
  238. if (rationRepId <= 0) {
  239. return false;
  240. }
  241. formData.append('rationRepId', rationRepId);
  242. $.ajax({
  243. url: '/rationRepository/api/upload',
  244. type: 'POST',
  245. data: formData,
  246. cache: false,
  247. contentType: false,
  248. processData: false,
  249. beforeSend: function() {
  250. self.attr('disabled', 'disabled');
  251. self.text('上传中...');
  252. },
  253. success: function(response){
  254. self.removeAttr('disabled');
  255. self.text('确定导入');
  256. if (response.err === 0) {
  257. $.bootstrapLoading.end();
  258. const message = response.msg !== undefined ? response.msg : '';
  259. if (message !== '') {
  260. alert(message);
  261. }
  262. // 成功则关闭窗体
  263. dialog.modal("hide");
  264. } else {
  265. $.bootstrapLoading.end();
  266. const message = response.msg !== undefined ? response.msg : '上传失败!';
  267. alert(message);
  268. }
  269. },
  270. error: function(){
  271. $.bootstrapLoading.end();
  272. alert("与服务器通信发生错误");
  273. self.removeAttr('disabled');
  274. self.text('确定导入');
  275. }
  276. });
  277. } catch(error) {
  278. alert(error);
  279. }
  280. });
  281. // 导出数据
  282. $("#showArea").on("click", ".export", function () {
  283. let id = $(this).data("id");
  284. id = parseInt(id);
  285. if (isNaN(id) || id <= 0) {
  286. return false;
  287. }
  288. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  289. });
  290. //设置补充定额库章节树模板
  291. $("#showArea").on("click", ".set-comple", function () {
  292. let id = $(this).data("id");
  293. id = parseInt(id);
  294. if (isNaN(id) || id <= 0) {
  295. return false;
  296. }
  297. rationRepId = id;
  298. $('#templateA').addClass('disabled');
  299. $('#template').modal('show');
  300. $('#compilations').empty();
  301. for (let data of compilationsArr) {
  302. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  303. $('#compilations').append($opt);
  304. }
  305. $('#compilations').change();
  306. });
  307. $('#compilations').change(function () {
  308. selCompilationId = $(this).select().val();
  309. CommonAjax.get(`/rationRepository/api/sectionTemplateCount/${selCompilationId}`, function (rstData) {
  310. rstData.data.count > 0 ?
  311. $('#templateText').text('该费用定额下已有定额章节树模板数据,是否确认覆盖数据?') :
  312. $('#templateText').text('确认是否将此库的章节树设置成该费用定额下补充定额章节树模板?');
  313. $('#templateA').removeClass('disabled');
  314. });
  315. });
  316. $('#templateA').click(function () {
  317. if (rationRepId <= 0 && selCompilationId) {
  318. return false;
  319. }
  320. $.bootstrapLoading.start();
  321. CommonAjax.post('/rationRepository/api/initSectionTemplate', {rationLibId: rationRepId, compilationId: selCompilationId}, function () {
  322. $.bootstrapLoading.end();
  323. $('#template').modal('hide');
  324. }, function () {
  325. $.bootstrapLoading.end();
  326. $('#template').modal('hide');
  327. });
  328. });
  329. });
  330. function getCompilationList(callback){
  331. $.ajax({
  332. type: 'post',
  333. url: '/rationRepository/api/getCompilationList',
  334. dataType: 'json',
  335. success: function (result) {
  336. //addoptions
  337. for(let i = 0; i < result.data.compilation.length; i++){
  338. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  339. $option.val( result.data.compilation[i]._id);
  340. $('#compilationSels').append($option);
  341. }
  342. //初始工料机库选项
  343. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  344. let compilationId = result.data.compilation[0]._id;
  345. //console.log(compilationId);
  346. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  347. for(let i = 0; i < gljLibOps.length; i++){
  348. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  349. $option.val(gljLibOps[i].ID);
  350. $('#gljLibSels').append($option);
  351. }
  352. }
  353. $('#compilationSels').on("change", function () {
  354. //刷新工料机库选项
  355. $('#gljLibSels').children().remove();
  356. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  357. for(let i = 0; i < newGljLibOps.length; i++){
  358. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  359. $option.val(newGljLibOps[i].ID);
  360. $('#gljLibSels').append($option);
  361. }
  362. });
  363. callback(result.data);
  364. }
  365. });
  366. }
  367. function getGljLibOps(compilationId, gljLibs){
  368. let rst = [];
  369. for(let i = 0; i < gljLibs.length; i++){
  370. if(gljLibs[i]){
  371. if(compilationId === gljLibs[i].compilationId){
  372. rst.push(gljLibs[i]);
  373. }
  374. }
  375. }
  376. return rst;
  377. }
  378. function createRationLib(rationObj){
  379. $.ajax({
  380. type: 'post',
  381. url: '/rationRepository/api/addRationRepository',
  382. data: {rationRepObj: JSON.stringify(rationObj)},
  383. dataType: 'json',
  384. success: function (result) {
  385. window.location.href = location.href;
  386. $('#cancelBtn').click();
  387. }
  388. })
  389. }
  390. function renameRationLib(renameObj){
  391. $.ajax({
  392. type: 'post',
  393. url: '/rationRepository/api/editRationLibs',
  394. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  395. dataType: 'json',
  396. success: function (result) {
  397. if(!result.error){
  398. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  399. $(jqSel).text(renameObj.newName);
  400. $(`#${renameObj.ID} td:eq(1)`).text(renameObj.newLibCode);
  401. let index = allNames.indexOf(renameObj.orgName);
  402. allNames.splice(index, 1);
  403. allNames.splice(index, 0, renameObj.newName);
  404. }
  405. $('#editCancelBtn').click();
  406. $('#renameText').val('');
  407. $('#renameCode').val('');
  408. }
  409. })
  410. }
  411. function removeRationLib(delObj){
  412. $.bootstrapLoading.start();
  413. $.ajax({
  414. type: 'post',
  415. url: '/rationRepository/api/deleteRationLibs',
  416. data: {oprtor: userAccount, libId: delObj.libId},
  417. dataType: 'json',
  418. success: function (result) {
  419. if(!result.error){
  420. var jqSel = "#"+ delObj.libId;
  421. $(jqSel).remove();
  422. let index = allNames.indexOf(delObj.libName);
  423. allNames.splice(index, 1);
  424. $('#delCancelBtn').click();
  425. }
  426. $.bootstrapLoading.end();
  427. },
  428. error: function () {
  429. alert('删除失败');
  430. $.bootstrapLoading.end();
  431. }
  432. })
  433. }