main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. function getAllRationLib(callback){
  78. $.ajax({
  79. type: 'post',
  80. url: 'api/getRationDisplayNames',
  81. dataType: 'json',
  82. success: function (result) {
  83. let dispNames = [];
  84. if(result.data.length > 0){
  85. for(let i = 0; i < result.data.length; i++){
  86. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data[i].ID, result.data[i].dispName);
  87. if(result.data[i].gljLib){
  88. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data[i].ID, result.data[i].gljLib);
  89. }
  90. let id = result.data[i].ID;
  91. let libName = result.data[i].dispName;
  92. let createDate = result.data[i].createDate.split(' ')[0];
  93. let compilationName = result.data[i].compilationName;
  94. dispNames.push(result.data[i].dispName);
  95. $("#showArea").append(
  96. "<tr id='tempId'>" +
  97. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  98. "<td>"+compilationName+" </td>" +
  99. "<td>"+createDate+" </td>" +
  100. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  101. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  102. "<i class='fa fa-remove'></i></a></td>" +
  103. "<td><a class='btn btn-secondary btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#import' title='导入原始数据'><i class='fa fa-sign-in fa-rotate-90'></i>导入</a></td>" +
  104. "<td><a class='btn btn-success btn-sm' href='javacript:void(0);' data-toggle='modal' data-target='#emport' title='导出内部数据'><i class='fa fa-sign-out fa-rotate-270'></i>导出</a> <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>" +
  105. "</tr>");
  106. var newHref = "/rationRepository/ration?repository="+id;
  107. $("#tempId td:first a").attr("href", newHref);
  108. $("#tempId").attr("id", id);
  109. }
  110. }
  111. callback(dispNames);
  112. }
  113. });
  114. }
  115. function getCompilationList(){
  116. $.ajax({
  117. type: 'post',
  118. url: 'api/getCompilationList',
  119. dataType: 'json',
  120. success: function (result) {
  121. console.log(result);
  122. //addoptions
  123. for(let i = 0; i < result.data.compilation.length; i++){
  124. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  125. $option.val( result.data.compilation[i]._id);
  126. $('#compilationSels').append($option);
  127. }
  128. //初始工料机库选项
  129. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  130. let compilationId = result.data.compilation[0]._id;
  131. //console.log(compilationId);
  132. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  133. console.log(gljLibOps);
  134. for(let i = 0; i < gljLibOps.length; i++){
  135. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  136. $option.val(gljLibOps[i].ID);
  137. $('#gljLibSels').append($option);
  138. }
  139. }
  140. $('#compilationSels').on("change", function () {
  141. //刷新工料机库选项
  142. $('#gljLibSels').children().remove();
  143. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  144. for(let i = 0; i < newGljLibOps.length; i++){
  145. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  146. $option.val(newGljLibOps[i].ID);
  147. $('#gljLibSels').append($option);
  148. }
  149. });
  150. }
  151. });
  152. }
  153. function getGljLibOps(compilationId, gljLibs){
  154. let rst = [];
  155. for(let i = 0; i < gljLibs.length; i++){
  156. if(gljLibs[i]){
  157. if(compilationId === gljLibs[i].compilationId){
  158. rst.push(gljLibs[i]);
  159. }
  160. }
  161. }
  162. return rst;
  163. }
  164. function createRationLib(rationObj, dispNamesArr){
  165. $.ajax({
  166. type: 'post',
  167. url: 'api/addRationRepository',
  168. data: {rationRepObj: JSON.stringify(rationObj)},
  169. dataType: 'json',
  170. success: function (result) {
  171. if(result.data){
  172. storageUtil.setSessionCache("RationGrp","repositoryID_" + result.data.ID, result.data.dispName);
  173. if(result.data.gljLib){
  174. storageUtil.setSessionCache("gljLib","repositoryID_" + result.data.ID, result.data.gljLib);
  175. }
  176. let id = result.data.ID;
  177. let libName = result.data.dispName;
  178. let createDate = result.data.createDate.split(' ')[0];
  179. let compilationName = result.data.compilationName;
  180. dispNamesArr.push(libName);
  181. $("#showArea").append(
  182. "<tr id='tempId'>" +
  183. "<td><a href='/stdGljRepository/glj'>"+libName+"</a></td>" +
  184. "<td>"+compilationName+" </td>" +
  185. "<td>"+createDate+" </td>" +
  186. "<td><a href='javascript:void(0);' data-toggle='modal' data-target='#edit' title='编辑'>" +
  187. "<i class='fa fa-pencil-square-o'></i></a> <a href='javascript:void(0);' data-toggle='modal' data-target='#del' class='text-danger' title='删除'>" +
  188. "<i class='fa fa-remove'></i></a></td></tr>");
  189. var newHref = "/rationRepository/ration?repository="+id;
  190. $("#tempId td:first a").attr("href", newHref);
  191. $("#tempId").attr("id", id);
  192. }
  193. $('#cancelBtn').click();
  194. }
  195. })
  196. }
  197. function renameRationLib(renameObj, dispNames){
  198. $.ajax({
  199. type: 'post',
  200. url: 'api/editRationLibs',
  201. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  202. dataType: 'json',
  203. success: function (result) {
  204. if(!result.error){
  205. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  206. $(jqSel).text(renameObj.newName);
  207. let index = dispNames.indexOf(renameObj.orgName);
  208. dispNames.splice(index, 1);
  209. dispNames.splice(index, 0, renameObj.newName);
  210. }
  211. $('#editCancelBtn').click();
  212. $('#renameText').val('');
  213. }
  214. })
  215. }
  216. function removeRationLib(delObj, dispNames){
  217. $.ajax({
  218. type: 'post',
  219. url: 'api/deleteRationLibs',
  220. data: {oprtor: userAccount, libId: delObj.libId},
  221. dataType: 'json',
  222. success: function (result) {
  223. if(!result.error){
  224. var jqSel = "#"+ delObj.libId;
  225. $(jqSel).remove();
  226. let index = dispNames.indexOf(delObj.libName);
  227. dispNames.splice(index, 1);
  228. $('#delCancelBtn').click();
  229. }
  230. }
  231. })
  232. }