main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Zhong
  6. * @date 2018/5/29
  7. * @version
  8. */
  9. const billsGuidanceMain = (function () {
  10. const updateType = {create: 'create', update: 'update', delete: 'delete'};
  11. const typeString = {1: '清单指引', 2: '清单精灵'};
  12. let guidanceLibs = [];
  13. let curLib = null;
  14. //上一个选择的库(三次确认删除同一库时用)
  15. let preLib = null;
  16. let deleteCount = 0;
  17. //获取编办及编办下清单库
  18. //@return {void}
  19. function getComBillsLibInfo(){
  20. CommonAjax.post('/billsGuidance/api/getComBillsLibInfo', {}, function (rstData) {
  21. const comSels = $('#comSels');
  22. const billsLibSels = $('#billsLibSels');
  23. //设置编办及清单规则库选择
  24. comSels.empty();
  25. function setBillsLib(libs){
  26. billsLibSels.empty();
  27. for(let lib of libs){
  28. let libOpt = `<option value="${lib.billsLibId}">${lib.billsLibName}</option>`;
  29. billsLibSels.append(libOpt);
  30. }
  31. }
  32. setBillsLib(rstData.billsLibs);
  33. for(let i = 0; i < rstData.compilationList.length; i++){
  34. let compilation = rstData.compilationList[i];
  35. let comOpt = `<option value = "${compilation._id}">${compilation.name}</option>`;
  36. comSels.append(comOpt);
  37. }
  38. });
  39. }
  40. //html新增库
  41. //@param {Object}tbody {Object}lib @return {void}
  42. function addLibToView(tbody, lib){
  43. let type = lib.type && typeString[lib.type] ? typeString[lib.type] : ''
  44. let tr = `<tr id="${lib.ID}">
  45. <td><a href="/billsGuidance/guidance/?libID=${lib.ID}&locked=true">${lib.name}</a>
  46. <td>${lib.compilationName}</td>
  47. <td>${lib.billsLibName}</td>
  48. <td>${type}</td>
  49. <td>${lib.createDate.split(' ')[0]}</td>
  50. <td style="text-align: center;">
  51. ${ isTemporary !== 'true' ? `
  52. <a class="lock-btn-control disabled" href="javascript:void(0);" data-toggle="modal" data-target="#edit" title="编辑"><i class="fa fa-pencil-square-o"></i></a>
  53. <a class="lock-btn-control disabled text-danger" href="javascript:void(0);" data-toggle="modal" data-target="#del" title="删除"><i class="fa fa-remove"></i></a>
  54. ` : '' }
  55. <a class="lock" data-locked="true" href="javascript:void(0);" title="解锁"><i class="fa fa-unlock-alt"></i></a>
  56. </td>
  57. ${ isTemporary !== 'true' ? `
  58. <td style="text-align: center;">
  59. <a class="btn btn-secondary btn-sm generate-class lock-btn-control disabled" href="javascript:void(0);" data-id="${lib.ID}" title="生成清单分类"><i class="fa fa-sign-in fa-rotate-90"></i>生成</a>
  60. </td>
  61. ` : '' }
  62. ${ isTemporary !== 'true' ? `
  63. <td style="text-align: center;">
  64. <a class="btn btn-success btn-sm export lock-btn-control disabled" href="/billsGuidance/api/exportClassExcel?libID=${lib.ID}" download="清单类别库.xlsx" title="导出清单分类excel"><i class="fa fa-sign-out fa-rotate-270"></i>导出</a>
  65. </td>
  66. ` : '' }
  67. </tr>`;
  68. tbody.append(tr);
  69. }
  70. //获取清单指引库
  71. //@return {void}
  72. function getLibs(){
  73. CommonAjax.post('/billsGuidance/api/getBillsGuideLibs', {}, function (rstData) {
  74. guidanceLibs = rstData;
  75. const tbody = $('.main').find('tbody');
  76. tbody.empty();
  77. for(let lib of rstData){
  78. addLibToView(tbody, lib);
  79. }
  80. });
  81. }
  82. //是否已存在此库
  83. //@param {Object}findSet {Array}libs @return {Object}
  84. function existLib(findSet, libs) {
  85. for(let lib of libs){
  86. if(lib[findSet.k] === findSet.v){
  87. return lib;
  88. }
  89. }
  90. return null;
  91. }
  92. //监听事件
  93. //@return {void}
  94. function eventListener(){
  95. //新建库确认按钮事件
  96. $('#addY').click(function () {
  97. try{
  98. let cName = $('#createName').val();
  99. if(!cName || cName.trim() === ''){
  100. throw '请输入名称!';
  101. }
  102. if(existLib({k: 'name', v: cName}, guidanceLibs)){
  103. throw '已存在此库!';
  104. }
  105. let compilationId = $('#comSels').select().val();
  106. let compilationName = $('#comSels').select().children('option:selected').text();
  107. if(!compilationId){
  108. throw '请选择编办!';
  109. }
  110. let billsLibId = $('#billsLibSels').select().val();
  111. let billsLibName = $('#billsLibSels').select().children('option:selected').text();
  112. if(!billsLibId){
  113. throw '请选择清单规则库';
  114. }
  115. //库类型
  116. let addType = $('#add').find('input:checked');
  117. if(!addType){
  118. throw '请选择库类型';
  119. }
  120. //新建
  121. $.bootstrapLoading.start();
  122. $('#addY').addClass('disabled');
  123. let createData = {
  124. type: parseInt(addType.val()),
  125. ID: uuid.v1(),
  126. name: cName,
  127. compilationId: compilationId,
  128. compilationName: compilationName,
  129. billsLibId: parseInt(billsLibId),
  130. billsLibName:billsLibName
  131. };
  132. let updateData = {updateType: updateType.create, updateData: createData};
  133. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  134. guidanceLibs.push(rstData);
  135. addLibToView($('.main').find('tbody'), rstData);
  136. $('#add').modal('hide');
  137. $('#addY').removeClass('disabled');
  138. $.bootstrapLoading.end();
  139. }, function () {
  140. $('#addY').removeClass('disabled');
  141. $.bootstrapLoading.end();
  142. });
  143. }
  144. catch(err){
  145. alert(err);
  146. $('#createName').focus();
  147. }
  148. });
  149. //新建模态框
  150. $('#add').on('hidden.bs.modal', function () {
  151. $('#createName').val('');
  152. });
  153. $('#add').on('shown.bs.modal', function () {
  154. $('#createName').focus();
  155. });
  156. // 生成清单分类
  157. $('.main').find('tbody').on('click', '.generate-class', function () {
  158. let tr = $(this).parent().parent();
  159. let selLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  160. curLib = selLib;
  161. $('#generate-class-modal').modal('show');
  162. $('#generate-class-info').html(`
  163. <p>确认根据库“${curLib.name}”生成清单分类?</p>
  164. <p style="color: #d9534f">注意,原有分类数据将被清空</p>
  165. `)
  166. console.log(curLib);
  167. });
  168. $('#generate-class-confirm').click(async () => {
  169. if (!curLib || !curLib.ID) {
  170. return;
  171. }
  172. $.bootstrapLoading.start();
  173. try {
  174. await ajaxPost('/billsGuidance/api/generateClassData', { libID: curLib.ID }, 1000 * 60 * 10);
  175. $('#generate-class-modal').modal('hide');
  176. } catch (error) {
  177. console.log(error);
  178. } finally {
  179. $.bootstrapLoading.end();
  180. }
  181. });
  182. //所有编辑按钮
  183. $('.main').find('tbody').on('click', '[data-target="#edit"]', function () {
  184. let tr = $(this).parent().parent();
  185. let selLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  186. curLib = selLib;
  187. $('#edName').val(curLib.name);
  188. $('#edComSels').select().children('option:selected').text(curLib.compilationName);
  189. $('#edBillsLibSels').select().children('option:selected').text(curLib.billsLibName);
  190. });
  191. //编辑确认
  192. $('#editY').click(function(){
  193. try{
  194. let newName = $('#edName').val();
  195. if(newName.trim() === curLib.name){
  196. $('#edit').modal('hide');
  197. return;
  198. }
  199. if(!newName || newName.trim() === ''){
  200. throw '名称不能为空!';
  201. }
  202. if(existLib({k: 'name', v: newName}, guidanceLibs)){
  203. throw '该库已存在!';
  204. }
  205. let updateData = {updateType: updateType.update, findData: {ID: curLib.ID}, updateData: {name: newName}};
  206. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  207. curLib.name = newName;
  208. $(`#${curLib.ID} td:first a`).text(newName);
  209. $('#edit').modal('hide');
  210. });
  211. }
  212. catch(err){
  213. alert(err);
  214. $('#edName').focus();
  215. }
  216. });
  217. //编辑模态框
  218. $('#edit').on('shown.bs.modal', function () {
  219. $('#edName').focus();
  220. });
  221. //所有删除按钮
  222. $('.main').find('tbody').on('click', '[data-target="#del"]', function () {
  223. let tr = $(this).parent().parent();
  224. curLib = existLib({k: 'ID', v: tr.attr('id')}, guidanceLibs);
  225. console.log(curLib);
  226. });
  227. //删除确认
  228. $('#delY').click(function () {
  229. try{
  230. if(!curLib){
  231. throw '不存在该库!';
  232. }
  233. if(preLib && preLib.ID !== curLib.ID){
  234. deleteCount = 0;
  235. }
  236. deleteCount++;
  237. preLib = curLib;
  238. if(deleteCount === 3){
  239. $('#del').modal('hide');
  240. $.bootstrapLoading.start();
  241. let updateData = {updateType: updateType.delete, findData: {ID: curLib.ID}, updateData: {deleted: true}};
  242. CommonAjax.post('/billsGuidance/api/updateBillsGuideLib', updateData, function (rstData) {
  243. $(`#${curLib.ID}`).remove();
  244. curLib = null;
  245. _.remove(guidanceLibs, function (lib) {
  246. return lib.ID === updateData.findData.ID;
  247. });
  248. $.bootstrapLoading.end();
  249. }, function () {
  250. $.bootstrapLoading.end();
  251. });
  252. }
  253. }
  254. catch(err){
  255. alert(err);
  256. }
  257. });
  258. //删除确认窗口关闭,重新计数
  259. $('#del').on('hidden.bs.modal', function () {
  260. deleteCount = 0;
  261. });
  262. // 锁定、解锁
  263. $('.main').find('tbody').on('click', '.lock', function () {
  264. lockUtil.handleLockClick($(this));
  265. });
  266. }
  267. return {getComBillsLibInfo, getLibs, eventListener};
  268. })();
  269. $(document).ready(function () {
  270. billsGuidanceMain.getComBillsLibInfo();
  271. billsGuidanceMain.getLibs();
  272. billsGuidanceMain.eventListener();
  273. });