ledger_revise.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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, lc.content,' +
  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, check_calc,' +
  93. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3)' +
  94. ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' +
  95. ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' +
  96. ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, tender_id, is_tp,' +
  97. ' sgfh_expr, sjcl_expr, qtcl_expr, 0,' +
  98. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3' +
  99. ' From ' + this.ctx.service.ledger.tableName +
  100. ' Where `tender_id` = ?';
  101. const sqlParam = [tid];
  102. await transaction.query(sql, sqlParam);
  103. }
  104. async _initRevisePos(transaction, tid) {
  105. const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName +
  106. ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  107. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, in_time, porder, position,' +
  108. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  109. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3)' +
  110. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_times, add_user,' +
  111. ' sgfh_qty, sjcl_qty, qtcl_qty, crid, in_time, porder, position,' +
  112. ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' +
  113. ' gxby_status, dagl_status, dagl_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3' +
  114. ' From ' + this.ctx.service.pos.tableName +
  115. ' Where `tid` = ?';
  116. const sqlParam = [tid];
  117. await transaction.query(sql, sqlParam);
  118. }
  119. async _initReviseAuditor(transaction, rid, order) {
  120. const inTime = new Date();
  121. const preRevise = order > 0 ? await this.getDataByCondition({tid: this.ctx.tender.id, corder: order}) : null;
  122. const newAuditors = [];
  123. const auditors = preRevise
  124. ? await this.ctx.service.reviseAudit.getAuditors(preRevise.id, preRevise.ledger_times)
  125. : await this.ctx.service.ledgerAudit.getAuditors(this.ctx.tender.id, this.ctx.tender.data.ledger_times);
  126. for (const a of auditors) {
  127. newAuditors.push({
  128. tender_id: this.ctx.tender.id,
  129. rid: rid,
  130. in_time: inTime,
  131. audit_order: a.audit_order,
  132. audit_id: a.audit_id,
  133. times: 1,
  134. status: audit.status.uncheck,
  135. });
  136. }
  137. if (auditors) await transaction.insert(this.ctx.service.reviseAudit.tableName, newAuditors);
  138. }
  139. /**
  140. * 备份
  141. * @param {Object} revise - 修订
  142. * @returns {Promise<void>}
  143. * @private
  144. */
  145. async backupReviseHistoryFile(revise) {
  146. const timestamp = (new Date()).getTime();
  147. const billsHis = '/revise/bills' + timestamp + '.json';
  148. const bills = await this.ctx.service.reviseBills.getData(revise.tid);
  149. await this.ctx.helper.saveBufferFile(JSON.stringify(bills), this.ctx.app.config.filePath + billsHis);
  150. const posHis = '/revise/pos' + timestamp + '.json';
  151. const pos = await this.ctx.service.revisePos.getData(revise.tid);
  152. await this.ctx.helper.saveBufferFile(JSON.stringify(pos), this.ctx.app.config.filePath + posHis);
  153. return [billsHis, posHis];
  154. }
  155. /**
  156. * 新增修订
  157. * @param {Number}tid - 标段id
  158. * @param {Number}uid - 提交人id
  159. * @returns {Promise<void>}
  160. */
  161. async add(tid, uid) {
  162. if (!tid && !uid) {
  163. throw '数据错误';
  164. }
  165. const maxOrder = await this.getNewOrder(tid);
  166. const data = {
  167. id: this.uuid.v4(), tid: tid, uid: uid,
  168. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
  169. };
  170. const transaction = await this.db.beginTransaction();
  171. try {
  172. const result = await transaction.insert(this.tableName, data);
  173. if (result.affectedRows !== 1) {
  174. throw '新增台账修订失败';
  175. }
  176. await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});
  177. await this._initReviseBills(transaction, tid, data.id);
  178. await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});
  179. await this._initRevisePos(transaction, tid, data.id);
  180. await this._initReviseAuditor(transaction, data.id, maxOrder);
  181. await transaction.commit();
  182. return data;
  183. } catch(err) {
  184. await transaction.rollback();
  185. throw err;
  186. }
  187. }
  188. /**
  189. * 作废修订
  190. * @param revise
  191. * @returns {Promise<void>}
  192. */
  193. async cancelRevise(revise) {
  194. const transaction = await this.db.beginTransaction();
  195. try {
  196. const [billsHis, posHis] = await this.backupReviseHistoryFile(revise);
  197. const result = await transaction.update(this.tableName, {
  198. id: revise.id, valid: false, end_time: new Date(),
  199. bills_file: billsHis, pos_file: posHis,
  200. });
  201. // 投资进度改变状态
  202. await transaction.update(this.ctx.service.schedule.tableName, { revising: 0 }, { where: { tid: this.ctx.tender.id } });
  203. await transaction.commit();
  204. return result.affectedRows === 1;
  205. } catch (err) {
  206. await transaction.rollback();
  207. throw err;
  208. }
  209. }
  210. }
  211. return LedgerRevise;
  212. };