revise_pos.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 addPos(tid, rid, data) {
  90. const transaction = await this.db.beginTransaction();
  91. try {
  92. if (data instanceof Array) {
  93. for (const d of data) {
  94. this._insertPosData(transaction, d, tid, rid);
  95. }
  96. } else {
  97. this._insertPosData(transaction, data, tid, rid);
  98. }
  99. await transaction.commit();
  100. } catch (err) {
  101. await transaction.rollback();
  102. throw err;
  103. }
  104. return {pos: data};
  105. }
  106. async updatePos(tid, data) {
  107. const billsIds = [];
  108. const datas = data instanceof Array ? data : [data];
  109. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  110. const transaction = await this.db.beginTransaction();
  111. try {
  112. for (const d of datas) {
  113. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  114. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  115. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  116. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  117. if (d.sgfh_qty !== undefined) {
  118. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  119. } else if (op) {
  120. d.sgfh_qty = op.sgfh_qty;
  121. }
  122. if (d.sjcl_qty !== undefined) {
  123. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  124. } else if (op) {
  125. d.sjcl_qty = op.sjcl_qty;
  126. }
  127. if (d.qtcl_qty !== undefined) {
  128. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  129. } else if (op) {
  130. d.qtcl_qty = op.qtcl_qty;
  131. }
  132. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  133. }
  134. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  135. if (d.quantity !== undefined && op && (billsIds.indexOf(op.lid) === -1)) {
  136. billsIds.push(op.lid);
  137. }
  138. }
  139. for (const lid of billsIds) {
  140. await this.ctx.service.reviseBills.calc(tid, lid, transaction);
  141. }
  142. await transaction.commit();
  143. } catch (err) {
  144. await transaction.rollback();
  145. throw err;
  146. }
  147. const bills = await this.ctx.service.reviseBills.getDataById(billsIds);
  148. return {pos: data, ledger: {update: bills}};
  149. }
  150. async deletePos(tid, data) {
  151. if (!data || data.length === 0) {
  152. throw '提交数据错误';
  153. }
  154. const pos = await this.getPosData({tid: tid, id: data});
  155. const ledgerIds = this._.map(pos, 'lid');
  156. const transaction = await this.db.beginTransaction();
  157. try {
  158. await transaction.delete(this.tableName, {tid: tid, id: data});
  159. for (const lid of ledgerIds) {
  160. await this.ctx.service.reviseBills.calc(tid, lid, transaction);
  161. }
  162. await transaction.commit();
  163. } catch (err) {
  164. await transaction.rollback();
  165. throw err;
  166. }
  167. const bills = await this.ctx.service.reviseBills.getDataById(ledgerIds);
  168. return {ledger: {update: bills}, pos: data};
  169. }
  170. /**
  171. * 保存部位明细数据
  172. * @param data
  173. * @param {Number} tid - 标段id
  174. * @returns {Promise<{ledger: {}, pos: null}>}
  175. */
  176. async savePosData(tid, rid, data) {
  177. const transaction = await this.db.beginTransaction();
  178. try {
  179. if (data.posPostType === 'add') {
  180. if (data.updateData instanceof Array) {
  181. for (const d of data.postData) {
  182. this._insertPosData(transaction, d, tid, rid);
  183. }
  184. } else {
  185. this._insertPosData(transaction, data.postData, tid, rid);
  186. }
  187. } else if (data.posPostType === 'update') {
  188. const datas = data.postData instanceof Array ? data.postData : [data.postData];
  189. result.ledger.update = [];
  190. const orgPos = await this.getPosData({tid: tid, id: this._.map(datas, 'id')});
  191. for (const d of datas) {
  192. const op = this._.find(orgPos, function (p) { return p.id = d.id; });
  193. if (d.sgfh_qty !== undefined || d.qtcl_qty !== undefined || d.sjcl_qty !== undefined) {
  194. const bills = await this.ctx.service.reviseBills.getDataById(op.lid);
  195. const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  196. if (d.sgfh_qty !== undefined) {
  197. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  198. } else if (op) {
  199. d.sgfh_qty = op.sgfh_qty;
  200. }
  201. if (d.sjcl_qty !== undefined) {
  202. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  203. } else if (op) {
  204. d.sjcl_qty = op.sjcl_qty;
  205. }
  206. if (d.qtcl_qty !== undefined) {
  207. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  208. } else if (op) {
  209. d.qtcl_qty = op.qtcl_qty;
  210. }
  211. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  212. }
  213. await transaction.update(this.tableName, d, {tid: tid, id: d.id});
  214. if (d.quantity !== undefined && op && (result.ledger.update.indexOf(op.lid) === -1)) {
  215. result.ledger.update.push(op.lid);
  216. }
  217. }
  218. for (const lid of result.ledger.update) {
  219. await this.ctx.service.reviseBills.calc(tid, lid, transaction);
  220. }
  221. } else if (data.posPostType === 'delete') {
  222. if (!data.postData || data.postData.length === 0) {
  223. throw '提交数据错误';
  224. }
  225. const pos = await this.getPosData({tid: tid, id: data.postData});
  226. const ledgerIds = this._.map(pos, 'lid');
  227. await transaction.delete(this.tableName, {tid: tid, id: data.postData});
  228. for (const lid of ledgerIds) {
  229. await this.ctx.service.reviseBills.calc(tid, lid, transaction);
  230. }
  231. result.ledger.update = ledgerIds;
  232. } else {
  233. throw '提交数据错误';
  234. }
  235. await transaction.commit();
  236. } catch (err) {
  237. await transaction.rollback();
  238. throw err;
  239. }
  240. result.pos = data.postData;
  241. result.ledger.update = await this.ctx.service.reviseBills.getDataById(result.ledger.update);
  242. return result;
  243. }
  244. /**
  245. * 复制粘贴 部位明细数据
  246. * @param {Array} data - 复制粘贴的数据
  247. * @param {Number} tid - 标段id
  248. * @returns {Promise<{ledger: {}, pos: null}>}
  249. */
  250. async pastePosData(tid, rid, data) {
  251. if (!(data instanceof Array)) throw '提交数据错误';
  252. const transaction = await this.db.beginTransaction();
  253. const result = { ledger: {}, pos: null }, updateLid = [];
  254. const orgPos = await this.getPosData({tid: tid, id: this._.map(data, 'id')});
  255. let bills = null, precision = null;
  256. try {
  257. for (const d of data) {
  258. const op = d.id ? this._.find(orgPos, {id: d.id}) : null;
  259. if (d.sgfh_qty || d.sjcl_qty || d.qtcl_qty) {
  260. if (!bills || bills.id !== d.lid) {
  261. bills = await this.ctx.service.reviseBills.getDataById(d.lid);
  262. precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
  263. updateLid.push(d.lid);
  264. }
  265. if (d.sgfh_qty !== undefined) {
  266. d.sgfh_qty = this.round(d.sgfh_qty, precision.value);
  267. } else if (op) {
  268. d.sgfh_qty = op.sgfh_qty;
  269. }
  270. if (d.sjcl_qty !== undefined) {
  271. d.sjcl_qty = this.round(d.sjcl_qty, precision.value);
  272. } else if (op) {
  273. d.sjcl_qty = op.sjcl_qty;
  274. }
  275. if (d.qtcl_qty) {
  276. d.qtcl_qty = this.round(d.qtcl_qty, precision.value);
  277. } else if (op) {
  278. d.qtcl_qty = op.qtcl_qty;
  279. }
  280. d.quantity = this.ctx.helper.sum([d.sgfh_qty, d.qtcl_qty, d.sjcl_qty]);
  281. }
  282. if (d.id) {
  283. await transaction.update(this.tableName, d);
  284. } else {
  285. this._insertPosData(transaction, d, tid, rid);
  286. }
  287. }
  288. for (const lid of updateLid) {
  289. await this.ctx.service.reviseBills.calc(tid, lid, transaction);
  290. }
  291. await transaction.commit();
  292. } catch (err) {
  293. await transaction.rollback();
  294. throw err;
  295. }
  296. result.pos = data;
  297. if (updateLid.length > 0) {
  298. result.ledger.update = await this.ctx.service.reviseBills.getDataById(updateLid);
  299. }
  300. return result;
  301. }
  302. }
  303. return RevisePos;
  304. };