tender_info.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/30
  7. * @version
  8. */
  9. const infoConst = require('../const/tender_info');
  10. const parseInfo = infoConst.parseInfo;
  11. const arrayInfo = infoConst.arrayInfo;
  12. const defaultInfo = infoConst.defaultInfo;
  13. const advanceConst = require('../const/audit').advance;
  14. module.exports = app => {
  15. class TenderInfo extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'tender_info';
  25. }
  26. /**
  27. * 新增 标段相关信息
  28. *
  29. * @param tenderId - 标段Id
  30. * @param projectId - 项目Id
  31. * @param transaction - 事务
  32. * @return {Promise<void>}
  33. */
  34. async addTenderInfo(tenderId, projectId, transaction) {
  35. const info = JSON.parse(JSON.stringify(defaultInfo));
  36. info.tid = tenderId;
  37. info.pid = projectId;
  38. for (const pi of parseInfo) {
  39. info[pi] = JSON.stringify(info[pi]);
  40. }
  41. for (const pi of arrayInfo) {
  42. info[pi] = JSON.stringify(info[pi]);
  43. }
  44. if (transaction) {
  45. await transaction.insert(this.tableName, info);
  46. } else {
  47. await this.db.insert(this.tableName, info);
  48. }
  49. return info;
  50. }
  51. /**
  52. * 获取标段相关信息
  53. * @param tenderId
  54. * @return {Promise<void>}
  55. */
  56. async getTenderInfo(tenderId) {
  57. let info = await this.getDataByCondition({ tid: tenderId });
  58. // 兼容不存在info的情况
  59. if (!info) {
  60. info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
  61. }
  62. for (const pi of parseInfo) {
  63. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  64. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  65. }
  66. for (const ai of arrayInfo) {
  67. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  68. }
  69. if (info.decimal) {
  70. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  71. }
  72. return info;
  73. }
  74. /**
  75. * 获取标段相关信息(报表用)
  76. * @param tenderId
  77. * @return {Promise<void>}
  78. */
  79. async getTenderInfoEx(tenderId) {
  80. const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
  81. // console.log('getTenderInfoEx: ' + tenderId);
  82. const sqlParam = [tenderId];
  83. const list = await this.db.query(sql, sqlParam);
  84. const info = list[0];
  85. const len = info.deal_info.length;
  86. info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
  87. // console.log(info);
  88. for (const pi of parseInfo) {
  89. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  90. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  91. }
  92. for (const ai of arrayInfo) {
  93. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  94. }
  95. if (info.decimal) {
  96. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  97. }
  98. return info;
  99. }
  100. /**
  101. * 保存标段相关信息
  102. *
  103. * @param data
  104. * @return {Promise<void>}
  105. */
  106. async saveTenderInfo(tenderId, data) {
  107. for (const di in data) {
  108. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  109. data[di] = JSON.stringify(data[di]);
  110. }
  111. }
  112. await this.db.update(this.tableName, data, { where: { tid: tenderId } });
  113. }
  114. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  115. const changePrecision = [];
  116. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  117. const defUnits = this._.map(newPrecision, 'units');
  118. const otherUnits = units.filter(function(u) { return defUnits.indexOf(u) === -1; });
  119. let changeUnits = [];
  120. for (const prop in newPrecision) {
  121. if (oldPrecision[prop]) {
  122. if (newPrecision[prop].value < oldPrecision[prop].value) {
  123. changePrecision.push(newPrecision[prop]);
  124. }
  125. } else {
  126. changePrecision.push(newPrecision[prop]);
  127. }
  128. }
  129. for (const cp of changePrecision) {
  130. if (cp.unit) {
  131. changeUnits.push(cp.unit);
  132. } else {
  133. changeUnits.push(otherUnits);
  134. }
  135. }
  136. changeUnits = this._.flatten(changeUnits);
  137. if (changeUnits.length > 0) {
  138. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  139. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  140. where: { tender_id: tenderId, unit: changeUnits, is_leaf: true },
  141. });
  142. const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
  143. for (const b of bills) {
  144. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  145. const bPos = this._.filter(pos, { lid: b.id });
  146. if (bPos.length > 0) {
  147. let sgfh_qty = 0,
  148. sjcl_qty = 0,
  149. qtcl_qty = 0,
  150. quantity = 0;
  151. for (const p of bPos) {
  152. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  153. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  154. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  155. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  156. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  157. quantity = this.ctx.helper.add(quantity, p.quantity);
  158. }
  159. b.sgfh_qty = sgfh_qty;
  160. b.sjcl_qty = sjcl_qty;
  161. b.qtcl_qty = qtcl_qty;
  162. b.quantity = quantity;
  163. } else {
  164. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  165. }
  166. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  167. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  168. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  169. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  170. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  171. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  172. }
  173. const transaction = await this.db.beginTransaction();
  174. try {
  175. await transaction.update(this.tableName,
  176. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  177. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  178. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  179. await transaction.commit();
  180. } catch (err) {
  181. await transaction.rollback();
  182. throw err;
  183. }
  184. } else {
  185. await this.db.update(this.tableName,
  186. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  187. }
  188. }
  189. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  190. const changeBills = [],
  191. changeAdvanceBills = [],
  192. calcUp = newDecimal.up < oldDecimal.up,
  193. calcTp = newDecimal.tp !== oldDecimal.tp,
  194. caclPayTp = (newDecimal.pay ? newDecimal.payTp : newDecimal.tp) < (oldDecimal.pay ? oldDecimal.payTp : oldDecimal.tp);
  195. if (calcUp || calcTp) {
  196. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  197. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  198. where: { tender_id: tenderId, is_leaf: true },
  199. });
  200. for (const b of bills) {
  201. const cb = { id: b.id };
  202. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  203. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  204. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  205. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  206. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  207. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  208. changeBills.push(cb);
  209. }
  210. }
  211. if (caclPayTp) {
  212. // 获取预付款需要修改的相关记录
  213. const ad_bills = await this.ctx.service.advance.getAllDataByCondition({
  214. columns: ['id', 'cur_amount', 'prev_amount', 'prev_total_amount'],
  215. where: { status: [advanceConst.status.uncheck, advanceConst.status.checkNo], tid: tenderId },
  216. });
  217. const decimal = newDecimal.pay ? newDecimal.payTp : newDecimal.tp;
  218. // 根据精度重新计算相关金额
  219. for (const ad of ad_bills) {
  220. const cb = { id: ad.id };
  221. cb.cur_amount = this.ctx.helper.round(ad.cur_amount, decimal);
  222. cb.prev_total_amount = this.ctx.helper.round(this.ctx.helper.add(ad.cur_amount, ad.prev_amount), decimal);
  223. changeAdvanceBills.push(cb);
  224. }
  225. }
  226. if (changeBills.length > 0) {
  227. const transaction = await this.db.beginTransaction();
  228. try {
  229. await transaction.update(this.tableName,
  230. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  231. await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  232. // await transaction.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  233. await transaction.commit();
  234. } catch (error) {
  235. await transaction.rollback();
  236. throw error;
  237. }
  238. } else {
  239. await this.db.update(this.tableName,
  240. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  241. }
  242. // 更新预付款记录
  243. if (changeAdvanceBills.length) {
  244. await this.db.updateRows(this.ctx.service.advance.tableName, changeAdvanceBills);
  245. }
  246. }
  247. }
  248. return TenderInfo;
  249. };