stage.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').stage;
  10. const payConst = require('../const/deal_pay.js');
  11. module.exports = app => {
  12. class Stage extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'stage';
  22. }
  23. /**
  24. * 获取 最新一期 期计量
  25. * @param tenderId
  26. * @param includeUnCheck
  27. * @returns {Promise<*>}
  28. */
  29. async getLastestStage(tenderId, includeUnCheck = false) {
  30. this.initSqlBuilder();
  31. this.sqlBuilder.setAndWhere('tid', {
  32. value: tenderId,
  33. operate: '=',
  34. });
  35. if (!includeUnCheck) {
  36. this.sqlBuilder.setAndWhere('status', {
  37. value: auditConst.status.uncheck,
  38. operate: '!=',
  39. });
  40. }
  41. this.sqlBuilder.orderBy = [['order', 'desc']];
  42. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  43. const stage = await this.db.queryOne(sql, sqlParam);
  44. return stage;
  45. }
  46. /**
  47. * 获取 最新一期 审批完成的 期计量
  48. * @param tenderId
  49. * @returns {Promise<*>}
  50. */
  51. async getLastestCompleteStage(tenderId) {
  52. this.initSqlBuilder();
  53. this.sqlBuilder.setAndWhere('tid', {
  54. value: tenderId,
  55. operate: '=',
  56. });
  57. this.sqlBuilder.setAndWhere('status', {
  58. value: auditConst.status.checked,
  59. operate: '=',
  60. });
  61. this.sqlBuilder.orderBy = [['order', 'desc']];
  62. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  63. const stage = await this.db.queryOne(sql, sqlParam);
  64. return stage;
  65. }
  66. /**
  67. * 获取标段下的全部计量期,按倒序
  68. * @param tenderId
  69. * @returns {Promise<void>}
  70. */
  71. async getValidStages(tenderId) {
  72. const stages = await this.db.select(this.tableName, {
  73. where: {tid: tenderId},
  74. orders: [['order', 'desc']],
  75. });
  76. for (const s of stages) {
  77. s.tp = this.ctx.helper.plus(s.contract_tp, s.qc_tp);
  78. s.pre_tp = this.ctx.helper.plus(s.pre_contract_tp, s.pre_qc_tp);
  79. s.end_tp = this.ctx.helper.plus(s.pre_tp, s.tp);
  80. }
  81. if (stages.length !== 0) {
  82. const lastStage = stages[stages.length - 1];
  83. if (lastStage.status === auditConst.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  84. stages.splice(stages.length - 1, 1);
  85. }
  86. }
  87. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  88. if (stages.length > 0 && stages[0].status !== auditConst.status.checked) {
  89. const stage = stages[0];
  90. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  91. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : stage.user_id === this.ctx.session.sessionUser.accountId;
  92. if (isActive) {
  93. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  94. stage.contract_tp = tpData.contract_tp;
  95. stage.qc_tp = tpData.qc_tp;
  96. stage.tp = this.ctx.helper.plus(stage.contract_tp, stage.qc_tp);
  97. stage.end_tp = this.ctx.helper.plus(stage.pre_tp, stage.tp);
  98. }
  99. }
  100. return stages;
  101. }
  102. /**
  103. *
  104. * @param tenderId - 标段id
  105. * @param date - 计量年月
  106. * @param period - 开始-截止日期
  107. * @returns {Promise<void>}
  108. */
  109. async addStage(tenderId, date, period) {
  110. const stages = await this.getAllDataByCondition({
  111. where: {tid: tenderId},
  112. order: ['order'],
  113. });
  114. const preStage = stages[stages.length - 1];
  115. if (stages.length > 0 && stages[stages.length - 1].status !== auditConst.status.checked) {
  116. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  117. };
  118. const order = stages.length + 1;
  119. const newStage = {
  120. sid: this.uuid.v4(),
  121. tid: tenderId,
  122. order: order,
  123. in_time: new Date(),
  124. s_time: date,
  125. period: period,
  126. times: 1,
  127. status: auditConst.status.uncheck,
  128. user_id: this.ctx.session.sessionUser.accountId,
  129. };
  130. if (preStage) {
  131. newStage.pre_contract_tp = this.ctx.helper.plus(preStage.pre_contract_tp, preStage.contract_tp);
  132. newStage.pre_qc_tp = this.ctx.helper.plus(preStage.pre_qc_tp, preStage.qc_tp);
  133. }
  134. const transaction = await this.db.beginTransaction();
  135. try {
  136. // 新增期记录
  137. const result = await transaction.insert(this.tableName, newStage);
  138. if (result.affectedRows === 1) {
  139. newStage.id = result.insertId;
  140. } else {
  141. throw '新增期数据失败';
  142. }
  143. // 新增期合同支付数据
  144. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  145. if (!dealResult) {
  146. throw '新增期合同支付数据失败';
  147. }
  148. await transaction.commit();
  149. return newStage;
  150. } catch (err) {
  151. await transaction.rollback();
  152. throw err;
  153. }
  154. }
  155. /**
  156. * 编辑计量期
  157. *
  158. * @param {Number} tenderId - 标段Id
  159. * @param {Number} order - 第N期
  160. * @param {String} date - 计量年月
  161. * @param {String} period - 开始-截止时间
  162. * @returns {Promise<void>}
  163. */
  164. async saveStage(tenderId, order, date, period) {
  165. await this.db.update(this.tableName, {
  166. s_time: date,
  167. period: period,
  168. }, { where: { tid: tenderId, order: order } });
  169. }
  170. /**
  171. * 设置 中间计量 生成规则,并生成数据
  172. * @param {Number} tenderId - 标段id
  173. * @param {Number} order - 期序号
  174. * @param {Number} data - 中间计量生成规则
  175. * @returns {Promise<void>}
  176. */
  177. async buildDetailData(tenderId, order, data) {
  178. const conn = await this.db.beginTransaction();
  179. try {
  180. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre }, { where: { tid: tenderId, order: order } });
  181. // to do 生成中间计量数据
  182. await conn.commit();
  183. } catch (err) {
  184. await conn.rollback();
  185. throw err;
  186. }
  187. }
  188. /**
  189. * 获取 当期的 计算基数
  190. * @returns {Promise<any>}
  191. */
  192. async getStagePayCalcBase() {
  193. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  194. const param = this.ctx.tender.info.deal_param;
  195. for (const cb of calcBase) {
  196. switch (cb.code) {
  197. case 'htj':
  198. cb.value = param.contractPrice;
  199. break;
  200. case 'zlje':
  201. cb.value = param.zanLiePrice;
  202. break;
  203. case 'htjszl':
  204. cb.value = this.ctx.helper.minus(param.contractPrice, param.zanLiePrice);
  205. break;
  206. case 'kgyfk':
  207. cb.value = param.startAdvance;
  208. break;
  209. case 'clyfk':
  210. cb.value = param.materialAdvance;
  211. break;
  212. case 'bqwc':
  213. const sum = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  214. cb.value = this.app._.add(sum.contract_tp, sum.qc_tp);
  215. break;
  216. case 'ybbqwc':
  217. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(this.ctx.stage, '^1[0-9]{2}-');
  218. cb.value = this.app._.add(sumGcl.contract_tp, sumGcl.qc_tp);
  219. break;
  220. default:
  221. cb.value = 0;
  222. }
  223. }
  224. return calcBase;
  225. }
  226. }
  227. return Stage;
  228. };