base_bills_service.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const TreeService = require('./base_tree_service');
  10. // sql拼装器
  11. const rootId = -1;
  12. const calcFields = ['unit_price', 'sgfh_qty', 'sgfh_tp', 'sjcl_qty', 'sjcl_tp', 'qtcl_qty', 'qtcl_tp', 'deal_qty', 'deal_tp', 'dgn_qty1', 'dgn_qty2'];
  13. const readOnlyFields = ['id', 'tender_id', 'ledger_id', 'ledger_pid', 'order', 'level', 'full_path', 'is_leaf'];
  14. const upFields = ['unit_price'];
  15. const qtyFields = ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'quantity', 'deal_qty', 'dgn_qty1', 'dgn_qty2'];
  16. const tpFields = ['sgfh_tp', 'sjcl_tp', 'qtcl_tp', 'total_price', 'deal_tp'];
  17. class BaseBillsSerivce extends TreeService {
  18. // 继承方法
  19. clearParentingData(data) {
  20. data.unit_price = null;
  21. data.sgfh_qty = null;
  22. data.sgfh_tp = null;
  23. data.sjcl_qty = null;
  24. data.sjcl_tp = null;
  25. data.qtcl_qty = null;
  26. data.qtcl_tp = null;
  27. data.quantity = null;
  28. data.total_price = null;
  29. data.deal_qty = null;
  30. data.deal_tp = null;
  31. }
  32. /**
  33. * 从标准数据中提取有效数据
  34. * @param {Object} stdData - 从标准库中查询所得
  35. * @returns {name, unit, source, code, b_code}
  36. * @private
  37. */
  38. _filterStdData(stdData) {
  39. const result = {
  40. name: stdData.name,
  41. unit: stdData.unit,
  42. source: stdData.source,
  43. node_type: stdData.node_type,
  44. };
  45. result.code = stdData.code ? stdData.code : '';
  46. result.b_code = stdData.b_code ? stdData.b_code : '';
  47. return result;
  48. }
  49. async addBillsNode(tenderId, selectId, data, reviseId) {
  50. if (data) {
  51. if (reviseId) data.crid = reviseId;
  52. return await this.addNode(tenderId, selectId, data);
  53. } else {
  54. return await this.addNode(tenderId, selectId, {crid: reviseId});
  55. }
  56. }
  57. /**
  58. * 新增子节点,并排在所有子节点的末尾
  59. * @param {Number} tenderId - 标段Id
  60. * @param {Number} selectId - 父节点Id
  61. * @param {Object} data - 新增节点数据(编号名称等)
  62. * @returns {Promise<*>}
  63. */
  64. async addChild(tenderId, selectId, data, reviseId) {
  65. if ((tenderId <= 0) || (selectId <= 0)) {
  66. return [];
  67. }
  68. const selectData = await this.getDataByKid(tenderId, selectId);
  69. if (!selectData) {
  70. throw '新增节点数据错误';
  71. }
  72. const children = await this.getChildrenByParentId(tenderId, selectId);
  73. const maxId = await this._getMaxLid(tenderId);
  74. data.id = this.uuid.v4();
  75. data.tender_id = tenderId;
  76. data.ledger_id = maxId + 1;
  77. data.ledger_pid = selectData.ledger_id;
  78. data.level = selectData.level + 1;
  79. data.order = children.length + 1;
  80. data.full_path = selectData.full_path + '-' + data.ledger_id;
  81. data.is_leaf = true;
  82. if (reviseId) data.crid = reviseId;
  83. this.transaction = await this.db.beginTransaction();
  84. try {
  85. const result = await this.transaction.insert(this.tableName, data);
  86. if (children.length === 0) {
  87. await this.transaction.update(this.tableName,
  88. {
  89. is_leaf: false,
  90. sgfh_qty: null, sgfh_tp: null, qtcl_qty: null, qtcl_tp: null, sjcl_qty: null, sjcl_tp: null,
  91. quantity: null, unit_price: null, total_price: null, deal_qty: null, deal_tp: null
  92. },
  93. { where: {tender_id: tenderId, ledger_id: selectData.ledger_id} });
  94. }
  95. await this.transaction.commit();
  96. } catch(err) {
  97. this.transaction.rollback();
  98. throw err;
  99. }
  100. this._cacheMaxLid(tenderId, maxId + 1);
  101. // 查询应返回的结果
  102. const resultData = {};
  103. resultData.create = await this.getDataByKid(tenderId, data.ledger_id);
  104. if (children.length === 0) {
  105. resultData.update = await this.getDataByKid(tenderId, selectId);
  106. }
  107. return resultData;
  108. }
  109. /**
  110. * 添加节点(来自标准清单)
  111. * @param {Number} tenderId
  112. * @param {Number} selectId
  113. * @param {Object} stdData
  114. * @return {Promise<*>}
  115. */
  116. async addStdNode(tenderId, selectId, stdData, reviseId) {
  117. const newData = this._filterStdData(stdData);
  118. const result = await this.addBillsNode(tenderId, selectId, newData, reviseId);
  119. return result;
  120. }
  121. /**
  122. * 添加标准节点,将选择的标准节点,添加为子节点(排序为末尾)
  123. * @param {Number} tenderId - 标段Id
  124. * @param {Number} selectId - 添加目标节点Id
  125. * @param {Object} stdData - 标准节点数据
  126. * @returns {Promise<*>}
  127. */
  128. async addStdNodeAsChild(tenderId, selectId, stdData, reviseId) {
  129. const newData = this._filterStdData(stdData);
  130. const result = await this.addChild(tenderId, selectId, newData, reviseId);
  131. return result;
  132. }
  133. /**
  134. * 根据parentData, data新增数据(新增为parentData的最后一个子项)
  135. * @param {Number} tenderId - 标段id
  136. * @param {Object} parentData - 父项数据
  137. * @param {Object} data - 新增节点,初始数据
  138. * @return {Promise<*>} - 新增结果
  139. * @private
  140. */
  141. async _addChildNodeData(tenderId, parentData, data, reviseId) {
  142. if (tenderId <= 0) {
  143. return undefined;
  144. }
  145. if (!data) {
  146. data = {};
  147. }
  148. const pid = parentData ? parentData.ledger_id : rootId;
  149. const maxId = await this._getMaxLid(tenderId);
  150. data.id = this.uuid.v4();
  151. data.tender_id = tenderId;
  152. data.ledger_id = maxId + 1;
  153. data.ledger_pid = pid;
  154. if (data.order === undefined) {
  155. data.order = 1;
  156. }
  157. data.level = parentData ? parentData.level + 1 : 1;
  158. data.full_path = parentData ? parentData.full_path + '-' + data.ledger_id : '' + data.ledger_id;
  159. if (data.is_leaf === undefined) {
  160. data.is_leaf = true;
  161. }
  162. if (reviseId) data.crid = reviseId;
  163. const result = await this.transaction.insert(this.tableName, data);
  164. this._cacheMaxLid(tenderId, maxId + 1);
  165. return [result, data];
  166. }
  167. /**
  168. * 根据parentData, data新增数据(自动排序)
  169. * @param tenderId
  170. * @param parentData
  171. * @param data
  172. * @return {Promise<void>}
  173. * @private
  174. */
  175. async _addChildAutoOrder(tenderId, parentData, data, reviseId) {
  176. const self = this;
  177. const findPreData = function(list, a) {
  178. if (!list || list.length === 0) { return null; }
  179. for (let i = 0, iLen = list.length; i < iLen; i++) {
  180. if (self.ctx.helper.compareCode(list[i].code, a.code) > 0) {
  181. return i > 0 ? list[i - 1] : null;
  182. }
  183. }
  184. return list[list.length - 1];
  185. };
  186. const pid = parentData ? parentData.ledger_id : rootId;
  187. const children = await this.getChildrenByParentId(tenderId, pid);
  188. const preData = findPreData(children, data);
  189. if (!preData || children.indexOf(preData) < children.length - 1) {
  190. await this._updateChildrenOrder(tenderId, pid, preData ? preData.order + 1 : 1);
  191. }
  192. data.order = preData ? preData.order + 1 : 1;
  193. const [addResult, node] = await this._addChildNodeData(tenderId, parentData, data, reviseId);
  194. return [addResult, node];
  195. }
  196. /**
  197. * 添加节点,并同步添加父节点
  198. * @param {Number} tenderId - 标段id
  199. * @param {Number} selectId - 选中节点id
  200. * @param {Object} stdData - 节点数据
  201. * @param {StandardLib} stdLib - 标准库
  202. * @return {Promise<void>}
  203. */
  204. async addStdNodeWithParent(tenderId, stdData, stdLib, reviseId) {
  205. // 查询完整标准清单,并按层次排序
  206. const fullLevel = await stdLib.getFullLevelDataByFullPath(stdData.list_id, stdData.full_path);
  207. fullLevel.sort(function(x, y) {
  208. return x.level - y.level;
  209. });
  210. let isNew = false,
  211. node,
  212. firstNew,
  213. updateParent,
  214. addResult;
  215. const expandIds = [];
  216. this.transaction = await this.db.beginTransaction();
  217. try {
  218. // 从最顶层节点依次查询是否存在,否则添加
  219. for (let i = 0, len = fullLevel.length; i < len; i++) {
  220. const stdNode = fullLevel[i];
  221. if (isNew) {
  222. const newData = this._filterStdData(stdNode);
  223. newData.is_leaf = (i === len - 1);
  224. [addResult, node] = await this._addChildNodeData(tenderId, node, newData, reviseId);
  225. } else {
  226. const parent = node;
  227. node = await this.getDataByCondition({
  228. tender_id: tenderId,
  229. ledger_pid: parent ? parent.ledger_id : rootId,
  230. code: stdNode.code,
  231. name: stdNode.name,
  232. });
  233. if (!node) {
  234. isNew = true;
  235. const newData = this._filterStdData(stdNode);
  236. newData.is_leaf = (i === len - 1);
  237. [addResult, node] = await this._addChildAutoOrder(tenderId, parent, newData, reviseId);
  238. if (parent && parent.is_leaf) {
  239. await this.transaction.update(this.tableName, { id: parent.id, is_leaf: false,
  240. unit_price: null, quantity: null, total_price: null, deal_qty: null, deal_tp: null});
  241. updateParent = parent;
  242. }
  243. firstNew = node;
  244. } else {
  245. expandIds.push(node.ledger_id);
  246. }
  247. }
  248. }
  249. await this.transaction.commit();
  250. } catch (err) {
  251. await this.transaction.rollback();
  252. throw err;
  253. }
  254. // 查询应返回的结果
  255. let createData = [],
  256. updateData = [];
  257. if (firstNew) {
  258. createData = await this.getDataByFullPath(tenderId, firstNew.full_path + '%');
  259. updateData = await this.getNextsData(tenderId, firstNew.ledger_pid, firstNew.order);
  260. if (updateParent) {
  261. updateData.push(await this.getDataByCondition({ id: updateParent.id }));
  262. }
  263. }
  264. return { create: createData, update: updateData };
  265. }
  266. /**
  267. * 过滤data中update方式不可提交的字段
  268. * @param {Number} id - 主键key
  269. * @param {Object} data
  270. * @return {Object<{id: *}>}
  271. * @private
  272. */
  273. _filterUpdateInvalidField(id, data) {
  274. const result = {
  275. id,
  276. };
  277. for (const prop in data) {
  278. if (readOnlyFields.indexOf(prop) === -1) {
  279. result[prop] = data[prop];
  280. }
  281. }
  282. return result;
  283. }
  284. /**
  285. * newData中,以orgData为基准,过滤掉orgData中未定义或值相等的部分
  286. * @param {Object} orgData
  287. * @param {Object} newData
  288. * @private
  289. */
  290. _filterChangedField(orgData, newData) {
  291. const result = {};
  292. let bChanged = false;
  293. for (const prop in orgData) {
  294. if (this._.isEmpty(newData[prop]) && newData[prop] !== orgData[prop]) {
  295. result[prop] = newData[prop];
  296. bChanged = true;
  297. }
  298. }
  299. return bChanged ? result : undefined;
  300. }
  301. /**
  302. * 检查data中是否含有计算字段
  303. * @param {Object} data
  304. * @return {boolean}
  305. * @private
  306. */
  307. _checkCalcField(data) {
  308. for (const prop in data) {
  309. if (calcFields.indexOf(prop) >= 0) {
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. /**
  316. * 提交数据 - 响应计算(增量方式计算)
  317. * @param {Number} tenderId
  318. * @param {Object} data
  319. * @return {Promise<*>}
  320. */
  321. async updateCalc(tenderId, data) {
  322. // 简单验证数据
  323. if (tenderId <= 0 || !this.ctx.tender) {
  324. throw '标段不存在';
  325. }
  326. const info = this.ctx.tender.info;
  327. if (!data) {
  328. throw '提交数据错误';
  329. }
  330. const datas = data instanceof Array ? data : [data];
  331. const ids = [];
  332. for (const row of datas) {
  333. if (tenderId !== row.tender_id) {
  334. throw '提交数据错误';
  335. }
  336. ids.push(row.id);
  337. }
  338. this.transaction = await this.db.beginTransaction();
  339. try {
  340. for (const row of datas) {
  341. const updateNode = await this.getDataById(row.id);
  342. if (!updateNode || tenderId !== updateNode.tender_id || row.ledger_id !== updateNode.ledger_id) {
  343. throw '提交数据错误';
  344. }
  345. let updateData;
  346. if (row.unit) {
  347. if (row.sgfh_qty === undefined) { row.sgfh_qty = updateNode.sgfh_qty; }
  348. if (row.sjcl_qty === undefined) { row.sjcl_qty = updateNode.sjcl_qty; }
  349. if (row.qtcl_qty === undefined) { row.qtcl_qty = updateNode.qtcl_qty; }
  350. if (row.deal_qty === undefined) { row.deal_qty = updateNode.deal_qty; }
  351. }
  352. if (row.b_code) {
  353. row.dgn_qty1 = null;
  354. row.dgn_qty2 = null;
  355. }
  356. if (this._checkCalcField(row)) {
  357. let calcData = JSON.parse(JSON.stringify(row));
  358. const precision = this.ctx.helper.findPrecision(info.precision, row.unit ? row.unit : updateNode.unit);
  359. // 数量保留小数位数
  360. this.ctx.helper.checkFieldPrecision(calcData, qtyFields, precision.value);
  361. // 单位保留小数位数
  362. this.ctx.helper.checkFieldPrecision(calcData, upFields, info.decimal.up);
  363. // 未提交单价则读取数据库单价
  364. if (row.unit_price === undefined) calcData.unit_price = updateNode.unit_price;
  365. // 计算
  366. if (row.sgfh_qty !== undefined || row.sjcl_qty !== undefined || row.qtcl_qty !== undefined ||
  367. row.deal_qty !== undefined || row.unit_price) {
  368. if (row.sgfh_qty === undefined) calcData.sgfh_qty = updateNode.sgfh_qty;
  369. if (row.sjcl_qty === undefined) calcData.sjcl_qty = updateNode.sjcl_qty;
  370. if (row.qtcl_qty === undefined) calcData.qtcl_qty = updateNode.qtcl_qty;
  371. if (row.deal_qty === undefined) calcData.deal_qty = updateNode.deal_qty;
  372. calcData.quantity = this.ctx.helper.sum([calcData.sgfh_qty, calcData.sjcl_qty, calcData.qtcl_qty]);
  373. calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
  374. calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
  375. calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
  376. calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
  377. calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
  378. } else if (row.sgfh_tp !== undefined || row.sjcl_tp !== undefined || row.qtcl_tp !== undefined || row.deal_tp !== undefined) {
  379. calcData.sgfh_qty = null;
  380. calcData.sjcl_qty = null;
  381. calcData.qtcl_qty = null;
  382. calcData.quantity = null;
  383. calcData.deal_qty = null;
  384. calcData.sgfh_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sgfh_tp, info.decimal.tp) : updateNode.sgfh_tp;
  385. calcData.sjcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.sjcl_tp, info.decimal.tp) : updateNode.sjcl_tp;
  386. calcData.qtcl_tp = (row.sgfh_tp !== undefined) ? this.ctx.helper.round(calcData.row.qtcl_tp, info.decimal.tp) : updateNode.qtcl_tp;
  387. calcData.total_price = this.ctx.helper.sum([calcData.sgfh_tp, calcData.sjcl_tp, calcData.qtcl_tp]);
  388. calcData.deal_tp = (row.deal_tp !== undefined) ? this.ctx.helper.round(calcData.row.deal_tp, info.decimal.tp) : updateNode.deal_tp;
  389. } else if (row.unit_price !== undefined) {
  390. calcData.sgfh_tp = this.ctx.helper.mul(calcData.sgfh_qty, calcData.unit_price, info.decimal.tp);
  391. calcData.sjcl_tp = this.ctx.helper.mul(calcData.sjcl_qty, calcData.unit_price, info.decimal.tp);
  392. calcData.qtcl_tp = this.ctx.helper.mul(calcData.qtcl_qty, calcData.unit_price, info.decimal.tp);
  393. calcData.total_price = this.ctx.helper.mul(calcData.quantity, calcData.unit_price, info.decimal.tp);
  394. calcData.deal_tp = this.ctx.helper.mul(calcData.deal_qty, calcData.unit_price, info.decimal.tp);
  395. }
  396. updateData = this._filterUpdateInvalidField(updateNode.id, calcData);
  397. } else {
  398. updateData = this._filterUpdateInvalidField(updateNode.id, row);
  399. }
  400. await this.transaction.update(this.tableName, updateData);
  401. }
  402. await this.transaction.commit();
  403. this.transaction = null;
  404. } catch (err) {
  405. await this.transaction.rollback();
  406. this.transaction = null;
  407. throw err;
  408. }
  409. return { update: await this.getDataById(ids) };
  410. }
  411. // 统计方法
  412. async addUp(condition) {
  413. const sql = 'SELECT Sum(total_price) As total_price, Sum(deal_tp) As deal_tp' +
  414. ' FROM ' + this.tableName + this.ctx.helper.whereSql(condition);
  415. const result = await this.db.queryOne(sql);
  416. return result;
  417. }
  418. // 导入Excel
  419. async importGclExcel(id, sheet, data) {
  420. const node = await this.getDataById(id);
  421. if (!node || !node.is_leaf || node.tender_id !== this.ctx.tender.id) throw '数据错误';
  422. const maxId = await this._getMaxLid(this.ctx.tender.id);
  423. const AnalysisExcel = require('../lib/analysis_excel').AnalysisGclExcelTree;
  424. const analysisExcel = new AnalysisExcel(this.ctx);
  425. const cacheData = analysisExcel.analysisData(sheet, node, maxId, data);
  426. const datas = [];
  427. for (const node of cacheData.items) {
  428. datas.push({
  429. id: node.id, tender_id: this.ctx.tender.id,
  430. ledger_id: node.ledger_id,
  431. ledger_pid: node.ledger_pid,
  432. level: node.level,
  433. order: node.order,
  434. is_leaf: !node.children || node.children.length === 0,
  435. full_path: node.full_path,
  436. b_code: node.b_code,
  437. name: node.name,
  438. unit: node.unit,
  439. sgfh_qty: node.sgfh_qty,
  440. sgfh_tp: node.sgfh_tp,
  441. quantity: node.quantity,
  442. unit_price: node.unit_price,
  443. total_price: node.total_price,
  444. crid: node.crid,
  445. });
  446. }
  447. const conn = await this.db.beginTransaction();
  448. try {
  449. await this.db.update(this.tableName, {id: node.id, is_leaf: false});
  450. await this.db.insert(this.tableName, datas);
  451. await conn.commit();
  452. } catch(err) {
  453. await conn.rollback();
  454. throw err;
  455. }
  456. this._cacheMaxLid(this.ctx.tender.id, cacheData.keyNodeId);
  457. node.is_leaf = false;
  458. return { create: datas, update: [node]};
  459. }
  460. }
  461. module.exports = BaseBillsSerivce;