ledger_revise.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.id, lc.tid, lc.corder, lc.in_time, lc.uid, lc.begin_time, lc.end_time, lc.times, lc.status, lc.valid,' +
  29. ' pa.name As user_name, pa.role As user_role, pa.company As user_company' +
  30. ' FROM ' + this.tableName + ' As lc' +
  31. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
  32. ' WHERE lc.tid = ?' +
  33. ' ORDER BY lc.in_time DESC' +
  34. ' LIMIT ?, ?';
  35. const Len = this.app.config.pageSize;
  36. const sqlParam = [tid, (this.ctx.page - 1) * Len, Len];
  37. return await this.db.query(sql, sqlParam);
  38. }
  39. async getLastestRevise(tid, valid = true) {
  40. const sql = 'SELECT lc.*,' +
  41. ' pa.name As user_name, pa.role As user_role, pa.company As user_company' +
  42. ' FROM ' + this.tableName + ' As lc' +
  43. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
  44. ' WHERE lc.tid = ? ' + (valid ? ' And lc.valid' : '') +
  45. ' ORDER BY lc.in_time DESC' +
  46. ' LIMIT 0, 1';
  47. const sqlParam = [tid];
  48. return await this.db.queryOne(sql, sqlParam);
  49. }
  50. async getRevise(tid, rid) {
  51. const sql = 'SELECT lc.*,' +
  52. ' pa.name As user_name, pa.role As user_role, pa.company As user_company' +
  53. ' FROM ' + this.tableName + ' As lc' +
  54. ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' +
  55. ' WHERE lc.tid = ? and lc.id = ? ' +
  56. ' ORDER BY lc.in_time DESC' +
  57. ' LIMIT 0, 1';
  58. const sqlParam = [tid, rid];
  59. return await this.db.queryOne(sql, sqlParam);
  60. }
  61. /**
  62. * 获取新增修订的序号
  63. * @param {Number}tid - 标段id
  64. * @returns {Promise<number>}
  65. */
  66. async getNewOrder(tid) {
  67. const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`';
  68. const sqlParam = [tid];
  69. const result = await this.db.queryOne(sql, sqlParam);
  70. return result.max_order || 0;
  71. }
  72. async _initReviseBills(transaction, tid) {
  73. const sql = 'Insert Into ' + this.ctx.service.reviseBills.tableName +
  74. ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  75. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  76. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  77. ' sgfh_expr, sjcl_expr, qtcl_expr, gxby_status, dagl_status)' +
  78. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  79. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  80. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  81. ' sgfh_expr, sjcl_expr, qtcl_expr, gxby_status, dagl_status' +
  82. ' From ' + this.ctx.service.ledger.tableName +
  83. ' Where `tender_id` = ?';
  84. const sqlParam = [tid];
  85. await transaction.query(sql, sqlParam);
  86. }
  87. async _initRevisePos(transaction, tid) {
  88. const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName +
  89. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  90. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, in_time, porder, position,' +
  91. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, gxby_status, dagl_status)' +
  92. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  93. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, in_time, porder, position,' +
  94. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty, gxby_status, dagl_status' +
  95. ' From ' + this.ctx.service.pos.tableName +
  96. ' Where `tid` = ?';
  97. const sqlParam = [tid];
  98. await transaction.query(sql, sqlParam);
  99. }
  100. async _initReviseAuditor(transaction, rid, order) {
  101. const inTime = new Date();
  102. const preRevise = order > 0 ? await this.getDataByCondition({tid: this.ctx.tender.id, corder: order}) : null;
  103. const newAuditors = [];
  104. const auditors = preRevise
  105. ? await this.ctx.service.reviseAudit.getAuditors(preRevise.id, preRevise.ledger_times)
  106. : await this.ctx.service.ledgerAudit.getAuditors(this.ctx.tender.id, this.ctx.tender.data.ledger_times);
  107. for (const a of auditors) {
  108. newAuditors.push({
  109. tender_id: this.ctx.tender.id,
  110. rid: rid,
  111. in_time: inTime,
  112. audit_order: a.audit_order,
  113. audit_id: a.audit_id,
  114. times: 1,
  115. status: audit.status.uncheck,
  116. });
  117. }
  118. await transaction.insert(this.ctx.service.reviseAudit.tableName, newAuditors);
  119. }
  120. /**
  121. * 备份
  122. * @param {Object} revise - 修订
  123. * @returns {Promise<void>}
  124. * @private
  125. */
  126. async backupReviseHistoryFile(revise) {
  127. const timestamp = (new Date()).getTime();
  128. const billsHis = '/revise/bills' + timestamp + '.json';
  129. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  130. await this.ctx.helper.saveBufferFile(JSON.stringify(bills), this.ctx.app.config.filePath + billsHis);
  131. const posHis = '/revise/pos' + timestamp + '.json';
  132. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  133. await this.ctx.helper.saveBufferFile(JSON.stringify(pos), this.ctx.app.config.filePath + posHis);
  134. return [billsHis, posHis];
  135. }
  136. /**
  137. * 新增修订
  138. * @param {Number}tid - 标段id
  139. * @param {Number}uid - 提交人id
  140. * @returns {Promise<void>}
  141. */
  142. async add(tid, uid) {
  143. if (!tid && !uid) {
  144. throw '数据错误';
  145. }
  146. const maxOrder = await this.getNewOrder(tid);
  147. const data = {
  148. id: this.uuid.v4(), tid: tid, uid: uid,
  149. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
  150. };
  151. const transaction = await this.db.beginTransaction();
  152. try {
  153. const result = await transaction.insert(this.tableName, data);
  154. if (result.affectedRows !== 1) {
  155. throw '新增台账修订失败';
  156. }
  157. await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});
  158. await this._initReviseBills(transaction, tid, data.id);
  159. await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});
  160. await this._initRevisePos(transaction, tid, data.id);
  161. await this._initReviseAuditor(transaction, data.id, maxOrder);
  162. await transaction.commit();
  163. return data;
  164. } catch(err) {
  165. await transaction.rollback();
  166. throw err;
  167. }
  168. }
  169. /**
  170. * 作废修订
  171. * @param revise
  172. * @returns {Promise<void>}
  173. */
  174. async cancelRevise(revise) {
  175. const [billsHis, posHis] = await this.backupReviseHistoryFile(revise);
  176. const result = await this.db.update(this.tableName, {
  177. id: revise.id, valid: false, end_time: new Date(),
  178. bills_file: billsHis, pos_file: posHis,
  179. });
  180. return result.affectedRows === 1;
  181. }
  182. }
  183. return LedgerRevise;
  184. };