trackTask.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import { Op } from 'sequelize';
  2. import TrackTask, { TrackTaskCreationAttributes } from '../models/trackTask';
  3. import TrackTaskDetail, {
  4. TrackTaskDetailCreationAttributes,
  5. } from '../models/trackTaskDetail';
  6. import sequelize from '../sequelizeInstance';
  7. import {
  8. TrackTaskStatusKey,
  9. TrackTaskDetailStatusKey,
  10. TRACK_TASK_STATUS,
  11. TRACK_TASK_DETAIL_STATUS,
  12. } from '../enumerate';
  13. export async function getUnfinishTrackTask(schoolId: string) {
  14. const task = await TrackTask.findOne({
  15. where: { status: TRACK_TASK_STATUS.READY, schoolId },
  16. }).catch((err) => {
  17. console.dir(err);
  18. });
  19. return task ? task.dataValues : null;
  20. }
  21. export async function createTrackTask(
  22. data: TrackTaskCreationAttributes,
  23. details: TrackTaskDetailCreationAttributes[]
  24. ) {
  25. const t = await sequelize.transaction();
  26. try {
  27. await TrackTask.update(
  28. {
  29. status: TRACK_TASK_STATUS.FINISH,
  30. error: '补漏结束',
  31. },
  32. {
  33. where: {
  34. status: { [Op.ne]: TRACK_TASK_STATUS.FINISH },
  35. },
  36. transaction: t,
  37. }
  38. );
  39. // 保存任务
  40. const task = await TrackTask.create(data, { transaction: t });
  41. details.forEach((item) => {
  42. item.trackTaskId = task.id;
  43. });
  44. await TrackTaskDetail.bulkCreate(details, { transaction: t });
  45. await t.commit();
  46. return task.dataValues;
  47. } catch (error) {
  48. await t.rollback();
  49. return Promise.reject(error);
  50. }
  51. }
  52. export async function updateTrackTaskStatus(data: {
  53. id: number;
  54. status: TrackTaskStatusKey;
  55. }) {
  56. await TrackTask.update(
  57. {
  58. status: TRACK_TASK_STATUS[data.status],
  59. },
  60. {
  61. where: {
  62. id: data.id,
  63. },
  64. }
  65. );
  66. }
  67. export async function createTrackTaskDetails(
  68. details: TrackTaskDetailCreationAttributes[]
  69. ) {
  70. const t = await sequelize.transaction();
  71. try {
  72. await TrackTaskDetail.bulkCreate(details, { transaction: t });
  73. await t.commit();
  74. return true;
  75. } catch (error) {
  76. await t.rollback();
  77. return Promise.reject(error);
  78. }
  79. }
  80. // track detail ------------------->
  81. export async function getTrackTaskDetailCount(data: {
  82. trackTaskId: number;
  83. status?: TrackTaskDetailStatusKey;
  84. }) {
  85. const count = await TrackTaskDetail.count({
  86. where: data,
  87. });
  88. return count;
  89. }
  90. export async function getUnfinishTrackTaskDetail(trackTaskId: number) {
  91. const task = await TrackTaskDetail.findOne({
  92. where: { status: TRACK_TASK_DETAIL_STATUS.INIT, trackTaskId },
  93. }).catch((err) => {
  94. console.dir(err);
  95. });
  96. if (!task) return null;
  97. await TrackTaskDetail.update(
  98. {
  99. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  100. },
  101. {
  102. where: {
  103. id: task.id,
  104. },
  105. }
  106. );
  107. return task.dataValues;
  108. }
  109. export async function updateTrackTaskDetailStatus(data: {
  110. id: number;
  111. status: TrackTaskDetailStatusKey;
  112. }) {
  113. await TrackTaskDetail.update(
  114. {
  115. status: TRACK_TASK_DETAIL_STATUS[data.status],
  116. },
  117. {
  118. where: {
  119. id: data.id,
  120. },
  121. }
  122. );
  123. }