ledger.js 8.5 KB

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