pos.js 13 KB

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