main.js 15 KB

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