main.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. });
  118. let selLibId = -1;
  119. const importType = {
  120. price: 1,
  121. component: 2
  122. };
  123. let importAction;
  124. $("#showArea").on("click", ".import-data", function () {
  125. importAction = importType.price;
  126. let id = $(this).data("id");
  127. id = parseInt(id);
  128. if (isNaN(id) || id <= 0) {
  129. return false;
  130. }
  131. selLibId = id;
  132. $("#import").modal("show");
  133. });
  134. $("#showArea").on("click", ".import-components", function () {
  135. importAction = importType.component;
  136. let id = $(this).data("id");
  137. id = parseInt(id);
  138. if (isNaN(id) || id <= 0) {
  139. return false;
  140. }
  141. selLibId = id;
  142. $("#import").modal("show");
  143. });
  144. function importExcel(url) {
  145. $.bootstrapLoading.start();
  146. const self = $(this);
  147. console.log(self);
  148. try {
  149. let formData = new FormData();
  150. let file = $("input[name='import_data']")[0];
  151. if (file.files.length <= 0) {
  152. throw '请选择文件!';
  153. }
  154. formData.append('file', file.files[0]);
  155. // 获取人材机库id
  156. if (selLibId <= 0) {
  157. return false;
  158. }
  159. formData.append('gljLibId', selLibId);
  160. $.ajax({
  161. url: url,
  162. type: 'POST',
  163. data: formData,
  164. cache: false,
  165. contentType: false,
  166. processData: false,
  167. beforeSend: function() {
  168. self.attr('disabled', 'disabled');
  169. self.text('上传中...');
  170. },
  171. success: function(response){
  172. self.removeAttr('disabled');
  173. self.text('确定导入');
  174. if (response.err === 0) {
  175. $.bootstrapLoading.end();
  176. const message = response.msg !== undefined ? response.msg : '';
  177. if (message !== '') {
  178. alert(message);
  179. }
  180. // 成功则关闭窗体
  181. $('#import').modal("hide");
  182. } else {
  183. $.bootstrapLoading.end();
  184. const message = response.msg !== undefined ? response.msg : '上传失败!';
  185. alert(message);
  186. }
  187. },
  188. error: function(){
  189. $.bootstrapLoading.end();
  190. alert("与服务器通信发生错误");
  191. self.removeAttr('disabled');
  192. self.text('确定导入');
  193. }
  194. });
  195. } catch(error) {
  196. alert(error);
  197. $.bootstrapLoading.end();
  198. }
  199. }
  200. //导入数据
  201. $("#data-import").click(function() {
  202. // 导入单价
  203. if (importAction === importType.price) {
  204. importExcel.call(this, '/stdGljRepository/api/importPrice');
  205. } else {
  206. // 导入组成物
  207. importExcel.call(this, '/stdGljRepository/api/importComponents');
  208. }
  209. });
  210. //设置补充人材机库分类树模板
  211. $("#showArea").on("click", ".set-comple", function () {
  212. let id = $(this).data("id");
  213. id = parseInt(id);
  214. if (isNaN(id) || id <= 0) {
  215. return false;
  216. }
  217. selLibId = id;
  218. $('#templateA').addClass('disabled');
  219. $('#template').modal('show');
  220. $('#compilations').empty();
  221. for (let data of compilationsArr) {
  222. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  223. $('#compilations').append($opt);
  224. }
  225. $('#compilations').change();
  226. });
  227. $('#compilations').change(function () {
  228. selCompilationId = $(this).select().val();
  229. CommonAjax.get(`api/classTemplateCount/${selCompilationId}`, function (rstData) {
  230. rstData.data.count > 0 ?
  231. $('#templateText').text('该费用定额下已有人材机分类树模板数据,是否确认覆盖数据?') :
  232. $('#templateText').text('确认是否将此库的分类树设置成该费用定额下补充人材机分类树模板?');
  233. $('#templateA').removeClass('disabled');
  234. });
  235. });
  236. $('#templateA').click(function () {
  237. if (selLibId <= 0 && selCompilationId) {
  238. return false;
  239. }
  240. $.bootstrapLoading.start();
  241. CommonAjax.post('api/initClassTemplate', {gljLibId: selLibId, compilationId: selCompilationId}, function () {
  242. $.bootstrapLoading.end();
  243. $('#template').modal('hide');
  244. }, function () {
  245. $.bootstrapLoading.end();
  246. $('#template').modal('hide');
  247. });
  248. });
  249. });
  250. function getAllGljLib(callback){
  251. $.ajax({
  252. type: 'post',
  253. url: 'api/getAllGljLib',
  254. dataType: 'json',
  255. success: function (result) {
  256. let dispNames = [];
  257. let compilationsUsedArr = [];
  258. if(result.data.length > 0){
  259. for(let i = 0; i < result.data.length; i++){
  260. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  261. let id = result.data[i].ID;
  262. let libName = result.data[i].dispName;
  263. let createDate = result.data[i].createDate.split(' ')[0];
  264. let compilationName = result.data[i].compilationName;
  265. let compilationId = result.data[i].compilationId;
  266. let rationLibs = result.data[i].rationLibs;
  267. let rationLibsName = '';
  268. for(let j = 0; j < rationLibs.length; j++){
  269. rationLibsName += rationLibs[j].dispName + "</br>";
  270. }
  271. dispNames.push(result.data[i].dispName);
  272. $("#showArea").append(
  273. "<tr id='"+id+"' data-compilationId='"+ compilationId + "'>" +
  274. "<td><a href='/stdGljRepository/glj?gljLibId="+id+"'>"+libName+"</a></td>" +
  275. "<td>"+compilationName+" </td>" +
  276. "<td>"+rationLibsName+" </td>" +
  277. "<td>"+createDate+" </td>" +
  278. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  279. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  280. "<i class='fa fa-remove'></i></a></td>" +
  281. "<td><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>" +
  282. "<td><a class='btn btn-secondary btn-sm import-components' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  283. "<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>" +
  284. "</tr>");
  285. }
  286. }
  287. callback(dispNames, compilationsUsedArr);
  288. }
  289. });
  290. }
  291. function getCompilationList(callback){
  292. $.ajax({
  293. type: 'post',
  294. url: 'api/getCompilationList',
  295. dataType: 'json',
  296. success: function (result) {
  297. for(let i = 0; i < result.data.length; i++){
  298. let $option = $("<option >"+ result.data[i].name +"</option>");
  299. $option.val( result.data[i]._id);
  300. $('#compilationSels').append($option);
  301. }
  302. $('#compilationSels').on("change", function () {
  303. });
  304. callback(result.data);
  305. }
  306. });
  307. }
  308. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  309. $.ajax({
  310. type: 'post',
  311. url: 'api/createGljLib',
  312. data: {gljLibObj: JSON.stringify(gljLibObj)},
  313. dataType: 'json',
  314. success: function (result) {
  315. if(result.data){
  316. let id = result.data.ID;
  317. let libName = result.data.dispName;
  318. let createDate = result.data.createDate.split(' ')[0];
  319. let compilationName = result.data.compilationName;
  320. dispNamesArr.push(libName);
  321. usedCom.push(gljLibObj.compilationId);
  322. $("#showArea").append(
  323. "<tr id='"+id+"' data-compilationId='"+ gljLibObj.compilationId + "'>" +
  324. "<td><a href='/stdGljRepository/glj?gljLibId="+id+"'>"+libName+"</a></td>" +
  325. "<td>"+compilationName+" </td>" +
  326. "<td>"+''+" </td>" +
  327. "<td>"+createDate+" </td>" +
  328. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  329. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  330. "<i class='fa fa-remove'></i></a></td>" +
  331. "<td><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>" +
  332. "<td><a class='btn btn-secondary btn-sm import-components' href='javacript:void(0);' data-id='"+ id +"' title='导入组成物'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  333. "<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>" +
  334. "</tr>");
  335. }
  336. $('#cancelBtn').click();
  337. }
  338. })
  339. }
  340. function renameGljLib(renameObj, dispNames){
  341. $.ajax({
  342. type: 'post',
  343. url: 'api/renameGljLib',
  344. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  345. dataType: 'json',
  346. success: function (result) {
  347. if(!result.error){
  348. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  349. $(jqSel).text(renameObj.newName);
  350. let index = dispNames.indexOf(renameObj.orgName);
  351. dispNames.splice(index, 1);
  352. dispNames.splice(index, 0, renameObj.newName);
  353. }
  354. $('#editCancelBtn').click();
  355. $('#renameText').val('');
  356. }
  357. })
  358. }
  359. function removeGljLib(delObj, dispNames){
  360. $.bootstrapLoading.start();
  361. $.ajax({
  362. type: 'post',
  363. url: 'api/removeGljLib',
  364. data: {oprtor: oprtor, libId: delObj.libId},
  365. dataType: 'json',
  366. success: function (result) {
  367. if(!result.error){
  368. if(result.message === '此人材机库已被引用!'){
  369. $('#delCancelBtn').click();
  370. alert("此人材机库已被引用,不可删除!");
  371. }
  372. else if(result.message === '删除成功'){
  373. var jqSel = "#"+ delObj.libId;
  374. $(jqSel).remove();
  375. let index = dispNames.indexOf(delObj.libName);
  376. dispNames.splice(index, 1);
  377. $('#delCancelBtn').click();
  378. }
  379. }
  380. $.bootstrapLoading.end();
  381. },
  382. error: function () {
  383. alert('服务器出错!');
  384. $.bootstrapLoading.end();
  385. }
  386. })
  387. }