main.js 11 KB

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