revise_pos.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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, check_calc: 1};
  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. if (data.length === 0) return;
  211. const op = await this.getDataById(data[0].id);
  212. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  213. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  214. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  215. let needUpdateBills;
  216. for (const d of data) {
  217. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined || d.ex_qty1 !== undefined) {
  218. if (d.sgfh_qty !== undefined) {
  219. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  220. } else if (op) {
  221. d.sgfh_qty = op.sgfh_qty;
  222. }
  223. if (d.sjcl_qty !== undefined) {
  224. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  225. } else if (op) {
  226. d.sjcl_qty = op.sjcl_qty;
  227. }
  228. if (d.qtcl_qty !== undefined) {
  229. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  230. } else if (op) {
  231. d.qtcl_qty = op.qtcl_qty;
  232. }
  233. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  234. if (d.ex_qty1 !== undefined) {
  235. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  236. } else if (op) {
  237. d.ex_qty1 = op.ex_qty1;
  238. }
  239. needUpdateBills = true;
  240. }
  241. }
  242. const updateBills = {id: bills.id, check_calc: 1};
  243. if (needUpdateBills) {
  244. for (const bp of billsPos) {
  245. const newPos = data.find(function (x) { return x.id === bp.id });
  246. updateBills.sgfh_qty = newPos && newPos.sgfh_qty !== undefined
  247. ? this.ctx.helper.add(updateBills.sgfh_qty, newPos.sgfh_qty)
  248. : this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  249. updateBills.sjcl_qty = newPos && newPos.sjcl_qty !== undefined
  250. ? this.ctx.helper.add(updateBills.sjcl_qty, newPos.sjcl_qty)
  251. : this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  252. updateBills.qtcl_qty = newPos && newPos.qtcl_qty !== undefined
  253. ? this.ctx.helper.add(updateBills.qtcl_qty, newPos.qtcl_qty)
  254. : this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  255. updateBills.quantity = newPos && newPos.quantity !== undefined
  256. ? this.ctx.helper.add(updateBills.quantity, newPos.quantity)
  257. : this.ctx.helper.add(updateBills.quantity, bp.quantity);
  258. updateBills.ex_qty1 = newPos && newPos.ex_qty1 !== undefined
  259. ? this.ctx.helper.add(updateBills.ex_qty1, newPos.ex_qty1)
  260. : this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  261. }
  262. const info = this.ctx.tender.info;
  263. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  264. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  265. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  266. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  267. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  268. }
  269. const transaction = await this.db.beginTransaction();
  270. try {
  271. for (const d of data) {
  272. await transaction.update(this.tableName, d);
  273. }
  274. if (needUpdateBills) await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  275. await transaction.commit();
  276. updateBills.ledger_id = bills.ledger_id;
  277. return {
  278. ledger: { update: needUpdateBills ? [updateBills] : [] },
  279. pos: data,
  280. }
  281. } catch(err) {
  282. await transaction.rollback();
  283. throw err;
  284. }
  285. }
  286. async deletePos(tid, data) {
  287. if (!data || data.length === 0) {
  288. throw '提交数据错误';
  289. }
  290. const pos = await this.getPosData({tid: tid, id: data});
  291. const bills = await this.ctx.service.reviseBills.getDataById(pos[0].lid);
  292. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  293. const updateBills = {id: bills.id, sgfh_qty: 0, sjcl_qty: 0, qtcl_qty: 0, quantity: 0, ex_qty1: 0, check_calc: 1};
  294. for (const bp of billsPos) {
  295. if (data.indexOf(bp.id) >= 0) continue;
  296. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  297. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  298. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  299. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  300. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  301. }
  302. const info = this.ctx.tender.info;
  303. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  304. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  305. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  306. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  307. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  308. const transaction = await this.db.beginTransaction();
  309. try {
  310. await transaction.delete(this.tableName, {tid: tid, id: data});
  311. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  312. await transaction.commit();
  313. updateBills.ledger_id = bills.ledger_id;
  314. return {
  315. ledger: { update: [updateBills] },
  316. pos: data
  317. };
  318. } catch(err) {
  319. await transaction.rollback();
  320. throw err;
  321. }
  322. }
  323. /**
  324. * 复制粘贴 部位明细数据
  325. * @param {Array} data - 复制粘贴的数据
  326. * @param {Number} tid - 标段id
  327. * @returns {Promise<{ledger: {}, pos: null}>}
  328. */
  329. async pastePosData(tid, rid, data) {
  330. if (!(data instanceof Array)) throw '提交数据错误';
  331. const transaction = await this.db.beginTransaction();
  332. const result = { ledger: {}, pos: null };
  333. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  334. const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  335. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  336. const updateBills = {id: bills.id, check_calc: 1};
  337. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  338. let needUpdateBills;
  339. for (const bp of billsPos) {
  340. const d = data.find(function (x) {
  341. return bp.id ? x.id === bp.id : false;
  342. });
  343. if (d) continue;
  344. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  345. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  346. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  347. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  348. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, bp.ex_qty1);
  349. }
  350. try {
  351. for (const d of data) {
  352. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  353. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined || d.ex_qty1 !== undefined) {
  354. if (d.sgfh_qty !== undefined) {
  355. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  356. } else if (op) {
  357. d.sgfh_qty = op.sgfh_qty;
  358. }
  359. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  360. if (d.sjcl_qty !== undefined) {
  361. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  362. } else if (op) {
  363. d.sjcl_qty = op.sjcl_qty;
  364. }
  365. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  366. if (d.qtcl_qty) {
  367. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  368. } else if (op) {
  369. d.qtcl_qty = op.qtcl_qty;
  370. }
  371. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  372. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  373. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  374. if (d.ex_qty1 !== undefined) {
  375. d.ex_qty1 = this.round(d.ex_qty1, precision.value);
  376. } else if (op) {
  377. d.ex_qty1 = op.ex_qty1;
  378. }
  379. updateBills.ex_qty1 = this.ctx.helper.add(updateBills.ex_qty1, d.ex_qty1);
  380. needUpdateBills = true;
  381. }
  382. if (d.id) {
  383. await transaction.update(this.tableName, d);
  384. } else {
  385. await this._insertPosData(transaction, d, tid, rid);
  386. }
  387. }
  388. const info = this.ctx.tender.info;
  389. if (needUpdateBills) {
  390. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  391. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  392. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  393. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  394. updateBills.ex_tp1 = this.ctx.helper.mul(updateBills.ex_qty1, bills.unit_price, info.decimal.tp);
  395. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  396. updateBills.ledger_id = bills.ledger_id;
  397. }
  398. await transaction.commit();
  399. } catch (err) {
  400. await transaction.rollback();
  401. throw err;
  402. }
  403. result.pos = data;
  404. result.ledger.update = needUpdateBills ? [updateBills] : [];
  405. return result;
  406. }
  407. }
  408. return RevisePos;
  409. };