ledger.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const itemsPre = 'id_';
  10. class billsTree {
  11. /**
  12. * 构造函数
  13. */
  14. constructor (ctx, setting) {
  15. this.ctx = ctx;
  16. // 无索引
  17. this.datas = [];
  18. // 以key为索引
  19. this.items = {};
  20. // 以排序为索引
  21. this.nodes = [];
  22. // 根节点
  23. this.children = [];
  24. // 树设置
  25. this.setting = setting;
  26. }
  27. /**
  28. * 根据id获取树结构节点数据
  29. * @param {Number} id
  30. * @returns {Object}
  31. */
  32. getItems (id) {
  33. return this.items[itemsPre + id];
  34. };
  35. /**
  36. * 查找node的parent
  37. * @param {Object} node
  38. * @returns {Object}
  39. */
  40. getParent (node) {
  41. return this.getItems(node[this.setting.pid]);
  42. };
  43. /**
  44. * 查询node的已下载子节点
  45. * @param {Object} node
  46. * @returns {Array}
  47. */
  48. getChildren (node) {
  49. const setting = this.setting;
  50. const pid = node ? node[setting.id] : setting.rootId;
  51. const children = this.datas.filter(function (x) {
  52. return x[setting.pid] === pid;
  53. });
  54. children.sort(function (a, b) {
  55. return a.order - b.order;
  56. });
  57. return children;
  58. };
  59. getNodeSerialNo(node) {
  60. return this.nodes.indexOf(node);
  61. }
  62. /**
  63. * 树结构根据显示排序
  64. */
  65. sortTreeNode (isResort) {
  66. const self = this;
  67. const addSortNodes = function (nodes) {
  68. if (!nodes) { return }
  69. for (let i = 0; i < nodes.length; i++) {
  70. self.nodes.push(nodes[i]);
  71. nodes[i].index = self.nodes.length - 1;
  72. if (!isResort) {
  73. nodes[i].children = self.getChildren(nodes[i]);
  74. } else {
  75. nodes[i].children.sort(function (a, b) {
  76. return a.order - b.order;
  77. })
  78. }
  79. addSortNodes(nodes[i].children);
  80. }
  81. };
  82. this.nodes = [];
  83. if (!isResort) {
  84. this.children = this.getChildren();
  85. } else {
  86. this.children.sort(function (a, b) {
  87. return a.order - b.order;
  88. })
  89. }
  90. addSortNodes(this.children);
  91. }
  92. /**
  93. * 加载数据(初始化), 并给数据添加部分树结构必须数据
  94. * @param datas
  95. */
  96. loadDatas (datas) {
  97. // 清空旧数据
  98. this.items = {};
  99. this.nodes = [];
  100. this.datas = [];
  101. this.children = [];
  102. // 加载全部数据
  103. datas.sort(function (a, b) {
  104. return a.level - b.level;
  105. });
  106. for (const data of datas) {
  107. const keyName = itemsPre + data[this.setting.id];
  108. if (!this.items[keyName]) {
  109. const item = JSON.parse(JSON.stringify(data));
  110. item.children = [];
  111. item.expanded = true;
  112. item.visible = true;
  113. this.items[keyName] = item;
  114. this.datas.push(item);
  115. if (item[this.setting.pid] === this.setting.rootId) {
  116. this.children.push(item);
  117. } else {
  118. const parent = this.getParent(item);
  119. if (parent) {
  120. parent.children.push(item);
  121. }
  122. }
  123. }
  124. }
  125. this.children.sort(function (a, b) {
  126. return a.order - b.order;
  127. });
  128. this.sortTreeNode(true);
  129. }
  130. /**
  131. * 递归方式 查询node的已下载的全部后代 (兼容full_path不存在的情况)
  132. * @param node
  133. * @returns {*}
  134. * @private
  135. */
  136. _recursiveGetPosterity (node) {
  137. let posterity = node.children;
  138. for (const c of node.children) {
  139. posterity = posterity.concat(this._recursiveGetPosterity(c));
  140. }
  141. return posterity;
  142. };
  143. /**
  144. * 查询node的已下载的全部后代
  145. * @param {Object} node
  146. * @returns {Array}
  147. */
  148. getPosterity (node) {
  149. if (node.full_path !== '') {
  150. const reg = new RegExp('^' + node.full_path + '-');
  151. return this.datas.filter(function (x) {
  152. return reg.test(x.full_path);
  153. });
  154. } else {
  155. return this._recursiveGetPosterity(node);
  156. }
  157. };
  158. /**
  159. * 检查节点是否是最底层项目节
  160. * @param node
  161. * @returns {boolean}
  162. */
  163. isLeafXmj(node) {
  164. if (node.b_code && node.b_code !== '') {
  165. return false;
  166. }
  167. for (const child of node.children) {
  168. if (!child.b_code || child.b_code === '') {
  169. return false;
  170. }
  171. }
  172. return true;
  173. }
  174. /**
  175. * 查询最底层项目节(本身或父项)
  176. * @param {Object} node - 查询节点
  177. * @returns {Object}
  178. */
  179. getLeafXmjParent(node) {
  180. let parent = node;
  181. while (parent) {
  182. if (this.isLeafXmj(parent)) {
  183. return parent;
  184. } else {
  185. parent = this.getParent(parent);
  186. }
  187. }
  188. return null;
  189. }
  190. _mapTreeNode () {
  191. let map = {}, maxLevel = 0;
  192. for (const node of this.nodes) {
  193. let levelArr = map[node.level];
  194. if (!levelArr) {
  195. levelArr = [];
  196. map[node.level] = levelArr;
  197. }
  198. if (node.level > maxLevel) {
  199. maxLevel = node.level;
  200. }
  201. levelArr.push(node);
  202. }
  203. return [maxLevel, map];
  204. }
  205. _calculateNode (node, fun) {
  206. const self = this;
  207. if (node.children && node.children.length > 0) {
  208. const gather = node.children.reduce(function (rst, x) {
  209. const result = {};
  210. for (const cf of self.setting.calcFields) {
  211. result[cf] = self.ctx.helper.add(rst[cf], x[cf]);
  212. }
  213. return result;
  214. });
  215. // 汇总子项
  216. for (const cf of this.setting.calcFields) {
  217. if (gather[cf]) {
  218. node[cf] = gather[cf];
  219. } else {
  220. node[cf] = null;
  221. }
  222. }
  223. }
  224. // 自身运算
  225. if (fun) {
  226. fun(node);
  227. } else if (this.setting.calc) {
  228. this.setting.calc(node);
  229. }
  230. }
  231. calculateAll(fun) {
  232. const [maxLevel, levelMap] = this._mapTreeNode();
  233. for (let i = maxLevel; i >= 0; i--) {
  234. const levelNodes = levelMap[i];
  235. if (levelNodes && levelNodes.length > 0) {
  236. for (const node of levelNodes) {
  237. this._calculateNode(node, fun);
  238. }
  239. }
  240. }
  241. }
  242. getDatas (fields) {
  243. const datas = [];
  244. for (const node of this.nodes) {
  245. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  246. const data = {};
  247. for (const field of fields) {
  248. data[field] = node[field];
  249. }
  250. datas.push(data);
  251. }
  252. return datas;
  253. }
  254. getDatasWithout (fields) {
  255. const datas = [];
  256. for (const node of this.nodes) {
  257. if (node.b_code && node.b_code !== '') node.chapter = this.ctx.helper.getChapterCode(node.b_code);
  258. const data = {};
  259. for (const field in node) {
  260. if (fields.indexOf(field) === -1) {
  261. data[field] = node[field];
  262. }
  263. }
  264. datas.push(data);
  265. }
  266. return datas;
  267. }
  268. getDefaultDatas() {
  269. return this.getDatasWithout(['expanded', 'visible', 'children', 'index']);
  270. }
  271. }
  272. class pos {
  273. /**
  274. * 构造函数
  275. * @param {id|Number, masterId|Number} setting
  276. */
  277. constructor (setting) {
  278. // 无索引
  279. this.datas = [];
  280. // 以key为索引
  281. this.items = {};
  282. // 以分类id为索引的有序
  283. this.ledgerPos = {};
  284. // pos设置
  285. this.setting = setting;
  286. }
  287. /**
  288. * 加载部位明细数据
  289. * @param datas
  290. */
  291. loadDatas(datas) {
  292. this.datas = datas;
  293. this.items = {};
  294. this.ledgerPos = {};
  295. for (const data of this.datas) {
  296. const key = itemsPre + data[this.setting.id];
  297. this.items[key] = data;
  298. const masterKey = itemsPre + data[this.setting.ledgerId];
  299. if (!this.ledgerPos[masterKey]) {
  300. this.ledgerPos[masterKey] = [];
  301. }
  302. this.ledgerPos[masterKey].push(data);
  303. }
  304. for (const prop in this.ledgerPos) {
  305. this.resortLedgerPos(this.ledgerPos[prop]);
  306. }
  307. }
  308. getLedgerPos(mid) {
  309. return this.ledgerPos[itemsPre + mid];
  310. }
  311. resortLedgerPos(ledgerPos) {
  312. if (ledgerPos instanceof Array) {
  313. ledgerPos.sort(function (a, b) {
  314. return a.porder - b.porder;
  315. })
  316. }
  317. }
  318. /**
  319. * 计算全部
  320. */
  321. calculateAll(fun) {
  322. const calcFun = fun ? fun : this.setting.calc;
  323. if (!calcFun) return;
  324. for (const pos of this.datas) {
  325. calcFun(pos);
  326. }
  327. }
  328. getDatas () {
  329. return this.datas;
  330. }
  331. }
  332. module.exports = {
  333. billsTree,
  334. pos,
  335. };