main.js 13 KB

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