stage.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. * 获取标段下的期最大数量(报表选择用,只需要一些key信息,不需要计算详细数据)
  68. * @param tenderId
  69. * @returns {Promise<void>}
  70. */
  71. async getValidStageShort(tenderId) {
  72. const sql = 'select id, zh_stage.order from zh_stage where tid = ? order by zh_stage.order';
  73. const sqlParam = [tenderId];
  74. return await this.db.query(sql, sqlParam);
  75. }
  76. /**
  77. * 获取标段下的全部计量期,按倒序
  78. * @param tenderId
  79. * @returns {Promise<void>}
  80. */
  81. async getValidStages(tenderId) {
  82. const stages = await this.db.select(this.tableName, {
  83. where: {tid: tenderId},
  84. orders: [['order', 'desc']],
  85. });
  86. for (const s of stages) {
  87. s.tp = this.ctx.helper.add(s.contract_tp, s.qc_tp);
  88. s.pre_tp = this.ctx.helper.add(s.pre_contract_tp, s.pre_qc_tp);
  89. s.end_tp = this.ctx.helper.add(s.pre_tp, s.tp);
  90. }
  91. if (stages.length !== 0) {
  92. const lastStage = stages[stages.length - 1];
  93. if (lastStage.status === auditConst.status.uncheck && lastStage.user_id !== this.ctx.session.sessionUser.accountId) {
  94. stages.splice(stages.length - 1, 1);
  95. }
  96. }
  97. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  98. if (stages.length > 0 && stages[0].status !== auditConst.status.checked) {
  99. const stage = stages[0];
  100. const curAuditor = await this.ctx.service.stageAudit.getCurAuditor(stage.id, stage.times);
  101. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : stage.user_id === this.ctx.session.sessionUser.accountId;
  102. if (isActive) {
  103. stage.curTimes = stage.times;
  104. stage.curOrder = curAuditor ? curAuditor.order : 0;
  105. const tpData = await this.ctx.service.stageBills.getSumTotalPrice(stage);
  106. stage.contract_tp = tpData.contract_tp;
  107. stage.qc_tp = tpData.qc_tp;
  108. stage.tp = this.ctx.helper.add(stage.contract_tp, stage.qc_tp);
  109. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  110. }
  111. }
  112. return stages;
  113. }
  114. /**
  115. *
  116. * @param tenderId - 标段id
  117. * @param date - 计量年月
  118. * @param period - 开始-截止日期
  119. * @returns {Promise<void>}
  120. */
  121. async addStage(tenderId, date, period) {
  122. const stages = await this.getAllDataByCondition({
  123. where: {tid: tenderId},
  124. order: ['order'],
  125. });
  126. const preStage = stages[stages.length - 1];
  127. if (stages.length > 0 && stages[stages.length - 1].status !== auditConst.status.checked) {
  128. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  129. };
  130. const order = stages.length + 1;
  131. const newStage = {
  132. sid: this.uuid.v4(),
  133. tid: tenderId,
  134. order: order,
  135. in_time: new Date(),
  136. s_time: date,
  137. period: period,
  138. times: 1,
  139. status: auditConst.status.uncheck,
  140. user_id: this.ctx.session.sessionUser.accountId,
  141. };
  142. if (preStage) {
  143. newStage.pre_contract_tp = this.ctx.helper.add(preStage.pre_contract_tp, preStage.contract_tp);
  144. newStage.pre_qc_tp = this.ctx.helper.add(preStage.pre_qc_tp, preStage.qc_tp);
  145. }
  146. const transaction = await this.db.beginTransaction();
  147. try {
  148. // 新增期记录
  149. const result = await transaction.insert(this.tableName, newStage);
  150. if (result.affectedRows === 1) {
  151. newStage.id = result.insertId;
  152. } else {
  153. throw '新增期数据失败';
  154. }
  155. // 存在上一期时,复制上一期审批流程
  156. if (preStage) {
  157. const auditResult = await this.ctx.service.stageAudit.copyPreStageAuditors(transaction, preStage, newStage);
  158. if (!auditResult) {
  159. throw '复制上一期审批流程失败';
  160. }
  161. }
  162. // 新增期合同支付数据
  163. const dealResult = await this.ctx.service.stagePay.addInitialStageData(newStage, transaction);
  164. if (!dealResult) {
  165. throw '新增期合同支付数据失败';
  166. }
  167. await transaction.commit();
  168. return newStage;
  169. } catch (err) {
  170. await transaction.rollback();
  171. throw err;
  172. }
  173. }
  174. /**
  175. * 编辑计量期
  176. *
  177. * @param {Number} tenderId - 标段Id
  178. * @param {Number} order - 第N期
  179. * @param {String} date - 计量年月
  180. * @param {String} period - 开始-截止时间
  181. * @returns {Promise<void>}
  182. */
  183. async saveStage(tenderId, order, date, period) {
  184. await this.db.update(this.tableName, {
  185. s_time: date,
  186. period: period,
  187. }, { where: { tid: tenderId, order: order } });
  188. }
  189. /**
  190. * 设置 中间计量 生成规则,并生成数据
  191. * @param {Number} tenderId - 标段id
  192. * @param {Number} order - 期序号
  193. * @param {Number} data - 中间计量生成规则
  194. * @returns {Promise<void>}
  195. */
  196. async buildDetailData(tenderId, order, data) {
  197. const conn = await this.db.beginTransaction();
  198. try {
  199. await conn.update(this.tableName, { im_type: data.im_type, im_pre: data.im_pre }, { where: { tid: tenderId, order: order } });
  200. // to do 生成中间计量数据
  201. await conn.commit();
  202. } catch (err) {
  203. await conn.rollback();
  204. throw err;
  205. }
  206. }
  207. /**
  208. * 获取 当期的 计算基数
  209. * @returns {Promise<any>}
  210. */
  211. async getStagePayCalcBase() {
  212. const calcBase = JSON.parse(JSON.stringify(payConst.calcBase));
  213. const param = this.ctx.tender.info.deal_param;
  214. for (const cb of calcBase) {
  215. switch (cb.code) {
  216. case 'htj':
  217. cb.value = param.contractPrice;
  218. break;
  219. case 'zlje':
  220. cb.value = param.zanLiePrice;
  221. break;
  222. case 'htjszl':
  223. cb.value = this.ctx.helper.sub(param.contractPrice, param.zanLiePrice);
  224. break;
  225. case 'kgyfk':
  226. cb.value = param.startAdvance;
  227. break;
  228. case 'clyfk':
  229. cb.value = param.materialAdvance;
  230. break;
  231. case 'bqwc':
  232. const sum = await this.ctx.service.stageBills.getSumTotalPrice(this.ctx.stage);
  233. cb.value = this.ctx.helper.add(sum.contract_tp, sum.qc_tp);
  234. break;
  235. case 'ybbqwc':
  236. const sumGcl = await this.ctx.service.stageBills.getSumTotalPriceGcl(this.ctx.stage, '^1[0-9]{2}-');
  237. cb.value = this.ctx.helper.add(sumGcl.contract_tp, sumGcl.qc_tp);
  238. break;
  239. default:
  240. cb.value = 0;
  241. }
  242. }
  243. return calcBase;
  244. }
  245. /**
  246. * 更新 check_detail 标识
  247. * @param {Integer}sid - 期id
  248. * @param {Boolean}check - 标记
  249. * @returns {Promise<void>}
  250. */
  251. async updateCheckDetailFlag(sid, check) {
  252. const result = await this.db.update(this.tableName, {id: sid, check_detail: check});
  253. return result.affectedRows === 1;
  254. }
  255. }
  256. return Stage;
  257. };