ledger_revise.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  74. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  75. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id)' +
  76. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  77. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  78. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id' +
  79. ' From ' + this.ctx.service.ledger.tableName +
  80. ' Where `tender_id` = ?';
  81. const sqlParam = [tid];
  82. await transaction.query(sql, sqlParam);
  83. }
  84. async _initRevisePos(transaction, tid) {
  85. const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName +
  86. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  87. ' sgfh_qty, sjcl_qty, qtcl_qty, crid)' +
  88. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  89. ' sgfh_qty, sjcl_qty, qtcl_qty, crid' +
  90. ' From ' + this.ctx.service.pos.tableName +
  91. ' Where `tid` = ?';
  92. const sqlParam = [tid];
  93. await transaction.query(sql, sqlParam);
  94. }
  95. /**
  96. * 新增修订
  97. * @param {Number}tid - 标段id
  98. * @param {Number}uid - 提交人id
  99. * @returns {Promise<void>}
  100. */
  101. async add(tid, uid) {
  102. if (!tid && !uid) {
  103. throw '数据错误';
  104. }
  105. const maxOrder = await this.getNewOrder(tid);
  106. const data = {
  107. id: this.uuid.v4(), tid: tid, uid: uid,
  108. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
  109. };
  110. const transaction = await this.db.beginTransaction();
  111. try {
  112. const result = await transaction.insert(this.tableName, data);
  113. if (result.affectedRows !== 1) {
  114. throw '新增台账修订失败';
  115. }
  116. await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});
  117. await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});
  118. await this._initReviseBills(transaction, tid, data.id);
  119. await this._initRevisePos(transaction, tid, data.id);
  120. await transaction.commit();
  121. return data;
  122. } catch(err) {
  123. await transaction.rollback();
  124. throw err;
  125. }
  126. }
  127. /**
  128. * 作废修订
  129. * @param id
  130. * @returns {Promise<void>}
  131. */
  132. async cancelRevise(id) {
  133. const result = await this.db.update(this.tableName, {id: id, valid: false});
  134. return result.affectedRows === 1;
  135. }
  136. }
  137. return LedgerRevise;
  138. };