main.js 13 KB

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