pos.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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.depart = 20;
  20. this.tableName = 'pos';
  21. }
  22. async getPosData(condition) {
  23. if (!condition.tid) throw '查询计量单元缺少必要信息';
  24. return await this.db.select(this.departTableName(condition.tid), {
  25. where: condition,
  26. columns: ['id', 'tid', 'lid', 'name', 'quantity', 'position', 'drawing_code', 'sgfh_qty', 'sjcl_qty',
  27. 'qtcl_qty', 'in_time', 'porder', 'add_stage', 'sgfh_expr', 'sjcl_expr', 'qtcl_expr', 'real_qty',
  28. 'dagl_status', 'dagl_url', 'gxby_status', 'gxby_url', 'gxby_limit', 'dagl_limit',
  29. 'ex_memo1', 'ex_memo2', 'ex_memo3'],
  30. order: [['porder', 'ASC']],
  31. });
  32. }
  33. async getPosDataWithAddStageOrder(condition) {
  34. if (!condition.tid) throw '查询计量单元缺少必要信息';
  35. const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.position, p.drawing_code,' +
  36. ' 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,' +
  37. ' p.sgfh_expr, p.sjcl_expr, p.qtcl_expr, p.real_qty, p.gxby_status, p.gxby_url, p.dagl_status, p.dagl_url,' +
  38. ' p.gxby_limit, p.dagl_limit, p.ex_memo1, p.ex_memo2, p.ex_memo3' +
  39. ' FROM ' + this.departTableName(condition.tid) + ' p ' +
  40. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
  41. ' ON p.add_stage = s.id'
  42. + this.ctx.helper.whereSql(condition, 'p');
  43. return await this.db.query(sql);
  44. }
  45. async getPosDataByIds(tid, ids) {
  46. if (ids instanceof Array && ids.length > 0) {
  47. const sql = 'SELECT id, tid, lid, name, quantity, position, drawing_code,' +
  48. ' sgfh_qty, sjcl_qty, qtcl_qty, add_stage, add_times, add_user, sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  49. ' dagl_status, dagl_url, gxby_status, gxby_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3' +
  50. ' FROM ' + this.departTableName(tid) +
  51. ' WHERE id in (' + this.ctx.helper.getInArrStrSqlFilter(ids) + ')';
  52. return await this.db.query(sql, []);
  53. } else {
  54. return [];
  55. }
  56. // this.initSqlBuilder();
  57. // this.sqlBuilder.setAndWhere('id', {
  58. // operate: 'in',
  59. // value: ids
  60. // });
  61. // this.sqlBuilder.columns = ['id', 'tid', 'lid', 'name', 'quantity', 'drawing_code', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty'];
  62. // const [sql, sqlParam] = this.sqlBuilder.build(this.tableName)
  63. // return await this.db.query(sql, sqlParam);
  64. }
  65. async getPosDataByUnits(tenderId, units) {
  66. const sql = 'SELECT p.id, p.lid, p.sgfh_qty, p.sjcl_qty, p.qtcl_qty' +
  67. ' FROM ' + this.departTableName(tenderId) + ' p' +
  68. ' LEFT JOIN ' + this.ctx.service.ledger.tableName + ' b' +
  69. ' ON p.lid = b.id ' +
  70. ' WHERE p.tid = ? and b.unit IN (?)';
  71. const sqlParam = [tenderId, units];
  72. return await this.db.query(sql, sqlParam);
  73. }
  74. async _insertPosData(transaction, data, tid) {
  75. data.id = this.uuid.v4();
  76. data.tid = tid;
  77. // todo 新增期
  78. data.add_stage = 0;
  79. data.add_times = 0;
  80. data.in_time = new Date();
  81. data.add_user = this.ctx.session.sessionUser.accountId;
  82. if (data.quantity) {
  83. const bills = await this.ctx.service.ledger.getDataById(data.lid);
  84. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  85. data.quantity = this.round(data.quantity, precision.value);
  86. }
  87. if (transaction) {
  88. const addRst = await transaction.insert(this.tableName, data);
  89. } else {
  90. const addRst = await this.db.insert(this.tableName, data);
  91. }
  92. }
  93. async _completeInsertPosData(tid, data) {
  94. data.id = this.uuid.v4();
  95. data.tid = tid;
  96. // todo 新增期
  97. data.add_stage = 0;
  98. data.add_times = 0;
  99. data.in_time = new Date();
  100. data.add_user = this.ctx.session.sessionUser.accountId;
  101. }
  102. async _getUpdateBills(data) {
  103. const datas = data instanceof Array ? data : [data];
  104. const bills = await this.ctx.service.ledger.getDataById(datas[0].lid);
  105. const billsPos = await this.getAllDataByCondition({where: {tid: bills.tender_id, lid: bills.id} });
  106. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  107. const updateBills = {id: bills.id};
  108. for (const bp of billsPos) {
  109. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  110. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  111. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  112. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  113. }
  114. for (const d of datas) {
  115. if (d.sgfh_qty) {
  116. d.sgfh_qty = this.ctx.helper.round(d.sgfh_qty, precision.value);
  117. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  118. }
  119. if (d.sjcl_qty) {
  120. d.sjcl_qty = this.ctx.helper.round(d.sjcl_qty, precision.value);
  121. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  122. }
  123. if (d.qtcl_qty) {
  124. d.qtcl_qty = this.ctx.helper.round(d.qtcl_qty, precision.value);
  125. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  126. }
  127. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  128. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  129. }
  130. const info = this.ctx.tender.info;
  131. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  132. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  133. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  134. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  135. return updateBills;
  136. }
  137. async _addPosData(tid, data) {
  138. let updateBills = null;
  139. if (data instanceof Array) {
  140. for (const d of data) {
  141. this._completeInsertPosData(tid, d);
  142. }
  143. if (data[0].sgfh_qty !== undefined || data[0].sjcl_qty !== undefined || data[0].qtcl_qty !== undefined) {
  144. updateBills = await this._getUpdateBills(data);
  145. }
  146. } else {
  147. this._completeInsertPosData(tid, data);
  148. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  149. updateBills = await this._getUpdateBills(data);
  150. }
  151. }
  152. if (updateBills) {
  153. const transaction = await this.db.beginTransaction();
  154. try {
  155. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  156. updateBills.ledger_id = bills.ledger_id;
  157. await transaction.commit();
  158. } catch (err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. return {
  163. ledger: { update: [updateBills] },
  164. pos: data,
  165. }
  166. } else {
  167. await this.db.insert(this.tableName, data);
  168. return { pos: data }
  169. }
  170. }
  171. async _updatePosData(tid, data) {
  172. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  173. const op = await this.getDataById(data.id);
  174. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  175. if (!bills) throw '数据错误';
  176. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  177. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  178. if (data.sgfh_qty !== undefined) {
  179. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  180. } else if (op) {
  181. data.sgfh_qty = op.sgfh_qty;
  182. }
  183. if (data.sjcl_qty !== undefined) {
  184. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  185. } else if (op) {
  186. data.sjcl_qty = op.sjcl_qty;
  187. }
  188. if (data.qtcl_qty !== undefined) {
  189. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  190. } else if (op) {
  191. data.qtcl_qty = op.qtcl_qty;
  192. }
  193. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  194. const updateBills = {id: bills.id};
  195. for (const bp of billsPos) {
  196. const calcData = bp.id === data.id ? data : bp;
  197. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  198. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  199. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  200. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  201. }
  202. const info = this.ctx.tender.info;
  203. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  204. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  205. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  206. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  207. const transaction = await this.db.beginTransaction();
  208. try {
  209. await transaction.update(this.tableName, data);
  210. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  211. await transaction.commit();
  212. updateBills.ledger_id = bills.ledger_id;
  213. return {
  214. ledger: { update: [updateBills] },
  215. pos: data,
  216. }
  217. } catch(err) {
  218. await transaction.rollback();
  219. throw err;
  220. }
  221. } else {
  222. await this.db.update(this.tableName, data);
  223. return {pos: data};
  224. }
  225. }
  226. async _updatePosDatas(tid, data) {
  227. if (data.length === 0) return;
  228. const op = await this.getDataById(data[0].id);
  229. const bills = await this.ctx.service.ledger.getDataById(op.lid);
  230. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  231. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  232. let needUpdateBills;
  233. for (const d of data) {
  234. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  235. if (d.sgfh_qty !== undefined) {
  236. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  237. } else if (op) {
  238. d.sgfh_qty = op.sgfh_qty;
  239. }
  240. if (d.sjcl_qty !== undefined) {
  241. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  242. } else if (op) {
  243. d.sjcl_qty = op.sjcl_qty;
  244. }
  245. if (d.qtcl_qty !== undefined) {
  246. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  247. } else if (op) {
  248. d.qtcl_qty = op.qtcl_qty;
  249. }
  250. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  251. needUpdateBills = true;
  252. }
  253. }
  254. const updateBills = {id: bills.id};
  255. if (needUpdateBills) {
  256. for (const bp of billsPos) {
  257. const newPos = data.find(function (x) { return x.id === bp.id });
  258. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  259. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  260. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  261. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  262. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  263. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  264. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  265. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  266. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  267. updateBills.quantity = newPos && newPos.quantity !== undefined
  268. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  269. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  270. }
  271. const info = this.ctx.tender.info;
  272. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  273. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  274. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  275. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  276. }
  277. const transaction = await this.db.beginTransaction();
  278. try {
  279. for (const d of data) {
  280. await transaction.update(this.tableName, d);
  281. }
  282. if (needUpdateBills) await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  283. await transaction.commit();
  284. updateBills.ledger_id = bills.ledger_id;
  285. return {
  286. ledger: { update: [updateBills] },
  287. pos: data,
  288. }
  289. } catch(err) {
  290. await transaction.rollback();
  291. throw err;
  292. }
  293. }
  294. async _deletePosData(tid, data) {
  295. if (!data || data.length === 0) throw '提交数据错误';
  296. const pos = await this.getPosData({tid: tid, id: data});
  297. if (!pos || pos.length === 0) throw '删除的计量单元不存在';
  298. const bills = await this.ctx.service.ledger.getDataById(pos[0].lid);
  299. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  300. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  301. for (const bp of billsPos) {
  302. if (data.indexOf(bp.id) >= 0) continue;
  303. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  304. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  305. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  306. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  307. }
  308. const info = this.ctx.tender.info;
  309. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  310. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  311. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  312. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  313. const transaction = await this.db.beginTransaction();
  314. try {
  315. await transaction.delete(this.tableName, {tid: tid, id: data});
  316. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  317. await transaction.commit();
  318. updateBills.ledger_id = bills.ledger_id;
  319. return { ledger: { update: [updateBills] }, pos: data };
  320. } catch(err) {
  321. await transaction.rollback();
  322. throw err;
  323. }
  324. }
  325. /**
  326. * 保存部位明细数据
  327. * @param data
  328. * @param {Number} tid - 标段id
  329. * @returns {Promise<{ledger: {}, pos: null}>}
  330. */
  331. async savePosData(data, tid) {
  332. switch (data.updateType) {
  333. case 'add':
  334. return await this._addPosData(tid, data.updateData);
  335. case 'update':
  336. if (data.updateData instanceof Array) {
  337. return await this._updatePosDatas(tid, data.updateData);
  338. } else {
  339. return await this._updatePosData(tid, data.updateData);
  340. }
  341. case 'delete':
  342. return await this._deletePosData(tid, data.updateData);
  343. }
  344. }
  345. /**
  346. * 复制粘贴 部位明细数据
  347. * @param {Array} data - 复制粘贴的数据
  348. * @param {Number} tid - 标段id
  349. * @returns {Promise<{ledger: {}, pos: null}>}
  350. */
  351. async pastePosData(data, tid) {
  352. if (!(data instanceof Array)) {
  353. throw '提交数据错误';
  354. }
  355. const transaction = await this.db.beginTransaction();
  356. const result = { ledger: {}, pos: null };
  357. const bills = await this.ctx.service.ledger.getDataById(data[0].lid);
  358. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  359. const updateBills = {id: bills.id};
  360. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  361. for (const bp of billsPos) {
  362. const d = data.find(function (x) {
  363. return bp.id ? x.id === bp.id : false;
  364. });
  365. if (d) continue;
  366. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  367. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  368. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  369. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  370. }
  371. try {
  372. for (const d of data) {
  373. const op = d.id ? this._.find(billsPos, {id: d.id}) : null;
  374. if (d.sgfh_qty !== undefined) {
  375. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  376. } else if (op) {
  377. d.sgfh_qty = op.sgfh_qty;
  378. }
  379. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  380. if (d.sjcl_qty !== undefined) {
  381. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  382. } else if (op) {
  383. d.sjcl_qty = op.sjcl_qty;
  384. }
  385. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  386. if (d.qtcl_qty) {
  387. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  388. } else if (op) {
  389. d.qtcl_qty = op.qtcl_qty;
  390. }
  391. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  392. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  393. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  394. if (d.id) {
  395. await transaction.update(this.tableName, d);
  396. } else {
  397. this._completeInsertPosData(tid, d);
  398. await transaction.insert(this.tableName, d);
  399. }
  400. }
  401. const info = this.ctx.tender.info;
  402. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  403. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  404. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  405. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  406. await transaction.update(this.ctx.service.ledger.tableName, updateBills);
  407. updateBills.ledger_id = bills.ledger_id;
  408. await transaction.commit();
  409. } catch (err) {
  410. await transaction.rollback();
  411. throw err;
  412. }
  413. result.pos = data;
  414. result.ledger.update = [updateBills];
  415. return result;
  416. }
  417. /**
  418. * 删除清单下部位明细数据(删除清单时调用)
  419. *
  420. * @param transaction - 事务
  421. * @param tid - 标段id
  422. * @param lid - 清单id
  423. * @returns {Promise<void>}
  424. */
  425. async deletePosData(transaction, tid, lid) {
  426. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  427. }
  428. }
  429. return Pos;
  430. };