tender_list_progress.js 16 KB

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