pos.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. if (transaction) {
  77. const addRst = await transaction.insert(this.tableName, data);
  78. } else {
  79. const addRst = await this.db.insert(this.tableName, data);
  80. }
  81. }
  82. async _completeInsertPosData(tid, data) {
  83. data.id = this.uuid.v4();
  84. data.tid = tid;
  85. // todo 新增期
  86. data.add_stage = 0;
  87. data.add_times = 0;
  88. data.in_time = new Date();
  89. data.add_user = this.ctx.session.sessionUser.accountId;
  90. }
  91. async _addPosData(tid, data) {
  92. if (data.updateData instanceof Array) {
  93. for (const d of data.updateData) {
  94. this._completeInsertPosData(tid, d);
  95. }
  96. } else {
  97. this._completeInsertPosData(tid, data);
  98. }
  99. await this.db.insert(this.tableName, data);
  100. return { pos: data }
  101. }
  102. async _updatePosData(tid, data) {
  103. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  104. const op = await this.getDataById(data.id);
  105. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  106. const billsPos = await this.getAllDataByCondition({where: {lid: op.lid} });
  107. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  108. if (data.sgfh_qty !== undefined) {
  109. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  110. } else if (op) {
  111. data.sgfh_qty = op.sgfh_qty;
  112. }
  113. if (data.sjcl_qty !== undefined) {
  114. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  115. } else if (op) {
  116. data.sjcl_qty = op.sjcl_qty;
  117. }
  118. if (data.qtcl_qty !== undefined) {
  119. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  120. } else if (op) {
  121. data.qtcl_qty = op.qtcl_qty;
  122. }
  123. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  124. const updateBills = {id: bills.id};
  125. for (const bp of billsPos) {
  126. const calcData = bp.id === data.id ? data : bp;
  127. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  128. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  129. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  130. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  131. }
  132. const info = this.ctx.tender.info;
  133. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  134. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  135. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  136. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  137. const transaction = await this.db.beginTransaction();
  138. try {
  139. transaction.update(this.tableName, data);
  140. transaction.update(this.ctx.service.ledger.tableName, updateBills);
  141. await transaction.commit();
  142. updateBills.ledger_id = bills.ledger_id;
  143. return {
  144. ledger: { update: [updateBills] },
  145. pos: data,
  146. }
  147. } catch(err) {
  148. await transaction.rollback();
  149. throw err;
  150. }
  151. } else {
  152. await this.db.update(this.tableName, data, {tid: tid, id: data.id});
  153. return {pos: data};
  154. }
  155. }
  156. async _deletePosData(tid, data) {
  157. if (!data || data.length === 0) {
  158. throw '提交数据错误';
  159. }
  160. const pos = await this.getPosData({tid: tid, id: data});
  161. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  162. const billsPos = await this.getAllDataByCondition({ where: {lid: bills.id} });
  163. const updateBills = {id: bills.id};
  164. for (const bp of billsPos) {
  165. if (data.indexOf(bp.id) >= 0) continue;
  166. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  167. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  168. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  169. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  170. }
  171. const info = this.ctx.tender.info;
  172. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  173. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  174. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  175. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  176. const transaction = await this.db.beginTransaction();
  177. try {
  178. await transaction.delete(this.tableName, {tid: tid, id: data});
  179. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  180. await transaction.commit();
  181. updateBills.ledger_id = bills.ledger_id;
  182. return { ledger: { update: [updateBills] }, pos: data };
  183. } catch(err) {
  184. await transaction.rollback();
  185. throw err;
  186. }
  187. }
  188. /**
  189. * 保存部位明细数据
  190. * @param data
  191. * @param {Number} tid - 标段id
  192. * @returns {Promise<{ledger: {}, pos: null}>}
  193. */
  194. async savePosData(data, tid) {
  195. switch (data.updateType) {
  196. case 'add':
  197. return await this._addPosData(tid, data.updateData);
  198. case 'update':
  199. return await this._updatePosData(tid, data.updateData);
  200. case 'delete':
  201. return await this._deletePosData(tid, data.updateData);
  202. }
  203. }
  204. /**
  205. * 复制粘贴 部位明细数据
  206. * @param {Array} data - 复制粘贴的数据
  207. * @param {Number} tid - 标段id
  208. * @returns {Promise<{ledger: {}, pos: null}>}
  209. */
  210. async pastePosData(data, tid) {
  211. if (!(data instanceof Array)) {
  212. throw '提交数据错误';
  213. }
  214. const transaction = await this.db.beginTransaction();
  215. const result = { ledger: {}, pos: null }, updateBills = [];
  216. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  217. let bills = null, precision = null;
  218. try {
  219. for (const d of data) {
  220. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  221. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  222. if (!bills || bills.id !== d.lid) {
  223. bills = await this.ctx.service.ledger.getDataById(d.lid);
  224. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  225. updateBills.push(bills);
  226. }
  227. if (d.sgfh_qty !== undefined) {
  228. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  229. } else if (op) {
  230. d.sgfh_qty = op.sgfh_qty;
  231. }
  232. if (d.sjcl_qty !== undefined) {
  233. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  234. } else if (op) {
  235. d.sjcl_qty = op.sjcl_qty;
  236. }
  237. if (d.qtcl_qty) {
  238. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  239. } else if (op) {
  240. d.qtcl_qty = op.qtcl_qty;
  241. }
  242. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  243. }
  244. if (d.id) {
  245. await transaction.update(this.tableName, d);
  246. } else {
  247. this._insertPosData(transaction, d, tid);
  248. }
  249. }
  250. for (const ub of updateBills) {
  251. await this.ctx.service.ledger.calcNode(ub, transaction);
  252. }
  253. await transaction.commit();
  254. } catch (err) {
  255. await transaction.rollback();
  256. throw err;
  257. }
  258. result.pos = data;
  259. if (updateBills.length > 0) {
  260. result.ledger.update = await this.ctx.service.ledger.getDataByIds(this._.map(updateBills, 'id'));
  261. }
  262. return result;
  263. }
  264. /**
  265. * 删除清单下部位明细数据(删除清单时调用)
  266. *
  267. * @param transaction - 事务
  268. * @param tid - 标段id
  269. * @param lid - 清单id
  270. * @returns {Promise<void>}
  271. */
  272. async deletePosData(transaction, tid, lid) {
  273. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  274. }
  275. /**
  276. * 复制整块 拷贝部位明细数据
  277. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  278. * @param {Number} newLid - 新的台账id
  279. * @param transaction - 复制整块事务
  280. * @returns {Promise<void>}
  281. */
  282. async copyBillsPosData(orgLid, newLid, transaction) {
  283. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  284. if (posData.length > 0) {
  285. for (const pd of posData) {
  286. pd.id = this.uuid.v4();
  287. pd.lid = newLid;
  288. pd.tid = this.ctx.tender.id;
  289. pd.in_time = new Date();
  290. }
  291. await transaction.insert(this.tableName, posData);
  292. }
  293. }
  294. /**
  295. * 批量插入部位数据 - 仅供批量插入清单部位调用
  296. * @param transaction - 所属事务
  297. * @param {Number} tid - 标段id
  298. * @param {Number} lid - 台账id
  299. * @param {Array} data - 新增数据
  300. * @returns {Promise<void>}
  301. */
  302. async insertLedgerPosData(transaction, tid, bills, data) {
  303. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  304. const insertDatas = [];
  305. for (const d of data) {
  306. const inD = {
  307. id: this.uuid.v4(), tid: tid, lid: bills.id,
  308. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  309. in_time: new Date(), porder: data.indexOf(d) + 1,
  310. name: d.name, drawing_code: d.drawing_code,
  311. };
  312. if (d.quantity) {
  313. inD.sgfh_qty = this.round(d.quantity, precision.value);
  314. inD.quantity = inD.sgfh_qty;
  315. }
  316. insertDatas.push(inD);
  317. }
  318. await transaction.insert(this.tableName, insertDatas);
  319. }
  320. }
  321. return Pos;
  322. };