123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- import { Op } from 'sequelize';
- import TrackTask, { TrackTaskCreationAttributes } from '../models/trackTask';
- import TrackTaskDetail, {
- TrackTaskDetailCreationAttributes,
- } from '../models/trackTaskDetail';
- import sequelize from '../sequelizeInstance';
- import {
- TrackTaskStatusKey,
- TrackTaskDetailStatusKey,
- TRACK_TASK_STATUS,
- TRACK_TASK_DETAIL_STATUS,
- } from '../enumerate';
- export async function getUnfinishTrackTask(schoolId: string) {
- const task = await TrackTask.findOne({
- where: { status: TRACK_TASK_STATUS.READY, schoolId },
- }).catch((err) => {
- console.dir(err);
- });
- return task ? task.dataValues : null;
- }
- export async function createTrackTask(
- data: TrackTaskCreationAttributes,
- details: TrackTaskDetailCreationAttributes[]
- ) {
- const t = await sequelize.transaction();
- try {
- await TrackTask.update(
- {
- status: TRACK_TASK_STATUS.FINISH,
- error: '补漏结束',
- },
- {
- where: {
- status: { [Op.ne]: TRACK_TASK_STATUS.FINISH },
- },
- transaction: t,
- }
- );
- // 保存任务
- const task = await TrackTask.create(data, { transaction: t });
- details.forEach((item) => {
- item.trackTaskId = task.id;
- });
- await TrackTaskDetail.bulkCreate(details, { transaction: t });
- await t.commit();
- return task.dataValues;
- } catch (error) {
- await t.rollback();
- return Promise.reject(error);
- }
- }
- export async function updateTrackTaskStatus(data: {
- id: number;
- status: TrackTaskStatusKey;
- }) {
- await TrackTask.update(
- {
- status: TRACK_TASK_STATUS[data.status],
- },
- {
- where: {
- id: data.id,
- },
- }
- );
- }
- export async function createTrackTaskDetails(
- details: TrackTaskDetailCreationAttributes[]
- ) {
- const t = await sequelize.transaction();
- try {
- await TrackTaskDetail.bulkCreate(details, { transaction: t });
- await t.commit();
- return true;
- } catch (error) {
- await t.rollback();
- return Promise.reject(error);
- }
- }
- // track detail ------------------->
- export async function getTrackTaskDetailCount(data: {
- trackTaskId: number;
- status?: TrackTaskDetailStatusKey;
- }) {
- const count = await TrackTaskDetail.count({
- where: data,
- });
- return count;
- }
- export async function getUnfinishTrackTaskDetail(trackTaskId: number) {
- const task = await TrackTaskDetail.findOne({
- where: { status: TRACK_TASK_DETAIL_STATUS.INIT, trackTaskId },
- }).catch((err) => {
- console.dir(err);
- });
- if (!task) return null;
- await TrackTaskDetail.update(
- {
- status: TRACK_TASK_DETAIL_STATUS.RUNNING,
- },
- {
- where: {
- id: task.id,
- },
- }
- );
- return task.dataValues;
- }
- export async function updateTrackTaskDetailStatus(data: {
- id: number;
- status: TrackTaskDetailStatusKey;
- }) {
- await TrackTaskDetail.update(
- {
- status: TRACK_TASK_DETAIL_STATUS[data.status],
- },
- {
- where: {
- id: data.id,
- },
- }
- );
- }
|