main.js 16 KB

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