ledger.test.js 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /**
  2. * 标段--台账 模型 单元测试
  3. *
  4. * @author Mai
  5. * @date 2017/2/1
  6. * @version
  7. */
  8. 'use strict';
  9. const excel = require('js-xlsx');
  10. const _ = require('lodash');
  11. const mockData = {};
  12. /*const testNodeData = [
  13. { ledger_id: 1, ledger_pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
  14. { ledger_id: 2, ledger_pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
  15. { ledger_id: 6, ledger_pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
  16. { ledger_id: 7, ledger_pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
  17. { ledger_id: 10, ledger_pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
  18. { ledger_id: 9, ledger_pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
  19. { ledger_id: 8, ledger_pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
  20. { ledger_id: 11, ledger_pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
  21. { ledger_id: 12, ledger_pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
  22. { ledger_id: 13, ledger_pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
  23. { ledger_id: 14, ledger_pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
  24. { ledger_id: 3, ledger_pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
  25. { ledger_id: 15, ledger_pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
  26. { ledger_id: 4, ledger_pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
  27. { ledger_id: 16, ledger_pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
  28. { ledger_id: 5, ledger_pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
  29. ];*/
  30. const testNodeData = [
  31. { id: 1, pid: -1, order: 1, level: 1, full_path: '1', code: '1', is_leaf: false },
  32. { id: 2, pid: 1, order: 1, level: 2, full_path: '1.2', code: '1-1', is_leaf: false },
  33. { id: 6, pid: 2, order: 1, level: 3, full_path: '1.2.6', code: '1-1-1', is_leaf: false },
  34. { id: 7, pid: 6, order: 1, level: 4, full_path: '1.2.6.7', code: '202-1', is_leaf: false },
  35. { id: 10, pid: 7, order: 2, level: 5, full_path: '1.2.6.7.10', code: '202-1-a', is_leaf: true },
  36. { id: 9, pid: 7, order: 1, level: 5, full_path: '1.2.6.7.9', code: '202-1-b', is_leaf: true },
  37. { id: 8, pid: 6, order: 2, level: 4, full_path: '1.2.6.8', code: '202-2', is_leaf: false },
  38. { id: 11, pid: 8, order: 1, level: 5, full_path: '1.2.6.8.11', code: '202-2-c', is_leaf: true },
  39. { id: 12, pid: 8, order: 2, level: 5, full_path: '1.2.6.8.12', code: '202-2-e', is_leaf: true },
  40. { id: 13, pid: 2, order: 2, level: 3, full_path: '1.2.13', code: '1-1-2', is_leaf: true },
  41. { id: 14, pid: 2, order: 3, level: 3, full_path: '1.2.14', code: '1-1-3', is_leaf: true },
  42. { id: 3, pid: 1, order: 2, level: 2, full_path: '1.3', code: '1-2', is_leaf: false },
  43. { id: 15, pid: 3, order: 1, level: 3, full_path: '1.3.15', code: '1-2-1', is_leaf: true },
  44. { id: 4, pid: 1, order: 3, level: 2, full_path: '1.4', code: '1-3', is_leaf: false },
  45. { id: 16, pid: 4, order: 1, level: 3, full_path: '1.4.16', code: '1-3-1', is_leaf: true },
  46. { id: 5, pid: 1, order: 4, level: 2, full_path: '1.5', code: '1-4', is_leaf: true },
  47. ];
  48. const { app, assert } = require('egg-mock/bootstrap');
  49. const findById = function(nodes, Id) {
  50. const filters = nodes.filter(function(x) {
  51. return x.ledger_id === Id;
  52. });
  53. return filters.length > 0 ? filters[0] : undefined;
  54. };
  55. describe('test/app/service/ledger.test.js', () => {
  56. // 准备测试数据
  57. // 准备测试数据,新增测试标段
  58. before(function* () {
  59. const ctx = app.mockContext(mockData);
  60. // 模拟登录session
  61. const postData = {
  62. account: '734406061@qq.com',
  63. project: 'T201711273363',
  64. project_password: 'mai654321',
  65. };
  66. ctx.session = {};
  67. const loginResult = yield ctx.service.projectAccount.accountLogin(postData, 2);
  68. assert(loginResult);
  69. mockData.session = ctx.session;
  70. // 模拟打开标段
  71. mockData.tender = {};
  72. const addData = {
  73. name: 'test_ledger',
  74. category: null,
  75. };
  76. const tenderData = yield ctx.service.tender.getDataByCondition({
  77. user_id: ctx.session.sessionUser.accountId,
  78. name: addData.name,
  79. });
  80. if (tenderData) {
  81. mockData.tender.data = tenderData;
  82. } else {
  83. const addResult = yield ctx.service.tender.add(addData);
  84. assert(addResult);
  85. mockData.tender.data = yield ctx.service.tender.getDataByCondition({
  86. user_id: ctx.session.sessionUser.accountId,
  87. name: addData.name,
  88. });
  89. }
  90. mockData.tender.id = mockData.tender.data.id;
  91. mockData.tender.info = yield ctx.service.tenderInfo.getTenderInfo(mockData.tender.id);
  92. ctx.tender = mockData.tender;
  93. // 清理旧测试数据
  94. let result = yield ctx.service.ledger.db.delete(ctx.service.ledger.tableName, { tender_id: ctx.tender.id });
  95. const posResult = yield ctx.service.pos.db.delete(ctx.service.ledger.tableName, { tender_id: ctx.tender.id });
  96. assert(result.affectedRows >= 0 && posResult.affectedRows >= 0);
  97. // 初始化测试基础数据
  98. for (const data of testNodeData) {
  99. data.tender_id = ctx.tender.id;
  100. }
  101. result = yield ctx.service.ledger.add(testNodeData, ctx.tender.id);
  102. assert(result);
  103. ctx.service.ledger.cache.set('tender_node_maxId:' + ctx.tender.id, 16, 'EX', ctx.app.config.cacheTime);
  104. });
  105. /* 期望运行结果:
  106. 1
  107. ├── 1-1
  108. │ ├── 1-1-1
  109. │ │ ├── 202-1
  110. │ │ │ ├── 202-1-a
  111. │ │ │ └── 202-1-b
  112. │ │ └── 202-2
  113. │ │ ├── 202-2-c
  114. │ │ └── 202-1-3
  115. │ ├── 1-1-2
  116. │ └── 1-1-3
  117. ├── 1-2
  118. │ └── 1-2-1
  119. ├── 1-3
  120. │ └── 1-3-1
  121. └── 1-4
  122. */
  123. // 测试R类方法
  124. it('test getDataByTenderId', function* () {
  125. const ctx = app.mockContext(mockData);
  126. // 查询前4层节点
  127. const result1 = yield ctx.service.ledger.getDataByTenderId(ctx.tender.id);
  128. assert(result1.length === 12);
  129. // 查询前3层节点
  130. const result2 = yield ctx.service.ledger.getDataByTenderId(ctx.tender.id, 3);
  131. assert(result2.length === 10);
  132. });
  133. it('test getDataByNodeId', function* () {
  134. const ctx = app.mockContext(mockData);
  135. // 查询节点202-1
  136. const node = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 7);
  137. assert(node);
  138. assert(node.code === '202-1');
  139. assert(node.full_path === '1.2.6.7');
  140. });
  141. it('test getDataByNodeIds', function* () {
  142. const ctx = app.mockContext(mockData);
  143. // 查询节点202-1-a与201-1-b
  144. const result = yield ctx.service.ledger.getDataByNodeIds(ctx.tender.id, [10, 9]);
  145. assert(result.length === 2);
  146. let node = findById(result, 10);
  147. assert(node.code === '202-1-a');
  148. node = findById(result, 9);
  149. assert(node.full_path === '1.2.6.7.9');
  150. });
  151. it('test getDataByIds', function* () {
  152. const ctx = app.mockContext(mockData);
  153. // 查询节点202-1
  154. const node = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 7);
  155. const result = yield ctx.service.ledger.getDataByIds([node.id]);
  156. assert(result.length === 1);
  157. assert(node.code === result[0].code);
  158. assert(node.full_path === result[0].full_path);
  159. });
  160. it('test getLastChildData', function* () {
  161. const ctx = app.mockContext(mockData);
  162. // 查询节点202-1最后一个子节点
  163. const result = yield ctx.service.ledger.getLastChildData(ctx.tender.id, 7);
  164. assert(result.ledger_id === 10);
  165. assert(result.full_path === '1.2.6.7.10');
  166. });
  167. it('test getDataByParentAndOrder', function* () {
  168. const ctx = app.mockContext(mockData);
  169. // 查询节点202-1 第1子节点
  170. const result1 = yield ctx.service.ledger.getDataByParentAndOrder(ctx.tender.id, 8, 1);
  171. assert(result1.ledger_id === 11);
  172. assert(result1.full_path === '1.2.6.8.11');
  173. // 查询节点1-1 第2/3子节点
  174. const result2 = yield ctx.service.ledger.getDataByParentAndOrder(ctx.tender.id, 2, [2, 3]);
  175. assert(result2.length === 2);
  176. let node = findById(result2, 13);
  177. assert(node);
  178. assert(node.code === '1-1-2');
  179. node = findById(result2, 14);
  180. assert(node);
  181. assert(node.code === '1-1-3');
  182. });
  183. it('test getChildrenByParentId', function* () {
  184. const ctx = app.mockContext(mockData);
  185. // 查询节点202-1最后一个子节点
  186. const result = yield ctx.service.ledger.getChildrenByParentId(ctx.tender.id, 8);
  187. assert(result.length === 2);
  188. let node = findById(result, 11);
  189. assert(node);
  190. assert(node.code === '202-2-c');
  191. node = findById(result, 12);
  192. assert(node);
  193. assert(node.full_path === '1.2.6.8.12');
  194. });
  195. it('test getNextsData', function* () {
  196. const ctx = app.mockContext(mockData);
  197. // 查询节点1-1-1的全部子节点
  198. const result = yield ctx.service.ledger.getNextsData(ctx.tender.id, 2, 1);
  199. assert(result.length === 2);
  200. let node = findById(result, 13);
  201. assert(node);
  202. assert(node.code === '1-1-2');
  203. node = findById(result, 14);
  204. assert(node);
  205. assert(node.code === '1-1-3');
  206. });
  207. it('test getDataByFullPath', function* () {
  208. const ctx = app.mockContext(mockData);
  209. // 查询节点202-2及其子节点
  210. const result = yield ctx.service.ledger.getDataByFullPath(ctx.tender.id, '1.2.6.8%');
  211. assert(result.length === 3);
  212. let node = findById(result, 8);
  213. assert(node);
  214. assert(node.code === '202-2');
  215. node = findById(result, 11);
  216. assert(node);
  217. assert(node.code === '202-2-c');
  218. node = findById(result, 12);
  219. assert(node);
  220. assert(node.full_path === '1.2.6.8.12');
  221. // 查询1-1-1的子孙节点
  222. const result1 = yield ctx.service.ledger.getDataByFullPath(ctx.tender.id, '1.2.6.%');
  223. assert(result1.length === 6);
  224. });
  225. it('test getFullLevelDataByFullPath', function* () {
  226. const ctx = app.mockContext(mockData);
  227. // 查询202-2-c及其全部父节点
  228. const result1 = yield ctx.service.ledger.getFullLevelDataByFullPath(ctx.tender.id, '1.2.6.8.11');
  229. assert(result1.length === 5);
  230. const result2 = yield ctx.service.ledger.getFullLevelDataByFullPath(ctx.tender.id, ['1.2.6.8.11', '1.2.6.7.9']);
  231. assert(result2.length === 7);
  232. });
  233. // 测试CUD类方法
  234. // 基本树结构操作
  235. it('test addNode', function* () {
  236. const ctx = app.mockContext(mockData);
  237. // 选中1-1-1,插入节点
  238. const resultData = yield ctx.service.ledger.addNode(ctx.tender.id, 6);
  239. assert(resultData.create.length === 1);
  240. assert(resultData.update.length === 2);
  241. assert(resultData.create[0].is_leaf === 1);
  242. assert(resultData.create[0].ledger_id === 17);
  243. });
  244. /* 期望运行结果:
  245. 1
  246. ├── 1-1
  247. │ ├── 1-1-1
  248. │ │ ├── 202-1
  249. │ │ │ ├── 202-1-a
  250. │ │ │ └── 202-1-b
  251. │ │ └── 202-2
  252. │ │ ├── 202-2-c
  253. │ │ └── 202-1-3
  254. │ ├── new
  255. │ ├── 1-1-2
  256. │ └── 1-1-3
  257. ├── 1-2
  258. │ └── 1-2-1
  259. ├── 1-3
  260. │ └── 1-3-1
  261. └── 1-4
  262. */
  263. it('test deleteNode', function* () {
  264. const ctx = app.mockContext(mockData);
  265. // 选中202-1,删除节点
  266. const resultData = yield ctx.service.ledger.deleteNode(ctx.tender.id, 7);
  267. assert(resultData.delete.length === 3);
  268. assert(resultData.update.length === 1);
  269. });
  270. /* 期望运行结果:
  271. 1
  272. ├── 1-1
  273. │ ├── 1-1-1
  274. │ │ └── 202-2
  275. │ │ ├── 202-2-c
  276. │ │ └── 202-2-e
  277. │ ├── new
  278. │ ├── 1-1-2
  279. │ └── 1-1-3
  280. ├── 1-2
  281. │ └── 1-2-1
  282. ├── 1-3
  283. │ └── 1-3-1
  284. └── 1-4
  285. */
  286. it('test upMoveNode', function* () {
  287. const ctx = app.mockContext(mockData);
  288. // 选中202-2-e上移
  289. const resultData = yield ctx.service.ledger.upMoveNode(ctx.tender.id, 12);
  290. resultData.update.sort(function(x, y) {
  291. return x.order - y.order;
  292. });
  293. assert(resultData.update.length === 2);
  294. assert(resultData.update[0].code === '202-2-e');
  295. });
  296. /* 期望运行结果:
  297. 1
  298. ├── 1-1
  299. │ ├── 1-1-1
  300. │ │ └── 202-2
  301. │ │ ├── 202-2-c
  302. │ │ └── 202-2-e
  303. │ ├── new
  304. │ ├── 1-1-2
  305. │ └── 1-1-3
  306. ├── 1-2
  307. │ └── 1-2-1
  308. ├── 1-3
  309. │ └── 1-3-1
  310. └── 1-4
  311. */
  312. it('test downMoveNode', function* () {
  313. const ctx = app.mockContext(mockData);
  314. // 选中202-2-e下移
  315. const resultData = yield ctx.service.ledger.downMoveNode(ctx.tender.id, 12);
  316. resultData.update.sort(function(x, y) {
  317. return x.order - y.order;
  318. });
  319. assert(resultData.update.length === 2);
  320. assert(resultData.update[0].code === '202-2-c');
  321. });
  322. /* 期望运行结果:
  323. 1
  324. ├── 1-1
  325. │ ├── 1-1-1
  326. │ │ └── 202-2
  327. │ │ ├── 202-2-c
  328. │ │ └── 202-2-e
  329. │ ├── new
  330. │ ├── 1-1-2
  331. │ └── 1-1-3
  332. ├── 1-2
  333. │ └── 1-2-1
  334. ├── 1-3
  335. │ └── 1-3-1
  336. └── 1-4
  337. */
  338. it('test upLevelNode', function* () {
  339. const ctx = app.mockContext(mockData);
  340. // 选中 1-1-2 升级
  341. const resultData = yield ctx.service.ledger.upLevelNode(ctx.tender.id, 13);
  342. assert(resultData);
  343. assert(resultData.update.length === 6);
  344. let node = findById(resultData.update, 13);
  345. assert(node.full_path === '1.13');
  346. assert(node.ledger_pid === 1);
  347. assert(!node.is_leaf);
  348. node = findById(resultData.update, 14);
  349. assert(node.ledger_pid === 13);
  350. assert(node.full_path === '1.13.14');
  351. node = findById(resultData.update, 3);
  352. assert(node.order === 3);
  353. node = findById(resultData.update, 4);
  354. assert(node.order === 4);
  355. node = findById(resultData.update, 5);
  356. assert(node.order === 5);
  357. });
  358. /* 期望运行结果:
  359. 1
  360. ├── 1-1
  361. │ ├── 1-1-1
  362. │ │ └── 202-2
  363. │ │ ├── 202-2-c
  364. │ │ └── 202-2-e
  365. │ └── new
  366. ├── 1-1-2
  367. │ └── 1-1-3
  368. ├── 1-2
  369. │ └── 1-2-1
  370. ├── 1-3
  371. │ └── 1-3-1
  372. └── 1-4
  373. */
  374. it('test downLevelNode', function* () {
  375. const ctx = app.mockContext(mockData);
  376. // 选中1-3 降级
  377. const resultData = yield ctx.service.ledger.downLevelNode(ctx.tender.id, 4);
  378. // 1-2/1-3/1-3-1/1-4修改
  379. assert(resultData.update.length === 4);
  380. let node = findById(resultData.update, 4);
  381. assert(node.full_path === '1.3.4');
  382. assert(node.ledger_pid === 3);
  383. assert(node.level === 3);
  384. assert(node.order === 2);
  385. node = findById(resultData.update, 16);
  386. assert(node.level === 4);
  387. assert(node.full_path === '1.3.4.16');
  388. node = findById(resultData.update, 5);
  389. assert(node.order === 4);
  390. });
  391. /* 期望运行结果:
  392. 1
  393. ├── 1-1
  394. │ ├── 1-1-1
  395. │ │ └── 202-2
  396. │ │ ├── 202-2-c
  397. │ │ └── 202-2-e
  398. │ └── new
  399. ├── 1-1-2
  400. │ └── 1-1-3
  401. ├── 1-2
  402. │ ├── 1-2-1
  403. │ └── 1-3
  404. │ └── 1-3-1
  405. └── 1-4
  406. */
  407. // 复制整块
  408. it('test pasteBlock', function* () {
  409. const ctx = app.mockContext(mockData);
  410. // 选中1-2-1, 粘贴1-1-1和new
  411. const resultData = yield ctx.service.ledger.pasteBlock(ctx.tender.id, 15, [6, 17]);
  412. const ledger = resultData.ledger;
  413. assert(ledger.create.length === 5);
  414. assert(ledger.create[0].order = 2);
  415. assert(ledger.create[0].level = 3);
  416. assert(ledger.create[0].full_path = '1.3.18');
  417. assert(ledger.create[1].order = 3);
  418. assert(ledger.create[1].level = 3);
  419. assert(ledger.create[1].full_path = '1.3.22');
  420. assert(ledger.update.length === 1);
  421. assert(ledger.update[0].order = 3);
  422. assert(resultData.pos.length === 0);
  423. });
  424. /* 期望运行结果:
  425. 1
  426. ├── 1-1
  427. │ ├── 1-1-1
  428. │ │ └── 202-2
  429. │ │ ├── 202-2-c
  430. │ │ └── 202-2-e
  431. │ └── new
  432. ├── 1-1-2
  433. │ └── 1-1-3
  434. ├── 1-2
  435. │ ├── 1-2-1
  436. │ ├── 1-1-1
  437. │ │ └── 202-2
  438. │ │ ├── 202-2-c
  439. │ │ └── 202-2-e
  440. │ ├── new
  441. │ └── 1-3
  442. │ └── 1-3-1
  443. └── 1-4
  444. */
  445. // 增量计算
  446. it('test updateInfo', function* () {
  447. const ctx = app.mockContext(mockData);
  448. // 修改new(id=17)的code 为 1-1-4
  449. const node = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 17);
  450. assert(node);
  451. const resultData = yield ctx.service.ledger.updateInfo(ctx.tender.id, {
  452. id: node.id,
  453. tender_id: node.tender_id,
  454. ledger_id: node.ledger_id,
  455. code: '1-1-4',
  456. });
  457. assert(resultData.code === '1-1-4');
  458. });
  459. /* 期望运行结果:
  460. 1
  461. ├── 1-1
  462. │ ├── 1-1-1
  463. │ │ └── 202-2
  464. │ │ ├── 202-2-c
  465. │ │ └── 202-2-e
  466. │ └── 1-1-4
  467. ├── 1-1-2
  468. │ └── 1-1-3
  469. ├── 1-2
  470. │ ├── 1-2-1
  471. │ ├── 1-1-1
  472. │ │ └── 202-2
  473. │ │ ├── 202-2-c
  474. │ │ └── 202-2-e
  475. │ ├── new
  476. │ └── 1-3
  477. │ └── 1-3-1
  478. └── 1-4
  479. */
  480. it('test updateInfos', function* () {
  481. const ctx = app.mockContext(mockData);
  482. // 修改1-1-1(id=18)的code 为 1-2-2、修改new(id=22)的code 为 1-2-3
  483. const node1 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 18);
  484. assert(node1);
  485. const node2 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 22);
  486. assert(node2);
  487. const resultData = yield ctx.service.ledger.updateInfos(ctx.tender.id, [{
  488. id: node1.id,
  489. tender_id: node1.tender_id,
  490. ledger_id: node1.ledger_id,
  491. code: '1-2-2',
  492. }, {
  493. id: node2.id,
  494. tender_id: node2.tender_id,
  495. ledger_id: node2.ledger_id,
  496. code: '1-2-3',
  497. }]);
  498. assert(resultData.length === 2);
  499. assert(resultData[0].code === '1-2-2');
  500. assert(resultData[1].code === '1-2-3');
  501. });
  502. /* 期望运行结果:
  503. 1
  504. ├── 1-1
  505. │ ├── 1-1-1
  506. │ │ └── 202-2
  507. │ │ ├── 202-2-c
  508. │ │ └── 202-2-e
  509. │ └── 1-1-4
  510. ├── 1-1-2
  511. │ └── 1-1-3
  512. ├── 1-2
  513. │ ├── 1-2-1
  514. │ ├── 1-2-2
  515. │ │ └── 202-2
  516. │ │ ├── 202-2-c
  517. │ │ └── 202-2-e
  518. │ ├── 1-2-3
  519. │ └── 1-3
  520. │ └── 1-3-1
  521. └── 1-4
  522. */
  523. it('test updateCalc - update 1', function* () {
  524. const ctx = app.mockContext(mockData);
  525. // 计算需使用清单精度、小数位数
  526. ctx.tender = {id: ctx.tender.id};
  527. ctx.tender.data = yield ctx.service.tender.getTender(ctx.tender.id);
  528. ctx.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
  529. // 修改202-2-e(1-1-1下)(id=12)的quantity为2.00000001, unit_price位3.0000005
  530. const qty = 2.00000001, fQty = ctx.helper.round(qty, ctx.tender.info.precision.other.value);
  531. const up = 3.0000005, fUp = ctx.helper.round(up, ctx.tender.info.decimal.up);
  532. const tp = 6.00000103, fTp = ctx.helper.times(fQty, fUp);
  533. const node1 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 12);
  534. assert(node1);
  535. const resultData = yield ctx.service.ledger.updateCalc(ctx.tender.id, {
  536. id: node1.id,
  537. tender_id: node1.tender_id,
  538. ledger_id: node1.ledger_id,
  539. sgfh_qty: qty,
  540. unit_price: up
  541. });
  542. assert(resultData.update.length === 1);
  543. let node = findById(resultData.update, 12);
  544. assert(node.sgfh_qty === fQty);
  545. assert(node.quantity === fQty);
  546. assert(node.unit_price === fUp);
  547. assert(node.sgfh_tp === fTp);
  548. assert(node.total_price == fTp);
  549. });
  550. /* 期望运行结果:
  551. 1
  552. ├── 1-1
  553. │ ├── 1-1-1
  554. │ │ └── 202-2
  555. │ │ ├── 202-2-c
  556. │ │ └── 202-2-e 2 3 6
  557. │ └── 1-1-4
  558. ├── 1-1-2
  559. │ └── 1-1-3
  560. ├── 1-2
  561. │ ├── 1-2-1
  562. │ ├── 1-2-2
  563. │ │ └── 202-2
  564. │ │ ├── 202-2-c
  565. │ │ └── 202-2-e
  566. │ ├── 1-2-3
  567. │ └── 1-3
  568. │ └── 1-3-1
  569. └── 1-4
  570. */
  571. it('test updateCalc - update N', function* () {
  572. const ctx = app.mockContext(mockData);
  573. // 计算需使用清单精度、小数位数
  574. ctx.tender = {id: ctx.tender.id};
  575. ctx.tender.data = yield ctx.service.tender.getTender(ctx.tender.id);
  576. ctx.tender.info = yield ctx.service.tenderInfo.getTenderInfo(ctx.tender.id);
  577. // 修改202-2-c(1-1-1下)(id=11)的quantity为4.00000025, unit_price为6.0000083
  578. // 202-2-c(1-2-2下)(id=20)的quantity为2.0000001, unit_price为5.000065
  579. // 202-2-e(1-2-2下)(id=21)的quantity为8.0000579, unit_price为4.0000086
  580. const qty = [4.00000025, 2.0000001, 8.0000579];
  581. const up = [6.0000083, 5.000065, 4.0000086];
  582. const tp = [24.0000347, 10.0001305, 32.0003004];
  583. // 实际结果
  584. const fQty = [], fUp = [], fTp = [];
  585. for (const q of qty) {
  586. fQty.push(ctx.helper.round(q, ctx.tender.info.precision.other.value));
  587. }
  588. for (const p of up) {
  589. fUp.push(ctx.helper.round(p, ctx.tender.info.decimal.up));
  590. }
  591. for (const i in qty) {
  592. fTp.push(ctx.helper.times(fQty[i], fUp[i]));
  593. }
  594. const node1 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 11);
  595. assert(node1);
  596. const node2 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 20);
  597. assert(node2);
  598. const node3 = yield ctx.service.ledger.getDataByNodeId(ctx.tender.id, 21);
  599. assert(node3);
  600. const resultData = yield ctx.service.ledger.updateCalc(ctx.tender.id, [{
  601. id: node1.id,
  602. tender_id: node1.tender_id,
  603. ledger_id: node1.ledger_id,
  604. sgfh_qty: qty[0],
  605. unit_price: up[0]
  606. }, {
  607. id: node2.id,
  608. tender_id: node2.tender_id,
  609. ledger_id: node2.ledger_id,
  610. sjcl_qty: qty[1],
  611. unit_price: up[1]
  612. }, {
  613. id: node3.id,
  614. tender_id: node3.tender_id,
  615. ledger_id: node3.ledger_id,
  616. qtcl_qty: qty[2],
  617. unit_price: up[2]
  618. }]);
  619. assert(resultData.update.length === 3);
  620. let node = findById(resultData.update, 11);
  621. assert(node.sgfh_qty === fQty[0]);
  622. assert(node.quantity === fQty[0]);
  623. assert(node.unit_price === fUp[0]);
  624. assert(node.sgfh_tp === fTp[0]);
  625. assert(node.total_price === fTp[0]);
  626. node = findById(resultData.update, 20);
  627. assert(node.sjcl_qty === fQty[1]);
  628. assert(node.quantity === fQty[1]);
  629. assert(node.unit_price === fUp[1]);
  630. assert(node.sjcl_tp === fTp[1]);
  631. assert(node.total_price === fTp[1]);
  632. node = findById(resultData.update, 21);
  633. assert(node.qtcl_qty === fQty[2]);
  634. assert(node.quantity === fQty[2]);
  635. assert(node.unit_price === fUp[2]);
  636. assert(node.qtcl_tp === fTp[2]);
  637. assert(node.total_price === fTp[2]);
  638. });
  639. /* 期望运行结果:
  640. 1
  641. ├── 1-1
  642. │ ├── 1-1-1
  643. │ │ └── 202-2
  644. │ │ ├── 202-2-c 4 6 24
  645. │ │ └── 202-2-e 2 3 6
  646. │ └── 1-1-4
  647. ├── 1-1-2
  648. │ └── 1-1-3
  649. ├── 1-2
  650. │ ├── 1-2-1
  651. │ ├── 1-2-2
  652. │ │ └── 202-2
  653. │ │ ├── 202-2-c 2 5 10
  654. │ │ └── 202-2-e 8 4 32
  655. │ ├── 1-2-3
  656. │ └── 1-3
  657. │ └── 1-3-1
  658. └── 1-4
  659. */
  660. // 复制整块+实时计算
  661. it('test pasteBlock - with Increment Calculate', function* () {
  662. const ctx = app.mockContext(mockData);
  663. // 选中1-1-4, 粘贴202-2(1-1-1下)
  664. const resultData = yield ctx.service.ledger.pasteBlock(ctx.tender.id, 17, [8]);
  665. assert(resultData.ledger.create.length === 3);
  666. assert(resultData.ledger.update.length === 0);
  667. });
  668. /* 期望运行结果:
  669. 1
  670. ├── 1-1
  671. │ ├── 1-1-1
  672. │ │ └── 202-2
  673. │ │ ├── 202-2-c 4 6 24
  674. │ │ └── 202-2-e 2 3 6
  675. │ ├── 1-1-4
  676. │ └── 202-2
  677. │ ├── 202-2-c 4 6 24
  678. │ └── 202-2-e 2 3 6
  679. ├── 1-1-2
  680. │ └── 1-1-3
  681. ├── 1-2
  682. │ ├── 1-2-1
  683. │ ├── 1-2-2
  684. │ │ └── 202-2
  685. │ │ ├── 202-2-c 2 5 10
  686. │ │ └── 202-2-e 8 4 32
  687. │ ├── 1-2-3
  688. │ └── 1-3
  689. │ └── 1-3-1
  690. └── 1-4
  691. */
  692. // 树结构基本操作+实时计算
  693. it('test downLevel - with Increment Calculate', function* () {
  694. const ctx = app.mockContext(mockData);
  695. // 选中202-2(1-1-4后兄弟节点) 降级
  696. const resultData = yield ctx.service.ledger.downLevelNode(ctx.tender.id, 23);
  697. assert(resultData.update.length === 4);
  698. });
  699. /* 期望运行结果:
  700. 1
  701. ├── 1-1
  702. │ ├── 1-1-1
  703. │ │ └── 202-2
  704. │ │ ├── 202-2-c 4 6 24
  705. │ │ └── 202-2-e 2 3 6
  706. │ └── 1-1-4
  707. │ └── 202-2
  708. │ ├── 202-2-c 4 6 24
  709. │ └── 202-2-e 2 3 6
  710. ├── 1-1-2
  711. │ └── 1-1-3
  712. ├── 1-2
  713. │ ├── 1-2-1
  714. │ ├── 1-2-2
  715. │ │ └── 202-2
  716. │ │ ├── 202-2-c 2 5 10
  717. │ │ └── 202-2-e 8 4 32
  718. │ ├── 1-2-3
  719. │ └── 1-3
  720. │ └── 1-3-1
  721. └── 1-4
  722. */
  723. it('test upLevel - with Increment Calculate', function* () {
  724. const ctx = app.mockContext(mockData);
  725. yield ctx.service.ledger.pasteBlock(ctx.tender.id, 23, [23]);
  726. /* 期望运行结果:
  727. 1
  728. ├── 1-1
  729. │ ├── 1-1-1
  730. │ │ └── 202-2
  731. │ │ ├── 202-2-c 4 6 24
  732. │ │ └── 202-2-e 2 3 6
  733. │ └── 1-1-4
  734. │ ├── 202-2
  735. │ │ ├── 202-2-c 4 6 24
  736. │ │ └── 202-2-e 2 3 6
  737. │ └── 202-2
  738. │ ├── 202-2-c 4 6 24
  739. │ └── 202-2-e 2 3 6
  740. ├── 1-1-2
  741. │ └── 1-1-3
  742. ├── 1-2
  743. │ ├── 1-2-1
  744. │ ├── 1-2-2
  745. │ │ └── 202-2
  746. │ │ ├── 202-2-c 2 5 10
  747. │ │ └── 202-2-e 8 4 32
  748. │ ├── 1-2-3
  749. │ └── 1-3
  750. │ └── 1-3-1
  751. └── 1-4
  752. */
  753. // 选中202-2-c(1-1-4下)(id=23)升级
  754. const resultData = yield ctx.service.ledger.upLevelNode(ctx.tender.id, 23);
  755. assert(resultData.update.length === 7);
  756. });
  757. /* 期望运行结果:
  758. 1
  759. ├── 1-1
  760. │ ├── 1-1-1
  761. │ │ └── 202-2
  762. │ │ ├── 202-2-c 4 6 24
  763. │ │ └── 202-2-e 2 3 6
  764. │ ├── 1-1-4
  765. │ └── 202-2
  766. │ ├── 202-2-c 4 6 24
  767. │ ├── 202-2-e 2 3 6
  768. │ └── 202-2
  769. │ ├── 202-2-c 4 6 24
  770. │ └── 202-2-e 2 3 6
  771. ├── 1-1-2
  772. │ └── 1-1-3
  773. ├── 1-2
  774. │ ├── 1-2-1
  775. │ ├── 1-2-2
  776. │ │ └── 202-2
  777. │ │ ├── 202-2-c 2 5 10
  778. │ │ └── 202-2-e 8 4 32
  779. │ ├── 1-2-3
  780. │ └── 1-3
  781. │ └── 1-3-1
  782. └── 1-4
  783. */
  784. it('test deleteNode - with Increment Calculate', function* () {
  785. const ctx = app.mockContext(mockData);
  786. // 选中202-2-c(1-2-2下)(id=20),删除节点
  787. const resultData = yield ctx.service.ledger.deleteNode(ctx.tender.id, 20);
  788. assert(resultData.delete.length === 1);
  789. assert(resultData.update.length === 1);
  790. });
  791. /* 期望运行结果:
  792. 1
  793. ├── 1-1
  794. │ ├── 1-1-1
  795. │ │ └── 202-2
  796. │ │ ├── 202-2-c 4 6 24
  797. │ │ └── 202-2-e 2 3 6
  798. │ ├── 1-1-4
  799. │ └── 202-2
  800. │ ├── 202-2-c 4 6 24
  801. │ ├── 202-2-e 2 3 6
  802. │ └── 202-2
  803. │ ├── 202-2-c 4 6 24
  804. │ └── 202-2-e 2 3 6
  805. ├── 1-1-2
  806. │ └── 1-1-3
  807. ├── 1-2
  808. │ ├── 1-2-1
  809. │ ├── 1-2-2
  810. │ │ └── 202-2
  811. │ │ └── 202-2-e 8 4 32
  812. │ ├── 1-2-3
  813. │ └── 1-3
  814. │ └── 1-3-1
  815. └── 1-4
  816. */
  817. // 从标准库添加数据
  818. // 检查添加,直接添加为选中节点子节点
  819. it('test addStdNode', function* () {
  820. const ctx = app.mockContext(mockData);
  821. // 选中1-1-4
  822. const selectData = yield ctx.service.ledger.getDataByCondition({ tender_id: ctx.tender.id, code: '1-1-4' });
  823. assert(selectData);
  824. // 从标准库中添加1-1-5
  825. const condition1 = { list_id: 1, code: '1-1-5' };
  826. const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
  827. assert(libData1);
  828. const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
  829. assert(stdData1);
  830. assert(stdData1.id === libData1.id);
  831. const result1 = yield ctx.service.ledger.addStdNode(ctx.tender.id, selectData.ledger_id, stdData1);
  832. assert(result1);
  833. assert(result1.create.length === 1);
  834. assert(result1.update.length === 1);
  835. // 从标准库中添加101-1
  836. });
  837. /* 期望运行结果:
  838. 1
  839. ├── 1-1
  840. │ ├── 1-1-1
  841. │ │ └── 202-2
  842. │ │ ├── 202-2-c 4 6 24
  843. │ │ └── 202-2-e 2 3 6
  844. │ ├── 1-1-4
  845. │ ├── 1-1-5
  846. │ └── 202-2
  847. │ ├── 202-2-c 4 6 24
  848. │ ├── 202-2-e 2 3 6
  849. │ └── 202-2
  850. │ ├── 202-2-c 4 6 24
  851. │ └── 202-2-e 2 3 6
  852. ├── 1-1-2
  853. │ └── 1-1-3
  854. ├── 1-2
  855. │ ├── 1-2-1
  856. │ ├── 1-2-2
  857. │ │ └── 202-2
  858. │ │ └── 202-2-e 8 4 32
  859. │ ├── 1-2-3
  860. │ └── 1-3
  861. │ └── 1-3-1
  862. └── 1-4
  863. */
  864. it('test addStdNodeWithParent', function* () {
  865. const ctx = app.mockContext(mockData);
  866. // 从标准库添加1-4-2-1
  867. const condition1 = { list_id: 1, code: '1-4-2-1'};
  868. const libData1 = yield ctx.service.stdChapter.getDataByCondition(condition1);
  869. assert(libData1);
  870. const stdData1 = yield ctx.service.stdChapter.getDataByDataId(1, libData1.chapter_id);
  871. assert(stdData1);
  872. assert(stdData1.id === libData1.id);
  873. const result1 = yield ctx.service.ledger.addStdNodeWithParent(ctx.tender.id, stdData1, ctx.service.stdChapter);
  874. assert(result1);
  875. assert(result1.create.length === 4);
  876. assert(!result1.update || result1.update.length === 0);
  877. assert(!result1.expand || result1.expand.length === 0);
  878. // 从标准库添加1-4-2-1-2
  879. const condition2 = { list_id: 1, code: '1-4-2-1-2'};
  880. const libData2 = yield ctx.service.stdChapter.getDataByCondition(condition2);
  881. assert(libData2);
  882. const stdData2 = yield ctx.service.stdChapter.getDataByDataId(1, libData2.chapter_id);
  883. assert(stdData2);
  884. assert(stdData2.id === libData2.id);
  885. const result2 = yield ctx.service.ledger.addStdNodeWithParent(ctx.tender.id, stdData2, ctx.service.stdChapter);
  886. assert(result2);
  887. assert(result2.create.length === 1);
  888. assert(result2.update.length === 1);
  889. assert(result2.update[0].code = '1-4-2-1');
  890. assert(!result2.update[0].is_leaf);
  891. // 从标准库添加1-4-2-1-1
  892. const condition3 = { list_id: 1, code: '1-4-2-1-1'};
  893. const libData3 = yield ctx.service.stdChapter.getDataByCondition(condition3);
  894. assert(libData3);
  895. const stdData3 = yield ctx.service.stdChapter.getDataByDataId(1, libData3.chapter_id);
  896. assert(stdData3);
  897. assert(stdData3.id === libData3.id);
  898. const result3 = yield ctx.service.ledger.addStdNodeWithParent(ctx.tender.id, stdData3, ctx.service.stdChapter);
  899. assert(result3);
  900. assert(result3.create.length === 1);
  901. assert(result3.create[0].order === 1);
  902. assert(result3.update.length === 1);
  903. assert(result3.update[0].code === '1-4-2-1-2');
  904. assert(result3.update[0].order === 2);
  905. });
  906. /* 期望运行结果:
  907. 1
  908. ├── 1-1
  909. │ ├── 1-1-1
  910. │ │ └── 202-2
  911. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  912. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  913. │ ├── 1-1-4
  914. │ ├── 1-1-5
  915. │ └── 202-2
  916. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  917. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  918. │ └── 202-2
  919. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  920. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  921. ├── 1-1-2
  922. │ └── 1-1-3
  923. ├── 1-2
  924. │ ├── 1-2-1
  925. │ ├── 1-2-2
  926. │ │ └── 202-2
  927. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  928. │ ├── 1-2-3
  929. │ └── 1-3
  930. │ └── 1-3-1
  931. └── 1-4
  932. └── 1-4-2
  933. └── 1-4-2-1
  934. ├── 1-4-2-1-1
  935. └── 1-4-2-1-2
  936. */
  937. // 批量插入
  938. it('test batchInsertChild', function* () {
  939. const ctx = app.mockContext(mockData);
  940. const batchData = [
  941. {
  942. b_code: 401-1, name: 'A1', unit: 'B1', price: 2,
  943. pos: [{name: 'X1', quantity: 1}, {name: 'X2', quantity: 2}],
  944. },
  945. {
  946. b_code: 402-1, name: 'A2', unit: 'B2', price: 3,
  947. pos: [{name: 'X1', quantity: 3}, {name: 'X2', quantity: 4}],
  948. },
  949. ];
  950. // 选中1-1-3(id=14)
  951. const result = yield ctx.service.ledger.batchInsertChild(ctx.tender.id, 14, batchData);
  952. assert(result.ledger.create.length === 2);
  953. let node = _.find(result.ledger.create, {name: 'A1'});
  954. assert(node.quantity === 3);
  955. assert(node.total_price === 6);
  956. node = _.find(result.ledger.create, {name: 'A2'});
  957. assert(node.quantity === 7);
  958. assert(node.total_price === 21);
  959. assert(result.pos.length === 4);
  960. });
  961. /* 期望运行结果:
  962. 1
  963. ├── 1-1
  964. │ ├── 1-1-1
  965. │ │ └── 202-2
  966. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  967. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  968. │ ├── 1-1-4
  969. │ ├── 1-1-5
  970. │ └── 202-2
  971. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  972. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  973. │ └── 202-2
  974. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  975. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  976. ├── 1-1-2
  977. │ └── 1-1-3
  978. │ ├── 401-1 2 3 6
  979. │ │ ├── (pos)X1 1
  980. │ │ └── (pos)X1 2
  981. │ └── 402-1 3 7 21
  982. │ ├── (pos)X1 3
  983. │ └── (pos)X2 4
  984. ├── 1-2
  985. │ ├── 1-2-1
  986. │ ├── 1-2-2
  987. │ │ └── 202-2
  988. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  989. │ ├── 1-2-3
  990. │ └── 1-3
  991. │ └── 1-3-1
  992. └── 1-4
  993. └── 1-4-2
  994. └── 1-4-2-1
  995. ├── 1-4-2-1-1
  996. └── 1-4-2-1-2
  997. */
  998. // 批量插入
  999. it('test batchInsertNext', function* () {
  1000. const ctx = app.mockContext(mockData);
  1001. const batchData = [
  1002. {
  1003. b_code: 403-1, name: 'A3', unit: 'B1', price: 2,
  1004. pos: [{name: 'Y1', quantity: 5}, {name: 'Y2', quantity: 6}],
  1005. },
  1006. {
  1007. b_code: 404-1, name: 'A4', unit: 'B2', price: 3,
  1008. pos: [{name: 'Y1', quantity: 7}, {name: 'Y2', quantity: 8}],
  1009. },
  1010. ];
  1011. // 选中1-1-3(id=14)
  1012. const result = yield ctx.service.ledger.batchInsertNext(ctx.tender.id, 14, batchData);
  1013. assert(result.ledger.create.length === 2);
  1014. let node = _.find(result.ledger.create, {name: 'A3'});
  1015. assert(node.quantity === 11);
  1016. assert(node.total_price === 22);
  1017. node = _.find(result.ledger.create, {name: 'A4'});
  1018. assert(node.quantity === 15);
  1019. assert(node.total_price === 45);
  1020. assert(result.pos.length === 4);
  1021. });
  1022. /* 期望运行结果:
  1023. 1
  1024. ├── 1-1
  1025. │ ├── 1-1-1
  1026. │ │ └── 202-2
  1027. │ │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  1028. │ │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  1029. │ ├── 1-1-4
  1030. │ ├── 1-1-5
  1031. │ └── 202-2
  1032. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  1033. │ ├── 202-2-e 2.00000001 3.0000005 6.00000103
  1034. │ └── 202-2
  1035. │ ├── 202-2-c 4.00000025 6.0000083 24.0000347
  1036. │ └── 202-2-e 2.00000001 3.0000005 6.00000103
  1037. ├── 1-1-2
  1038. │ ├── 1-1-3
  1039. │ │ ├── 401-1 2 3 6
  1040. │ │ │ ├── (pos)X1 1
  1041. │ │ │ └── (pos)X1 2
  1042. │ │ └── 402-1 3 7 21
  1043. │ │ ├── (pos)X1 3
  1044. │ │ └── (pos)X2 4
  1045. │ ├── 403-1 2 11 22
  1046. │ │ ├── (pos)Y1 5
  1047. │ │ └── (pos)Y2 6
  1048. │ └── 404-1 3 15 45
  1049. │ ├── (pos)Y1 7
  1050. │ └── (pos)Y2 8
  1051. ├── 1-2
  1052. │ ├── 1-2-1
  1053. │ ├── 1-2-2
  1054. │ │ └── 202-2
  1055. │ │ └── 202-2-e 8.0000579 4.0000086 32.0003004
  1056. │ ├── 1-2-3
  1057. │ └── 1-3
  1058. │ └── 1-3-1
  1059. └── 1-4
  1060. └── 1-4-2
  1061. └── 1-4-2-1
  1062. ├── 1-4-2-1-1
  1063. └── 1-4-2-1-2
  1064. */
  1065. // 测试统计类方法
  1066. it('test addUpChildren', function* () {
  1067. const ctx = app.mockContext(mockData);
  1068. // 统计202-2(id=23)前两个子节点的金额
  1069. const result1 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 2, '<=');
  1070. assert(result1 && result1 === 30);
  1071. // 数据库不再存储父项金额,以下两个查询不能获得实际金额
  1072. // 统计202-2(id=23)后两个子节点的金额
  1073. //const result2 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 2, '>=');
  1074. //assert(result2 && result2.toFixed(8) == 36.00003676);
  1075. // 统计202-2(id=23)全部子节点的金额
  1076. //const result3 = yield ctx.service.ledger.addUpChildren(ctx.tender.id, 23, 0, '>');
  1077. //assert(result3 && result3.toFixed(8) == 60.00007146);
  1078. });
  1079. // 测试搜索类方法
  1080. it('test search', function* () {
  1081. const ctx = app.mockContext(mockData);
  1082. const result = yield ctx.service.ledger.search(ctx.tender.id, {
  1083. value: app.mysql.escape('%1-3%'),
  1084. operate: 'Like',
  1085. fields: ['code', 'name'],
  1086. });
  1087. assert(result.length === 3);
  1088. });
  1089. it('test searchRange', function* () {
  1090. const ctx = app.mockContext(mockData);
  1091. const result = yield ctx.service.ledger.searchRange(ctx.tender.id, {
  1092. value: app.mysql.escape('%1-3%'),
  1093. operate: 'Like',
  1094. fields: ['code', 'name'],
  1095. }, [{
  1096. type: 'And',
  1097. value: app.mysql.escape('1.13.%'),
  1098. operate: 'Like',
  1099. fields: ['full_path'],
  1100. }]);
  1101. assert(result.length === 1);
  1102. assert(result[0].code === '1-1-3');
  1103. });
  1104. // 测试导入excel 在\lib\analysis_excel.test.js中进行单元测试
  1105. // 小数位数策略示例:
  1106. /* 先加总再保留3位小数:
  1107. 1 35.585
  1108. ├── 1-1 35.585
  1109. │ ├── 1-1-1 35.585
  1110. │ │ └── 202-2 35.585
  1111. │ │ ├── 202-2-c 4.25 6.83 29.028(29.0275)
  1112. │ │ └── 202-2-e 2.15 3.05 6.558(6.5575)
  1113. │ └── 1-1-3
  1114. ├── 1-1-2
  1115. │ └── 1-1-3
  1116. ├── 1-2
  1117. │ ├── 1-2-1
  1118. │ ├── 1-2-2
  1119. │ │ └── 202-2
  1120. │ │ ├── 202-2-c
  1121. │ │ └── 202-2-e
  1122. │ ├── 1-2-3
  1123. │ └── 1-3
  1124. │ └── 1-3-1
  1125. └── 1-4
  1126. */
  1127. /* 先保留3位小数再加总:
  1128. 1 35.586
  1129. ├── 1-1 35.586
  1130. │ ├── 1-1-1 35.586
  1131. │ │ └── 202-2 35.586
  1132. │ │ ├── 202-2-c 4.25 6.83 29.028(29.0275)
  1133. │ │ └── 202-2-e 2.15 3.05 6.558(6.5575)
  1134. │ └── 1-1-3
  1135. ├── 1-1-2
  1136. │ └── 1-1-3
  1137. ├── 1-2
  1138. │ ├── 1-2-1
  1139. │ ├── 1-2-2
  1140. │ │ └── 202-2
  1141. │ │ ├── 202-2-c
  1142. │ │ └── 202-2-e
  1143. │ ├── 1-2-3
  1144. │ └── 1-3
  1145. │ └── 1-3-1
  1146. └── 1-4
  1147. */
  1148. });