material.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. 'use strict';
  2. /**
  3. * 期计量 数据模型
  4. *
  5. * @author Mai
  6. * @date 2018/8/13
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').material;
  10. const payConst = require('../const/deal_pay.js');
  11. const fs = require('fs');
  12. const path = require('path');
  13. module.exports = app => {
  14. class Material extends app.BaseService {
  15. /**
  16. * 构造函数
  17. *
  18. * @param {Object} ctx - egg全局变量
  19. * @return {void}
  20. */
  21. constructor(ctx) {
  22. super(ctx);
  23. this.tableName = 'material';
  24. }
  25. /**
  26. * 获取 最新一期 材料调差期计量
  27. * @param tenderId
  28. * @param includeUnCheck
  29. * @returns {Promise<*>}
  30. */
  31. async getLastestMaterial(tenderId, includeUnCheck = false) {
  32. this.initSqlBuilder();
  33. this.sqlBuilder.setAndWhere('tid', {
  34. value: tenderId,
  35. operate: '=',
  36. });
  37. if (!includeUnCheck) {
  38. this.sqlBuilder.setAndWhere('status', {
  39. value: auditConst.status.uncheck,
  40. operate: '!=',
  41. });
  42. }
  43. this.sqlBuilder.orderBy = [['order', 'desc']];
  44. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  45. const material = await this.db.queryOne(sql, sqlParam);
  46. return material;
  47. }
  48. /**
  49. * 获取 最新一期 审批完成的 材料调差期计量
  50. * @param tenderId
  51. * @returns {Promise<*>}
  52. */
  53. async getLastestCompleteMaterial(tenderId) {
  54. this.initSqlBuilder();
  55. this.sqlBuilder.setAndWhere('tid', {
  56. value: tenderId,
  57. operate: '=',
  58. });
  59. this.sqlBuilder.setAndWhere('status', {
  60. value: auditConst.status.checked,
  61. operate: '=',
  62. });
  63. this.sqlBuilder.orderBy = [['order', 'desc']];
  64. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  65. const material = await this.db.queryOne(sql, sqlParam);
  66. return material;
  67. }
  68. /**
  69. * 获取标段下的全部计量期,按倒序
  70. * @param tenderId
  71. * @returns {Promise<void>}
  72. */
  73. async getValidMaterials(tenderId) {
  74. const materials = await this.db.select(this.tableName, {
  75. where: { tid: tenderId },
  76. orders: [['order', 'desc']],
  77. });
  78. if (materials.length !== 0) {
  79. const lastMaterial = materials[materials.length - 1];
  80. if (lastMaterial.status === auditConst.status.uncheck && lastMaterial.user_id !== this.ctx.session.sessionUser.accountId) {
  81. materials.splice(materials.length - 1, 1);
  82. }
  83. }
  84. // 最新一期计量(未审批完成),当前操作人的期详细数据,应实时计算
  85. if (materials.length > 0 && materials[0].status !== auditConst.status.checked) {
  86. const material = materials[0];
  87. const curAuditor = await this.ctx.service.materialAudit.getCurAuditor(material.id, material.times);
  88. const isActive = curAuditor ? curAuditor.id === this.ctx.session.sessionUser.accountId : material.user_id === this.ctx.session.sessionUser.accountId;
  89. if (isActive) {
  90. material.curTimes = material.times;
  91. material.curOrder = curAuditor ? curAuditor.order : 0;
  92. }
  93. }
  94. return materials;
  95. }
  96. /**
  97. * 添加材料调差期
  98. * @param tenderId - 标段id
  99. * @param data - post的数据
  100. * @returns {Promise<void>}
  101. */
  102. async addMaterial(tenderId, data) {
  103. const materials = await this.getAllDataByCondition({
  104. where: { tid: tenderId },
  105. order: ['order'],
  106. });
  107. const preMaterial = materials[materials.length - 1];
  108. if (materials.length > 0 && materials[materials.length - 1].status !== auditConst.status.checked) {
  109. throw '上一期未审批通过,请等待上一期审批通过后,再新增数据';
  110. }
  111. const order = materials.length + 1;
  112. const newMaterial = {
  113. tid: tenderId,
  114. order,
  115. in_time: new Date(),
  116. times: 1,
  117. status: auditConst.status.uncheck,
  118. user_id: this.ctx.session.sessionUser.accountId,
  119. stage_id: data.stage_id.join(','),
  120. s_order: data.s_order,
  121. };
  122. const transaction = await this.db.beginTransaction();
  123. try {
  124. // 新增期记录
  125. const result = await transaction.insert(this.tableName, newMaterial);
  126. if (result.affectedRows === 1) {
  127. newMaterial.id = result.insertId;
  128. } else {
  129. throw '新增期数据失败';
  130. }
  131. // 存在上一期时,复制上一期审批流程
  132. if (preMaterial) {
  133. const auditResult = await this.ctx.service.materialAudit.copyPreMaterialAuditors(transaction, preMaterial, newMaterial);
  134. if (!auditResult) {
  135. throw '复制上一期审批流程失败';
  136. }
  137. }
  138. await transaction.commit();
  139. return newMaterial;
  140. } catch (err) {
  141. await transaction.rollback();
  142. throw err;
  143. }
  144. }
  145. /**
  146. * 删除材料调差期
  147. *
  148. * @param {Number} id - 期Id
  149. * @returns {Promise<void>}
  150. */
  151. async deleteMaterial(id) {
  152. const transaction = await this.db.beginTransaction();
  153. try {
  154. await transaction.delete(this.tableName, { id });
  155. await transaction.delete(this.ctx.service.materialAudit.tableName, { mid: id });
  156. await transaction.commit();
  157. return true;
  158. } catch (err) {
  159. await transaction.rollback();
  160. throw err;
  161. }
  162. }
  163. }
  164. return Material;
  165. };