pos.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. console.log(data);
  140. transaction.update(this.tableName, data);
  141. console.log(updateBills);
  142. transaction.update(this.ctx.service.ledger.tableName, updateBills);
  143. await transaction.commit();
  144. updateBills.ledger_id = bills.ledger_id;
  145. return {
  146. ledger: { update: [updateBills] },
  147. pos: data,
  148. }
  149. } catch(err) {
  150. await transaction.rollback();
  151. throw err;
  152. }
  153. } else {
  154. await this.db.update(this.tableName, data);
  155. return {pos: data};
  156. }
  157. }
  158. async _updatePosDatas(tid, data) {
  159. if (data.length === 0) return;
  160. const op = await this.getDataById(data[0].id);
  161. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  162. const billsPos = await this.getAllDataByCondition({where: {lid: op.lid} });
  163. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  164. for (const d of data) {
  165. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  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 !== undefined) {
  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. }
  184. const updateBills = {id: bills.id};
  185. for (const bp of billsPos) {
  186. const newPos = data.find(function (x) { return x.id === bp.id });
  187. const calcData = newPos ? newPos : bp;
  188. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  189. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  190. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  191. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  192. }
  193. const info = this.ctx.tender.info;
  194. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  195. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  196. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  197. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  198. const transaction = await this.db.beginTransaction();
  199. try {
  200. for (const d of data) {
  201. transaction.update(this.tableName, d);
  202. }
  203. transaction.update(this.ctx.service.ledger.tableName, updateBills);
  204. await transaction.commit();
  205. updateBills.ledger_id = bills.ledger_id;
  206. return {
  207. ledger: { update: [updateBills] },
  208. pos: data,
  209. }
  210. } catch(err) {
  211. await transaction.rollback();
  212. throw err;
  213. }
  214. }
  215. async _deletePosData(tid, data) {
  216. if (!data || data.length === 0) {
  217. throw '提交数据错误';
  218. }
  219. const pos = await this.getPosData({tid: tid, id: data});
  220. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  221. const billsPos = await this.getAllDataByCondition({ where: {lid: bills.id} });
  222. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  223. for (const bp of billsPos) {
  224. if (data.indexOf(bp.id) >= 0) continue;
  225. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  226. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  227. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  228. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  229. }
  230. const info = this.ctx.tender.info;
  231. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  232. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  233. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  234. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  235. const transaction = await this.db.beginTransaction();
  236. try {
  237. await transaction.delete(this.tableName, {tid: tid, id: data});
  238. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  239. await transaction.commit();
  240. updateBills.ledger_id = bills.ledger_id;
  241. return { ledger: { update: [updateBills] }, pos: data };
  242. } catch(err) {
  243. await transaction.rollback();
  244. throw err;
  245. }
  246. }
  247. /**
  248. * 保存部位明细数据
  249. * @param data
  250. * @param {Number} tid - 标段id
  251. * @returns {Promise<{ledger: {}, pos: null}>}
  252. */
  253. async savePosData(data, tid) {
  254. switch (data.updateType) {
  255. case 'add':
  256. return await this._addPosData(tid, data.updateData);
  257. case 'update':
  258. if (data.updateData instanceof Array) {
  259. return await this._updatePosDatas(tid, data.updateData);
  260. } else {
  261. return await this._updatePosData(tid, data.updateData);
  262. }
  263. case 'delete':
  264. return await this._deletePosData(tid, data.updateData);
  265. }
  266. }
  267. /**
  268. * 复制粘贴 部位明细数据
  269. * @param {Array} data - 复制粘贴的数据
  270. * @param {Number} tid - 标段id
  271. * @returns {Promise<{ledger: {}, pos: null}>}
  272. */
  273. async pastePosData(data, tid) {
  274. if (!(data instanceof Array)) {
  275. throw '提交数据错误';
  276. }
  277. const transaction = await this.db.beginTransaction();
  278. const result = { ledger: {}, pos: null }, updateBills = [];
  279. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  280. let bills = null, precision = null;
  281. try {
  282. for (const d of data) {
  283. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  284. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  285. if (!bills || bills.id !== d.lid) {
  286. bills = await this.ctx.service.ledger.getDataById(d.lid);
  287. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  288. updateBills.push(bills);
  289. }
  290. if (d.sgfh_qty !== undefined) {
  291. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  292. } else if (op) {
  293. d.sgfh_qty = op.sgfh_qty;
  294. }
  295. if (d.sjcl_qty !== undefined) {
  296. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  297. } else if (op) {
  298. d.sjcl_qty = op.sjcl_qty;
  299. }
  300. if (d.qtcl_qty) {
  301. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  302. } else if (op) {
  303. d.qtcl_qty = op.qtcl_qty;
  304. }
  305. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  306. }
  307. if (d.id) {
  308. await transaction.update(this.tableName, d);
  309. } else {
  310. this._insertPosData(transaction, d, tid);
  311. }
  312. }
  313. for (const ub of updateBills) {
  314. await this.ctx.service.ledger.calcNode(ub, transaction);
  315. }
  316. await transaction.commit();
  317. } catch (err) {
  318. await transaction.rollback();
  319. throw err;
  320. }
  321. result.pos = data;
  322. if (updateBills.length > 0) {
  323. result.ledger.update = await this.ctx.service.ledger.getDataByIds(this._.map(updateBills, 'id'));
  324. }
  325. return result;
  326. }
  327. /**
  328. * 删除清单下部位明细数据(删除清单时调用)
  329. *
  330. * @param transaction - 事务
  331. * @param tid - 标段id
  332. * @param lid - 清单id
  333. * @returns {Promise<void>}
  334. */
  335. async deletePosData(transaction, tid, lid) {
  336. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  337. }
  338. /**
  339. * 复制整块 拷贝部位明细数据
  340. * @param {Number} orgLid - 拷贝的部位明细所属台账id
  341. * @param {Number} newLid - 新的台账id
  342. * @param transaction - 复制整块事务
  343. * @returns {Promise<void>}
  344. */
  345. async copyBillsPosData(orgLid, newLid, transaction) {
  346. const posData = await this.getAllDataByCondition({ where: { lid: orgLid } });
  347. if (posData.length > 0) {
  348. for (const pd of posData) {
  349. pd.id = this.uuid.v4();
  350. pd.lid = newLid;
  351. pd.tid = this.ctx.tender.id;
  352. pd.in_time = new Date();
  353. }
  354. await transaction.insert(this.tableName, posData);
  355. }
  356. }
  357. /**
  358. * 批量插入部位数据 - 仅供批量插入清单部位调用
  359. * @param transaction - 所属事务
  360. * @param {Number} tid - 标段id
  361. * @param {Number} lid - 台账id
  362. * @param {Array} data - 新增数据
  363. * @returns {Promise<void>}
  364. */
  365. async insertLedgerPosData(transaction, tid, bills, data) {
  366. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  367. const insertDatas = [];
  368. for (const d of data) {
  369. const inD = {
  370. id: this.uuid.v4(), tid: tid, lid: bills.id,
  371. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  372. in_time: new Date(), porder: data.indexOf(d) + 1,
  373. name: d.name, drawing_code: d.drawing_code,
  374. };
  375. if (d.quantity) {
  376. inD.sgfh_qty = this.round(d.quantity, precision.value);
  377. inD.quantity = inD.sgfh_qty;
  378. }
  379. insertDatas.push(inD);
  380. }
  381. await transaction.insert(this.tableName, insertDatas);
  382. }
  383. }
  384. return Pos;
  385. };