<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yida.data.school.secondClass.mapper.EduSecondClassCourseMapper">
  <resultMap id="courseDetail" type="com.yida.data.school.vo.secondClass.CourseH5DetailVO">
    <result column="name" property="name"/>
    <result column="visit_num" property="visitNum"/>
    <result column="remark" property="remark"/>
    <collection property="contentList" ofType="com.yida.data.school.vo.secondClass.CourseContentVO">
      <result property="name" column="cName"/>
      <result property="fileUrl" column="cFileUrl"/>
      <result property="fileCoverUrl" column="cFileCoverUrl"/>
    </collection>
    <collection property="lecturerList" ofType="com.yida.data.school.vo.secondClass.CourseLecturerVO">
      <result column="lName" property="name"/>
      <result column="lAvatar" property="avatar"/>
      <result column="lRemark" property="remark"/>
    </collection>
  </resultMap>
  <select id="listCourse" resultType="com.yida.data.school.vo.secondClass.CourseVO">
    select a.id,
           a.name,
           a.remark,
           a.cover_url,
           a.type,
           a.status,
           a.home_recommend,
           a.subject_id,
    a.visit_num,
           group_concat(distinct c.name) lecturerName,
           ifnull(sum(b.duration), 0)    duration
    from edu_second_class_course a
           left join edu_second_class_course_content b on a.id = b.course_id and b.del_flag = 0
           left join edu_second_class_course_lecturer c on b.lecturer_id = c.id and c.del_flag = 0
      where a.del_flag = 0
    <if test="dto.deptId != null">
      and a.dept_id = #{dto.deptId}
    </if>
    <if test="dto.lecturerId != null">
      and a.id in (select d.course_id
                   from edu_second_class_course_content d
                          left join edu_second_class_course_lecturer e on d.lecturer_id = e.id
                   where d.del_flag = 0
                     and e.del_flag = 0
                     and d.lecturer_id = #{dto.lecturerId}
        )
    </if>
    <if test="dto.name != null and dto.name != ''">
      and a.name like concat('%', #{dto.name}, '%')
    </if>
    <if test="dto.status != null">
      and a.status = #{dto.status}
    </if>
    <if test="dto.homeRecommed != null">
      and a.home_recommend = #{dto.homeRecommed}
    </if>
    <if test="dto.subjectId != null">
      and a.subject_id = #{dto.subjectId}
    </if>
    group by a.id, a.name, a.remark, a.type, a.status, a.home_recommend, a.create_date, a.visit_num, a.subject_id
    <choose>
      <when test="dto.sortType != null and dto.sortType == 1">
        order by a.visit_num desc
      </when>
      <otherwise>
        order by a.create_date desc
      </otherwise>
    </choose>
    <if test="dto.limit != null">
      limit #{limit}
    </if>
  </select>

  <select id="getInfo" resultType="com.yida.data.school.vo.secondClass.CourseVO">
    select id, name, remark, type, cover_url
    from edu_second_class_course
    where del_flag = 0
      and id = #{id}
  </select>

  <select id="getCourseDetail" resultMap="courseDetail">
    select a.name,
           a.visit_num,
           a.remark,
           c.name           lName,
           c.avatar         lAvatar,
           c.remark         lRemark,
           b.name           cName,
           b.file_url       cFileUrl,
           b.file_cover_url cFileCoverUrl
    from edu_second_class_course a
           left join edu_second_class_course_content b on a.id = b.course_id and b.del_flag = 0
           left join edu_second_class_course_lecturer c on b.lecturer_id = c.id and c.del_flag = 0
    where a.del_flag = 0
      and a.id = #{id}
  </select>

  <update id="incrVisitNum">
    update edu_second_class_course
    set visit_num=visit_num + 1
    where del_flag = 0
      and id = #{id}
  </update>

  <select id="listCoursePageBySubject" resultType="com.yida.data.school.vo.secondClass.CourseVO">
    select a.id,
           a.name,
           a.cover_url,
           a.visit_num,
           group_concat(distinct c.name) lecturerName
    from edu_second_class_course a
           left join edu_second_class_course_content b on a.id = b.course_id and b.del_flag = 0
           left join edu_second_class_course_lecturer c on b.lecturer_id = c.id and c.del_flag = 0
      where a.del_flag = 0
        and a.dept_id = #{deptId}
    <if test="subjectId != null">
      and a.subject_id = #{subjectId}
    </if>
    and a.status = 1
      group by a.id, a.name, a.cover_url, a.visit_num
      order by a.visit_num desc, a.id
  </select>
</mapper>
