main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /**
  2. * Created by Zhong on 2017/8/14.
  3. */
  4. $(function () {
  5. let dispNameArr;
  6. let compilationsArr;
  7. let usedCom;
  8. let deleteCount = 0;
  9. let preDeleteId = null;
  10. let selCompilationId;
  11. $('#del').on('hidden.bs.modal', function () {
  12. deleteCount = 0;
  13. });
  14. getCompilationList(function (datas) {
  15. compilationsArr = datas;
  16. getAllGljLib(function (dispNames, compilationsUsedArr) {
  17. usedCom = compilationsUsedArr;
  18. dispNameArr = dispNames;
  19. //添加
  20. $('#addBtn').click(function () {
  21. let compilationName = $('#compilationSels option:selected').text();
  22. let compilationId = $('#compilationSels option:selected').val();
  23. let libName = $('#libNameTxt').val();
  24. if(libName.trim().length === 0){
  25. alert('名称不可为空!');
  26. $('#libNameTxt').val('')
  27. }
  28. else if(compilationsUsedArr.indexOf(compilationId) !== -1){// compilationsUsedArr;//在该编办下已建库,限制一个编办只能有一个库
  29. alert('该编办已被绑定!');
  30. $('#libNameTxt').val('')
  31. }
  32. else if(dispNames.indexOf(libName) !== -1){
  33. alert('此人材机库已存在!');
  34. $('#libNameTxt').val('')
  35. }
  36. else if(compilationName.trim().length === 0){
  37. alert('编办不可为空!');
  38. }
  39. else{
  40. let newGljLib = {};
  41. newGljLib.dispName = libName;
  42. newGljLib.compilationId = compilationId;
  43. newGljLib.compilationName = compilationName;
  44. newGljLib.creator = oprtor;
  45. newGljLib.appType = "建筑";
  46. $('#libNameTxt').val('');
  47. createGljLib(newGljLib, dispNameArr, usedCom);
  48. }
  49. });
  50. $('#edit').on('shown.bs.modal', function () {
  51. setTimeout(function () {
  52. $('#renameText').focus();
  53. }, 100);
  54. });
  55. $('#add').on('shown.bs.modal', function () {
  56. setTimeout(function () {
  57. $('#libNameTxt').focus();
  58. }, 100);
  59. });
  60. $('#add').on('hidden.bs.modal', function () {
  61. $('#libNameTxt').val('');
  62. });
  63. //重命名
  64. $("#showArea").on("click", "[data-target = '#edit']", function(){
  65. let renameId = $(this).parent().parent().attr("id");
  66. let compilationName = $(this).parent().parent().children()[1].textContent;
  67. $('#compilationEdit option').text(compilationName);
  68. $('#renameText').val($(this).parent().parent().find('td:first-child').text());
  69. $("#renameA").attr("renameId", renameId);
  70. });
  71. $("#renameA").click(function(){
  72. let newName = $("#renameText").val();
  73. let libId = $(this).attr("renameId");
  74. let jqSel = "#" + libId + " td:first" + " a";
  75. let orgName = $(jqSel).text();
  76. if(newName.trim().length === 0){
  77. alert("名称不可为空!");
  78. $("#renameText").val('');
  79. }
  80. else if(dispNameArr.indexOf(newName) !== -1){
  81. alert("该人材机库已存在!");
  82. $("#renameText").val('');
  83. }
  84. else{
  85. renameGljLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  86. }
  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. let compilationName = $("#" + deleteId + " td:eq(1)").text();
  105. if(deleteCount === 3){
  106. for(let i = 0, len = compilationsArr.length; i < len; i++){
  107. if(compilationsArr[i].name === compilationName.trim()){
  108. usedCom.splice(usedCom.indexOf(compilationsArr[i]._id), 1);
  109. break;
  110. }
  111. }
  112. removeGljLib({libId: deleteId, libName: libName}, dispNameArr);
  113. deleteCount = 0;
  114. }
  115. });
  116. // 锁定、解锁
  117. $('#showArea').on('click', '.lock', function () {
  118. lockUtil.handleLockClick($(this));
  119. });
  120. });
  121. });
  122. let selLibId = -1;
  123. const importType = {
  124. price: 1,
  125. component: 2
  126. };
  127. let importAction;
  128. $("#showArea").on("click", ".import-data", function () {
  129. importAction = importType.price;
  130. let id = $(this).data("id");
  131. id = parseInt(id);
  132. if (isNaN(id) || id <= 0) {
  133. return false;
  134. }
  135. selLibId = id;
  136. $("#import").modal("show");
  137. });
  138. $("#showArea").on("click", ".import-components", function () {
  139. importAction = importType.component;
  140. let id = $(this).data("id");
  141. id = parseInt(id);
  142. if (isNaN(id) || id <= 0) {
  143. return false;
  144. }
  145. selLibId = id;
  146. $("#import").modal("show");
  147. });
  148. function importExcel(url) {
  149. $.bootstrapLoading.start();
  150. const self = $(this);
  151. console.log(self);
  152. try {
  153. let formData = new FormData();
  154. let file = $("input[name='import_data']")[0];
  155. if (file.files.length <= 0) {
  156. throw '请选择文件!';
  157. }
  158. formData.append('file', file.files[0]);
  159. // 获取人材机库id
  160. if (selLibId <= 0) {
  161. return false;
  162. }
  163. formData.append('gljLibId', selLibId);
  164. $.ajax({
  165. url: url,
  166. type: 'POST',
  167. data: formData,
  168. cache: false,
  169. contentType: false,
  170. processData: false,
  171. beforeSend: function() {
  172. self.attr('disabled', 'disabled');
  173. self.text('上传中...');
  174. },
  175. success: function(response){
  176. self.removeAttr('disabled');
  177. self.text('确定导入');
  178. if (response.err === 0) {
  179. $.bootstrapLoading.end();
  180. const message = response.msg !== undefined ? response.msg : '';
  181. if (message !== '') {
  182. alert(message);
  183. }
  184. // 成功则关闭窗体
  185. $('#import').modal("hide");
  186. } else {
  187. $.bootstrapLoading.end();
  188. const message = response.msg !== undefined ? response.msg : '上传失败!';
  189. alert(message);
  190. }
  191. },
  192. error: function(){
  193. $.bootstrapLoading.end();
  194. alert("与服务器通信发生错误");
  195. self.removeAttr('disabled');
  196. self.text('确定导入');
  197. }
  198. });
  199. } catch(error) {
  200. alert(error);
  201. $.bootstrapLoading.end();
  202. }
  203. }
  204. //导入数据
  205. $("#data-import").click(function() {
  206. // 导入单价
  207. if (importAction === importType.price) {
  208. importExcel.call(this, '/stdGljRepository/api/importPrice');
  209. } else {
  210. // 导入组成物
  211. importExcel.call(this, '/stdGljRepository/api/importComponents');
  212. }
  213. });
  214. //设置补充人材机库分类树模板
  215. $("#showArea").on("click", ".set-comple", function () {
  216. let id = $(this).data("id");
  217. id = parseInt(id);
  218. if (isNaN(id) || id <= 0) {
  219. return false;
  220. }
  221. selLibId = id;
  222. $('#templateA').addClass('disabled');
  223. $('#template').modal('show');
  224. $('#compilations').empty();
  225. for (let data of compilationsArr) {
  226. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  227. $('#compilations').append($opt);
  228. }
  229. $('#compilations').change();
  230. });
  231. $('#compilations').change(function () {
  232. selCompilationId = $(this).select().val();
  233. CommonAjax.get(`api/classTemplateCount/${selCompilationId}`, function (rstData) {
  234. rstData.data.count > 0 ?
  235. $('#templateText').text('该费用定额下已有人材机分类树模板数据,是否确认覆盖数据?') :
  236. $('#templateText').text('确认是否将此库的分类树设置成该费用定额下补充人材机分类树模板?');
  237. $('#templateA').removeClass('disabled');
  238. });
  239. });
  240. $('#templateA').click(function () {
  241. if (selLibId <= 0 && selCompilationId) {
  242. return false;
  243. }
  244. $.bootstrapLoading.start();
  245. CommonAjax.post('api/initClassTemplate', {gljLibId: selLibId, compilationId: selCompilationId}, function () {
  246. $.bootstrapLoading.end();
  247. $('#template').modal('hide');
  248. }, function () {
  249. $.bootstrapLoading.end();
  250. $('#template').modal('hide');
  251. });
  252. });
  253. });
  254. function getAllGljLib(callback){
  255. $.ajax({
  256. type: 'post',
  257. url: 'api/getAllGljLib',
  258. dataType: 'json',
  259. success: function (result) {
  260. let dispNames = [];
  261. let compilationsUsedArr = [];
  262. if(result.data.length > 0){
  263. for(let i = 0; i < result.data.length; i++){
  264. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  265. let id = result.data[i].ID;
  266. let libName = result.data[i].dispName;
  267. let createDate = result.data[i].createDate.split(' ')[0];
  268. let compilationName = result.data[i].compilationName;
  269. let compilationId = result.data[i].compilationId;
  270. let rationLibs = result.data[i].rationLibs;
  271. let rationLibsName = '';
  272. for(let j = 0; j < rationLibs.length; j++){
  273. rationLibsName += rationLibs[j].dispName + "</br>";
  274. }
  275. dispNames.push(result.data[i].dispName);
  276. $("#showArea").append(
  277. "<tr id='"+id+"' data-compilationId='"+ compilationId + "'>" +
  278. "<td><a href='/stdGljRepository/glj?gljLibId="+id+"&locked=true'>"+libName+"</a></td>" +
  279. "<td>"+compilationName+" </td>" +
  280. "<td>"+rationLibsName+" </td>" +
  281. "<td>"+createDate+" </td>" +
  282. "<td><a class='lock-btn-control disabled' href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  283. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger lock-btn-control disabled' title='删除'>" +
  284. "<i class='fa fa-remove'></i></a> " +
  285. "<a class='lock' data-locked='true' href='javascript:void(0);' title='解锁'><i class='fa fa-unlock-alt'></i></a>" +
  286. "</td>" +
  287. "<td><a class='btn btn-secondary btn-sm import-data lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='导入数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  288. "<td><a class='btn btn-secondary btn-sm import-components lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  289. "<td><a class='btn btn-secondary btn-sm set-comple lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='将分类树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
  290. "</tr>");
  291. }
  292. }
  293. callback(dispNames, compilationsUsedArr);
  294. }
  295. });
  296. }
  297. function getCompilationList(callback){
  298. $.ajax({
  299. type: 'post',
  300. url: 'api/getCompilationList',
  301. dataType: 'json',
  302. success: function (result) {
  303. for(let i = 0; i < result.data.length; i++){
  304. let $option = $("<option >"+ result.data[i].name +"</option>");
  305. $option.val( result.data[i]._id);
  306. $('#compilationSels').append($option);
  307. }
  308. $('#compilationSels').on("change", function () {
  309. });
  310. callback(result.data);
  311. }
  312. });
  313. }
  314. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  315. $.ajax({
  316. type: 'post',
  317. url: 'api/createGljLib',
  318. data: {gljLibObj: JSON.stringify(gljLibObj)},
  319. dataType: 'json',
  320. success: function (result) {
  321. if(result.data){
  322. let id = result.data.ID;
  323. let libName = result.data.dispName;
  324. let createDate = result.data.createDate.split(' ')[0];
  325. let compilationName = result.data.compilationName;
  326. dispNamesArr.push(libName);
  327. usedCom.push(gljLibObj.compilationId);
  328. $("#showArea").append(
  329. "<tr id='"+id+"' data-compilationId='"+ gljLibObj.compilationId + "'>" +
  330. "<td><a href='/stdGljRepository/glj?gljLibId="+id+"&locked=true'>"+libName+"</a></td>" +
  331. "<td>"+compilationName+" </td>" +
  332. "<td>"+''+" </td>" +
  333. "<td>"+createDate+" </td>" +
  334. "<td><a class='lock-btn-control disabled' href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  335. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger lock-btn-control disabled' title='删除'>" +
  336. "<i class='fa fa-remove'></i></a>" +
  337. "<a class='lock' data-locked='true' href='javascript:void(0);' title='解锁'><i class='fa fa-unlock-alt'></i></a>" +
  338. "</td>" +
  339. "<td><a class='btn btn-secondary btn-sm import-data lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='导入数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  340. "<td><a class='btn btn-secondary btn-sm import-components lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  341. "<td><a class='btn btn-secondary btn-sm set-comple lock-btn-control disabled' href='javacript:void(0);' data-id='"+ id +"' title='将分类树设为补充模板数据'><i class='fa fa-sign-in fa-rotate-90'></i>设置</a></td>" +
  342. "</tr>");
  343. }
  344. $('#cancelBtn').click();
  345. }
  346. })
  347. }
  348. function renameGljLib(renameObj, dispNames){
  349. $.ajax({
  350. type: 'post',
  351. url: 'api/renameGljLib',
  352. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  353. dataType: 'json',
  354. success: function (result) {
  355. if(!result.error){
  356. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  357. $(jqSel).text(renameObj.newName);
  358. let index = dispNames.indexOf(renameObj.orgName);
  359. dispNames.splice(index, 1);
  360. dispNames.splice(index, 0, renameObj.newName);
  361. }
  362. $('#editCancelBtn').click();
  363. $('#renameText').val('');
  364. }
  365. })
  366. }
  367. function removeGljLib(delObj, dispNames){
  368. $.bootstrapLoading.start();
  369. $.ajax({
  370. type: 'post',
  371. url: 'api/removeGljLib',
  372. data: {oprtor: oprtor, libId: delObj.libId},
  373. dataType: 'json',
  374. success: function (result) {
  375. if(!result.error){
  376. if(result.message === '此人材机库已被引用!'){
  377. $('#delCancelBtn').click();
  378. alert("此人材机库已被引用,不可删除!");
  379. }
  380. else if(result.message === '删除成功'){
  381. var jqSel = "#"+ delObj.libId;
  382. $(jqSel).remove();
  383. let index = dispNames.indexOf(delObj.libName);
  384. dispNames.splice(index, 1);
  385. $('#delCancelBtn').click();
  386. }
  387. }
  388. $.bootstrapLoading.end();
  389. },
  390. error: function () {
  391. alert('服务器出错!');
  392. $.bootstrapLoading.end();
  393. }
  394. })
  395. }