tender_list_manage.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. console.log(node);
  236. html.push('<tr>');
  237. // 名称
  238. html.push('<td class="in-' + node.level + '">');
  239. if (node.cid) {
  240. html.push('<i class="fa fa-folder-o"></i> ', node.name);
  241. } else {
  242. html.push('<span class="text-muted mr-2">');
  243. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  244. html.push('</span>');
  245. html.push('<a href="javascript: void(0)" name="name" id="' + node.id + '">', node.name, '</a>');
  246. }
  247. html.push('</td>');
  248. // 创建人
  249. html.push('<td>', node.user_name, '</td>');
  250. // 创建时间
  251. html.push('<td>', node.create_time ? moment(node.create_time).format('YYYY-MM-DD hh:mm:ss') : '', '</td>');
  252. // 完成期数
  253. html.push('<td>');
  254. if (!node.cid) {
  255. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '第0期');
  256. }
  257. html.push('</td>');
  258. // 管理
  259. html.push('<td tid="' + node.id + '">');
  260. if (!node.cid) {
  261. html.push('<a href="#javascript: void(0)" name="edit" class="btn btn-outline-primary btn-sm">编辑</a>');
  262. if (node.ledger_status === auditConst.ledger.status.uncheck) {
  263. html.push('<a href="javascript: void(0)" name="del" class="btn btn-outline-danger btn-sm">删除</a>');
  264. }
  265. }
  266. html.push('</td>');
  267. html.push('</tr>');
  268. if (node.children) {
  269. for (const c of node.children) {
  270. html.push(recursiveGetTenderNodeHtml(c, node.children));
  271. }
  272. }
  273. return html.join('');
  274. }
  275. function getTenderTreeHeaderHtml() {
  276. const html = [];
  277. html.push('<thead>', '<tr>');
  278. html.push('<th>', '名称', '</th>');
  279. html.push('<th>', '创建人', '</th>');
  280. html.push('<th>', '创建时间', '</th>');
  281. html.push('<th>', '完成期数', '</th>');
  282. html.push('<th>', '管理', '</th>');
  283. html.push('</tr>', '</thead>');
  284. return html.join('');
  285. }
  286. // 根据TenderTree数据获取Html代码
  287. function getTenderTreeHtml () {
  288. if (tenderTree.length > 0) {
  289. const html = [];
  290. html.push('<table class="table table-bordered">');
  291. html.push(getTenderTreeHeaderHtml());
  292. for (const t of tenderTree) {
  293. html.push(recursiveGetTenderNodeHtml(t, tenderTree));
  294. }
  295. html.push('</table>');
  296. return html.join('');
  297. } else {
  298. return EmptyTenderHtml.join('');
  299. }
  300. }
  301. function bindTenderUrl() {
  302. // 打开标段
  303. $('a[name=name]', '.c-body').bind('click', function () {
  304. const tenderId = parseInt($(this).attr('id'));
  305. const tender = _.find(tenders, function (t) {
  306. return t.id === tenderId;
  307. });
  308. if (tender.measure_type) {
  309. window.location.href = '/tender/' + tenderId;
  310. } else {
  311. for (const a of $('a', '#jlms')) {
  312. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  313. }
  314. $('#jlms').modal('show');
  315. }
  316. });
  317. // 编辑
  318. $('a[name=edit]', '.c-body').bind('click', function () {
  319. const tid = parseInt($(this).parent().attr('tid'));
  320. const tender = _.find(tenders, {id: tid});
  321. $('[name=name]', '#edit-bd').val(tender.name);
  322. for (const c of tender.category) {
  323. $('input[value=' + c.value + ']', '#edit-bd').attr('checked', 'checked');
  324. $('option[value=' + c.value + ']', '#edit-bd').attr('selected', true);
  325. }
  326. $('#edit-bd-ok').attr('tid', tid);
  327. $('#edit-bd').modal('show');
  328. });
  329. // 删除
  330. $('a[name=del]', '.c-body').bind('click', function () {
  331. $('#del-bd-ok').attr('tid', $(this).parent().attr('tid'));
  332. $('#del-bd').modal('show');
  333. });
  334. }
  335. $(document).ready(() => {
  336. sortCategory();
  337. // 初始化分类数据
  338. initCategoryLevelNode();
  339. $('.modal-body', '#add-bd').append(getCategoryHtml());
  340. $('.modal-body', '#edit-bd').append(getCategoryHtml());
  341. // 初始化标段树结构
  342. initTenderTree();
  343. $('.c-body').html(getTenderTreeHtml());
  344. bindTenderUrl();
  345. // 分类
  346. $('#cate-set').on('show.bs.modal', function () {
  347. createTree();
  348. });
  349. $('#set-cate-ok').click(function () {
  350. const data = [];
  351. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  352. for (const c of category) {
  353. const node = zTree.getNodeByParam('id', c.id);
  354. const parent = node.getParentNode();
  355. if (parent.lid === 1) {
  356. data.push({id: c.id, level: 0});
  357. } else {
  358. data.push({id: c.id, level: node.getPath().length - 1});
  359. }
  360. }
  361. postData('/setting/category/level', data, function (rst) {
  362. for (const d of data) {
  363. const c = findNode('id', d.id, category);
  364. c.level = d.level;
  365. }
  366. sortCategory();
  367. initCategoryLevelNode();
  368. initTenderTree();
  369. $('.c-body').html(getTenderTreeHtml());
  370. $('#cate-set').modal('hide');
  371. });
  372. });
  373. // 新增标段
  374. $('#add-bd-ok').click(function () {
  375. const data = {
  376. name: $('[name=name]', '#add-bd').val(),
  377. category: [],
  378. };
  379. if (!data.name || data.name === '') {
  380. // TODO 提示用户
  381. return;
  382. }
  383. for (const c of category) {
  384. const cate = {cid: c.id};
  385. const cateObj = $('[cate-id=' + c.id + ']', '#add-bd');
  386. if (c.type === categoryType.key.dropDown) {
  387. cate.value = parseInt($('select', cateObj).val());
  388. } else if (c.type === categoryType.key.radio) {
  389. cate.value = parseInt($('input:checked', cateObj).val());
  390. }
  391. data.category.push(cate);
  392. }
  393. postData('/list/add', data, function (result) {
  394. tenders.push(result);
  395. initTenderTree();
  396. $('.c-body').html(getTenderTreeHtml());
  397. bindTenderUrl();
  398. $('#add-bd').modal('hide');
  399. $('[name=name]', '#add-bd').val('');
  400. });
  401. });
  402. // 编辑标段
  403. $('#edit-bd-ok').click(function () {
  404. const data = {
  405. id: parseInt($(this).attr('tid')),
  406. name: $('[name=name]', '#edit-bd').val(),
  407. category: [],
  408. };
  409. if (!data.name || data.name === '') {
  410. // TODO 提示用户
  411. return;
  412. }
  413. for (const c of category) {
  414. const cate = {cid: c.id};
  415. const cateObj = $('[cate-id=' + c.id + ']', '#edit-bd');
  416. if (c.type === categoryType.key.dropDown) {
  417. cate.value = parseInt($('select', cateObj).val());
  418. } else if (c.type === categoryType.key.radio) {
  419. cate.value = parseInt($('input:checked', cateObj).val());
  420. }
  421. data.category.push(cate);
  422. }
  423. postData('/list/update', data, function (result) {
  424. const tender = _.find(tenders, {id: result.id});
  425. _.assign(tender, result);
  426. initTenderTree();
  427. $('.c-body').html(getTenderTreeHtml());
  428. bindTenderUrl();
  429. $('#edit-bd').modal('hide');
  430. });
  431. });
  432. // 删除标段
  433. $('#del-bd-ok').click(function () {
  434. const tid = parseInt($(this).attr('tid'));
  435. if (tid >= 0) {
  436. postData('/list/del', [tid], function (result) {
  437. function getCategory(arr, id) {
  438. for (const a of arr) {
  439. if (a.cid) {
  440. const ac = getCategory(a.children, id);
  441. if (ac) {
  442. return ac;
  443. }
  444. } else if (a.id === id) {
  445. return arr;
  446. }
  447. }
  448. return null;
  449. }
  450. for (const rid of result) {
  451. const tr = $('td[tid=' + rid + ']').parent();
  452. const arr = getCategory(tenderTree, rid);
  453. if (arr) {
  454. const a = arr.find(function (x) {
  455. return x.id === rid;
  456. });
  457. console.log(arr.indexOf(a));
  458. console.log(arr.length - 1);
  459. if (arr.length > 1 && arr.indexOf(a) === arr.length - 1) {
  460. const span = $('span', tr.prev());
  461. span.text('└');
  462. }
  463. arr.splice(arr.indexOf(a), 1);
  464. }
  465. _.remove(tenders, function (n) {
  466. return n.id === rid;
  467. });
  468. tr.remove();
  469. $('#del-bd').modal('hide');
  470. }
  471. });
  472. } else {
  473. $('#del-bd').modal('hide');
  474. }
  475. });
  476. });