revise_pos.js 15 KB

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