main.js 14 KB

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