base_bills_service.js 18 KB

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