tender_list_manage.js 20 KB

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