budget.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2021/11/9
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Budget extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'budget';
  20. }
  21. /**
  22. * 数据规则
  23. *
  24. * @param {String} scene - 场景
  25. * @return {Object} - 返回数据规则
  26. */
  27. rule(scene) {
  28. let rule = {};
  29. switch (scene) {
  30. case 'add':
  31. rule = {
  32. name: { type: 'string', required: true, min: 2 },
  33. std_id: { type: 'string', required: true, min: 1 },
  34. };
  35. break;
  36. case 'save':
  37. rule = {
  38. name: { type: 'string', required: true, min: 2, max: 100, },
  39. };
  40. default:
  41. break;
  42. }
  43. return rule;
  44. }
  45. async getBudget(admin) {
  46. let result = await this.getAllDataByCondition({
  47. where: { pid: this.ctx.session.sessionProject.id },
  48. orders: [['name', 'asc']],
  49. });
  50. if (admin) return result;
  51. const permissionConst = this.ctx.service.budgetPermission.PermissionConst;
  52. const permissionBudget = await this.ctx.service.budgetPermission.getUserPermission();
  53. result = result.filter(x => {
  54. const pb = permissionBudget.find(y => { return x.id === y.bid});
  55. if (pb) {
  56. x.canEdit = pb.permission.indexOf(permissionConst.edit.value) >= 0;
  57. }
  58. return !!pb;
  59. });
  60. return result;
  61. }
  62. /**
  63. * 新增标段
  64. *
  65. * @param {Object} data - 提交的数据
  66. * @return {Boolean} - 返回新增结果
  67. */
  68. async add(data) {
  69. const budgetStd = await this.ctx.service.budgetStd.getDataById(data.std_id);
  70. if (!budgetStd) throw '选择的概算标准不存在,请刷新页面重试';
  71. const conn = await this.db.beginTransaction();
  72. try {
  73. // 获取当前用户信息
  74. const sessionUser = this.ctx.session.sessionUser;
  75. // 获取当前项目信息
  76. const sessionProject = this.ctx.session.sessionProject;
  77. const insertData = {
  78. pid: sessionProject.id, user_id: sessionUser.accountId, in_time: new Date(),
  79. name: data.name, std_id: data.std_id,
  80. };
  81. const operate = await conn.insert(this.tableName, insertData);
  82. if (operate.insertId === 0) throw '新增标段数据失败';
  83. // 获取合同支付模板 并添加到标段
  84. await this.ctx.service.budgetGu.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
  85. await this.ctx.service.budgetGai.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
  86. await this.ctx.service.budgetYu.initByTemplate(conn, operate.insertId, budgetStd.gu_template_id);
  87. await conn.commit();
  88. return await this.getDataById(operate.insertId);
  89. } catch (error) {
  90. await conn.rollback();
  91. throw error;
  92. }
  93. }
  94. /**
  95. * 保存标段
  96. *
  97. * @param {Number} id
  98. * @param {Object} postData - 表单post过来的数
  99. * @return {Boolean} - 返回执行结果
  100. */
  101. async save(data) {
  102. const result = await this.db.update(this.tableName, data);
  103. return result.affectedRows > 0;
  104. }
  105. /**
  106. * 假删除
  107. *
  108. * @param {Number} id - 删除的id
  109. * @return {Boolean} - 删除结果
  110. */
  111. async deleteBudget(id) {
  112. const updateData = { id, status: this.status.DISABLE };
  113. const result = await this.db.update(this.tableName, updateData);
  114. return result.affectedRows > 0;
  115. }
  116. /**
  117. * 真删除
  118. * @param {Number} id - 删除的标段id
  119. * @return {Promise<boolean>} - 结果
  120. */
  121. async deleteBudgetNoBackup(id) {
  122. const transaction = await this.db.beginTransaction();
  123. try {
  124. await transaction.delete(this.tableName, { id });
  125. await transaction.delete(this.ctx.service.budgetGu.tableName, { bid: id });
  126. await transaction.delete(this.ctx.service.budgetGai.tableName, { bid: id });
  127. await transaction.delete(this.ctx.service.budgetYu.tableName, { bid: id });
  128. await transaction.commit();
  129. return true;
  130. } catch (err) {
  131. this.ctx.log(err);
  132. await transaction.rollback();
  133. return false;
  134. }
  135. }
  136. }
  137. return Budget;
  138. };