pos.js 15 KB

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