main.js 13 KB

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