tender_list_progress.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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('<lable>', cate.name, '</lable>');
  144. html.push('<select class="form-control">');
  145. for (const v of cate.value) {
  146. html.push('<option value="' + v.id + '">', v.value, '</option>');
  147. }
  148. html.push('</select>');
  149. html.push('</div>');
  150. return html.join('');
  151. }
  152. function getRadioCategoryHtml (cate) {
  153. const html = [];
  154. html.push('<div class="form-group" cate-id="' + cate.id + '">');
  155. html.push('<lable>', cate.name, '</lable>');
  156. html.push('<div>');
  157. for (const iV in cate.value) {
  158. const v = cate.value[iV];
  159. html.push('<div class="form-check-inline">');
  160. html.push('<input class="form-check-input" type="radio"', 'name="' + cate.name + '" ', 'value="' , v.id, (iV == 0 ? '" checked="' : ''), '">');
  161. html.push('<label class="form-check-label">', v.value, '</label>');
  162. html.push('</div>');
  163. }
  164. html.push('</div>');
  165. html.push('</div>');
  166. return html.join('');
  167. }
  168. const html = [];
  169. for (const c of category) {
  170. // if (c.type === categoryType.key.dropDown) {
  171. html.push(getSelectCategoryHtml(c));
  172. // } else if (c.type === categoryType.key.radio) {
  173. // html.push(getRadioCategoryHtml(c));
  174. // }
  175. }
  176. return html.join('');
  177. }
  178. // 初始化TenderTree数据
  179. function initTenderTree () {
  180. const levelCategory = category.filter(function (c) {
  181. return c.level && c.level > 0;
  182. });
  183. function findCategoryNode(cid, value, array) {
  184. for (const a of array) {
  185. if (a.cid === cid && a.vid === value) {
  186. return a;
  187. }
  188. }
  189. }
  190. function getCategoryNode(category, value, parent, i = null) {
  191. const array = parent ? parent.children : tenderTree;
  192. let cate = findCategoryNode(category.id, value, array);
  193. if (!cate) {
  194. const cateValue = findNode('id', value, category.value);
  195. if (!cateValue) return null;
  196. cate = {
  197. cid: category.id,
  198. vid: value,
  199. name: cateValue.value,
  200. children: [],
  201. level: i ? i : category.level,
  202. sort_id: ++parentId,
  203. };
  204. array.push(cate);
  205. }
  206. return cate;
  207. }
  208. function loadTenderCategory (tender) {
  209. let tenderCategory = null;
  210. for (const [index, lc] of levelCategory.entries()) {
  211. const tenderCate = findNode('cid', lc.id, tender.category);
  212. if (tenderCate) {
  213. tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
  214. } else {
  215. if (index === 0 && tender.category) {
  216. for (const [i,c] of tender.category.entries()) {
  217. const cate = findNode('id', c.cid, category);
  218. tenderCategory = getCategoryNode(cate, c.value, tenderCategory, i+1);
  219. }
  220. }
  221. return tenderCategory;
  222. }
  223. }
  224. return tenderCategory;
  225. }
  226. function calculateTender(tender) {
  227. if (tender.lastStage) {
  228. tender.end_qc_tp = ZhCalc.add(tender.lastStage.pre_qc_tp, tender.lastStage.qc_tp);
  229. tender.pre_gather_tp = ZhCalc.add(tender.lastStage.pre_contract_tp, tender.lastStage.pre_qc_tp);
  230. tender.gather_tp = ZhCalc.add(tender.lastStage.contract_tp, tender.lastStage.qc_tp);
  231. tender.sum_tp = ZhCalc.add(tender.total_price, tender.end_qc_tp);
  232. } else {
  233. tender.sum_tp = tender.total_price;
  234. }
  235. }
  236. tenderTree.splice(0, tenderTree.length);
  237. for (const t of tenders) {
  238. calculateTender(t);
  239. t.valid = true;
  240. delete t.level;
  241. if (t.category && levelCategory.length > 0) {
  242. const parent = loadTenderCategory(t);
  243. if (parent) {
  244. t.level = parent.level + 1;
  245. parent.children.push(t);
  246. } else {
  247. tenderTree.push(t);
  248. }
  249. } else {
  250. tenderTree.push(t);
  251. }
  252. }
  253. }
  254. function getProgressHtml(total, pre, cur) {
  255. if (total !== 0) {
  256. let preP = ZhCalc.mul(ZhCalc.div(pre, total, 2), 100, 0);
  257. let curP = ZhCalc.mul(ZhCalc.div(cur, total, 2), 100, 0);
  258. let other = Math.max(ZhCalc.sub(ZhCalc.sub(total, pre), cur), 0);
  259. let otherP = Math.max(100 - preP - curP, 0);
  260. const html = '<div class="progress">' +
  261. '<div class="progress-bar bg-success" style="width: ' + preP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="截止上期完成:¥' + pre + '">' + preP + '%</div>' +
  262. '<div class="progress-bar bg-info" style="width: ' + curP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="本期完成:¥' + cur + '">' + curP + '%</div>' +
  263. '<div class="progress-bar bg-gray" style="width: ' + otherP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="未完成:¥' + other + '">' + otherP + '%</div>' +
  264. '</div>';
  265. return html;
  266. } else {
  267. return '';
  268. }
  269. }
  270. function recursiveGetTenderNodeHtml (node, arr, pid) {
  271. const html = [];
  272. html.push('<tr pid="' + pid + '">');
  273. // 名称
  274. html.push('<td class="in-' + node.level + '">');
  275. if (node.cid) {
  276. 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);
  277. } else {
  278. html.push('<span class="text-muted mr-2">');
  279. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  280. html.push('</span>');
  281. //html.push('<a href="/tender/' + node.id + '">', node[c.field], '</a>');
  282. html.push('<a href="javascript: void(0)" id="' + node.id + '">', node.name, '</a>');
  283. }
  284. html.push('</td>');
  285. // 计量期数
  286. html.push('<td>');
  287. if (!node.cid) {
  288. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '台账');
  289. }
  290. html.push('</td>');
  291. // 累计合同计量
  292. html.push('<td>');
  293. const sum = node.lastStage ? ZhCalc.add(node.total_price, node.lastStage.end_qc_tp) : node.total_price;
  294. html.push(node.sum_tp ? node.sum_tp : '');
  295. html.push('</td>');
  296. // 截止本期累计完成/本期完成/未完成
  297. html.push('<td>');
  298. if (node.lastStage) {
  299. html.push(getProgressHtml(node.sum_tp, node.pre_gather_tp, node.gather_tp));
  300. } else {
  301. html.push('');
  302. }
  303. html.push('</td>');
  304. html.push('</tr>');
  305. if (node.children) {
  306. for (const c of node.children) {
  307. html.push(recursiveGetTenderNodeHtml(c, node.children, node.sort_id));
  308. }
  309. }
  310. return html.join('');
  311. }
  312. // 根据TenderTree数据获取Html代码
  313. function getTenderTreeHtml () {
  314. if (tenderTree.length > 0) {
  315. const html = [];
  316. html.push('<table class="table table-hover table-bordered">');
  317. html.push('<thead>', '<tr>');
  318. html.push('<th>', '名称', '</th>');
  319. html.push('<th width="120">', '计量期数', '</th>');
  320. html.push('<th>', '总价 <i class="fa fa-question-circle text-primary" data-placement="bottom" data-toggle="tooltip" data-original-title="0号台账+截止本期数量变更"></i>', '</th>');
  321. html.push('<th>', '截止上期完成/本期完成/未完成', '</th>');
  322. html.push('</tr>', '</thead>');
  323. for (const t of tenderTree) {
  324. html.push(recursiveGetTenderNodeHtml(t, tenderTree, ''));
  325. }
  326. html.push('</table>');
  327. return html.join('');
  328. } else {
  329. return EmptyTenderHtml.join('');
  330. }
  331. }
  332. function bindTenderUrl() {
  333. $('a', '.c-body').bind('click', function () {
  334. const tenderId = parseInt($(this).attr('id'));
  335. const tender = _.find(tenders, function (t) {
  336. return t.id === tenderId;
  337. });
  338. if (tender.measure_type) {
  339. window.location.href = '/tender/' + tenderId;
  340. } else {
  341. for (const a of $('a', '#jlms')) {
  342. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  343. }
  344. $('#jlms').modal('show');
  345. }
  346. });
  347. }
  348. $(document).ready(() => {
  349. autoFlashHeight();
  350. sortCategory();
  351. // 初始化分类数据
  352. initCategoryLevelNode();
  353. $('.modal-body', '#add-bd').append(getCategoryHtml());
  354. // 初始化标段树结构
  355. initTenderTree();
  356. $('.c-body').html(getTenderTreeHtml());
  357. bindTenderUrl();
  358. // 分类
  359. $('#cate-set').on('show.bs.modal', function () {
  360. createTree();
  361. });
  362. $('#set-cate-ok').click(function () {
  363. const data = [];
  364. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  365. for (const c of category) {
  366. const node = zTree.getNodeByParam('id', c.id);
  367. const parent = node.getParentNode();
  368. if (parent.lid === 1) {
  369. data.push({id: c.id, level: 0});
  370. } else {
  371. data.push({id: c.id, level: node.getPath().length - 1});
  372. }
  373. }
  374. postData('/setting/category/level', data, function (rst) {
  375. for (const d of data) {
  376. const c = findNode('id', d.id, category);
  377. c.level = d.level;
  378. }
  379. sortCategory();
  380. initCategoryLevelNode();
  381. initTenderTree();
  382. $('.c-body').html(getTenderTreeHtml());
  383. $('#cate-set').modal('hide');
  384. });
  385. });
  386. // 新增标段
  387. $('#add-bd-ok').click(function () {
  388. const data = {
  389. name: $('[name=name]', '#add-bd').val(),
  390. valuation: $('[name=valuation]:checked').val(),
  391. category: [],
  392. };
  393. if (!data.name || data.name === '') {
  394. // TODO 提示用户
  395. return;
  396. }
  397. for (const c of category) {
  398. if (parseInt($('select', '[cate-id=' + c.id + ']').val()) !== 0) {
  399. const cate = {cid: c.id};
  400. // if (c.type === categoryType.key.dropDown) {
  401. cate.value = parseInt($('select', '[cate-id=' + c.id + ']').val());
  402. // } else if (c.type === categoryType.key.radio) {
  403. // cate.value = parseInt($('input:checked', '[cate-id=' + c.id + ']').val());
  404. // }
  405. data.category.push(cate);
  406. }
  407. }
  408. postData('/list/add', data, function (result) {
  409. tenders.push(result);
  410. initTenderTree();
  411. $('.c-body').html(getTenderTreeHtml());
  412. bindTenderUrl();
  413. $('#add-bd').modal('hide');
  414. $('[name=name]', '#add-bd').val('');
  415. });
  416. });
  417. // 展开和收起
  418. $('body').on('click', '.fold-switch', function () {
  419. if ($(this).children('i').hasClass('fa-minus-square-o')) {
  420. $(this).children('i').removeClass('fa-minus-square-o').addClass('fa-plus-square-o');
  421. $(this).attr('title', '展开');
  422. const cid = $(this).attr('cid');
  423. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  424. doTrStatus(returnItem, 'hide');
  425. } else {
  426. $(this).children('i').removeClass('fa-plus-square-o').addClass('fa-minus-square-o');
  427. $(this).attr('title', '收起');
  428. const cid = $(this).attr('cid');
  429. const node = findTenderTreeNode(parseInt(cid), tenderTree);
  430. doTrStatus(returnItem, 'show');
  431. }
  432. })
  433. });
  434. function doTrStatus(node, status) {
  435. if (status === 'show') {
  436. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').show();
  437. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '收起');
  438. $('.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');
  439. } else {
  440. $('.c-body').find('tr[pid="'+ node.sort_id +'"]').hide();
  441. $('.c-body').find('tr[pid="'+ node.sort_id +'"] .fold-switch').attr('title', '展开');
  442. $('.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');
  443. }
  444. // 判断是否还有一层
  445. if (node.children) {
  446. for (const c of node.children) {
  447. doTrStatus(c, status);
  448. }
  449. }
  450. }
  451. let returnItem;
  452. const findTenderTreeNode = function(sortId, tree) {
  453. tree.forEach((item) => {
  454. if (item.sort_id !== undefined && item.sort_id === sortId) {
  455. returnItem = item;
  456. return item;
  457. } else if (item.children && item.children.length > 0) {
  458. findTenderTreeNode(sortId, item.children);
  459. }
  460. });
  461. }