ledger_revise.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 *, 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. /**
  39. * 获取新增修订的序号
  40. * @param {Number}tid - 标段id
  41. * @returns {Promise<number>}
  42. */
  43. async getNewOrder(tid) {
  44. const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`';
  45. const sqlParam = [tid];
  46. const result = await this.db.queryOne(sql, sqlParam);
  47. return result.max_order || 0;
  48. // if (result && result.max_order) {
  49. // return result.max_order;
  50. // } else {
  51. // return 0;
  52. // }
  53. }
  54. /**
  55. * 新增修订
  56. * @param {Number}tid - 标段id
  57. * @param {Number}uid - 提交人id
  58. * @returns {Promise<void>}
  59. */
  60. async add(tid, uid) {
  61. if (!tid && !uid) {
  62. throw '数据错误';
  63. }
  64. const maxOrder = await this.getNewOrder(tid);
  65. const data = {
  66. id: this.uuid.v4(), tid: tid, uid: uid,
  67. corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck,
  68. };
  69. const transaction = await this.db.beginTransaction();
  70. try {
  71. const result = await transaction.insert(this.tableName, data);
  72. if (result.affectedRows !== 1) {
  73. throw '新增台账修订失败';
  74. }
  75. // todo 拷贝数据(清单、部位明细)
  76. await transaction.commit();
  77. } catch(err) {
  78. await transaction.rollback();
  79. throw err;
  80. }
  81. }
  82. }
  83. return LedgerRevise;
  84. };