main.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. });
  87. $("#deleteA").click(function(){
  88. let deleteId = $(this).attr("deleteId");
  89. if(preDeleteId && preDeleteId !== deleteId){
  90. deleteCount = 0;
  91. }
  92. preDeleteId = deleteId;
  93. deleteCount++;
  94. let jqSel = "#" + deleteId + " td:first" + " a";
  95. let libName = $(jqSel).text();
  96. if(deleteCount === 3){
  97. deleteCount = 0;
  98. removeRationLib({libId: deleteId, libName: libName}, dispNameArr);
  99. $('#del').modal('hide');
  100. }
  101. });
  102. //全部计算
  103. $("#showArea").on("click", "[data-target = '#reCalcAll']", function(){
  104. let recalcId = $(this).parent().parent().attr("id");
  105. $("#reCalcConfirm").attr("recalcId", recalcId);
  106. });
  107. $("#reCalcConfirm").click(function(){
  108. $('#reCalcConfirm').addClass('disabled');
  109. $.bootstrapLoading.start();
  110. let recalcId = $(this).attr("recalcId");
  111. CommonAjax.post('/rationRepository/api/reCalcAll', {rationRepId: recalcId}, function (rstData) {
  112. $.bootstrapLoading.end();
  113. $('#reCalcAll').modal('hide');
  114. $('#reCalcConfirm').removeClass('disabled');
  115. }, function () {
  116. $.bootstrapLoading.end();
  117. $('#reCalcAll').modal('hide');
  118. $('#reCalcConfirm').removeClass('disabled')
  119. });
  120. });
  121. });
  122. getCompilationList();
  123. // 导入原始数据按钮
  124. let rationRepId = 0;
  125. $("#showArea").on("click", ".import-source", function () {
  126. let id = $(this).data("id");
  127. id = parseInt(id);
  128. if (isNaN(id) || id <= 0) {
  129. return false;
  130. }
  131. rationRepId = id;
  132. $("#import").modal("show");
  133. });
  134. // 导入内部数据
  135. $("#showArea").on("click", ".import-data", function () {
  136. let id = $(this).data("id");
  137. id = parseInt(id);
  138. if (isNaN(id) || id <= 0) {
  139. return false;
  140. }
  141. rationRepId = id;
  142. $("#import2").modal("show");
  143. });
  144. // 导入原始数据确认
  145. $("#source-import,#data-import").click(function() {
  146. $.bootstrapLoading.start();
  147. const self = $(this);
  148. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  149. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  150. try {
  151. let formData = new FormData();
  152. let file = $("input[name='"+ type +"']")[0];
  153. if (file.files.length <= 0) {
  154. throw '请选择文件!';
  155. }
  156. formData.append('file', file.files[0]);
  157. // 获取定额库id
  158. if (rationRepId <= 0) {
  159. return false;
  160. }
  161. formData.append('rationRepId', rationRepId);
  162. formData.append('type', type);
  163. $.ajax({
  164. url: '/rationRepository/api/upload',
  165. type: 'POST',
  166. data: formData,
  167. cache: false,
  168. contentType: false,
  169. processData: false,
  170. beforeSend: function() {
  171. self.attr('disabled', 'disabled');
  172. self.text('上传中...');
  173. },
  174. success: function(response){
  175. self.removeAttr('disabled');
  176. self.text('确定导入');
  177. if (response.err === 0) {
  178. $.bootstrapLoading.end();
  179. const message = response.msg !== undefined ? response.msg : '';
  180. if (message !== '') {
  181. alert(message);
  182. }
  183. // 成功则关闭窗体
  184. dialog.modal("hide");
  185. } else {
  186. $.bootstrapLoading.end();
  187. const message = response.msg !== undefined ? response.msg : '上传失败!';
  188. alert(message);
  189. }
  190. },
  191. error: function(){
  192. $.bootstrapLoading.end();
  193. alert("与服务器通信发生错误");
  194. self.removeAttr('disabled');
  195. self.text('确定导入');
  196. }
  197. });
  198. } catch(error) {
  199. alert(error);
  200. }
  201. });
  202. // 导出数据
  203. $("#showArea").on("click", ".export", function () {
  204. let id = $(this).data("id");
  205. id = parseInt(id);
  206. if (isNaN(id) || id <= 0) {
  207. return false;
  208. }
  209. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  210. });
  211. });
  212. function getAllRationLib(callback){
  213. $.ajax({
  214. type: 'post',
  215. url: 'api/getRationDisplayNames',
  216. dataType: 'json',
  217. success: function (result) {
  218. let dispNames = [];
  219. if(result.data.length > 0){
  220. for(let i = 0; i < result.data.length; i++){
  221. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
  222. if(result.data[i].gljLib){
  223. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data[i].ID, result.data[i].gljLib);
  224. }
  225. let id = result.data[i].ID;
  226. let libName = result.data[i].dispName;
  227. let createDate = result.data[i].createDate.split(' ')[0];
  228. let compilationName = result.data[i].compilationName;
  229. dispNames.push(result.data[i].dispName);
  230. $("#showArea").append(
  231. "<tr id='tempId'>" +
  232. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  233. "<td>"+compilationName+" </td>" +
  234. "<td>"+createDate+" </td>" +
  235. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  236. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  237. "<i class='fa fa-remove'></i></a>" +
  238. " <a href='javascript:void(0);' data-toggle='modal' data-target='#reCalcAll' title='全部计算'><i class='fa fa-calculator'></i></a></td>"+
  239. "<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>" +
  240. "<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> " +
  241. "<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>" +
  242. "</tr>");
  243. var newHref = "/rationRepository/ration?repository="+id;
  244. $("#tempId td:first a").attr("href", newHref);
  245. $("#tempId").attr("id", id);
  246. }
  247. }
  248. callback(dispNames);
  249. }
  250. });
  251. }
  252. function getCompilationList(){
  253. $.ajax({
  254. type: 'post',
  255. url: 'api/getCompilationList',
  256. dataType: 'json',
  257. success: function (result) {
  258. console.log(result);
  259. //addoptions
  260. for(let i = 0; i < result.data.compilation.length; i++){
  261. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  262. $option.val( result.data.compilation[i]._id);
  263. $('#compilationSels').append($option);
  264. }
  265. //初始工料机库选项
  266. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  267. let compilationId = result.data.compilation[0]._id;
  268. //console.log(compilationId);
  269. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  270. console.log(gljLibOps);
  271. for(let i = 0; i < gljLibOps.length; i++){
  272. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  273. $option.val(gljLibOps[i].ID);
  274. $('#gljLibSels').append($option);
  275. }
  276. }
  277. $('#compilationSels').on("change", function () {
  278. //刷新工料机库选项
  279. $('#gljLibSels').children().remove();
  280. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  281. for(let i = 0; i < newGljLibOps.length; i++){
  282. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  283. $option.val(newGljLibOps[i].ID);
  284. $('#gljLibSels').append($option);
  285. }
  286. });
  287. }
  288. });
  289. }
  290. function getGljLibOps(compilationId, gljLibs){
  291. let rst = [];
  292. for(let i = 0; i < gljLibs.length; i++){
  293. if(gljLibs[i]){
  294. if(compilationId === gljLibs[i].compilationId){
  295. rst.push(gljLibs[i]);
  296. }
  297. }
  298. }
  299. return rst;
  300. }
  301. function createRationLib(rationObj, dispNamesArr){
  302. $.ajax({
  303. type: 'post',
  304. url: 'api/addRationRepository',
  305. data: {rationRepObj: JSON.stringify(rationObj)},
  306. dataType: 'json',
  307. success: function (result) {
  308. if(result.data){
  309. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
  310. if(result.data.gljLib){
  311. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data.ID, result.data.gljLib);
  312. }
  313. let id = result.data.ID;
  314. let libName = result.data.dispName;
  315. let createDate = result.data.createDate.split(' ')[0];
  316. let compilationName = result.data.compilationName;
  317. dispNamesArr.push(libName);
  318. $("#showArea").append(
  319. "<tr id='tempId'>" +
  320. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  321. "<td>"+compilationName+" </td>" +
  322. "<td>"+createDate+" </td>" +
  323. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  324. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  325. "<i class='fa fa-remove'></i></a>" +
  326. " <a href='javascript:void(0);' data-toggle='modal' data-target='#reCalcAll' title='全部计算'><i class='fa fa-calculator'></i></a>"+
  327. "<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>" +
  328. "<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> " +
  329. "<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>" +
  330. "</tr>");
  331. var newHref = "/rationRepository/ration?repository="+id;
  332. $("#tempId td:first a").attr("href", newHref);
  333. $("#tempId").attr("id", id);
  334. }
  335. $('#cancelBtn').click();
  336. }
  337. })
  338. }
  339. function renameRationLib(renameObj, dispNames){
  340. $.ajax({
  341. type: 'post',
  342. url: 'api/editRationLibs',
  343. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  344. dataType: 'json',
  345. success: function (result) {
  346. if(!result.error){
  347. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  348. $(jqSel).text(renameObj.newName);
  349. let index = dispNames.indexOf(renameObj.orgName);
  350. dispNames.splice(index, 1);
  351. dispNames.splice(index, 0, renameObj.newName);
  352. }
  353. $('#editCancelBtn').click();
  354. $('#renameText').val('');
  355. }
  356. })
  357. }
  358. function removeRationLib(delObj, dispNames){
  359. $.bootstrapLoading.start();
  360. $.ajax({
  361. type: 'post',
  362. url: 'api/deleteRationLibs',
  363. data: {oprtor: userAccount, libId: delObj.libId},
  364. dataType: 'json',
  365. success: function (result) {
  366. if(!result.error){
  367. var jqSel = "#"+ delObj.libId;
  368. $(jqSel).remove();
  369. let index = dispNames.indexOf(delObj.libName);
  370. dispNames.splice(index, 1);
  371. $('#delCancelBtn').click();
  372. }
  373. $.bootstrapLoading.end();
  374. },
  375. error: function () {
  376. alert('删除失败');
  377. $.bootstrapLoading.end();
  378. }
  379. })
  380. }