revise_pos.js 15 KB

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