main.js 19 KB

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