main.js 12 KB

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