trackTask.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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 getUnfinishTrackTaskDetails(
  128. trackTaskId: number,
  129. count = 10
  130. ) {
  131. const t = await sequelize.transaction();
  132. try {
  133. const limitCount = Math.max(10, Math.min(100, count));
  134. const rows = await TrackTaskDetail.findAll({
  135. where: { status: TRACK_TASK_DETAIL_STATUS.INIT, trackTaskId },
  136. limit: limitCount,
  137. transaction: t,
  138. });
  139. if (rows.length) {
  140. await TrackTaskDetail.update(
  141. {
  142. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  143. },
  144. {
  145. where: {
  146. id: {
  147. [Op.in]: rows.map((row) => row.dataValues.id),
  148. },
  149. },
  150. transaction: t,
  151. }
  152. );
  153. }
  154. await t.commit();
  155. return rows.map((row) => row.dataValues);
  156. } catch (error) {
  157. await t.rollback();
  158. return Promise.reject(error);
  159. }
  160. }
  161. export async function updateTrackTaskDetailStatus(data: {
  162. id: number;
  163. status: TrackTaskDetailStatusKey;
  164. }) {
  165. await TrackTaskDetail.update(
  166. {
  167. status: TRACK_TASK_DETAIL_STATUS[data.status],
  168. },
  169. {
  170. where: {
  171. id: data.id,
  172. },
  173. }
  174. );
  175. }
  176. export async function releaseAllRunningTaskDetail(trackTaskId: number) {
  177. await TrackTaskDetail.update(
  178. {
  179. status: TRACK_TASK_DETAIL_STATUS.INIT,
  180. },
  181. {
  182. where: {
  183. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  184. trackTaskId,
  185. },
  186. }
  187. );
  188. }
  189. export async function releaseAllRunningTaskDetailByIds(
  190. trackTaskDetailIds: number[]
  191. ) {
  192. await TrackTaskDetail.update(
  193. {
  194. status: TRACK_TASK_DETAIL_STATUS.INIT,
  195. },
  196. {
  197. where: {
  198. status: TRACK_TASK_DETAIL_STATUS.RUNNING,
  199. id: {
  200. [Op.in]: trackTaskDetailIds,
  201. },
  202. },
  203. }
  204. );
  205. }