base_bills_service.js 17 KB

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