pos.js 25 KB

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