revise_pos.js 21 KB

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