main.js 15 KB

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