main.js 17 KB

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