measure_audit.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/7/16
  7. * @version
  8. */
  9. const auditConst = require('../const/audit').flow;
  10. module.exports = app => {
  11. class MeasureAudit extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'measure_audit';
  21. }
  22. /**
  23. * 获取 中间计量 审核人信息
  24. *
  25. * @param {uuid} mid - 中间计量id
  26. * @param {Number} auditorId - 审核人id
  27. * @param {Number} times - 第几次审批
  28. * @return {Promise<*>}
  29. */
  30. async getAuditor(mid, auditorId, times = 1) {
  31. const sql = 'SELECT ma.*, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone` ' +
  32. 'FROM ?? AS ma, ?? AS pa ' +
  33. 'WHERE ma.`mid` = ? and ma.`audit_id` = ? and ma.`times` = ?' +
  34. ' and ma.`audit_id` = pa.`id`';
  35. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, mid, auditorId, times];
  36. return await this.db.queryOne(sql, sqlParam);
  37. }
  38. /**
  39. * 获取 中间计量 审核人列表信息
  40. *
  41. * @param {uuid} mid - 中间计量id
  42. * @param {Number} times - 第几次审批
  43. * @return {Promise<*>}
  44. */
  45. async getAuditors(mid, times = 1) {
  46. const sql = 'SELECT ma.*, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone` ' +
  47. 'FROM ?? AS ma, ?? AS pa ' +
  48. 'WHERE ma.`mid` = ? and ma.`times` = ?' +
  49. ' and ma.`audit_id` = pa.`id` and ma.`order` > 0';
  50. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, mid, times];
  51. return await this.db.query(sql, sqlParam);
  52. }
  53. /**
  54. * 获取 中间计量 当前审核人
  55. *
  56. * @param {uuid} mid - 中间计量id
  57. * @param {Number} times - 第几次审批
  58. * @return {Promise<*>}
  59. */
  60. async getCurAuditor(mid, times = 1) {
  61. const sql = 'SELECT ma.`audit_id`, pa.`name`, pa.`company`, pa.`role`, pa.`mobile`, pa.`telephone`, ma.`times`, ma.`order`, ma.`status`, ma.`opinion`, ma.`begin_time`, ma.`end_time` ' +
  62. 'FROM ?? AS ma, ?? AS pa ' +
  63. 'WHERE ma.`mid` = ? and ma.`status` = ? and ma.`times` = ?' +
  64. ' and ma.`audit_id` = pa.`id`';
  65. const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, mid, auditConst.status.checking, times];
  66. return await this.db.queryOne(sql, sqlParam);
  67. }
  68. /**
  69. * 获取 中间计量 最新审核顺序
  70. *
  71. * @param {uuid} mid - 中间计量id
  72. * @param {Number} times - 第几次审批
  73. * @return {Promise<number>}
  74. */
  75. async getNewOrder(mid, times = 1) {
  76. const sql = 'SELECT Max(??) As max_order FROM ?? Where `mid` = ? and `times` = ? and `order` > 0';
  77. const sqlParam = ['order', this.tableName, mid, times];
  78. const result = await this.db.queryOne(sql, sqlParam);
  79. return result && result.max_order ? result.max_order + 1 : 1;
  80. }
  81. /**
  82. * 新增审核人
  83. *
  84. * @param {Number} tenderId - 标段id
  85. * @param {uuid} mid - 中间计量id
  86. * @param {Number} auditorId - 审核人id
  87. * @param {Number} times - 第几次审批
  88. * @return {Promise<number>}
  89. */
  90. async addAuditor(tenderId, mid, auditorId, times = 1) {
  91. const newOrder = await this.getNewOrder(mid, times);
  92. const data = {
  93. tender_id: tenderId,
  94. mid,
  95. audit_id: auditorId,
  96. times,
  97. order: newOrder,
  98. status: auditConst.status.uncheck,
  99. };
  100. const result = await this.db.insert(this.tableName, data);
  101. return result.effectRows = 1;
  102. }
  103. /**
  104. * 移除审核人时,同步其后审核人order
  105. * @param transaction - 事务
  106. * @param {uuid} mid - 中间计量id
  107. * @param {Number} auditorId - 审核人id
  108. * @param {Number} times - 第几次审批
  109. * @return {Promise<*>}
  110. * @private
  111. */
  112. async _syncOrderByDelete(transaction, mid, order, times) {
  113. this.initSqlBuilder();
  114. this.sqlBuilder.setAndWhere('mid', {
  115. value: this.db.escape(mid),
  116. operate: '=',
  117. });
  118. this.sqlBuilder.setAndWhere('order', {
  119. value: order,
  120. operate: '>=',
  121. });
  122. this.sqlBuilder.setAndWhere('times', {
  123. value: times,
  124. operate: '=',
  125. });
  126. this.sqlBuilder.setUpdateData('order', {
  127. value: 1,
  128. selfOperate: '-',
  129. });
  130. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName, 'update');
  131. const data = await transaction.query(sql, sqlParam);
  132. return data;
  133. }
  134. /**
  135. * 移除审核人
  136. *
  137. * @param {uuid} mid - 中间计量id
  138. * @param {Number} auditorId - 审核人id
  139. * @param {Number} times - 第几次审批
  140. * @return {Promise<boolean>}
  141. */
  142. async deleteAuditor(mid, auditorId, times = 1) {
  143. const transaction = await this.db.beginTransaction();
  144. try {
  145. const condition = { mid, audit_id: auditorId, times };
  146. const auditor = await this.getDataByCondition(condition);
  147. if (!auditor) {
  148. throw '该审核人不存在';
  149. }
  150. await this._syncOrderByDelete(transaction, mid, auditor.order, times);
  151. await transaction.delete(this.tableName, condition);
  152. await transaction.commit();
  153. } catch (err) {
  154. await transaction.rollback();
  155. throw err;
  156. }
  157. return true;
  158. }
  159. _sumBills(bills) {
  160. const result = { deal: 0, qc: 0 };
  161. for (const b of bills) {
  162. result.deal = result.deal + (b.deal_totalprice ? b.deal_totalprice : 0);
  163. result.qc = result.qc + (b.qc_totalprice ? b.qc_totalprice : 0);
  164. }
  165. return result;
  166. }
  167. /**
  168. * 拷贝原报的历史数据
  169. *
  170. * @param transacation - 事务
  171. * @param {uuid} mid - 中间计量
  172. * @param {Number} times - 次数
  173. * @return {Promise<void>}
  174. * @private
  175. */
  176. async _copyReportBillsHistory(transacation, mid, times) {
  177. const bills = await this.ctx.service.measureBills.getAllDataByCondition({ where: { mid } });
  178. const sum = this._sumBills(bills);
  179. const history = await this.getDataByCondition({ mid, times, order: 0 });
  180. if (history) {
  181. await transacation.update(this.tableName, {
  182. id: history.id,
  183. bills: JSON.stringify(bills),
  184. deal_sum: sum.deal,
  185. qc_sum: sum.qc,
  186. });
  187. } else {
  188. const measure = await this.ctx.service.measure.getDataByCondition({ mid });
  189. await transacation.insert(this.tableName, {
  190. tender_id: measure.tender_id,
  191. mid,
  192. times,
  193. order: 0,
  194. audit_id: measure.user_id,
  195. status: 0,
  196. bills: JSON.stringify(bills),
  197. deal_sum: sum.deal,
  198. qc_sum: sum.qc,
  199. });
  200. }
  201. return sum;
  202. }
  203. /**
  204. * 开始审批
  205. *
  206. * @param {uuid} mid - 中间计量id
  207. * @param {Number} times - 第几次审批
  208. * @return {Promise<boolean>}
  209. */
  210. async start(mid, times = 1) {
  211. const audit = await this.getDataByCondition({ mid, times, order: 1 });
  212. if (!audit) {
  213. throw '审核人信息错误';
  214. }
  215. const transaction = await this.db.beginTransaction();
  216. try {
  217. // 记录历史数据
  218. const sum = await this._copyReportBillsHistory(transaction, mid, times);
  219. // 更新审核人状态
  220. await transaction.update(this.tableName, {
  221. id: audit.id,
  222. status: auditConst.status.checking,
  223. begin_time: new Date(),
  224. });
  225. // 改变中间计量状态
  226. await transaction.update(this.ctx.service.measure.tableName, {
  227. times,
  228. status: auditConst.status.checking,
  229. deal_sum: sum.deal,
  230. qc_sum: sum.qc,
  231. }, { where: { mid } });
  232. await transaction.commit();
  233. } catch (err) {
  234. await transaction.rollback();
  235. throw err;
  236. }
  237. return true;
  238. }
  239. /**
  240. * 审批
  241. * @param {uuid} mid - 中间计量id
  242. * @param {auditConst.status.checked|auditConst.status.checkNo} checkType - 审批结果
  243. * @param {Number} times - 第几次审批
  244. * @return {Promise<void>}
  245. */
  246. async check(mid, checkType, opinion, times = 1) {
  247. if (checkType !== auditConst.status.checked && checkType !== auditConst.status.checkNo) {
  248. throw '提交数据错误';
  249. }
  250. const transaction = await this.db.beginTransaction();
  251. try {
  252. // 整理当前流程审核人状态更新
  253. const time = new Date();
  254. const audit = await this.getDataByCondition({ mid, times, status: auditConst.status.checking });
  255. if (!audit) {
  256. throw '审核数据错误';
  257. }
  258. // 更新当前审核流程
  259. const bills = await this.ctx.service.measureBills.getAllDataByCondition({ where: { mid } });
  260. const sum = this._sumBills(bills);
  261. await transaction.update(this.tableName, {
  262. id: audit.id,
  263. status: checkType,
  264. opinion,
  265. end_time: time,
  266. bills: JSON.stringify(bills),
  267. deal_sum: sum.deal,
  268. qc_sum: sum.qc,
  269. });
  270. const measureTable = this.ctx.service.measure.tableName;
  271. if (checkType === auditConst.status.checked) {
  272. const nextAudit = await this.getDataByCondition({ mid, times, order: audit.order + 1 });
  273. // 无下一审核人表示,审核结束
  274. if (nextAudit) {
  275. await transaction.update(this.tableName, { id: nextAudit.id, status: auditConst.status.checking, begin_time: time });
  276. await transaction.update(measureTable, { deal_sum: sum.deal, qc_sum: sum.qc }, { where: { mid } });
  277. } else {
  278. // 同步标段信息
  279. await transaction.update(measureTable, { status: checkType, deal_sum: sum.deal, qc_sum: sum.qc }, { where: { mid } });
  280. }
  281. } else {
  282. // 同步标段信息
  283. await transaction.update(measureTable, { times: times + 1, status: checkType, deal_sum: sum.deal, qc_sum: sum.qc }, { where: { mid } });
  284. // 拷贝新一次审核流程列表
  285. const auditors = await this.getAllDataByCondition({
  286. where: { mid, times },
  287. columns: ['tender_id', 'order', 'audit_id', 'mid'],
  288. });
  289. for (const a of auditors) {
  290. a.times = times + 1;
  291. a.status = auditConst.status.uncheck;
  292. }
  293. await transaction.insert(this.tableName, auditors);
  294. }
  295. await transaction.commit();
  296. } catch (err) {
  297. await transaction.rollback();
  298. throw err;
  299. }
  300. }
  301. /**
  302. * 获取审核人需要审核的标段列表
  303. *
  304. * @param auditorId
  305. * @return {Promise<*>}
  306. */
  307. async getAuditTender(auditorId) {
  308. const sql = 'SELECT ma.`audit_id`, ma.`times`, ma.`order`, ma.`begin_time`, t.`id`, t.`name`, t.`project_id`, t.`type`, t.`user_id` ' +
  309. 'FROM ?? AS ma, ?? AS t ' +
  310. 'WHERE ma.`audit_id` = ? and ma.`status` = ?' +
  311. ' and ma.`tender_id` = t.`id`';
  312. const sqlParam = [this.tableName, this.ctx.service.tender.tableName, auditorId, auditConst.status.checking];
  313. return await this.db.query(sql, sqlParam);
  314. }
  315. }
  316. return MeasureAudit;
  317. };