ledger_revise.js 9.6 KB

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