ledger_revise.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, lc.pre_his_id, lc.his_id,' +
  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, lc.pre_his_id, lc.his_id,' +
  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, gxby_url, 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, gxby_url, 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_stage_order, 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, gxby_url, dagl_url, gxby_limit, dagl_limit, ex_memo1, ex_memo2, ex_memo3)' +
  110. ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, 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, gxby_url, 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 {Number}tid - 标段id
  142. * @param {Number}uid - 提交人id
  143. * @returns {Promise<void>}
  144. */
  145. async add(tid, uid) {
  146. if (!tid && !uid) {
  147. throw '数据错误';
  148. }
  149. const maxOrder = await this.getNewOrder(tid);
  150. const latest = await this.ctx.service.ledgerHistory.getLatestHistory(tid);
  151. if (!latest) throw '台账历史数据有误';
  152. const data = {
  153. id: this.uuid.v4(), tid: tid, uid: uid,
  154. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck, pre_his_id: latest.id,
  155. };
  156. const transaction = await this.db.beginTransaction();
  157. try {
  158. const result = await transaction.insert(this.tableName, data);
  159. if (result.affectedRows !== 1) {
  160. throw '新增台账修订失败';
  161. }
  162. await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid});
  163. await this._initReviseBills(transaction, tid, data.id);
  164. await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid});
  165. await this._initRevisePos(transaction, tid, data.id);
  166. await this._initReviseAuditor(transaction, data.id, maxOrder);
  167. await transaction.commit();
  168. return data;
  169. } catch(err) {
  170. await transaction.rollback();
  171. throw err;
  172. }
  173. }
  174. /**
  175. * 作废修订
  176. * @param revise
  177. * @returns {Promise<void>}
  178. */
  179. async cancelRevise(revise) {
  180. const his_id = await this.ctx.service.ledgerHistory.backupReviseLedgerHistory(revise);
  181. const transaction = await this.db.beginTransaction();
  182. try {
  183. const result = await transaction.update(this.tableName, {
  184. id: revise.id, valid: false, end_time: new Date(),
  185. his_id,
  186. });
  187. await transaction.update(this.ctx.service.ledgerHistory.tableName, { id: his_id, valid: 0 });
  188. if (revise.his_id > 0) await transaction.update(this.ctx.service.ledgerHistory.tableName, { id: revise.his_id, valid: 0 });
  189. // 投资进度改变状态
  190. await transaction.update(this.ctx.service.schedule.tableName, { revising: 0 }, { where: { tid: this.ctx.tender.id } });
  191. await transaction.commit();
  192. return result.affectedRows === 1;
  193. } catch (err) {
  194. await transaction.rollback();
  195. throw err;
  196. }
  197. }
  198. }
  199. return LedgerRevise;
  200. };