main.js 14 KB

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