stage.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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.add(s.contract_tp, s.qc_tp);
  78. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  79. s.end_tp = this.ctx.helper.add(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.add(stage.contract_tp, stage.qc_tp);
  97. stage.end_tp = this.ctx.helper.add(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.add(preStage.pre_contract_tp, preStage.contract_tp);
  132. newStage.pre_qc_tp = this.ctx.helper.add(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. if (preStage) {
  145. const auditResult = await this.ctx.service.stageAudit.copyPreStageAuditors(transaction, preStage, newStage);
  146. if (!auditResult) {
  147. throw '复制上一期审批流程失败';
  148. }
  149. }
  150. // 新增期合同支付数据
  151. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  152. if (!dealResult) {
  153. throw '新增期合同支付数据失败';
  154. }
  155. await transaction.commit();
  156. return newStage;
  157. } catch (err) {
  158. await transaction.rollback();
  159. throw err;
  160. }
  161. }
  162. /**
  163. * 编辑计量期
  164. *
  165. * @param {Number} tenderId - 标段Id
  166. * @param {Number} order - 第N期
  167. * @param {String} date - 计量年月
  168. * @param {String} period - 开始-截止时间
  169. * @returns {Promise<void>}
  170. */
  171. async saveStage(tenderId, order, date, period) {
  172. await this.db.update(this.tableName, {
  173. s_time: date,
  174. period: period,
  175. }, { where: { tid: tenderId, order: order } });
  176. }
  177. /**
  178. * 设置 中间计量 生成规则,并生成数据
  179. * @param {Number} tenderId - 标段id
  180. * @param {Number} order - 期序号
  181. * @param {Number} data - 中间计量生成规则
  182. * @returns {Promise<void>}
  183. */
  184. async buildDetailData(tenderId, order, data) {
  185. const conn = await this.db.beginTransaction();
  186. try {
  187. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre }, { where: { tid: tenderId, order: order } });
  188. // to do 生成中间计量数据
  189. await conn.commit();
  190. } catch (err) {
  191. await conn.rollback();
  192. throw err;
  193. }
  194. }
  195. /**
  196. * 获取 当期的 计算基数
  197. * @returns {Promise<any>}
  198. */
  199. async getStagePayCalcBase() {
  200. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  201. const param = this.ctx.tender.info.deal_param;
  202. for (const cb of calcBase) {
  203. switch (cb.code) {
  204. case 'htj':
  205. cb.value = param.contractPrice;
  206. break;
  207. case 'zlje':
  208. cb.value = param.zanLiePrice;
  209. break;
  210. case 'htjszl':
  211. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  212. break;
  213. case 'kgyfk':
  214. cb.value = param.startAdvance;
  215. break;
  216. case 'clyfk':
  217. cb.value = param.materialAdvance;
  218. break;
  219. case 'bqwc':
  220. const sum = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  221. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  222. break;
  223. case 'ybbqwc':
  224. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(this.ctx.stage, '^1[0-9]{2}-');
  225. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  226. break;
  227. default:
  228. cb.value = 0;
  229. }
  230. }
  231. return calcBase;
  232. }
  233. /**
  234. * 更新 check_detail 标识
  235. * @param {Integer}sid - 期id
  236. * @param {Boolean}check - 标记
  237. * @returns {Promise<void>}
  238. */
  239. async updateCheckDetailFlag(sid, check) {
  240. const result = await this.db.update(this.tableName, {id: sid, check_detail: check});
  241. return result.affectedRows === 1;
  242. }
  243. }
  244. return Stage;
  245. };