pos.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. 'use strict';
  2. /**
  3. * 部位明细
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Pos extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.depart = 20;
  20. this.tableName = 'pos';
  21. }
  22. async getPosData(condition) {
  23. if (!condition.tid) throw '查询计量单元缺少必要信息';
  24. return await this.db.select(this.departTableName(condition.tid), {
  25. where: condition,
  26. columns: ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
  27. 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty',
  28. 'dagl_status', 'dagl_url', 'gxby_status', 'gxby_limit', 'dagl_limit', 'dagl_status'],
  29. order: [['porder', 'ASC']],
  30. });
  31. }
  32. async getPosDataWithAddStageOrder(condition) {
  33. const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.position, p.drawing_code,' +
  34. ' p.sgfh_qty, p.sjcl_qty, p.qtcl_qty, p.porder, p.add_stage, p.add_times, p.add_user, s.order As add_stage_order,' +
  35. ' p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty, p.gxby_status, p.dagl_status, p.dagl_url,' +
  36. ' p.gxby_limit, p.dagl_limit' +
  37. ' FROM ' + this.tableName + ' p ' +
  38. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
  39. ' ON p.add_stage = s.id'
  40. + this.ctx.helper.whereSql(condition, 'p');
  41. return await this.db.query(sql);
  42. }
  43. async getPosDataByIds(ids) {
  44. if (ids instanceof Array && ids.length > 0) {
  45. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  46. ' sgfh_qty, sjcl_qty, qtcl_qty, add_stage, add_times, add_user, sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  47. ' dagl_status, dagl_url, gxby_status, gxby_limit, dagl_limit' +
  48. ' FROM ' + this.tableName +
  49. ' WHERE id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')';
  50. return await this.db.query(sql, []);
  51. } else {
  52. return [];
  53. }
  54. // this.initSqlBuilder();
  55. // this.sqlBuilder.setAndWhere('id', {
  56. // operate: 'in',
  57. // value: ids
  58. // });
  59. // this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  60. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  61. // return await this.db.query(sql, sqlParam);
  62. }
  63. async getPosDataByUnits(tenderId, units) {
  64. const sql = 'SELECT p.id, p.lid, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty' +
  65. ' FROM ' + this.tableName + ' p' +
  66. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' b' +
  67. ' ON p.lid = b.id ' +
  68. ' WHERE p.tid = ? and b.unit IN (?)';
  69. const sqlParam = [tenderId, units];
  70. return await this.db.query(sql, sqlParam);
  71. }
  72. async _insertPosData(transaction, data, tid) {
  73. data.id = this.uuid.v4();
  74. data.tid = tid;
  75. // todo 新增期
  76. data.add_stage = 0;
  77. data.add_times = 0;
  78. data.in_time = new Date();
  79. data.add_user = this.ctx.session.sessionUser.accountId;
  80. if (data.quantity) {
  81. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  82. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  83. data.quantity = this.round(data.quantity, precision.value);
  84. }
  85. if (transaction) {
  86. const addRst = await transaction.insert(this.tableName, data);
  87. } else {
  88. const addRst = await this.db.insert(this.tableName, data);
  89. }
  90. }
  91. async _completeInsertPosData(tid, data) {
  92. data.id = this.uuid.v4();
  93. data.tid = tid;
  94. // todo 新增期
  95. data.add_stage = 0;
  96. data.add_times = 0;
  97. data.in_time = new Date();
  98. data.add_user = this.ctx.session.sessionUser.accountId;
  99. }
  100. async _getUpdateBills(data) {
  101. const datas = data instanceof Array ? data : [data];
  102. const bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  103. const billsPos = await this.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  104. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  105. const updateBills = {id: bills.id};
  106. for (const bp of billsPos) {
  107. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  108. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  109. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  110. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  111. }
  112. for (const d of datas) {
  113. if (d.sgfh_qty) {
  114. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  115. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  116. }
  117. if (d.sjcl_qty) {
  118. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  119. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  120. }
  121. if (d.qtcl_qty) {
  122. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  123. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  124. }
  125. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  126. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  127. }
  128. const info = this.ctx.tender.info;
  129. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  130. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  131. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  132. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  133. return updateBills;
  134. }
  135. async _addPosData(tid, data) {
  136. let updateBills = null;
  137. if (data instanceof Array) {
  138. for (const d of data) {
  139. this._completeInsertPosData(tid, d);
  140. }
  141. if (data[0].sgfh_qty !== undefined || data[0].sjcl_qty !== undefined || data[0].qtcl_qty !== undefined) {
  142. updateBills = await this._getUpdateBills(data);
  143. }
  144. } else {
  145. this._completeInsertPosData(tid, data);
  146. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  147. updateBills = await this._getUpdateBills(data);
  148. }
  149. }
  150. if (updateBills) {
  151. const transaction = await this.db.beginTransaction();
  152. try {
  153. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  154. updateBills.ledger_id = bills.ledger_id;
  155. await transaction.commit();
  156. } catch (err) {
  157. await transaction.rollback();
  158. throw err;
  159. }
  160. return {
  161. ledger: { update: [updateBills] },
  162. pos: data,
  163. }
  164. } else {
  165. await this.db.insert(this.tableName, data);
  166. return { pos: data }
  167. }
  168. }
  169. async _updatePosData(tid, data) {
  170. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  171. const op = await this.getDataById(data.id);
  172. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  173. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  174. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  175. if (data.sgfh_qty !== undefined) {
  176. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  177. } else if (op) {
  178. data.sgfh_qty = op.sgfh_qty;
  179. }
  180. if (data.sjcl_qty !== undefined) {
  181. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  182. } else if (op) {
  183. data.sjcl_qty = op.sjcl_qty;
  184. }
  185. if (data.qtcl_qty !== undefined) {
  186. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  187. } else if (op) {
  188. data.qtcl_qty = op.qtcl_qty;
  189. }
  190. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  191. const updateBills = {id: bills.id};
  192. for (const bp of billsPos) {
  193. const calcData = bp.id === data.id ? data : bp;
  194. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  195. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  196. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  197. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  198. }
  199. const info = this.ctx.tender.info;
  200. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  201. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  202. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  203. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  204. const transaction = await this.db.beginTransaction();
  205. try {
  206. await transaction.update(this.tableName, data);
  207. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  208. await transaction.commit();
  209. updateBills.ledger_id = bills.ledger_id;
  210. return {
  211. ledger: { update: [updateBills] },
  212. pos: data,
  213. }
  214. } catch(err) {
  215. await transaction.rollback();
  216. throw err;
  217. }
  218. } else {
  219. await this.db.update(this.tableName, data);
  220. return {pos: data};
  221. }
  222. }
  223. async _updatePosDatas(tid, data) {
  224. if (data.length === 0) return;
  225. const op = await this.getDataById(data[0].id);
  226. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  227. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  228. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  229. let needUpdateBills;
  230. for (const d of data) {
  231. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  232. if (d.sgfh_qty !== undefined) {
  233. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  234. } else if (op) {
  235. d.sgfh_qty = op.sgfh_qty;
  236. }
  237. if (d.sjcl_qty !== undefined) {
  238. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  239. } else if (op) {
  240. d.sjcl_qty = op.sjcl_qty;
  241. }
  242. if (d.qtcl_qty !== undefined) {
  243. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  244. } else if (op) {
  245. d.qtcl_qty = op.qtcl_qty;
  246. }
  247. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  248. needUpdateBills = true;
  249. }
  250. }
  251. const updateBills = {id: bills.id};
  252. if (needUpdateBills) {
  253. for (const bp of billsPos) {
  254. const newPos = data.find(function (x) { return x.id === bp.id });
  255. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  256. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  257. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  258. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  259. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  260. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  261. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  262. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  263. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  264. updateBills.quantity = newPos && newPos.quantity !== undefined
  265. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  266. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  267. }
  268. const info = this.ctx.tender.info;
  269. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  270. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  271. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  272. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  273. }
  274. const transaction = await this.db.beginTransaction();
  275. try {
  276. for (const d of data) {
  277. await transaction.update(this.tableName, d);
  278. }
  279. if (needUpdateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  280. await transaction.commit();
  281. updateBills.ledger_id = bills.ledger_id;
  282. return {
  283. ledger: { update: [updateBills] },
  284. pos: data,
  285. }
  286. } catch(err) {
  287. await transaction.rollback();
  288. throw err;
  289. }
  290. }
  291. async _deletePosData(tid, data) {
  292. if (!data || data.length === 0) {
  293. throw '提交数据错误';
  294. }
  295. const pos = await this.getPosData({tid: tid, id: data});
  296. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  297. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  298. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  299. for (const bp of billsPos) {
  300. if (data.indexOf(bp.id) >= 0) continue;
  301. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  302. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  303. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  304. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  305. }
  306. const info = this.ctx.tender.info;
  307. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  308. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  309. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  310. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  311. const transaction = await this.db.beginTransaction();
  312. try {
  313. await transaction.delete(this.tableName, {tid: tid, id: data});
  314. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  315. await transaction.commit();
  316. updateBills.ledger_id = bills.ledger_id;
  317. return { ledger: { update: [updateBills] }, pos: data };
  318. } catch(err) {
  319. await transaction.rollback();
  320. throw err;
  321. }
  322. }
  323. /**
  324. * 保存部位明细数据
  325. * @param data
  326. * @param {Number} tid - 标段id
  327. * @returns {Promise<{ledger: {}, pos: null}>}
  328. */
  329. async savePosData(data, tid) {
  330. switch (data.updateType) {
  331. case 'add':
  332. return await this._addPosData(tid, data.updateData);
  333. case 'update':
  334. if (data.updateData instanceof Array) {
  335. return await this._updatePosDatas(tid, data.updateData);
  336. } else {
  337. return await this._updatePosData(tid, data.updateData);
  338. }
  339. case 'delete':
  340. return await this._deletePosData(tid, data.updateData);
  341. }
  342. }
  343. /**
  344. * 复制粘贴 部位明细数据
  345. * @param {Array} data - 复制粘贴的数据
  346. * @param {Number} tid - 标段id
  347. * @returns {Promise<{ledger: {}, pos: null}>}
  348. */
  349. async pastePosData(data, tid) {
  350. if (!(data instanceof Array)) {
  351. throw '提交数据错误';
  352. }
  353. const transaction = await this.db.beginTransaction();
  354. const result = { ledger: {}, pos: null };
  355. const bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  356. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  357. const updateBills = {id: bills.id};
  358. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  359. for (const bp of billsPos) {
  360. const d = data.find(function (x) {
  361. return bp.id ? x.id === bp.id : false;
  362. });
  363. if (d) continue;
  364. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  365. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  366. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  367. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  368. }
  369. try {
  370. for (const d of data) {
  371. const op = d.id ? this._.find(billsPos, {id: d.id}) : null;
  372. if (d.sgfh_qty !== undefined) {
  373. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  374. } else if (op) {
  375. d.sgfh_qty = op.sgfh_qty;
  376. }
  377. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  378. if (d.sjcl_qty !== undefined) {
  379. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  380. } else if (op) {
  381. d.sjcl_qty = op.sjcl_qty;
  382. }
  383. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  384. if (d.qtcl_qty) {
  385. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  386. } else if (op) {
  387. d.qtcl_qty = op.qtcl_qty;
  388. }
  389. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  390. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  391. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  392. if (d.id) {
  393. await transaction.update(this.tableName, d);
  394. } else {
  395. this._completeInsertPosData(tid, d);
  396. await transaction.insert(this.tableName, d);
  397. }
  398. }
  399. const info = this.ctx.tender.info;
  400. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  401. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  402. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  403. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  404. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  405. updateBills.ledger_id = bills.ledger_id;
  406. await transaction.commit();
  407. } catch (err) {
  408. await transaction.rollback();
  409. throw err;
  410. }
  411. result.pos = data;
  412. result.ledger.update = [updateBills];
  413. return result;
  414. }
  415. /**
  416. * 删除清单下部位明细数据(删除清单时调用)
  417. *
  418. * @param transaction - 事务
  419. * @param tid - 标段id
  420. * @param lid - 清单id
  421. * @returns {Promise<void>}
  422. */
  423. async deletePosData(transaction, tid, lid) {
  424. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  425. }
  426. /**
  427. * 复制整块 拷贝部位明细数据
  428. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  429. * @param {Number} newLid - 新的台账id
  430. * @param transaction - 复制整块事务
  431. * @returns {Promise<void>}
  432. */
  433. async copyBillsPosData(lid, transaction) {
  434. const lidArr = lid instanceof Array ? lid : [lid];
  435. const pasteData = [];
  436. for (const id of lidArr) {
  437. const posData = await this.getAllDataByCondition({ where: { lid: id.org } });
  438. if (posData.length > 0) {
  439. for (const pd of posData) {
  440. pd.id = this.uuid.v4();
  441. pd.lid = id.new;
  442. pd.tid = this.ctx.tender.id;
  443. pd.in_time = new Date();
  444. pasteData.push(pd);
  445. }
  446. }
  447. }
  448. await transaction.insert(this.tableName, pasteData);
  449. }
  450. /**
  451. * 批量插入部位数据 - 仅供批量插入清单部位调用
  452. * @param transaction - 所属事务
  453. * @param {Number} tid - 标段id
  454. * @param {Number} lid - 台账id
  455. * @param {Array} data - 新增数据
  456. * @returns {Promise<void>}
  457. */
  458. async insertLedgerPosData(transaction, tid, bills, data) {
  459. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  460. const insertDatas = [];
  461. for (const d of data) {
  462. const inD = {
  463. id: this.uuid.v4(), tid: tid, lid: bills.id,
  464. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  465. in_time: new Date(), porder: data.indexOf(d) + 1,
  466. name: d.name, drawing_code: d.drawing_code,
  467. };
  468. if (d.quantity) {
  469. inD.sgfh_qty = this.round(d.quantity, precision.value);
  470. inD.quantity = inD.sgfh_qty;
  471. }
  472. insertDatas.push(inD);
  473. }
  474. await transaction.insert(this.tableName, insertDatas);
  475. }
  476. }
  477. return Pos;
  478. };