revise_pos.js 19 KB

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