revise_pos.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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.tableName = 'revise_pos';
  20. }
  21. async getPosData(condition) {
  22. const sql = 'SELECT p.id, p.tid, p.lid, p.name, p.quantity, p.position, p.drawing_code, p.sgfh_qty, ' +
  23. ' p.sjcl_qty, p.qtcl_qty, p.porder, p.add_stage, p.add_times, p.add_user, s.order As add_stage_order,' +
  24. ' p.sgfh_expr, p.sjcl_expr, p.qtcl_expr ' +
  25. ' FROM ' + this.tableName + ' p ' +
  26. ' LEFT JOIN ' + this.ctx.service.stage.tableName + ' s' +
  27. ' ON add_stage = s.id'
  28. + this.ctx.helper.whereSql(condition, 'p') +
  29. ' ORDER By p.porder ASC';
  30. return await this.db.query(sql);
  31. }
  32. /**
  33. * 获取 修订 清单数据
  34. * @param {Number}tid - 标段id
  35. * @param {uuid}rid - 修订id
  36. * @returns {Promise<void>}
  37. */
  38. async getData(tid) {
  39. return await this.db.select(this.tableName, {
  40. where: {tid: tid}
  41. });
  42. }
  43. async getDataByLid(tid, lid) {
  44. return await this.db.select(this.tableName, {
  45. where: {tid: tid, lid: lid}
  46. });
  47. }
  48. async insertLedgerPosData(transaction, tid, rid, bills, data) {
  49. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  50. const insertDatas = [];
  51. for (const d of data) {
  52. const inD = {
  53. id: this.uuid.v4(), tid: tid, lid: bills.id, crid: rid,
  54. add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
  55. name: d.name, drawing_code: d.drawing_code,
  56. };
  57. if (d.quantity) {
  58. inD.sgfh_qty = this.round(d.quantity, precision.value);
  59. inD.quantity = inD.sgfh_qty;
  60. }
  61. insertDatas.push(inD);
  62. }
  63. await transaction.insert(this.tableName, insertDatas);
  64. }
  65. /**
  66. * 删除清单下部位明细数据(删除清单时调用)
  67. *
  68. * @param transaction - 事务
  69. * @param tid - 标段id
  70. * @param lid - 清单id
  71. * @returns {Promise<void>}
  72. */
  73. async deletePosData(transaction, tid, lid) {
  74. await transaction.delete(this.tableName, {tid: tid, lid: lid});
  75. }
  76. async _insertPosData(transaction, data, tid, rid) {
  77. data.id = this.uuid.v4();
  78. data.tid = tid;
  79. // todo 新增期
  80. data.add_stage = 0;
  81. data.add_times = 0;
  82. data.in_time = new Date();
  83. data.add_user = this.ctx.session.sessionUser.accountId;
  84. data.crid = rid;
  85. if (data.quantity) {
  86. const bills = await this.ctx.service.reviseBills.getDataById(data.lid);
  87. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  88. data.quantity = this.round(data.quantity, precision.value);
  89. }
  90. const addRst = await transaction.insert(this.tableName, data);
  91. }
  92. async _completeInsertPosData(tid, rid, data) {
  93. data.id = this.uuid.v4();
  94. data.tid = tid;
  95. // todo 新增期
  96. data.add_stage = 0;
  97. data.add_times = 0;
  98. data.in_time = new Date();
  99. data.add_user = this.ctx.session.sessionUser.accountId;
  100. data.crid = rid;
  101. }
  102. async addPos(tid, rid, data) {
  103. if (data instanceof Array) {
  104. for (const d of data) {
  105. this._completeInsertPosData(tid, rid, d);
  106. }
  107. } else {
  108. this._completeInsertPosData(tid, rid, data);
  109. }
  110. this.db.insert(this.tableName, data);
  111. return {pos: data};
  112. }
  113. async updatePos(tid, data) {
  114. if (data.sgfh_qty !== undefined || data.sjcl_qty !== undefined || data.qtcl_qty !== undefined) {
  115. const op = await this.getDataById(data.id);
  116. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  117. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  118. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  119. if (data.sgfh_qty !== undefined) {
  120. data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
  121. } else if (op) {
  122. data.sgfh_qty = op.sgfh_qty;
  123. }
  124. if (data.sjcl_qty !== undefined) {
  125. data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
  126. } else if (op) {
  127. data.sjcl_qty = op.sjcl_qty;
  128. }
  129. if (data.qtcl_qty !== undefined) {
  130. data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
  131. } else if (op) {
  132. data.qtcl_qty = op.qtcl_qty;
  133. }
  134. data.quantity = this.ctx.helper.sum([data.sgfh_qty, data.qtcl_qty, data.sjcl_qty]);
  135. const updateBills = {id: bills.id};
  136. for (const bp of billsPos) {
  137. const calcData = bp.id === data.id ? data : bp;
  138. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, calcData.sgfh_qty);
  139. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, calcData.sjcl_qty);
  140. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, calcData.qtcl_qty);
  141. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, calcData.quantity);
  142. }
  143. const info = this.ctx.tender.info;
  144. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  145. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  146. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  147. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  148. const transaction = await this.db.beginTransaction();
  149. try {
  150. transaction.update(this.tableName, data);
  151. transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  152. await transaction.commit();
  153. updateBills.ledger_id = bills.ledger_id;
  154. return {
  155. ledger: { update: [updateBills] },
  156. pos: data,
  157. }
  158. } catch(err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. } else {
  163. await this.db.update(this.tableName, data, {tid: tid, id: data.id});
  164. return {pos: data};
  165. }
  166. }
  167. async updatePosArr(tid, data) {
  168. if (data.length === 0) return;
  169. const op = await this.getDataById(data[0].id);
  170. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  171. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: op.lid} });
  172. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  173. for (const d of data) {
  174. if (d.sgfh_qty !== undefined || d.sjcl_qty !== undefined || d.qtcl_qty !== undefined) {
  175. if (d.sgfh_qty !== undefined) {
  176. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  177. } else if (op) {
  178. d.sgfh_qty = op.sgfh_qty;
  179. }
  180. if (d.sjcl_qty !== undefined) {
  181. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  182. } else if (op) {
  183. d.sjcl_qty = op.sjcl_qty;
  184. }
  185. if (d.qtcl_qty !== undefined) {
  186. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  187. } else if (op) {
  188. d.qtcl_qty = op.qtcl_qty;
  189. }
  190. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  191. }
  192. }
  193. const updateBills = {id: bills.id};
  194. for (const bp of billsPos) {
  195. const newPos = data.find(function (x) { return x.id === bp.id });
  196. const calcData = newPos ? newPos : 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. for (const d of data) {
  210. transaction.update(this.tableName, d);
  211. }
  212. transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  213. await transaction.commit();
  214. updateBills.ledger_id = bills.ledger_id;
  215. return {
  216. ledger: { update: [updateBills] },
  217. pos: data,
  218. }
  219. } catch(err) {
  220. await transaction.rollback();
  221. throw err;
  222. }
  223. }
  224. async deletePos(tid, data) {
  225. if (!data || data.length === 0) {
  226. throw '提交数据错误';
  227. }
  228. const pos = await this.getPosData({tid: tid, id: data});
  229. const bills = await this.ctx.service.reviseBills.getDataById(pos[0].lid);
  230. const billsPos = await this.getAllDataByCondition({ where: {tid: tid, lid: bills.id} });
  231. const updateBills = {id: bills.id, sgfh_qty: null, sjcl_qty: null, qtcl_qty: null, quantity: null};
  232. for (const bp of billsPos) {
  233. if (data.indexOf(bp.id) >= 0) continue;
  234. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  235. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  236. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  237. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  238. }
  239. const info = this.ctx.tender.info;
  240. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  241. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  242. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  243. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  244. const transaction = await this.db.beginTransaction();
  245. try {
  246. await transaction.delete(this.tableName, {tid: tid, id: data});
  247. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  248. await transaction.commit();
  249. updateBills.ledger_id = bills.ledger_id;
  250. return { ledger: { update: [updateBills] }, pos: data };
  251. } catch(err) {
  252. await transaction.rollback();
  253. throw err;
  254. }
  255. }
  256. /**
  257. * 复制粘贴 部位明细数据
  258. * @param {Array} data - 复制粘贴的数据
  259. * @param {Number} tid - 标段id
  260. * @returns {Promise<{ledger: {}, pos: null}>}
  261. */
  262. async pastePosData(tid, rid, data) {
  263. if (!(data instanceof Array)) throw '提交数据错误';
  264. const transaction = await this.db.beginTransaction();
  265. const result = { ledger: {}, pos: null };
  266. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  267. const bills = await this.ctx.service.reviseBills.getDataById(data[0].lid);
  268. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  269. const updateBills = {id: bills.id};
  270. const billsPos = await this.getAllDataByCondition({where: {tid: tid, lid: bills.id} });
  271. for (const bp of billsPos) {
  272. const d = data.find(function (x) {
  273. return bp.id ? x.id === bp.id : false;
  274. });
  275. if (d) continue;
  276. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, bp.sgfh_qty);
  277. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, bp.sjcl_qty);
  278. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, bp.qtcl_qty);
  279. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, bp.quantity);
  280. }
  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 (d.sgfh_qty !== undefined) {
  286. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  287. } else if (op) {
  288. d.sgfh_qty = op.sgfh_qty;
  289. }
  290. updateBills.sgfh_qty = this.ctx.helper.add(updateBills.sgfh_qty, d.sgfh_qty);
  291. if (d.sjcl_qty !== undefined) {
  292. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  293. } else if (op) {
  294. d.sjcl_qty = op.sjcl_qty;
  295. }
  296. updateBills.sjcl_qty = this.ctx.helper.add(updateBills.sjcl_qty, d.sjcl_qty);
  297. if (d.qtcl_qty) {
  298. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  299. } else if (op) {
  300. d.qtcl_qty = op.qtcl_qty;
  301. }
  302. updateBills.qtcl_qty = this.ctx.helper.add(updateBills.qtcl_qty, d.qtcl_qty);
  303. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  304. updateBills.quantity = this.ctx.helper.add(updateBills.quantity, d.quantity);
  305. }
  306. if (d.id) {
  307. await transaction.update(this.tableName, d);
  308. } else {
  309. this._insertPosData(transaction, d, tid, rid);
  310. }
  311. }
  312. const info = this.ctx.tender.info;
  313. updateBills.sgfh_tp = this.ctx.helper.mul(updateBills.sgfh_qty, bills.unit_price, info.decimal.tp);
  314. updateBills.sjcl_tp = this.ctx.helper.mul(updateBills.sjcl_qty, bills.unit_price, info.decimal.tp);
  315. updateBills.qtcl_tp = this.ctx.helper.mul(updateBills.qtcl_qty, bills.unit_price, info.decimal.tp);
  316. updateBills.total_price = this.ctx.helper.mul(updateBills.quantity, bills.unit_price, info.decimal.tp);
  317. await transaction.update(this.ctx.service.reviseBills.tableName, updateBills);
  318. updateBills.ledger_id = bills.ledger_id;
  319. await transaction.commit();
  320. } catch (err) {
  321. await transaction.rollback();
  322. throw err;
  323. }
  324. result.pos = data;
  325. result.ledger.update = [updateBills];
  326. return result;
  327. }
  328. }
  329. return RevisePos;
  330. };