import { DataSource, Repository } from 'typeorm';
import { CourseList } from 'src/online-course/entities/course-manage/course-list.entity';
import { CourseStudyRecord } from 'src/online-course/entities/course-my/course-study-record.entity';
import { CourseStudyExercise } from 'src/online-course/entities/course-my/course-study-exercise.entity';
import { myCourseService } from 'src/online-course/service/my-course.service';

export async function handler(dataSource: DataSource, { user }) {
  const res: any = await dataSource
    .createQueryBuilder(CourseList, 'course')
    .leftJoinAndMapOne(
      'course.record',
      CourseStudyRecord,
      'record',
      `record.del_flag = 0 AND record.courseId = course.id AND record.student_id = '${user.userId}'`,
    )
    .leftJoinAndMapMany(
      'record.exercise',
      CourseStudyExercise,
      'exercise',
      'exercise.recordId = record.id and exercise.del_flag = 0',
    )
    .andWhere({ delFlag: 0, studyOrgId: +user.orgId })
    .getManyAndCount();

  const needAdd = [];
  res[0].forEach((course) => {
    if (!course.record) {
      const newRecord: any = {
        studentId: user.userId,
        courseId: course.id,
        studyTime: 0,
        studyProgress: 1,
        exerciseGrade: 0,
        selfEvael: '',
        courseEveal: '',
        score: 0,
        createTime: course.createTime,
        creator: course.creator,
        delFlag: 0,
        updater: course.updater,
        remarks: null,
        version: 1,
        id: undefined,
      };
      course.record = newRecord;
      needAdd.push(newRecord);
    }
  });
  needAdd.length && (await dataSource.manager.save(CourseStudyRecord, needAdd));

  const { all, no, start, over } = (await dataSource.query(sqlString(user)))[0];
  const data = [];
  data.push({
    item: '学习中',
    count: start,
    percent: +(start / all).toFixed(2),
  });
  data.push({
    item: '未开始',
    count: no,
    percent: +(no / all).toFixed(2),
  });
  data.push({
    item: '已完成',
    count: over,
    percent: +(over / all).toFixed(2),
  });
  return data;
}
const sqlString = ({ orgId, userId }) => {
  return `
  SELECT (
    SELECT
      COUNT( course_study_record.id ) AS countList
    FROM
      course_study_record
      INNER JOIN course_list ON course_study_record.course_id = course_list.id 
    WHERE
      course_list.study_org_id = ${orgId}
      AND course_list.del_flag = 0 
      AND course_study_record.del_flag = 0 
      AND course_study_record.student_id = '${userId}' 
      AND course_study_record.study_progress = 1 ) as 'no',(
    
    SELECT
      COUNT( course_study_record.id ) AS aaa 
    FROM
      course_study_record
      INNER JOIN course_list ON course_study_record.course_id = course_list.id 
    WHERE
      course_list.study_org_id = ${orgId}
      AND course_list.del_flag = 0 
      AND course_study_record.del_flag = 0 
      AND course_study_record.student_id = '${userId}' 
      AND course_study_record.study_progress = 2 ) as 'start',
    
    
    (
    SELECT
      COUNT( course_study_record.id ) AS aaa 
    FROM
      course_study_record
      INNER JOIN course_list ON course_study_record.course_id = course_list.id 
    WHERE
      course_list.study_org_id = ${orgId}
      AND course_list.del_flag = 0 
      AND course_study_record.del_flag = 0 
      AND course_study_record.student_id = '${userId}' 
      AND course_study_record.study_progress = 3) as 'over',
    
    (
    SELECT
      COUNT(course_list.id)
    FROM
      course_list
    WHERE
      course_list.study_org_id = ${orgId} AND
      course_list.del_flag = 0
      ) as 'all'
  `;
};
