main.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. getCompilationList();
  104. // 导入原始数据按钮
  105. let rationRepId = 0;
  106. $("#showArea").on("click", ".import-source", function () {
  107. let id = $(this).data("id");
  108. id = parseInt(id);
  109. if (isNaN(id) || id <= 0) {
  110. return false;
  111. }
  112. rationRepId = id;
  113. $("#import").modal("show");
  114. });
  115. // 导入内部数据
  116. $("#showArea").on("click", ".import-data", function () {
  117. let id = $(this).data("id");
  118. id = parseInt(id);
  119. if (isNaN(id) || id <= 0) {
  120. return false;
  121. }
  122. rationRepId = id;
  123. $("#import2").modal("show");
  124. });
  125. // 导入原始数据确认
  126. $("#source-import,#data-import").click(function() {
  127. $.bootstrapLoading.start();
  128. const self = $(this);
  129. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  130. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  131. try {
  132. let formData = new FormData();
  133. let file = $("input[name='"+ type +"']")[0];
  134. if (file.files.length <= 0) {
  135. throw '请选择文件!';
  136. }
  137. formData.append('file', file.files[0]);
  138. // 获取定额库id
  139. if (rationRepId <= 0) {
  140. return false;
  141. }
  142. formData.append('rationRepId', rationRepId);
  143. formData.append('type', type);
  144. $.ajax({
  145. url: '/rationRepository/api/upload',
  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. dialog.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. }
  182. });
  183. // 导出数据
  184. $("#showArea").on("click", ".export", function () {
  185. let id = $(this).data("id");
  186. id = parseInt(id);
  187. if (isNaN(id) || id <= 0) {
  188. return false;
  189. }
  190. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  191. });
  192. });
  193. function getAllRationLib(callback){
  194. $.ajax({
  195. type: 'post',
  196. url: 'api/getRationDisplayNames',
  197. dataType: 'json',
  198. success: function (result) {
  199. let dispNames = [];
  200. if(result.data.length > 0){
  201. for(let i = 0; i < result.data.length; i++){
  202. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
  203. if(result.data[i].gljLib){
  204. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data[i].ID, result.data[i].gljLib);
  205. }
  206. let id = result.data[i].ID;
  207. let libName = result.data[i].dispName;
  208. let createDate = result.data[i].createDate.split(' ')[0];
  209. let compilationName = result.data[i].compilationName;
  210. dispNames.push(result.data[i].dispName);
  211. $("#showArea").append(
  212. "<tr id='tempId'>" +
  213. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  214. "<td>"+compilationName+" </td>" +
  215. "<td>"+createDate+" </td>" +
  216. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  217. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  218. "<i class='fa fa-remove'></i></a></td>" +
  219. "<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>" +
  220. "<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> " +
  221. "<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>" +
  222. "</tr>");
  223. var newHref = "/rationRepository/ration?repository="+id;
  224. $("#tempId td:first a").attr("href", newHref);
  225. $("#tempId").attr("id", id);
  226. }
  227. }
  228. callback(dispNames);
  229. }
  230. });
  231. }
  232. function getCompilationList(){
  233. $.ajax({
  234. type: 'post',
  235. url: 'api/getCompilationList',
  236. dataType: 'json',
  237. success: function (result) {
  238. console.log(result);
  239. //addoptions
  240. for(let i = 0; i < result.data.compilation.length; i++){
  241. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  242. $option.val( result.data.compilation[i]._id);
  243. $('#compilationSels').append($option);
  244. }
  245. //初始工料机库选项
  246. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  247. let compilationId = result.data.compilation[0]._id;
  248. //console.log(compilationId);
  249. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  250. console.log(gljLibOps);
  251. for(let i = 0; i < gljLibOps.length; i++){
  252. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  253. $option.val(gljLibOps[i].ID);
  254. $('#gljLibSels').append($option);
  255. }
  256. }
  257. $('#compilationSels').on("change", function () {
  258. //刷新工料机库选项
  259. $('#gljLibSels').children().remove();
  260. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  261. for(let i = 0; i < newGljLibOps.length; i++){
  262. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  263. $option.val(newGljLibOps[i].ID);
  264. $('#gljLibSels').append($option);
  265. }
  266. });
  267. }
  268. });
  269. }
  270. function getGljLibOps(compilationId, gljLibs){
  271. let rst = [];
  272. for(let i = 0; i < gljLibs.length; i++){
  273. if(gljLibs[i]){
  274. if(compilationId === gljLibs[i].compilationId){
  275. rst.push(gljLibs[i]);
  276. }
  277. }
  278. }
  279. return rst;
  280. }
  281. function createRationLib(rationObj, dispNamesArr){
  282. $.ajax({
  283. type: 'post',
  284. url: 'api/addRationRepository',
  285. data: {rationRepObj: JSON.stringify(rationObj)},
  286. dataType: 'json',
  287. success: function (result) {
  288. if(result.data){
  289. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
  290. if(result.data.gljLib){
  291. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data.ID, result.data.gljLib);
  292. }
  293. let id = result.data.ID;
  294. let libName = result.data.dispName;
  295. let createDate = result.data.createDate.split(' ')[0];
  296. let compilationName = result.data.compilationName;
  297. dispNamesArr.push(libName);
  298. $("#showArea").append(
  299. "<tr id='tempId'>" +
  300. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  301. "<td>"+compilationName+" </td>" +
  302. "<td>"+createDate+" </td>" +
  303. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  304. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  305. "<i class='fa fa-remove'></i></a>" +
  306. "<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>" +
  307. "<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> " +
  308. "<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>" +
  309. "</tr>");
  310. var newHref = "/rationRepository/ration?repository="+id;
  311. $("#tempId td:first a").attr("href", newHref);
  312. $("#tempId").attr("id", id);
  313. }
  314. $('#cancelBtn').click();
  315. }
  316. })
  317. }
  318. function renameRationLib(renameObj, dispNames){
  319. $.ajax({
  320. type: 'post',
  321. url: 'api/editRationLibs',
  322. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  323. dataType: 'json',
  324. success: function (result) {
  325. if(!result.error){
  326. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  327. $(jqSel).text(renameObj.newName);
  328. let index = dispNames.indexOf(renameObj.orgName);
  329. dispNames.splice(index, 1);
  330. dispNames.splice(index, 0, renameObj.newName);
  331. }
  332. $('#editCancelBtn').click();
  333. $('#renameText').val('');
  334. }
  335. })
  336. }
  337. function removeRationLib(delObj, dispNames){
  338. $.bootstrapLoading.start();
  339. $.ajax({
  340. type: 'post',
  341. url: 'api/deleteRationLibs',
  342. data: {oprtor: userAccount, libId: delObj.libId},
  343. dataType: 'json',
  344. success: function (result) {
  345. if(!result.error){
  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. $.bootstrapLoading.end();
  353. },
  354. error: function () {
  355. alert('删除失败');
  356. $.bootstrapLoading.end();
  357. }
  358. })
  359. }