tender_list.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/11
  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('<label>', cate.name, '</label>');
  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. function calculateTender(tender) {
  228. if (tender.lastStage) {
  229. tender.gather_tp = ZhCalc.add(tender.lastStage.contract_tp, tender.lastStage.qc_tp);
  230. tender.end_contract_tp = ZhCalc.add(tender.lastStage.pre_contract_tp, tender.lastStage.contract_tp);
  231. tender.end_qc_tp = ZhCalc.add(tender.lastStage.pre_qc_tp, tender.lastStage.qc_tp);
  232. tender.end_gather_tp = ZhCalc.add(tender.end_contract_tp, tender.end_qc_tp);
  233. tender.pre_gather_tp = ZhCalc.add(tender.lastStage.pre_contract_tp, tender.lastStage.pre_qc_tp);
  234. tender.yf_tp = ZhCalc.add(tender.lastStage.yf_tp);
  235. tender.end_yf_tp = ZhCalc.add(tender.lastStage.pre_yf_tp, tender.yf_tp);
  236. }
  237. }
  238. tenderTree.splice(0, tenderTree.length);
  239. for (const t of tenders) {
  240. calculateTender(t);
  241. t.valid = true;
  242. delete t.level;
  243. if (t.category && levelCategory.length > 0) {
  244. const parent = loadTenderCategory(t);
  245. if (parent) {
  246. t.level = parent.level + 1;
  247. parent.children.push(t);
  248. } else {
  249. tenderTree.push(t);
  250. }
  251. } else {
  252. tenderTree.push(t);
  253. }
  254. }
  255. }
  256. function recursiveGetTenderNodeHtml (node, arr, pid) {
  257. const html = [];
  258. html.push('<tr pid="' + pid + '">');
  259. // 名称
  260. html.push('<td class="in-' + node.level + '">');
  261. if (node.cid) {
  262. 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);
  263. } else {
  264. html.push('<span class="text-muted mr-2">');
  265. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  266. html.push('</span>');
  267. //html.push('<a href="/tender/' + node.id + '">', node[c.field], '</a>');
  268. html.push('<a href="javascript: void(0)" id="' + node.id + '">', node.name, '</a>');
  269. }
  270. html.push('</td>');
  271. // 创建人
  272. html.push('<td>', node.user_name ? 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.measure_type) {
  278. // html.push(node.measure_type === measureType.tz.value ? '0号台账' : '工程量清单');
  279. // }
  280. // html.push('</td>');
  281. // 计量期数
  282. html.push('<td>');
  283. if (!node.cid) {
  284. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '台账');
  285. }
  286. html.push('</td>');
  287. // 审批状态
  288. html.push('<td>');
  289. html.push(node.lastStage ? auditConst.stage.statusString[node.lastStage.status] : auditConst.ledger.statusString[node.ledger_status]);
  290. html.push('</td>');
  291. html.push('</tr>');
  292. if (node.children) {
  293. for (const c of node.children) {
  294. html.push(recursiveGetTenderNodeHtml(c, node.children, node.sort_id));
  295. }
  296. }
  297. return html.join('');
  298. }
  299. // 根据TenderTree数据获取Html代码
  300. function getTenderTreeHtml () {
  301. if (tenderTree.length > 0) {
  302. const html = [];
  303. html.push('<table class="table table-hover table-bordered">');
  304. html.push('<thead>', '<tr>');
  305. html.push('<th>', '标段名称', '</th>');
  306. html.push('<th>', '创建人', '</th>');
  307. html.push('<th>', '创建时间', '</th>');
  308. html.push('<th>', '计量期数', '</th>');
  309. html.push('<th>', '审批状态', '</th>');
  310. html.push('</tr>', '</thead>');
  311. for (const t of tenderTree) {
  312. html.push(recursiveGetTenderNodeHtml(t, tenderTree, ''));
  313. }
  314. html.push('</table>');
  315. return html.join('');
  316. } else {
  317. return EmptyTenderHtml.join('');
  318. }
  319. }
  320. function bindTenderUrl() {
  321. $('a', '.c-body').bind('click', function () {
  322. const tenderId = parseInt($(this).attr('id'));
  323. const tender = _.find(tenders, function (t) {
  324. return t.id === tenderId;
  325. });
  326. if (tender.measure_type) {
  327. window.location.href = '/tender/' + tenderId;
  328. } else {
  329. for (const a of $('a', '#jlms')) {
  330. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  331. }
  332. $('#jlms').modal('show');
  333. }
  334. });
  335. }
  336. $(document).ready(() => {
  337. autoFlashHeight();
  338. sortCategory();
  339. // 初始化分类数据
  340. initCategoryLevelNode();
  341. $('.modal-body', '#add-bd').append(getCategoryHtml());
  342. // 初始化标段树结构
  343. initTenderTree();
  344. $('.c-body').html(getTenderTreeHtml());
  345. bindTenderUrl();
  346. // 分类
  347. $('#cate-set').on('show.bs.modal', function () {
  348. createTree();
  349. });
  350. $('#set-cate-ok').click(function () {
  351. const data = [];
  352. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  353. for (const c of category) {
  354. const node = zTree.getNodeByParam('id', c.id);
  355. const parent = node.getParentNode();
  356. if (parent.lid === 1) {
  357. data.push({id: c.id, level: 0});
  358. } else {
  359. data.push({id: c.id, level: node.getPath().length - 1});
  360. }
  361. }
  362. postData('/setting/category/level', data, function (rst) {
  363. for (const d of data) {
  364. const c = findNode('id', d.id, category);
  365. c.level = d.level;
  366. }
  367. sortCategory();
  368. initCategoryLevelNode();
  369. initTenderTree();
  370. $('.c-body').html(getTenderTreeHtml());
  371. $('#cate-set').modal('hide');
  372. });
  373. });
  374. // 新增标段
  375. $('#add-bd-ok').click(function () {
  376. const data = {
  377. name: $('[name=name]', '#add-bd').val(),
  378. valuation: $('[name=valuation]:checked').val(),
  379. category: [],
  380. };
  381. if (!data.name || data.name === '') {
  382. // TODO 提示用户
  383. return;
  384. }
  385. for (const c of category) {
  386. if (parseInt($('select', '[cate-id=' + c.id + ']').val()) !== 0) {
  387. const cate = {cid: c.id};
  388. // if (c.type === categoryType.key.dropDown) {
  389. cate.value = parseInt($('select', '[cate-id=' + c.id + ']').val());
  390. // } else if (c.type === categoryType.key.radio) {
  391. // cate.value = parseInt($('input:checked', '[cate-id=' + c.id + ']').val());
  392. // }
  393. data.category.push(cate);
  394. }
  395. }
  396. $('#hide-all').show();
  397. postData('/list/add', data, function (result) {
  398. tenders.push(result);
  399. initTenderTree();
  400. $('.c-body').html(getTenderTreeHtml());
  401. bindTenderUrl();
  402. $('#add-bd').modal('hide');
  403. $('[name=name]', '#add-bd').val('');
  404. $('#hide-all').hide();
  405. });
  406. });
  407. // 展开和收起
  408. $('body').on('click', '.fold-switch', function () {
  409. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  410. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  411. $(this).attr('title', '展开');
  412. const cid = $(this).attr('cid');
  413. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  414. doTrStatus(returnItem, 'hide');
  415. } else {
  416. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  417. $(this).attr('title', '收起');
  418. const cid = $(this).attr('cid');
  419. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  420. doTrStatus(returnItem, 'show');
  421. }
  422. })
  423. });
  424. function doTrStatus(node, status) {
  425. if (status === 'show') {
  426. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  427. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  428. $('.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');
  429. } else {
  430. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  431. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  432. $('.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');
  433. }
  434. // 判断是否还有一层
  435. if (node.children) {
  436. for (const c of node.children) {
  437. doTrStatus(c, status);
  438. }
  439. }
  440. }
  441. let returnItem;
  442. const findTenderTreeNode = function(sortId, tree) {
  443. tree.forEach((item) => {
  444. if (item.sort_id !== undefined && item.sort_id === sortId) {
  445. returnItem = item;
  446. return item;
  447. } else if (item.children && item.children.length > 0) {
  448. findTenderTreeNode(sortId, item.children);
  449. }
  450. });
  451. }