pos.js 12 KB

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