main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. $('#del').on('hidden.bs.modal', function () {
  11. deleteCount = 0;
  12. });
  13. getCompilationList(function (datas) {
  14. compilationsArr = datas;
  15. getAllGljLib(function (dispNames, compilationsUsedArr) {
  16. usedCom = compilationsUsedArr;
  17. dispNameArr = dispNames;
  18. //添加
  19. $('#addBtn').click(function () {
  20. let compilationName = $('#compilationSels option:selected').text();
  21. let compilationId = $('#compilationSels option:selected').val();
  22. let libName = $('#libNameTxt').val();
  23. if(libName.trim().length === 0){
  24. alert('名称不可为空!');
  25. $('#libNameTxt').val('')
  26. }
  27. else if(compilationsUsedArr.indexOf(compilationId) !== -1){// compilationsUsedArr;//在该编办下已建库,限制一个编办只能有一个库
  28. alert('该编办已被绑定!');
  29. $('#libNameTxt').val('')
  30. }
  31. else if(dispNames.indexOf(libName) !== -1){
  32. alert('此人材机库已存在!');
  33. $('#libNameTxt').val('')
  34. }
  35. else if(compilationName.trim().length === 0){
  36. alert('编办不可为空!');
  37. }
  38. else{
  39. let newGljLib = {};
  40. newGljLib.dispName = libName;
  41. newGljLib.compilationId = compilationId;
  42. newGljLib.compilationName = compilationName;
  43. newGljLib.creator = oprtor;
  44. newGljLib.appType = "建筑";
  45. $('#libNameTxt').val('');
  46. createGljLib(newGljLib, dispNameArr, usedCom);
  47. }
  48. });
  49. $('#edit').on('shown.bs.modal', function () {
  50. setTimeout(function () {
  51. $('#renameText').focus();
  52. }, 100);
  53. });
  54. $('#add').on('shown.bs.modal', function () {
  55. setTimeout(function () {
  56. $('#libNameTxt').focus();
  57. }, 100);
  58. });
  59. $('#add').on('hidden.bs.modal', function () {
  60. $('#libNameTxt').val('');
  61. });
  62. //重命名
  63. $("#showArea").on("click", "[data-target = '#edit']", function(){
  64. let renameId = $(this).parent().parent().attr("id");
  65. let compilationName = $(this).parent().parent().children()[1].textContent;
  66. $('#compilationEdit option').text(compilationName);
  67. $('#renameText').val($(this).parent().parent().find('td:first-child').text());
  68. $("#renameA").attr("renameId", renameId);
  69. });
  70. $("#renameA").click(function(){
  71. let newName = $("#renameText").val();
  72. let libId = $(this).attr("renameId");
  73. let jqSel = "#" + libId + " td:first" + " a";
  74. let orgName = $(jqSel).text();
  75. if(newName.trim().length === 0){
  76. alert("名称不可为空!");
  77. $("#renameText").val('');
  78. }
  79. else if(dispNameArr.indexOf(newName) !== -1){
  80. alert("该人材机库已存在!");
  81. $("#renameText").val('');
  82. }
  83. else{
  84. renameGljLib({ID: libId, newName: newName, orgName: orgName}, dispNameArr);
  85. }
  86. });
  87. //删除
  88. $("#showArea").on("click", "[data-target = '#del']", function(){
  89. let deleteId = $(this).parent().parent().attr("id");
  90. $("#deleteA").attr("deleteId", deleteId);
  91. let delLibName = $(`#${deleteId}`).find('td:first').text();
  92. $('#del').find('.modal-body h5').text(`准备删除 “${delLibName}”,会导致已引用此库的地方出错,确定要删除吗?`)
  93. });
  94. $("#deleteA").click(function(){
  95. let deleteId = $(this).attr("deleteId");
  96. if(preDeleteId && preDeleteId !== deleteId){
  97. deleteCount = 0;
  98. }
  99. preDeleteId = deleteId;
  100. deleteCount++;
  101. let jqSel = "#" + deleteId + " td:first" + " a";
  102. let libName = $(jqSel).text();
  103. let compilationName = $("#" + deleteId + " td:eq(1)").text();
  104. if(deleteCount === 3){
  105. for(let i = 0, len = compilationsArr.length; i < len; i++){
  106. if(compilationsArr[i].name === compilationName.trim()){
  107. usedCom.splice(usedCom.indexOf(compilationsArr[i]._id), 1);
  108. break;
  109. }
  110. }
  111. removeGljLib({libId: deleteId, libName: libName}, dispNameArr);
  112. deleteCount = 0;
  113. }
  114. });
  115. });
  116. });
  117. let selLibId = -1;
  118. $("#showArea").on("click", ".import-data", function () {
  119. let id = $(this).data("id");
  120. id = parseInt(id);
  121. if (isNaN(id) || id <= 0) {
  122. return false;
  123. }
  124. selLibId = id;
  125. $("#import").modal("show");
  126. });
  127. //导入单价数据
  128. $("#data-import").click(function() {
  129. $.bootstrapLoading.start();
  130. const self = $(this);
  131. try {
  132. let formData = new FormData();
  133. let file = $("input[name='import_data']")[0];
  134. if (file.files.length <= 0) {
  135. throw '请选择文件!';
  136. }
  137. formData.append('file', file.files[0]);
  138. // 获取人材机库id
  139. if (selLibId <= 0) {
  140. return false;
  141. }
  142. formData.append('gljLibId', selLibId);
  143. $.ajax({
  144. url: 'api/importPrice',
  145. type: 'POST',
  146. data: formData,
  147. cache: false,
  148. contentType: false,
  149. processData: false,
  150. beforeSend: function() {
  151. self.attr('disabled', 'disabled');
  152. self.text('上传中...');
  153. },
  154. success: function(response){
  155. self.removeAttr('disabled');
  156. self.text('确定导入');
  157. if (response.err === 0) {
  158. $.bootstrapLoading.end();
  159. const message = response.msg !== undefined ? response.msg : '';
  160. if (message !== '') {
  161. alert(message);
  162. }
  163. // 成功则关闭窗体
  164. $('#import').modal("hide");
  165. } else {
  166. $.bootstrapLoading.end();
  167. const message = response.msg !== undefined ? response.msg : '上传失败!';
  168. alert(message);
  169. }
  170. },
  171. error: function(){
  172. $.bootstrapLoading.end();
  173. alert("与服务器通信发生错误");
  174. self.removeAttr('disabled');
  175. self.text('确定导入');
  176. }
  177. });
  178. } catch(error) {
  179. alert(error);
  180. $.bootstrapLoading.end();
  181. }
  182. });
  183. });
  184. function getAllGljLib(callback){
  185. $.ajax({
  186. type: 'post',
  187. url: 'api/getAllGljLib',
  188. dataType: 'json',
  189. success: function (result) {
  190. let dispNames = [];
  191. let compilationsUsedArr = [];
  192. if(result.data.length > 0){
  193. for(let i = 0; i < result.data.length; i++){
  194. compilationsUsedArr.push(result.data[i].compilationId);//已建库的编办
  195. let id = result.data[i].ID;
  196. let libName = result.data[i].dispName;
  197. let createDate = result.data[i].createDate.split(' ')[0];
  198. let compilationName = result.data[i].compilationName;
  199. let rationLibs = result.data[i].rationLibs;
  200. let rationLibsName = '';
  201. for(let j = 0; j < rationLibs.length; j++){
  202. rationLibsName += rationLibs[j].dispName + "</br>";
  203. }
  204. dispNames.push(result.data[i].dispName);
  205. $("#showArea").append(
  206. "<tr id='tempId'>" +
  207. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  208. "<td>"+compilationName+" </td>" +
  209. "<td>"+rationLibsName+" </td>" +
  210. "<td>"+createDate+" </td>" +
  211. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  212. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  213. "<i class='fa fa-remove'></i></a></td>" +
  214. "<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>" +
  215. "</tr>");
  216. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  217. $("#tempId td:first a").attr("href", newHref);
  218. $("#tempId").attr("id", id);
  219. }
  220. }
  221. callback(dispNames, compilationsUsedArr);
  222. }
  223. });
  224. }
  225. function getCompilationList(callback){
  226. $.ajax({
  227. type: 'post',
  228. url: 'api/getCompilationList',
  229. dataType: 'json',
  230. success: function (result) {
  231. //addoptions
  232. for(let i = 0; i < result.data.length; i++){
  233. let $option = $("<option >"+ result.data[i].name +"</option>");
  234. $option.val( result.data[i]._id);
  235. $('#compilationSels').append($option);
  236. }
  237. $('#compilationSels').on("change", function () {
  238. });
  239. callback(result.data);
  240. }
  241. });
  242. }
  243. function createGljLib(gljLibObj, dispNamesArr, usedCom){
  244. $.ajax({
  245. type: 'post',
  246. url: 'api/createGljLib',
  247. data: {gljLibObj: JSON.stringify(gljLibObj)},
  248. dataType: 'json',
  249. success: function (result) {
  250. if(result.data){
  251. let id = result.data.ID;
  252. let libName = result.data.dispName;
  253. let createDate = result.data.createDate.split(' ')[0];
  254. let compilationName = result.data.compilationName;
  255. dispNamesArr.push(libName);
  256. usedCom.push(gljLibObj.compilationId);
  257. $("#showArea").append(
  258. "<tr id='tempId'>" +
  259. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  260. "<td>"+compilationName+" </td>" +
  261. "<td>"+''+" </td>" +
  262. "<td>"+createDate+" </td>" +
  263. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  264. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  265. "<i class='fa fa-remove'></i></a></td>" +
  266. "<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>" +
  267. "</tr>");
  268. var newHref = "/stdGljRepository/glj?gljLibId="+id;
  269. $("#tempId td:first a").attr("href", newHref);
  270. $("#tempId").attr("id", id);
  271. }
  272. $('#cancelBtn').click();
  273. }
  274. })
  275. }
  276. function renameGljLib(renameObj, dispNames){
  277. $.ajax({
  278. type: 'post',
  279. url: 'api/renameGljLib',
  280. data: {oprtor: oprtor, renameObj: JSON.stringify(renameObj)},
  281. dataType: 'json',
  282. success: function (result) {
  283. if(!result.error){
  284. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  285. $(jqSel).text(renameObj.newName);
  286. let index = dispNames.indexOf(renameObj.orgName);
  287. dispNames.splice(index, 1);
  288. dispNames.splice(index, 0, renameObj.newName);
  289. }
  290. $('#editCancelBtn').click();
  291. $('#renameText').val('');
  292. }
  293. })
  294. }
  295. function removeGljLib(delObj, dispNames){
  296. $.bootstrapLoading.start();
  297. $.ajax({
  298. type: 'post',
  299. url: 'api/removeGljLib',
  300. data: {oprtor: oprtor, libId: delObj.libId},
  301. dataType: 'json',
  302. success: function (result) {
  303. if(!result.error){
  304. if(result.message === '此人材机库已被引用!'){
  305. $('#delCancelBtn').click();
  306. alert("此人材机库已被引用,不可删除!");
  307. }
  308. else if(result.message === '删除成功'){
  309. var jqSel = "#"+ delObj.libId;
  310. $(jqSel).remove();
  311. let index = dispNames.indexOf(delObj.libName);
  312. dispNames.splice(index, 1);
  313. $('#delCancelBtn').click();
  314. }
  315. }
  316. $.bootstrapLoading.end();
  317. },
  318. error: function () {
  319. alert('服务器出错!');
  320. $.bootstrapLoading.end();
  321. }
  322. })
  323. }