pos.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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.tableName = 'pos';
  20. }
  21. async getPosData(condition) {
  22. const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.drawing_code, 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 ' +
  23. ' FROM ' + this.tableName + ' p ' +
  24. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
  25. ' ON add_stage = s.id'
  26. + this.ctx.helper.whereSql(condition, 'p');
  27. return await this.db.query(sql);
  28. // return await this.db.select(this.tableName, {
  29. // where: condition,
  30. // columns: ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'in_time', 'porder', 'add_stage'],
  31. // order: [['porder', 'ASC']],
  32. // });
  33. }
  34. async getPosDataByIds(ids) {
  35. if (ids instanceof Array && ids.length > 0) {
  36. const sql = 'SELECT id, tid, lid, name, quantity, drawing_code, sgfh_qty, sjcl_qty, qtcl_qty' +
  37. ' FROM ' + this.tableName +
  38. ' WHERE id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')';
  39. return await this.db.query(sql, []);
  40. } else {
  41. return [];
  42. }
  43. // this.initSqlBuilder();
  44. // this.sqlBuilder.setAndWhere('id', {
  45. // operate: 'in',
  46. // value: ids
  47. // });
  48. // this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  49. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  50. // return await this.db.query(sql, sqlParam);
  51. }
  52. async _insertPosData(transaction, data, tid) {
  53. data.id = this.uuid.v4();
  54. data.tid = tid;
  55. // todo 新增期
  56. data.add_stage = 0;
  57. data.add_times = 0;
  58. data.in_time = new Date();
  59. data.add_user = this.ctx.session.sessionUser.accountId;
  60. if (data.quantity) {
  61. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  62. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  63. data.quantity = this.round(data.quantity, precision.value);
  64. }
  65. const addRst = await transaction.insert(this.tableName, data);
  66. }
  67. /**
  68. * 保存部位明细数据
  69. * @param data
  70. * @param {Number} tid - 标段id
  71. * @returns {Promise<{ledger: {}, pos: null}>}
  72. */
  73. async savePosData(data, tid) {
  74. const result = { ledger: {}, pos: null };
  75. const transaction = await this.db.beginTransaction();
  76. try {
  77. if (data.updateType === 'add') {
  78. const tender = await this.ctx.service.tender.getTender(tid);
  79. if (data.updateData instanceof Array) {
  80. for (const d of data.updateData) {
  81. this._insertPosData(transaction, d, tid);
  82. }
  83. } else {
  84. this._insertPosData(transaction, data.updateData, tid);
  85. }
  86. } else if (data.updateType === 'update') {
  87. const datas = data.updateData instanceof Array ? data.updateData : [data.updateData];
  88. result.ledger.update = [];
  89. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  90. for (const d of datas) {
  91. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  92. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  93. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  94. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  95. if (d.sgfh_qty !== undefined) {
  96. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  97. } else if (op) {
  98. d.sgfh_qty = op.sgfh_qty;
  99. }
  100. if (d.sjcl_qty !== undefined) {
  101. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  102. } else if (op) {
  103. d.sjcl_qty = op.sjcl_qty;
  104. }
  105. if (d.qtcl_qty !== undefined) {
  106. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  107. } else if (op) {
  108. d.qtcl_qty = op.qtcl_qty;
  109. }
  110. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  111. }
  112. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  113. if (d.quantity !== undefined && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  114. result.ledger.update.push(op.lid);
  115. }
  116. }
  117. for (const lid of result.ledger.update) {
  118. await this.ctx.service.ledger.calc(tid, lid, transaction);
  119. }
  120. } else if (data.updateType === 'delete') {
  121. if (!data.updateData || data.updateData.length === 0) {
  122. throw '提交数据错误';
  123. }
  124. const pos = await this.getPosData({tid: tid, id: data.updateData});
  125. const ledgerIds = this._.map(pos, 'lid');
  126. await transaction.delete(this.tableName, {tid: tid, id: data.updateData});
  127. for (const lid of ledgerIds) {
  128. await this.ctx.service.ledger.calc(tid, lid, transaction);
  129. }
  130. result.ledger.update = ledgerIds;
  131. } else {
  132. throw '提交数据错误';
  133. }
  134. await transaction.commit();
  135. } catch (err) {
  136. await transaction.rollback();
  137. throw err;
  138. }
  139. result.pos = data.updateData;
  140. result.ledger.update = await this.ctx.service.ledger.getDataByIds(result.ledger.update);
  141. return result;
  142. }
  143. /**
  144. * 复制粘贴 部位明细数据
  145. * @param {Array} data - 复制粘贴的数据
  146. * @param {Number} tid - 标段id
  147. * @returns {Promise<{ledger: {}, pos: null}>}
  148. */
  149. async pastePosData(data, tid) {
  150. if (!(data instanceof Array)) {
  151. throw '提交数据错误';
  152. }
  153. const transaction = await this.db.beginTransaction();
  154. const result = { ledger: {}, pos: null }, updateLid = [];
  155. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  156. let bills = null, precision = null;
  157. try {
  158. for (const d of data) {
  159. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  160. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  161. if (!bills || bills.id !== d.lid) {
  162. bills = await this.ctx.service.ledger.getDataById(d.lid);
  163. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  164. updateLid.push(d.lid);
  165. }
  166. if (d.sgfh_qty !== undefined) {
  167. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  168. } else if (op) {
  169. d.sgfh_qty = op.sgfh_qty;
  170. }
  171. if (d.sjcl_qty !== undefined) {
  172. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  173. } else if (op) {
  174. d.sjcl_qty = op.sjcl_qty;
  175. }
  176. if (d.qtcl_qty) {
  177. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  178. } else if (op) {
  179. d.qtcl_qty = op.qtcl_qty;
  180. }
  181. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  182. }
  183. if (d.id) {
  184. await transaction.update(this.tableName, d);
  185. } else {
  186. this._insertPosData(transaction, d, tid);
  187. }
  188. }
  189. for (const lid of updateLid) {
  190. await this.ctx.service.ledger.calc(tid, lid, transaction);
  191. }
  192. await transaction.commit();
  193. } catch (err) {
  194. await transaction.rollback();
  195. throw err;
  196. }
  197. result.pos = data;
  198. if (updateLid.length > 0) {
  199. result.ledger.update = await this.ctx.service.ledger.getDataByIds(updateLid);
  200. }
  201. return result;
  202. }
  203. /**
  204. * 删除清单下部位明细数据(删除清单时调用)
  205. *
  206. * @param transaction - 事务
  207. * @param tid - 标段id
  208. * @param lid - 清单id
  209. * @returns {Promise<void>}
  210. */
  211. async deletePosData(transaction, tid, lid) {
  212. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  213. }
  214. /**
  215. * 复制整块 拷贝部位明细数据
  216. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  217. * @param {Number} newLid - 新的台账id
  218. * @param transaction - 复制整块事务
  219. * @returns {Promise<void>}
  220. */
  221. async copyBillsPosData(orgLid, newLid, transaction) {
  222. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  223. if (posData.length > 0) {
  224. for (const pd of posData) {
  225. pd.id = this.uuid.v4();
  226. pd.lid = newLid;
  227. pd.tid = this.ctx.tender.id;
  228. pd.in_time = new Date();
  229. }
  230. await transaction.insert(this.tableName, posData);
  231. }
  232. }
  233. /**
  234. * 批量插入部位数据 - 仅供批量插入清单部位调用
  235. * @param transaction - 所属事务
  236. * @param {Number} tid - 标段id
  237. * @param {Number} lid - 台账id
  238. * @param {Array} data - 新增数据
  239. * @returns {Promise<void>}
  240. */
  241. async insertLedgerPosData(transaction, tid, bills, data) {
  242. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  243. const insertDatas = [];
  244. for (const d of data) {
  245. const inD = {
  246. id: this.uuid.v4(), tid: tid, lid: bills.id,
  247. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  248. in_time: new Date(), porder: data.indexOf(d) + 1,
  249. name: d.name, drawing_code: d.drawing_code,
  250. };
  251. if (d.quantity) {
  252. inD.sgfh_qty = this.round(d.quantity, precision.value);
  253. inD.quantity = inD.sgfh_qty;
  254. }
  255. insertDatas.push(inD);
  256. }
  257. await transaction.insert(this.tableName, insertDatas);
  258. }
  259. }
  260. return Pos;
  261. };