trackTask.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. schoolId: data.schoolId,
  36. },
  37. transaction: t,
  38. }
  39. );
  40. // 保存任务
  41. const task = await TrackTask.create(data, { transaction: t });
  42. details.forEach((item) => {
  43. item.trackTaskId = task.id;
  44. });
  45. await TrackTaskDetail.bulkCreate(details, { transaction: t });
  46. await t.commit();
  47. return task.dataValues;
  48. } catch (error) {
  49. await t.rollback();
  50. return Promise.reject(error);
  51. }
  52. }
  53. export async function finishAllUnfinishTask(schoolId: string) {
  54. await TrackTask.update(
  55. {
  56. status: TRACK_TASK_STATUS.FINISH,
  57. error: '强制结束',
  58. },
  59. {
  60. where: {
  61. status: { [Op.ne]: TRACK_TASK_STATUS.FINISH },
  62. schoolId,
  63. },
  64. }
  65. );
  66. }
  67. export async function updateTrackTaskStatus(data: {
  68. id: number;
  69. status: TrackTaskStatusKey;
  70. }) {
  71. await TrackTask.update(
  72. {
  73. status: TRACK_TASK_STATUS[data.status],
  74. },
  75. {
  76. where: {
  77. id: data.id,
  78. },
  79. }
  80. );
  81. }
  82. export async function createTrackTaskDetails(
  83. details: TrackTaskDetailCreationAttributes[]
  84. ) {
  85. const t = await sequelize.transaction();
  86. try {
  87. await TrackTaskDetail.bulkCreate(details, { transaction: t });
  88. await t.commit();
  89. return true;
  90. } catch (error) {
  91. await t.rollback();
  92. return Promise.reject(error);
  93. }
  94. }
  95. // track detail ------------------->
  96. export async function getTrackTaskDetailCount(data: {
  97. trackTaskId: number;
  98. status?: TrackTaskDetailStatusKey;
  99. }) {
  100. const count = await TrackTaskDetail.count({
  101. where: {
  102. trackTaskId: data.trackTaskId,
  103. ...(data.status ? { status: TRACK_TASK_DETAIL_STATUS[data.status] } : {}),
  104. },
  105. });
  106. return count;
  107. }
  108. export async function getUnfinishTrackTaskDetail(trackTaskId: number) {
  109. const task = await TrackTaskDetail.findOne({
  110. where: { status: TRACK_TASK_DETAIL_STATUS.INIT, trackTaskId },
  111. }).catch((err) => {
  112. console.dir(err);
  113. });
  114. if (!task) return null;
  115. await TrackTaskDetail.update(
  116. {
  117. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  118. },
  119. {
  120. where: {
  121. id: task.id,
  122. },
  123. }
  124. );
  125. return task.dataValues;
  126. }
  127. export async function updateTrackTaskDetailStatus(data: {
  128. id: number;
  129. status: TrackTaskDetailStatusKey;
  130. }) {
  131. await TrackTaskDetail.update(
  132. {
  133. status: TRACK_TASK_DETAIL_STATUS[data.status],
  134. },
  135. {
  136. where: {
  137. id: data.id,
  138. },
  139. }
  140. );
  141. }
  142. export async function releaseAllRunningTaskDetail(trackTaskId: number) {
  143. await TrackTaskDetail.update(
  144. {
  145. status: TRACK_TASK_DETAIL_STATUS.INIT,
  146. },
  147. {
  148. where: {
  149. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  150. trackTaskId,
  151. },
  152. }
  153. );
  154. }