tender_list_manage.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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. let parentId = 0;
  48. function createTree() {
  49. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  50. if (zTree) {
  51. zTree.destroy();
  52. }
  53. $.fn.zTree.init($("#treeLevel"), levelTreeSetting, levelNodes);
  54. }
  55. function beforeDropNode(treeId, treeNodes, targetNode, moveType, isCopy) {
  56. if (targetNode.lid !== 1) {
  57. const parent = targetNode.getParentNode();
  58. if (parent && parent.lid === 1) {
  59. return false;
  60. }
  61. }
  62. }
  63. function onDropNode(event, treeId, treeNodes, targetNode, moveType) {
  64. const zTree = $.fn.zTree.getZTreeObj(treeId);
  65. function resetFixNode(id) {
  66. const node = zTree.getNodeByParam('lid', id);
  67. node.isParent = true;
  68. zTree.updateNode(node, false);
  69. zTree.expandNode(node, true);
  70. }
  71. function moveChildren(children, node) {
  72. if (!children || children.length === 0) { return }
  73. for (const c of children) {
  74. moveChildren(c.children, node);
  75. zTree.moveNode(node, c, 'inner');
  76. }
  77. }
  78. resetFixNode(1);
  79. resetFixNode(2);
  80. if (targetNode.lid === 1 && treeNodes[0].children && treeNodes[0].children.length !== 0) {
  81. moveChildren(treeNodes[0].children, zTree.getNodeByParam('lid', 1));
  82. } else if (targetNode.lid !== 1) {
  83. if (targetNode.children.length >= 2) {
  84. for (const c of targetNode.children) {
  85. if (c.lid !== treeNodes[0].lid) {
  86. zTree.moveNode(treeNodes[0], c, 'inner');
  87. }
  88. }
  89. }
  90. }
  91. }
  92. // 查询方法
  93. function findNode (key, value, arr) {
  94. for (const a of arr) {
  95. if (a[key] && a[key] === value) {
  96. return a;
  97. }
  98. }
  99. }
  100. function getPId(level) {
  101. if (level !== 1) {
  102. const p = findNode('level', level - 1, levelNodes);
  103. if (p) {
  104. return p.lid
  105. } else {
  106. return 1;
  107. }
  108. } else {
  109. return 2;
  110. }
  111. }
  112. // 分类数据排序
  113. function sortCategory() {
  114. category.sort(function (a, b) {
  115. return a.level ? (b.level ? a.level - b.level : -1) : a.id - b.id;
  116. });
  117. }
  118. // 初始化分类树结构数据
  119. function initCategoryLevelNode() {
  120. levelNodes.splice(0, levelNodes.length);
  121. levelNodes.push(
  122. { lid:1, lpId:0, name:"可用类别", open:true, isParent: true},
  123. { lid:2, lpId:0, name:"已用类别", open:true, isParent: true}
  124. );
  125. for (const c of category) {
  126. const cate = JSON.parse(JSON.stringify(c));
  127. cate.lid = levelNodes.length + 1;
  128. cate.open = true;
  129. if (!cate.level) {
  130. cate.lpId = 1;
  131. levelNodes.push(cate);
  132. } else {
  133. cate.lpId = getPId(cate.level);
  134. levelNodes.push(cate);
  135. }
  136. }
  137. }
  138. // 新建标段 -- 分类属性选择
  139. function getCategoryHtml() {
  140. function getSelectCategoryHtml (cate) {
  141. const html = [];
  142. html.push('<div class="form-group" cate-id="' + cate.id + '">');
  143. html.push('<label>', cate.name, '</label>');
  144. html.push('<select class="form-control form-control-sm">');
  145. html.push('<option value="0">不选</option>');
  146. for (const v of cate.value) {
  147. html.push('<option value="' + v.id + '">', v.value, '</option>');
  148. }
  149. html.push('</select>');
  150. html.push('</div>');
  151. return html.join('');
  152. }
  153. function getRadioCategoryHtml (cate) {
  154. const html = [];
  155. html.push('<div class="form-group" cate-id="' + cate.id + '">');
  156. html.push('<lable>', cate.name, '</lable>');
  157. html.push('<div>');
  158. for (const iV in cate.value) {
  159. const v = cate.value[iV];
  160. html.push('<div class="form-check-inline">');
  161. html.push('<input class="form-check-input" type="radio"', 'name="' + cate.name + '" ', 'value="' , v.id, (iV == 0 ? '" checked="' : ''), '">');
  162. html.push('<label class="form-check-label">', v.value, '</label>');
  163. html.push('</div>');
  164. }
  165. html.push('</div>');
  166. html.push('</div>');
  167. return html.join('');
  168. }
  169. const html = [];
  170. for (const c of category) {
  171. // if (c.type === categoryType.key.dropDown) {
  172. html.push(getSelectCategoryHtml(c));
  173. // } else if (c.type === categoryType.key.radio) {
  174. // html.push(getRadioCategoryHtml(c));
  175. // }
  176. }
  177. return html.join('');
  178. }
  179. // 初始化TenderTree数据
  180. function initTenderTree () {
  181. const levelCategory = category.filter(function (c) {
  182. return c.level && c.level > 0;
  183. });
  184. function findCategoryNode(cid, value, array) {
  185. for (const a of array) {
  186. if (a.cid === cid && a.vid === value) {
  187. return a;
  188. }
  189. }
  190. }
  191. function getCategoryNode(category, value, parent, i = null) {
  192. const array = parent ? parent.children : tenderTree;
  193. let cate = findCategoryNode(category.id, value, array);
  194. if (!cate) {
  195. const cateValue = findNode('id', value, category.value);
  196. if (!cateValue) return null;
  197. cate = {
  198. cid: category.id,
  199. vid: value,
  200. name: cateValue.value,
  201. children: [],
  202. level: i ? i : category.level,
  203. sort_id: ++parentId,
  204. };
  205. array.push(cate);
  206. }
  207. return cate;
  208. }
  209. function loadTenderCategory (tender) {
  210. let tenderCategory = null;
  211. for (const [index,lc] of levelCategory.entries()) {
  212. const tenderCate = findNode('cid', lc.id, tender.category);
  213. if (tenderCate) {
  214. tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
  215. } else {
  216. if (index === 0 && tender.category) {
  217. for (const [i,c] of tender.category.entries()) {
  218. const cate = findNode('id', c.cid, category);
  219. tenderCategory = getCategoryNode(cate, c.value, tenderCategory, i+1);
  220. }
  221. }
  222. return tenderCategory;
  223. }
  224. }
  225. return tenderCategory;
  226. }
  227. tenderTree.splice(0, tenderTree.length);
  228. for (const t of tenders) {
  229. t.valid = true;
  230. delete t.level;
  231. if (t.category && levelCategory.length > 0) {
  232. const parent = loadTenderCategory(t);
  233. if (parent) {
  234. t.level = parent.level + 1;
  235. parent.children.push(t);
  236. } else {
  237. tenderTree.push(t);
  238. }
  239. } else {
  240. tenderTree.push(t);
  241. }
  242. }
  243. }
  244. function recursiveGetTenderNodeHtml (node, arr, pid) {
  245. const html = [];
  246. html.push('<tr pid="' + pid + '">');
  247. // 名称
  248. html.push('<td class="in-' + node.level + '">');
  249. if (node.cid) {
  250. html.push('<span class="fold-switch mr-1" title="收起" cid="'+ node.sort_id +'"><i class="fa fa-minus-square-o"></i></span> <i class="fa fa-folder-o"></i> ', node.name);
  251. } else {
  252. html.push('<span class="text-muted mr-2">');
  253. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  254. html.push('</span>');
  255. html.push('<a href="javascript: void(0)" name="name" id="' + node.id + '">', node.name, '</a>');
  256. }
  257. html.push('</td>');
  258. // 创建人
  259. html.push('<td>', node.user_name, '</td>');
  260. // 创建时间
  261. html.push('<td>', node.create_time ? moment(node.create_time).format('YYYY-MM-DD HH:mm:ss') : '', '</td>');
  262. // 完成期数
  263. html.push('<td>');
  264. if (!node.cid) {
  265. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '第0期');
  266. }
  267. html.push('</td>');
  268. // 管理
  269. html.push('<td tid="' + node.id + '">');
  270. if (!node.cid) {
  271. html.push('<a href="javascript: void(0)" name="edit" class="btn btn-outline-primary btn-sm">编辑</a>');
  272. if (node.lastStage === null || node.lastStage === undefined) {
  273. html.push('<a href="javascript: void(0)" name="del" class="btn btn-outline-danger btn-sm ml-1">删除</a>');
  274. } else {
  275. html.push('<button class="btn btn-outline-secondary btn-sm ml-1" data-toggle="tooltip" data-placement="top" title="请先删除所有期">删除</button>');
  276. }
  277. }
  278. html.push('</td>');
  279. html.push('</tr>');
  280. if (node.children) {
  281. for (const c of node.children) {
  282. html.push(recursiveGetTenderNodeHtml(c, node.children, node.sort_id));
  283. }
  284. }
  285. return html.join('');
  286. }
  287. function getTenderTreeHeaderHtml() {
  288. const html = [];
  289. html.push('<thead>', '<tr>');
  290. html.push('<th>', '名称', '</th>');
  291. html.push('<th>', '创建人', '</th>');
  292. html.push('<th>', '创建时间', '</th>');
  293. html.push('<th>', '完成期数', '</th>');
  294. html.push('<th>', '管理', '</th>');
  295. html.push('</tr>', '</thead>');
  296. return html.join('');
  297. }
  298. // 根据TenderTree数据获取Html代码
  299. function getTenderTreeHtml () {
  300. if (tenderTree.length > 0) {
  301. const html = [];
  302. html.push('<table class="table table-hover table-bordered">');
  303. html.push(getTenderTreeHeaderHtml());
  304. for (const t of tenderTree) {
  305. html.push(recursiveGetTenderNodeHtml(t, tenderTree, ''));
  306. }
  307. html.push('</table>');
  308. return html.join('');
  309. } else {
  310. return EmptyTenderHtml.join('');
  311. }
  312. }
  313. function bindTenderUrl() {
  314. // 打开标段
  315. $('body').on('click', '.c-body a[name=name]', function () {
  316. const tenderId = parseInt($(this).attr('id'));
  317. const tender = _.find(tenders, function (t) {
  318. return t.id === tenderId;
  319. });
  320. if (tender.measure_type) {
  321. window.location.href = '/tender/' + tenderId;
  322. } else {
  323. for (const a of $('a', '#jlms')) {
  324. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  325. }
  326. $('#jlms').modal('show');
  327. }
  328. });
  329. // 编辑
  330. $('body').on('click', '.c-body a[name=edit]', function () {
  331. const tid = parseInt($(this).parent().attr('tid'));
  332. const tender = _.find(tenders, {id: tid});
  333. $('[name=name]', '#edit-bd').val(tender.name);
  334. $('input[type=radio]', '#add-bd').prop('checked', false);
  335. for (const c of tender.category) {
  336. $('input[value=' + c.value + ']', '#edit-bd').prop('checked', 'checked');
  337. $('option[value=' + c.value + ']', '#edit-bd').prop('selected', true);
  338. }
  339. $('#edit-bd-ok').attr('tid', tid);
  340. $('#edit-bd').modal('show');
  341. });
  342. // 删除
  343. $('body').on('click', '.c-body a[name=del]', function () {
  344. $('#del-bd-ok').attr('tid', $(this).parent().attr('tid'));
  345. $('#del-bd').modal('show');
  346. });
  347. }
  348. $(document).ready(() => {
  349. autoFlashHeight();
  350. sortCategory();
  351. // 初始化分类数据
  352. initCategoryLevelNode();
  353. $('.modal-body', '#add-bd').append(getCategoryHtml());
  354. $('.modal-body', '#edit-bd').append(getCategoryHtml());
  355. // 初始化标段树结构
  356. initTenderTree();
  357. $('.c-body').html(getTenderTreeHtml());
  358. bindTenderUrl();
  359. console.log(tenderTree);
  360. console.log(category);
  361. // 分类
  362. $('#cate-set').on('show.bs.modal', function () {
  363. createTree();
  364. });
  365. $('#set-cate-ok').click(function () {
  366. const data = [];
  367. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  368. for (const c of category) {
  369. const node = zTree.getNodeByParam('id', c.id);
  370. const parent = node.getParentNode();
  371. if (parent.lid === 1) {
  372. data.push({id: c.id, level: 0});
  373. } else {
  374. data.push({id: c.id, level: node.getPath().length - 1});
  375. }
  376. }
  377. postData('/setting/category/level', data, function (rst) {
  378. for (const d of data) {
  379. const c = findNode('id', d.id, category);
  380. c.level = d.level;
  381. }
  382. sortCategory();
  383. initCategoryLevelNode();
  384. initTenderTree();
  385. $('.c-body').html(getTenderTreeHtml());
  386. $('#cate-set').modal('hide');
  387. });
  388. });
  389. $('a[name=add]').click(function () {
  390. $('input[type=radio]', '#edit-bd').prop('checked', false);
  391. $('input[type=radio]', '#add-bd').eq(0).prop('checked', true);
  392. });
  393. // 新增标段
  394. $('#add-bd-ok').click(function () {
  395. const data = {
  396. name: $('[name=name]', '#add-bd').val(),
  397. valuation: $('[name=valuation]:checked').val(),
  398. category: [],
  399. };
  400. if (!data.name || data.name === '') {
  401. // TODO 提示用户
  402. return;
  403. }
  404. for (const c of category) {
  405. const cateObj = $('[cate-id=' + c.id + ']', '#add-bd');
  406. if (parseInt($('select', cateObj).val()) !== 0) {
  407. const cate = {cid: c.id};
  408. cate.value = parseInt($('select', cateObj).val());
  409. data.category.push(cate);
  410. }
  411. // if (c.type === categoryType.key.dropDown) {
  412. // cate.value = parseInt($('select', cateObj).val());
  413. // } else if (c.type === categoryType.key.radio) {
  414. // cate.value = parseInt($('input:checked', cateObj).val());
  415. // }
  416. }
  417. postData('/list/add', data, function (result) {
  418. tenders.push(result);
  419. initTenderTree();
  420. $('.c-body').html(getTenderTreeHtml());
  421. bindTenderUrl();
  422. $('#add-bd').modal('hide');
  423. $('[name=name]', '#add-bd').val('');
  424. });
  425. });
  426. // 编辑标段
  427. $('#edit-bd-ok').click(function () {
  428. const data = {
  429. id: parseInt($(this).attr('tid')),
  430. name: $('[name=name]', '#edit-bd').val(),
  431. category: [],
  432. };
  433. if (!data.name || data.name === '') {
  434. // TODO 提示用户
  435. return;
  436. }
  437. for (const c of category) {
  438. const cateObj = $('[cate-id=' + c.id + ']', '#edit-bd');
  439. if (parseInt($('select', cateObj).val()) !== 0) {
  440. const cate = {cid: c.id};
  441. cate.value = parseInt($('select', cateObj).val());
  442. data.category.push(cate);
  443. }
  444. // if (c.type === categoryType.key.dropDown) {
  445. // cate.value = parseInt($('select', cateObj).val());
  446. // } else if (c.type === categoryType.key.radio) {
  447. // cate.value = parseInt($('input:checked', cateObj).val());
  448. // }
  449. // data.category.push(cate);
  450. }
  451. postData('/list/update', data, function (result) {
  452. const tender = _.find(tenders, {id: result.id});
  453. _.assign(tender, result);
  454. initTenderTree();
  455. $('.c-body').html(getTenderTreeHtml());
  456. bindTenderUrl();
  457. $('#edit-bd').modal('hide');
  458. });
  459. });
  460. // 删除标段
  461. $('#del-bd-ok').click(function () {
  462. const tid = parseInt($(this).attr('tid'));
  463. if (tid >= 0) {
  464. postData('/list/del', [tid], function (result) {
  465. function getCategory(arr, id) {
  466. for (const a of arr) {
  467. if (a.cid) {
  468. const ac = getCategory(a.children, id);
  469. if (ac) {
  470. return ac;
  471. }
  472. } else if (a.id === id) {
  473. return arr;
  474. }
  475. }
  476. return null;
  477. }
  478. for (const rid of result) {
  479. const tr = $('td[tid=' + rid + ']').parent();
  480. const arr = getCategory(tenderTree, rid);
  481. if (arr) {
  482. const a = arr.find(function (x) {
  483. return x.id === rid;
  484. });
  485. if (arr.length > 1 && arr.indexOf(a) === arr.length - 1) {
  486. const span = $('span', tr.prev());
  487. span.text('└');
  488. }
  489. arr.splice(arr.indexOf(a), 1);
  490. }
  491. _.remove(tenders, function (n) {
  492. return n.id === rid;
  493. });
  494. tr.remove();
  495. $('#del-bd').modal('hide');
  496. }
  497. });
  498. } else {
  499. $('#del-bd').modal('hide');
  500. }
  501. });
  502. // 展开和收起
  503. $('body').on('click', '.fold-switch', function () {
  504. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  505. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  506. $(this).attr('title', '展开');
  507. const cid = $(this).attr('cid');
  508. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  509. doTrStatus(returnItem, 'hide');
  510. } else {
  511. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  512. $(this).attr('title', '收起');
  513. const cid = $(this).attr('cid');
  514. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  515. doTrStatus(returnItem, 'show');
  516. }
  517. })
  518. });
  519. function doTrStatus(node, status) {
  520. if (status === 'show') {
  521. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  522. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  523. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-plus-square-o').removeClass('fa-minus-square-o').addClass('fa-minus-square-o');
  524. } else {
  525. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  526. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  527. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch i').removeClass('fa-minus-square-o').removeClass('fa-plus-square-o').addClass('fa-plus-square-o');
  528. }
  529. // 判断是否还有一层
  530. if (node.children) {
  531. for (const c of node.children) {
  532. doTrStatus(c, status);
  533. }
  534. }
  535. }
  536. let returnItem;
  537. const findTenderTreeNode = function(sortId, tree) {
  538. tree.forEach((item) => {
  539. if (item.sort_id !== undefined && item.sort_id === sortId) {
  540. returnItem = item;
  541. return item;
  542. } else if (item.children && item.children.length > 0) {
  543. findTenderTreeNode(sortId, item.children);
  544. }
  545. });
  546. }