tender_list_progress.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. 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. if (!cateValue) return null;
  195. cate = {
  196. cid: category.id,
  197. vid: value,
  198. name: cateValue.value,
  199. children: [],
  200. level: category.level,
  201. };
  202. array.push(cate);
  203. }
  204. return cate;
  205. }
  206. function loadTenderCategory (tender) {
  207. let tenderCategory = null;
  208. for (const lc of levelCategory) {
  209. const tenderCate = findNode('cid', lc.id, tender.category);
  210. if (tenderCate) {
  211. tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
  212. } else {
  213. return tenderCategory;
  214. }
  215. }
  216. return tenderCategory;
  217. }
  218. function calculateTender(tender) {
  219. if (tender.lastStage) {
  220. tender.end_qc_tp = ZhCalc.add(tender.lastStage.pre_qc_tp, tender.lastStage.qc_tp);
  221. tender.pre_gather_tp = ZhCalc.add(tender.lastStage.pre_contract_tp, tender.lastStage.pre_qc_tp);
  222. tender.gather_tp = ZhCalc.add(tender.lastStage.contract_tp, tender.lastStage.qc_tp);
  223. tender.sum_tp = ZhCalc.add(tender.total_price, tender.end_qc_tp);
  224. } else {
  225. tender.sum_tp = tender.total_price;
  226. }
  227. }
  228. tenderTree.splice(0, tenderTree.length);
  229. for (const t of tenders) {
  230. calculateTender(t);
  231. t.valid = true;
  232. if (t.category && levelCategory.length > 0) {
  233. const parent = loadTenderCategory(t);
  234. if (parent) {
  235. t.level = parent.level + 1;
  236. parent.children.push(t);
  237. } else {
  238. tenderTree.push(t);
  239. }
  240. } else {
  241. tenderTree.push(t);
  242. }
  243. }
  244. }
  245. function getProgressHtml(total, pre, cur) {
  246. if (total !== 0) {
  247. let preP = ZhCalc.mul(ZhCalc.div(pre, total, 2), 100, 0);
  248. let curP = ZhCalc.mul(ZhCalc.div(cur, total, 2), 100, 0);
  249. let other = Math.max(ZhCalc.div(ZhCalc.div(total, pre), cur), 0);
  250. let otherP = Math.max(100 - preP - curP, 0);
  251. const html = '<div class="progress">' +
  252. '<div class="progress-bar bg-success" style="width: ' + preP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="截止上期完成:¥' + pre + '">' + preP + '%</div>' +
  253. '<div class="progress-bar bg-info" style="width: ' + curP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="本期完成:¥' + cur + '">' + curP + '%</div>' +
  254. '<div class="progress-bar bg-gray" style="width: ' + otherP + '%;" data-placement="bottom" data-toggle="tooltip" data-original-title="未完成:¥' + other + '">' + otherP + '%</div>' +
  255. '</div>';
  256. return html;
  257. } else {
  258. return '';
  259. }
  260. }
  261. function recursiveGetTenderNodeHtml (node, arr) {
  262. const html = [];
  263. html.push('<tr>');
  264. // 名称
  265. html.push('<td class="in-' + node.level + '">');
  266. if (node.cid) {
  267. html.push('<i class="fa fa-folder-o"></i> ', node.name);
  268. } else {
  269. html.push('<span class="text-muted mr-2">');
  270. html.push(arr.indexOf(node) === arr.length - 1 ? '└' : '├');
  271. html.push('</span>');
  272. //html.push('<a href="/tender/' + node.id + '">', node[c.field], '</a>');
  273. html.push('<a href="javascript: void(0)" id="' + node.id + '">', node.name, '</a>');
  274. }
  275. html.push('</td>');
  276. // 计量期数
  277. html.push('<td>');
  278. if (!node.cid) {
  279. html.push(node.lastStage ? '第' + node.lastStage.order + '期' : '台账');
  280. }
  281. html.push('</td>');
  282. // 累计合同计量
  283. html.push('<td>');
  284. const sum = node.lastStage ? ZhCalc.add(node.total_price, node.lastStage.end_qc_tp) : node.total_price;
  285. html.push(node.sum_tp ? node.sum_tp : '');
  286. html.push('</td>');
  287. // 截止本期累计完成/本期完成/未完成
  288. html.push('<td>');
  289. if (node.lastStage) {
  290. html.push(getProgressHtml(node.sum_tp, node.pre_gather_tp, node.gather_tp));
  291. } else {
  292. html.push('');
  293. }
  294. html.push('</td>');
  295. html.push('</tr>');
  296. if (node.children) {
  297. for (const c of node.children) {
  298. html.push(recursiveGetTenderNodeHtml(c, node.children));
  299. }
  300. }
  301. return html.join('');
  302. }
  303. // 根据TenderTree数据获取Html代码
  304. function getTenderTreeHtml () {
  305. if (tenderTree.length > 0) {
  306. const html = [];
  307. html.push('<table class="table table-hover table-bordered">');
  308. html.push('<thead>', '<tr>');
  309. html.push('<th>', '名称', '</th>');
  310. html.push('<th width="120">', '计量期数', '</th>');
  311. html.push('<th>', '总价 <i class="fa fa-question-circle text-primary" data-placement="bottom" data-toggle="tooltip" data-original-title="0号台账+截止本期数量变更"></i>', '</th>');
  312. html.push('<th>', '截止上期完成/本期完成/未完成', '</th>');
  313. html.push('</tr>', '</thead>');
  314. for (const t of tenderTree) {
  315. html.push(recursiveGetTenderNodeHtml(t, tenderTree));
  316. }
  317. html.push('</table>');
  318. return html.join('');
  319. } else {
  320. return EmptyTenderHtml.join('');
  321. }
  322. }
  323. function bindTenderUrl() {
  324. $('a', '.c-body').bind('click', function () {
  325. const tenderId = parseInt($(this).attr('id'));
  326. const tender = _.find(tenders, function (t) {
  327. return t.id === tenderId;
  328. });
  329. if (tender.measure_type) {
  330. window.location.href = '/tender/' + tenderId;
  331. } else {
  332. for (const a of $('a', '#jlms')) {
  333. a.href = '/tender/' + tenderId + '/type?type=' + $(a).attr('mst');
  334. }
  335. $('#jlms').modal('show');
  336. }
  337. });
  338. }
  339. $(document).ready(() => {
  340. autoFlashHeight();
  341. sortCategory();
  342. // 初始化分类数据
  343. initCategoryLevelNode();
  344. $('.modal-body', '#add-bd').append(getCategoryHtml());
  345. // 初始化标段树结构
  346. initTenderTree();
  347. $('.c-body').html(getTenderTreeHtml());
  348. bindTenderUrl();
  349. // 分类
  350. $('#cate-set').on('show.bs.modal', function () {
  351. createTree();
  352. });
  353. $('#set-cate-ok').click(function () {
  354. const data = [];
  355. const zTree = $.fn.zTree.getZTreeObj('treeLevel');
  356. for (const c of category) {
  357. const node = zTree.getNodeByParam('id', c.id);
  358. const parent = node.getParentNode();
  359. if (parent.lid === 1) {
  360. data.push({id: c.id, level: 0});
  361. } else {
  362. data.push({id: c.id, level: node.getPath().length - 1});
  363. }
  364. }
  365. postData('/setting/category/level', data, function (rst) {
  366. for (const d of data) {
  367. const c = findNode('id', d.id, category);
  368. c.level = d.level;
  369. }
  370. sortCategory();
  371. initCategoryLevelNode();
  372. initTenderTree();
  373. $('.c-body').html(getTenderTreeHtml());
  374. $('#cate-set').modal('hide');
  375. });
  376. });
  377. // 新增标段
  378. $('#add-bd-ok').click(function () {
  379. const data = {
  380. name: $('[name=name]', '#add-bd').val(),
  381. valuation: $('[name=valuation]:checked').val(),
  382. category: [],
  383. };
  384. if (!data.name || data.name === '') {
  385. // TODO 提示用户
  386. return;
  387. }
  388. for (const c of category) {
  389. const cate = {cid: c.id};
  390. if (c.type === categoryType.key.dropDown) {
  391. cate.value = parseInt($('select', '[cate-id=' + c.id + ']').val());
  392. } else if (c.type === categoryType.key.radio) {
  393. cate.value = parseInt($('input:checked', '[cate-id=' + c.id + ']').val());
  394. }
  395. data.category.push(cate);
  396. }
  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. });
  405. });
  406. });