schedule_stage.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. module.exports = app => {
  3. class ScheduleStage extends app.BaseService {
  4. constructor(ctx) {
  5. super(ctx);
  6. this.tableName = 'schedule_stage';
  7. }
  8. async getLastPlanMonth() {
  9. const sql = 'SELECT `yearmonth` FROM ?? WHERE `tid` = ? ORDER BY `yearmonth` DESC Limit 0,1';
  10. const sqlParam = [this.tableName, this.ctx.tender.id];
  11. return await this.db.query(sql, sqlParam);
  12. }
  13. async add(data) {
  14. const transaction = await this.db.beginTransaction();
  15. try {
  16. const insertData = {
  17. tid: this.ctx.tender.id,
  18. yearmonth: data.yearmonth,
  19. order: data.order,
  20. };
  21. // 更新schedule_month stage_tp_used为1
  22. const updateData = {
  23. stage_tp_used: 1,
  24. };
  25. const option = {
  26. where: {
  27. tid: this.ctx.tender.id,
  28. yearmonth: data.yearmonth,
  29. },
  30. };
  31. await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  32. await transaction.insert(this.tableName, insertData);
  33. await transaction.commit();
  34. return true;
  35. } catch (err) {
  36. await transaction.rollback();
  37. throw err;
  38. }
  39. }
  40. async del(data) {
  41. const transaction = await this.db.beginTransaction();
  42. try {
  43. const info = await this.getDataById(data.id);
  44. await transaction.delete(this.tableName, { id: data.id });
  45. // 更新schedule_month stage_tp_used为0
  46. const updateData = {
  47. stage_tp_used: 0,
  48. };
  49. const option = {
  50. where: {
  51. tid: this.ctx.tender.id,
  52. yearmonth: info.yearmonth,
  53. },
  54. };
  55. await transaction.update(this.ctx.service.scheduleMonth.tableName, updateData, option);
  56. await transaction.commit();
  57. return true;
  58. } catch (err) {
  59. await transaction.rollback();
  60. throw err;
  61. }
  62. }
  63. async changeOrder(data) {
  64. const transaction = await this.db.beginTransaction();
  65. try {
  66. const updateData = {
  67. id: data.id,
  68. order: data.order,
  69. };
  70. await transaction.update(this.tableName, updateData);
  71. await transaction.commit();
  72. return true;
  73. } catch (err) {
  74. await transaction.rollback();
  75. throw err;
  76. }
  77. }
  78. }
  79. return ScheduleStage;
  80. };