main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. // 锁定、解锁
  123. $('#showArea').on('click', '.lock', function () {
  124. lockUtil.handleLockClick($(this));
  125. });
  126. getCompilationList(function (data) {
  127. compilationsArr = data.compilation;
  128. });
  129. // 导入原始数据按钮
  130. let rationRepId = 0;
  131. $("#showArea").on("click", ".import-source", function () {
  132. let id = $(this).data("id");
  133. id = parseInt(id);
  134. if (isNaN(id) || id <= 0) {
  135. return false;
  136. }
  137. rationRepId = id;
  138. $("#import").modal("show");
  139. });
  140. // 导入内部数据
  141. $("#showArea").on("click", ".import-data", function () {
  142. let id = $(this).data("id");
  143. id = parseInt(id);
  144. if (isNaN(id) || id <= 0) {
  145. return false;
  146. }
  147. rationRepId = id;
  148. $("#import2").modal("show");
  149. });
  150. // 导入原始数据确认
  151. $("#source-import,#data-import").click(function() {
  152. $.bootstrapLoading.start();
  153. const self = $(this);
  154. const type = self.is("#source-import") ? 'source_file' : 'import_data';
  155. const dialog = type === 'source_file' ? $("#import") : $("#import2");
  156. try {
  157. let formData = new FormData();
  158. let file = $("input[name='"+ type +"']")[0];
  159. if (file.files.length <= 0) {
  160. throw '请选择文件!';
  161. }
  162. formData.append('file', file.files[0]);
  163. // 获取定额库id
  164. if (rationRepId <= 0) {
  165. return false;
  166. }
  167. formData.append('rationRepId', rationRepId);
  168. formData.append('type', type);
  169. $.ajax({
  170. url: '/rationRepository/api/upload',
  171. type: 'POST',
  172. data: formData,
  173. cache: false,
  174. contentType: false,
  175. processData: false,
  176. beforeSend: function() {
  177. self.attr('disabled', 'disabled');
  178. self.text('上传中...');
  179. },
  180. success: function(response){
  181. self.removeAttr('disabled');
  182. self.text('确定导入');
  183. if (response.err === 0) {
  184. $.bootstrapLoading.end();
  185. const message = response.msg !== undefined ? response.msg : '';
  186. if (message !== '') {
  187. alert(message);
  188. }
  189. // 成功则关闭窗体
  190. dialog.modal("hide");
  191. } else {
  192. $.bootstrapLoading.end();
  193. const message = response.msg !== undefined ? response.msg : '上传失败!';
  194. alert(message);
  195. }
  196. },
  197. error: function(){
  198. $.bootstrapLoading.end();
  199. alert("与服务器通信发生错误");
  200. self.removeAttr('disabled');
  201. self.text('确定导入');
  202. }
  203. });
  204. } catch(error) {
  205. alert(error);
  206. }
  207. });
  208. // 导出数据
  209. $("#showArea").on("click", ".export", function () {
  210. let id = $(this).data("id");
  211. id = parseInt(id);
  212. if (isNaN(id) || id <= 0) {
  213. return false;
  214. }
  215. window.location.href = '/rationRepository/api/export?rationRepId=' + id;
  216. });
  217. //设置补充定额库章节树模板
  218. $("#showArea").on("click", ".set-comple", function () {
  219. let id = $(this).data("id");
  220. id = parseInt(id);
  221. if (isNaN(id) || id <= 0) {
  222. return false;
  223. }
  224. rationRepId = id;
  225. $('#templateA').addClass('disabled');
  226. $('#template').modal('show');
  227. $('#compilations').empty();
  228. for (let data of compilationsArr) {
  229. let $opt = $(`<option value="${data._id}">${data.name}</option>`);
  230. $('#compilations').append($opt);
  231. }
  232. $('#compilations').change();
  233. });
  234. $('#compilations').change(function () {
  235. selCompilationId = $(this).select().val();
  236. CommonAjax.get(`/rationRepository/api/sectionTemplateCount/${selCompilationId}`, function (rstData) {
  237. rstData.data.count > 0 ?
  238. $('#templateText').text('该费用定额下已有定额章节树模板数据,是否确认覆盖数据?') :
  239. $('#templateText').text('确认是否将此库的章节树设置成该费用定额下补充定额章节树模板?');
  240. $('#templateA').removeClass('disabled');
  241. });
  242. });
  243. $('#templateA').click(function () {
  244. if (rationRepId <= 0 && selCompilationId) {
  245. return false;
  246. }
  247. $.bootstrapLoading.start();
  248. CommonAjax.post('/rationRepository/api/initSectionTemplate', {rationLibId: rationRepId, compilationId: selCompilationId}, function () {
  249. $.bootstrapLoading.end();
  250. $('#template').modal('hide');
  251. }, function () {
  252. $.bootstrapLoading.end();
  253. $('#template').modal('hide');
  254. });
  255. });
  256. });
  257. function getCompilationList(callback){
  258. $.ajax({
  259. type: 'post',
  260. url: '/rationRepository/api/getCompilationList',
  261. dataType: 'json',
  262. success: function (result) {
  263. //addoptions
  264. for(let i = 0; i < result.data.compilation.length; i++){
  265. let $option = $("<option >"+ result.data.compilation[i].name +"</option>");
  266. $option.val( result.data.compilation[i]._id);
  267. $('#compilationSels').append($option);
  268. }
  269. //初始工料机库选项
  270. if(result.data.compilation.length > 0 && result.data.gljLibs.length > 0){
  271. let compilationId = result.data.compilation[0]._id;
  272. //console.log(compilationId);
  273. let gljLibOps = getGljLibOps(compilationId, result.data.gljLibs);
  274. for(let i = 0; i < gljLibOps.length; i++){
  275. let $option = $("<option >"+ gljLibOps[i].dispName +"</option>");
  276. $option.val(gljLibOps[i].ID);
  277. $('#gljLibSels').append($option);
  278. }
  279. }
  280. $('#compilationSels').on("change", function () {
  281. //刷新工料机库选项
  282. $('#gljLibSels').children().remove();
  283. let newGljLibOps = getGljLibOps(this.selectedOptions[0].value, result.data.gljLibs);
  284. for(let i = 0; i < newGljLibOps.length; i++){
  285. let $option = $("<option >"+ newGljLibOps[i].dispName +"</option>");
  286. $option.val(newGljLibOps[i].ID);
  287. $('#gljLibSels').append($option);
  288. }
  289. });
  290. callback(result.data);
  291. }
  292. });
  293. }
  294. function getGljLibOps(compilationId, gljLibs){
  295. let rst = [];
  296. for(let i = 0; i < gljLibs.length; i++){
  297. if(gljLibs[i]){
  298. if(compilationId === gljLibs[i].compilationId){
  299. rst.push(gljLibs[i]);
  300. }
  301. }
  302. }
  303. return rst;
  304. }
  305. function createRationLib(rationObj){
  306. $.ajax({
  307. type: 'post',
  308. url: '/rationRepository/api/addRationRepository',
  309. data: {rationRepObj: JSON.stringify(rationObj)},
  310. dataType: 'json',
  311. success: function (result) {
  312. window.location.href = location.href;
  313. }
  314. })
  315. }
  316. function renameRationLib(renameObj){
  317. $.ajax({
  318. type: 'post',
  319. url: '/rationRepository/api/editRationLibs',
  320. data: {oprtor: userAccount, renameObj: JSON.stringify(renameObj)},
  321. dataType: 'json',
  322. success: function (result) {
  323. if(!result.error){
  324. let jqSel = "#" + renameObj.ID + " td:first" + " a";
  325. $(jqSel).text(renameObj.newName);
  326. let index = allNames.indexOf(renameObj.orgName);
  327. allNames.splice(index, 1);
  328. allNames.splice(index, 0, renameObj.newName);
  329. }
  330. $('#editCancelBtn').click();
  331. $('#renameText').val('');
  332. }
  333. })
  334. }
  335. function removeRationLib(delObj){
  336. $.bootstrapLoading.start();
  337. $.ajax({
  338. type: 'post',
  339. url: '/rationRepository/api/deleteRationLibs',
  340. data: {oprtor: userAccount, libId: delObj.libId},
  341. dataType: 'json',
  342. success: function (result) {
  343. if(!result.error){
  344. var jqSel = "#"+ delObj.libId;
  345. $(jqSel).remove();
  346. let index = allNames.indexOf(delObj.libName);
  347. allNames.splice(index, 1);
  348. $('#delCancelBtn').click();
  349. }
  350. $.bootstrapLoading.end();
  351. },
  352. error: function () {
  353. alert('删除失败');
  354. $.bootstrapLoading.end();
  355. }
  356. })
  357. }