tender_list_manage.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2019/3/7
  7. * @version
  8. */
  9. const EmptyTenderHtml = [
  10. '<div class="jumbotron">',
  11. '<h3 class="display-6">还没有标段数据</h3>',
  12. '</div>'
  13. ];
  14. // levelTree - setting
  15. const levelTreeSetting = {
  16. view: {
  17. selectedMulti: false
  18. },
  19. data: {
  20. simpleData: {
  21. idKey: 'lid',
  22. pIdKey: 'lpId',
  23. rootPId: 0,
  24. enable: true,
  25. }
  26. },
  27. edit: {
  28. enable: true,
  29. showRemoveBtn: false,
  30. showRenameBtn: false,
  31. drag: {
  32. autoExpandTrigger: true,
  33. isCopy: false,
  34. isMove: true,
  35. prev: false,
  36. next: false,
  37. inner: true,
  38. }
  39. },
  40. callback: {
  41. beforeDrop: beforeDropNode,
  42. onDrop: onDropNode,
  43. }
  44. };
  45. const levelNodes =[];
  46. const tenderTree = [];
  47. function createTree() {
  48. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  49. if (zTree) {
  50. zTree.destroy();
  51. }
  52. $.fn.zTree.init($("#treeLevel"), levelTreeSetting, levelNodes);
  53. }
  54. function beforeDropNode(treeId, treeNodes, targetNode, moveType, isCopy) {
  55. if (targetNode.lid !== 1) {
  56. const parent = targetNode.getParentNode();
  57. if (parent && parent.lid === 1) {
  58. return false;
  59. }
  60. }
  61. }
  62. function onDropNode(event, treeId, treeNodes, targetNode, moveType) {
  63. const zTree = $.fn.zTree.getZTreeObj(treeId);
  64. function resetFixNode(id) {
  65. const node = zTree.getNodeByParam('lid', id);
  66. node.isParent = true;
  67. zTree.updateNode(node, false);
  68. zTree.expandNode(node, true);
  69. }
  70. function moveChildren(children, node) {
  71. if (!children || children.length === 0) { return }
  72. for (const c of children) {
  73. moveChildren(c.children, node);
  74. zTree.moveNode(node, c, 'inner');
  75. }
  76. }
  77. resetFixNode(1);
  78. resetFixNode(2);
  79. if (targetNode.lid === 1 && treeNodes[0].children && treeNodes[0].children.length !== 0) {
  80. moveChildren(treeNodes[0].children, zTree.getNodeByParam('lid', 1));
  81. } else if (targetNode.lid !== 1) {
  82. if (targetNode.children.length >= 2) {
  83. for (const c of targetNode.children) {
  84. if (c.lid !== treeNodes[0].lid) {
  85. zTree.moveNode(treeNodes[0], c, 'inner');
  86. }
  87. }
  88. }
  89. }
  90. }
  91. // 查询方法
  92. function findNode (key, value, arr) {
  93. for (const a of arr) {
  94. if (a[key] && a[key] === value) {
  95. return a;
  96. }
  97. }
  98. }
  99. function getPId(level) {
  100. if (level !== 1) {
  101. const p = findNode('level', level - 1, levelNodes);
  102. if (p) {
  103. return p.lid
  104. } else {
  105. return 1;
  106. }
  107. } else {
  108. return 2;
  109. }
  110. }
  111. // 分类数据排序
  112. function sortCategory() {
  113. category.sort(function (a, b) {
  114. return a.level ? (b.level ? a.level - b.level : -1) : a.id - b.id;
  115. });
  116. }
  117. // 初始化分类树结构数据
  118. function initCategoryLevelNode() {
  119. levelNodes.splice(0, levelNodes.length);
  120. levelNodes.push(
  121. { lid:1, lpId:0, name:"可用类别", open:true, isParent: true},
  122. { lid:2, lpId:0, name:"已用类别", open:true, isParent: true}
  123. );
  124. for (const c of category) {
  125. const cate = JSON.parse(JSON.stringify(c));
  126. cate.lid = levelNodes.length + 1;
  127. cate.open = true;
  128. if (!cate.level) {
  129. cate.lpId = 1;
  130. levelNodes.push(cate);
  131. } else {
  132. cate.lpId = getPId(cate.level);
  133. levelNodes.push(cate);
  134. }
  135. }
  136. }
  137. // 新建标段 -- 分类属性选择
  138. function getCategoryHtml() {
  139. function getSelectCategoryHtml (cate) {
  140. const html = [];
  141. html.push('<div class="form-group" cate-id="' + cate.id + '">');
  142. html.push('<lable>', cate.name, '</lable>');
  143. html.push('<select class="form-control">');
  144. for (const v of cate.value) {
  145. html.push('<option value="' + v.id + '">', v.value, '</option>');
  146. }
  147. html.push('</select>');
  148. html.push('</div>');
  149. return html.join('');
  150. }
  151. function getRadioCategoryHtml (cate) {
  152. const html = [];
  153. html.push('<div class="form-group" cate-id="' + cate.id + '">');
  154. html.push('<lable>', cate.name, '</lable>');
  155. html.push('<div>');
  156. for (const iV in cate.value) {
  157. const v = cate.value[iV];
  158. html.push('<div class="form-check-inline">');
  159. html.push('<input class="form-check-input" type="radio"', 'name="' + cate.name + '" ', 'value="' , v.id, (iV == 0 ? '" checked="' : ''), '">');
  160. html.push('<label class="form-check-label">', v.value, '</label>');
  161. html.push('</div>');
  162. }
  163. html.push('</div>');
  164. html.push('</div>');
  165. return html.join('');
  166. }
  167. const html = [];
  168. for (const c of category) {
  169. if (c.type === categoryType.key.dropDown) {
  170. html.push(getSelectCategoryHtml(c));
  171. } else if (c.type === categoryType.key.radio) {
  172. html.push(getRadioCategoryHtml(c));
  173. }
  174. }
  175. return html.join('');
  176. }
  177. // 初始化TenderTree数据
  178. function initTenderTree () {
  179. const levelCategory = category.filter(function (c) {
  180. return c.level && c.level > 0;
  181. });
  182. function findCategoryNode(cid, value, array) {
  183. for (const a of array) {
  184. if (a.cid === cid && a.vid === value) {
  185. return a;
  186. }
  187. }
  188. }
  189. function getCategoryNode(category, value, parent) {
  190. const array = parent ? parent.children : tenderTree;
  191. let cate = findCategoryNode(category.id, value, array);
  192. if (!cate) {
  193. const cateValue = findNode('id', value, category.value);
  194. cate = {
  195. cid: category.id,
  196. vid: value,
  197. name: cateValue.value,
  198. children: [],
  199. level: category.level,
  200. };
  201. array.push(cate);
  202. }
  203. return cate;
  204. }
  205. function loadTenderCategory (tender) {
  206. let tenderCategory = null;
  207. for (const lc of levelCategory) {
  208. const tenderCate = findNode('cid', lc.id, tender.category);
  209. if (tenderCate) {
  210. tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
  211. } else {
  212. return tenderCategory;
  213. }
  214. }
  215. return tenderCategory;
  216. }
  217. tenderTree.splice(0, tenderTree.length);
  218. for (const t of tenders) {
  219. t.valid = true;
  220. if (t.category && levelCategory.length > 0) {
  221. const parent = loadTenderCategory(t);
  222. if (parent) {
  223. t.level = parent.level + 1;
  224. parent.children.push(t);
  225. } else {
  226. tenderTree.push(t);
  227. }
  228. } else {
  229. tenderTree.push(t);
  230. }
  231. }
  232. }
  233. function recursiveGetTenderNodeHtml (node, arr) {
  234. const html = [];
  235. html.push('<tr>');
  236. // 名称
  237. html.push('<td class="in-' + node.level + '">');
  238. if (node.cid) {
  239. html.push('<i class="fa fa-folder-o"></i> ', node.name);
  240. } else {
  241. html.push('<span class="text-muted mr-2">');
  242. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  243. html.push('</span>');
  244. html.push('<a href="javascript: void(0)" name="name" id="' + node.id + '">', node.name, '</a>');
  245. }
  246. html.push('</td>');
  247. // 完成期数
  248. html.push('<td>');
  249. if (!node.cid) {
  250. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '第0期');
  251. }
  252. html.push('</td>');
  253. // 管理
  254. html.push('<td tid="' + node.id + '">');
  255. if (!node.cid) {
  256. html.push('<a href="#javascript: void(0)" name="edit" class="btn btn-outline-primary btn-sm">编辑</a>');
  257. if (node.ledger_status === auditConst.ledger.status.uncheck) {
  258. html.push('<a href="javascript: void(0)" name="del" class="btn btn-outline-danger btn-sm">删除</a>');
  259. }
  260. }
  261. html.push('</td>');
  262. html.push('</tr>');
  263. if (node.children) {
  264. for (const c of node.children) {
  265. html.push(recursiveGetTenderNodeHtml(c, node.children));
  266. }
  267. }
  268. return html.join('');
  269. }
  270. function getTenderTreeHeaderHtml() {
  271. const html = [];
  272. html.push('<thead>', '<tr>');
  273. html.push('<th>', '名称', '</th>');
  274. html.push('<th>', '完成期数', '</th>');
  275. html.push('<th>', '管理', '</th>');
  276. html.push('</tr>', '</thead>');
  277. }
  278. // 根据TenderTree数据获取Html代码
  279. function getTenderTreeHtml () {
  280. if (tenderTree.length > 0) {
  281. const html = [];
  282. html.push('<table class="table table-bordered">');
  283. html.push(getTenderTreeHeaderHtml());
  284. for (const t of tenderTree) {
  285. html.push(recursiveGetTenderNodeHtml(t, tenderTree));
  286. }
  287. html.push('</table>');
  288. return html.join('');
  289. } else {
  290. return EmptyTenderHtml.join('');
  291. }
  292. }
  293. function bindTenderUrl() {
  294. // 打开标段
  295. $('a[name=name]', '.c-body').bind('click', function () {
  296. const tenderId = parseInt($(this).attr('id'));
  297. const tender = _.find(tenders, function (t) {
  298. return t.id === tenderId;
  299. });
  300. if (tender.measure_type) {
  301. window.location.href = '/tender/' + tenderId;
  302. } else {
  303. for (const a of $('a', '#jlms')) {
  304. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  305. }
  306. $('#jlms').modal('show');
  307. }
  308. });
  309. // 编辑
  310. $('a[name=edit]', '.c-body').bind('click', function () {
  311. const tid = parseInt($(this).parent().attr('tid'));
  312. const tender = _.find(tenders, {id: tid});
  313. $('[name=name]', '#edit-bd').val(tender.name);
  314. for (const c of tender.category) {
  315. $('input[value=' + c.value + ']', '#edit-bd').attr('checked', 'checked');
  316. $('option[value=' + c.value + ']', '#edit-bd').attr('selected', true);
  317. }
  318. $('#edit-bd-ok').attr('tid', tid);
  319. $('#edit-bd').modal('show');
  320. });
  321. // 删除
  322. $('a[name=del]', '.c-body').bind('click', function () {
  323. $('#del-bd-ok').attr('tid', $(this).parent().attr('tid'));
  324. $('#del-bd').modal('show');
  325. });
  326. }
  327. $(document).ready(() => {
  328. sortCategory();
  329. // 初始化分类数据
  330. initCategoryLevelNode();
  331. $('.modal-body', '#add-bd').append(getCategoryHtml());
  332. $('.modal-body', '#edit-bd').append(getCategoryHtml());
  333. // 初始化标段树结构
  334. initTenderTree();
  335. $('.c-body').html(getTenderTreeHtml());
  336. bindTenderUrl();
  337. // 分类
  338. $('#cate-set').on('show.bs.modal', function () {
  339. createTree();
  340. });
  341. $('#set-cate-ok').click(function () {
  342. const data = [];
  343. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  344. for (const c of category) {
  345. const node = zTree.getNodeByParam('id', c.id);
  346. const parent = node.getParentNode();
  347. if (parent.lid === 1) {
  348. data.push({id: c.id, level: 0});
  349. } else {
  350. data.push({id: c.id, level: node.getPath().length - 1});
  351. }
  352. }
  353. postData('/setting/category/level', data, function (rst) {
  354. for (const d of data) {
  355. const c = findNode('id', d.id, category);
  356. c.level = d.level;
  357. }
  358. sortCategory();
  359. initCategoryLevelNode();
  360. initTenderTree();
  361. $('.c-body').html(getTenderTreeHtml());
  362. $('#cate-set').modal('hide');
  363. });
  364. });
  365. // 新增标段
  366. $('#add-bd-ok').click(function () {
  367. const data = {
  368. name: $('[name=name]', '#add-bd').val(),
  369. category: [],
  370. };
  371. if (!data.name || data.name === '') {
  372. // TODO 提示用户
  373. return;
  374. }
  375. for (const c of category) {
  376. const cate = {cid: c.id};
  377. const cateObj = $('[cate-id=' + c.id + ']', '#add-bd');
  378. if (c.type === categoryType.key.dropDown) {
  379. cate.value = parseInt($('select', cateObj).val());
  380. } else if (c.type === categoryType.key.radio) {
  381. cate.value = parseInt($('input:checked', cateObj).val());
  382. }
  383. data.category.push(cate);
  384. }
  385. postData('/list/add', data, function (result) {
  386. tenders.push(result);
  387. initTenderTree();
  388. $('.c-body').html(getTenderTreeHtml());
  389. bindTenderUrl();
  390. $('#add-bd').modal('hide');
  391. $('[name=name]', '#add-bd').val('');
  392. });
  393. });
  394. // 编辑标段
  395. $('#edit-bd-ok').click(function () {
  396. const data = {
  397. id: parseInt($(this).attr('tid')),
  398. name: $('[name=name]', '#edit-bd').val(),
  399. category: [],
  400. };
  401. if (!data.name || data.name === '') {
  402. // TODO 提示用户
  403. return;
  404. }
  405. for (const c of category) {
  406. const cate = {cid: c.id};
  407. const cateObj = $('[cate-id=' + c.id + ']', '#edit-bd');
  408. if (c.type === categoryType.key.dropDown) {
  409. cate.value = parseInt($('select', cateObj).val());
  410. } else if (c.type === categoryType.key.radio) {
  411. cate.value = parseInt($('input:checked', cateObj).val());
  412. }
  413. data.category.push(cate);
  414. }
  415. postData('/list/update', data, function (result) {
  416. const tender = _.find(tenders, {id: result.id});
  417. _.assign(tender, result);
  418. initTenderTree();
  419. $('.c-body').html(getTenderTreeHtml());
  420. bindTenderUrl();
  421. $('#edit-bd').modal('hide');
  422. });
  423. });
  424. // 删除标段
  425. $('#del-bd-ok').click(function () {
  426. const tid = parseInt($(this).attr('tid'));
  427. if (tid >= 0) {
  428. postData('/list/del', [tid], function (result) {
  429. function getCategory(arr, id) {
  430. for (const a of arr) {
  431. if (a.cid) {
  432. const ac = getCategory(a.children, id);
  433. if (ac) {
  434. return ac;
  435. }
  436. } else if (a.id === id) {
  437. return arr;
  438. }
  439. }
  440. return null;
  441. }
  442. for (const rid of result) {
  443. const tr = $('td[tid=' + rid + ']').parent();
  444. const arr = getCategory(tenderTree, rid);
  445. if (arr) {
  446. const a = arr.find(function (x) {
  447. return x.id === rid;
  448. });
  449. console.log(arr.indexOf(a));
  450. console.log(arr.length - 1);
  451. if (arr.length > 1 && arr.indexOf(a) === arr.length - 1) {
  452. const span = $('span', tr.prev());
  453. span.text('└');
  454. }
  455. arr.splice(arr.indexOf(a), 1);
  456. }
  457. _.remove(tenders, function (n) {
  458. return n.id === rid;
  459. });
  460. tr.remove();
  461. $('#del-bd').modal('hide');
  462. }
  463. });
  464. } else {
  465. $('#del-bd').modal('hide');
  466. }
  467. });
  468. });