ledger_revise.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/6/12
  7. * @version
  8. */
  9. const audit = require('../const/audit').revise;
  10. module.exports = app => {
  11. class LedgerRevise extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'ledger_revise';
  21. }
  22. /**
  23. * 获取标段下,修订(分页,且按时间倒序)
  24. * @param {Number}tid - 标段id
  25. * @returns {Promise<*>} - ledger_change下所有数据,并关联 project_account(读取提交人名称、单位、公司)
  26. */
  27. async getReviseList (tid) {
  28. const sql = 'SELECT lc.*, pa.name As user_name, pa.role As user_role, pa.company As user_company' +
  29. ' FROM ' + this.tableName + ' As lc' +
  30. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
  31. ' WHERE lc.tid = ?' +
  32. ' ORDER BY lc.in_time DESC' +
  33. ' LIMIT ?, ?';
  34. const Len = this.app.config.pageSize;
  35. const sqlParam = [tid, (this.ctx.page - 1) * Len, Len];
  36. return await this.db.query(sql, sqlParam);
  37. }
  38. async getLastestRevise(tid) {
  39. const sql = 'SELECT lc.*, pa.name As user_name, pa.role As user_role, pa.company As user_company' +
  40. ' FROM ' + this.tableName + ' As lc' +
  41. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
  42. ' WHERE lc.tid = ?' +
  43. ' ORDER BY lc.in_time DESC' +
  44. ' LIMIT 0, 1';
  45. const sqlParam = [tid];
  46. return await this.db.queryOne(sql, sqlParam);
  47. // const revise = await this.db.select(this.tableName, {
  48. // where: {tid: tid},
  49. // orders: [['in_time', 'DESC']],
  50. // limit: 1,
  51. // offset: 0,
  52. // });
  53. // return revise.length > 0 ? revise[0] : null;
  54. }
  55. /**
  56. * 获取新增修订的序号
  57. * @param {Number}tid - 标段id
  58. * @returns {Promise<number>}
  59. */
  60. async getNewOrder(tid) {
  61. const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`';
  62. const sqlParam = [tid];
  63. const result = await this.db.queryOne(sql, sqlParam);
  64. return result.max_order || 0;
  65. // if (result && result.max_order) {
  66. // return result.max_order;
  67. // } else {
  68. // return 0;
  69. // }
  70. }
  71. async _initReviseBills(transaction, tid) {
  72. const sql = 'Insert Into ' + this.ctx.service.reviseBills.tableName +
  73. ' Select * From ' + this.ctx.service.ledger.tableName +
  74. ' Where `tender_id` = ?';
  75. const sqlParam = [tid];
  76. await transaction.query(sql, sqlParam);
  77. }
  78. async _initRevisePos(transaction, tid) {
  79. const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName +
  80. ' Select * From ' + this.ctx.service.pos.tableName +
  81. ' Where `tid` = ?';
  82. const sqlParam = [tid];
  83. await transaction.query(sql, sqlParam);
  84. }
  85. /**
  86. * 新增修订
  87. * @param {Number}tid - 标段id
  88. * @param {Number}uid - 提交人id
  89. * @returns {Promise<void>}
  90. */
  91. async add(tid, uid) {
  92. if (!tid && !uid) {
  93. throw '数据错误';
  94. }
  95. const maxOrder = await this.getNewOrder(tid);
  96. const data = {
  97. id: this.uuid.v4(), tid: tid, uid: uid,
  98. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
  99. };
  100. const transaction = await this.db.beginTransaction();
  101. try {
  102. const result = await transaction.insert(this.tableName, data);
  103. if (result.affectedRows !== 1) {
  104. throw '新增台账修订失败';
  105. }
  106. await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});
  107. await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});
  108. await this._initReviseBills(transaction, tid, data.id);
  109. await this._initRevisePos(transaction, tid, data.id);
  110. await transaction.commit();
  111. return data;
  112. } catch(err) {
  113. await transaction.rollback();
  114. throw err;
  115. }
  116. }
  117. /**
  118. * 作废修订
  119. * @param id
  120. * @returns {Promise<void>}
  121. */
  122. async cancelRevise(id) {
  123. const result = await this.db.update(this.tableName, {id: id, valid: false});
  124. return result.affectedRows === 1;
  125. }
  126. }
  127. return LedgerRevise;
  128. };