<?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.zcloud.mapper.datasource.task.StageExamMapper">

    <!--表名 -->
    <sql id="tableName">
        BUS_STAGEEXAM
    </sql>

    <!--数据字典表名 -->
    <sql id="dicTableName">
        SYS_DICTIONARIES
    </sql>

    <!-- 字段 -->
    <sql id="Field">
        f
        .
        CURRICULUM_ID
        ,
		f.STAGEEXAMPAPER_ID,
		f.USER_ID,
		f.EXAMTIMEBEGIN,
		f.EXAMTIMEEND,
		f.EXAMQUESTIONNUM,
		f.EXAMQUESTIONRIGHT,
		f.EXAMQUESTIONWRONG,
		f.EXAMSCORE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.STUDYTASK_ID,
		f.STAGEEXAM_ID
    </sql>

    <!-- 字段用于新增 -->
    <sql id="Field2">
        CURRICULUM_ID
        ,
		STAGEEXAMPAPER_ID,
		USER_ID,
		EXAMTIMEBEGIN,
		EXAMTIMEEND,
		EXAMQUESTIONNUM,
		EXAMQUESTIONRIGHT,
		EXAMQUESTIONWRONG,
		EXAMSCORE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		CORPINFO_ID,
		STUDYTASK_ID,
		STAGEEXAM_ID
    </sql>

    <!-- 字段值 -->
    <sql id="FieldValue">
        #{CURRICULUM_ID}
        ,
        #{STAGEEXAMPAPER_ID},
        #{USER_ID},
        #{EXAMTIMEBEGIN},
        #{EXAMTIMEEND},
        #{EXAMQUESTIONNUM},
        #{EXAMQUESTIONRIGHT},
        #{EXAMQUESTIONWRONG},
        #{EXAMSCORE},
        #{CREATOR},
        #{CREATTIME},
        #{OPERATOR},
        #{OPERATTIME},
        #{ISDELETE},
        #{CORPINFO_ID},
        #{STUDYTASK_ID},
        #{STAGEEXAM_ID}
    </sql>

    <!-- 新增-->
    <insert id="save" parameterType="pd">
        insert into
        <include refid="tableName"></include>
        (
        <include refid="Field2"></include>
        ) values (
        <include refid="FieldValue"></include>
        )
    </insert>

    <!-- 删除-->
    <delete id="delete" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1',
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        STAGEEXAM_ID = #{STAGEEXAM_ID}
    </delete>

    <!-- 修改 -->
    <update id="edit" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        CURRICULUM_ID = #{CURRICULUM_ID},
        STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID},
        USER_ID = #{USER_ID},
        EXAMTIMEBEGIN = #{EXAMTIMEBEGIN},
        EXAMTIMEEND = #{EXAMTIMEEND},
        EXAMQUESTIONNUM = #{EXAMQUESTIONNUM},
        EXAMQUESTIONRIGHT = #{EXAMQUESTIONRIGHT},
        EXAMQUESTIONWRONG = #{EXAMQUESTIONWRONG},
        EXAMSCORE = #{EXAMSCORE},
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME},
        STAGEEXAM_ID = STAGEEXAM_ID
        where
        STAGEEXAM_ID = #{STAGEEXAM_ID}
    </update>

    <!-- 通过ID获取数据 -->
    <select id="findById" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        ,u.NAME USERNAME
        ,p.EXAMNAME
        ,p.EXAMTIME
        ,p.ANSWERSHEETTIME
        ,p.EXAMSCORE
        ,p.CURRICULUMUSERS
        ,p.QUESTIONNUM
        from
        <include refid="tableName"></include>
        f
        LEFT JOIN sys_user u ON u.USER_ID = f.USER_ID
        LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID
        where
        f.STAGEEXAM_ID = #{STAGEEXAM_ID}
    </select>

    <!-- 列表 -->
    <select id="datalistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            <!--	根据需求自己加检索条件
                字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
                 or
                字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            -->
            )
        </if>
        ORDER BY f.OPERATTIME DESC
    </select>

    <!-- 列表(全部) -->
    <select id="listAll" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
    </select>

    <!-- 批量删除 -->
    <delete id="deleteAll" parameterType="pd">
        update
        <include refid="tableName"></include>
        set
        ISDELETE = '1',
        OPERATOR = #{OPERATOR},
        OPERATTIME = #{OPERATTIME}
        where
        STAGEEXAM_ID in
        <foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    <!-- 列表(根据所选ID获取数据) -->
    <select id="findByIds" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        from
        <include refid="tableName"></include>
        f
        where
        STAGEEXAM_ID in
        <foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <!-- 通过ID获取数据 -->
    <select id="findByPaperUser" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        ,u.NAME USERNAME
        ,p.EXAMNAME
        ,p.EXAMTIME
        ,p.ANSWERSHEETTIME
        ,p.EXAMSCORE
        ,p.CURRICULUMUSERS
        ,p.QUESTIONNUM
        from
        <include refid="tableName"></include>
        f
        LEFT JOIN sys_user u ON u.USER_ID = f.USER_ID
        LEFT JOIN BUS_STAGEEXAMPAPER p ON p.STAGEEXAMPAPER_ID = f.STAGEEXAMPAPER_ID
        where
        f.STUDYTASK_ID = #{STUDYTASK_ID} AND f.STAGEEXAMPAPER_ID = #{STAGEEXAMPAPER_ID} AND f.USER_ID = #{USER_ID}
        ORDER BY f.CREATTIME desc
        LIMIT 1
    </select>
    <!-- 根据任务id获取全部用户的全部考试成绩 -->
    <select id="getUserExamScoreByStudystaskId" parameterType="pd" resultType="pd">
        select
        <include refid="Field"></include>
        ,(select r.USER_SIGN_PATH from BUS_STAGESTUDENTRELATION r where r.ISDELETE = '0' and r.STUDYTASK_ID =
        f.STUDYTASK_ID and r.USER_ID = f.USER_ID) AS USER_SIGN_PATH
        from
        <include refid="tableName"></include>
        f
        where f.ISDELETE = '0' and f.CORPINFO_ID = #{CORPINFO_ID}
        <if test="STUDYTASK_ID!= null and STUDYTASK_ID != ''"><!-- 关键词检索 -->
            and f.STUDYTASK_ID =#{STUDYTASK_ID}
        </if>
        <if test="USER_ID!= null and USER_ID != ''"><!-- 关键词检索 -->
            and f.USER_ID =#{USER_ID}
        </if>
    </select>
    <select id="studyIndex" resultType="com.zcloud.entity.PageData">
        SELECT
        c.CORPINFO_ID,
        IFNULL(s.task_num,0)TASK_NUM,
        IFNULL( s.SUM_CLASSHOUR, 0 ) SUM_CLASSHOUR,
        IFNULL( s.userCount, 0 ) USER_COUNT,
        IFNULL( s.hg, 0 ) HG,
        IFNULL( s.cj, 0 ) CJ
        FROM
        bus_corp_info c
        LEFT JOIN (
        SELECT
        f.CORPINFO_ID,
        count(f.STUDYTASK_ID) task_num,
        SUM( f.SUM_CLASSHOUR ) SUM_CLASSHOUR,
        SUM(( SELECT count(*) FROM bus_stagestudentrelation s1 WHERE s1.STUDYTASK_ID = f.STUDYTASK_ID AND s1.ISDELETE =
        '0' ) ) AS userCount,
        SUM((
        SELECT
        count(*)
        FROM
        bus_stagestudentrelation s2
        WHERE
        s2.STUDYTASK_ID = f.STUDYTASK_ID
        AND s2.ISDELETE = '0'
        AND CAST( st.PASSSCORE AS DECIMAL ) &lt;= CAST( s2.STAGEEXAMSCORE AS DECIMAL ) AND s2.STAGEEXAMSCORE > '-1'
        )) AS hg,
        SUM((
        SELECT
        count(*)
        FROM
        bus_stagestudentrelation s3
        WHERE
        s3.STUDYTASK_ID = f.STUDYTASK_ID
        AND s3.ISDELETE = '0'
        AND s3.STAGEEXAMSCORE > '-1'
        )) AS cj
        FROM
        BUS_STUDYTASK f
        LEFT JOIN BUS_STAGEEXAMPAPER st ON st.STUDYTASK_ID = f.STUDYTASK_ID
        WHERE
        f.ISDELETE = 0
        <if test="year != null and year != ''">
            AND YEAR(STR_TO_DATE( `PEIXUE_START_TIME`, '%Y-%m-%d' )) = YEAR(CURDATE())
        </if>
        <if test="current != null and current != ''">
            AND (
            STR_TO_DATE(`PEIXUE_START_TIME`, '%Y-%m-%d') &lt;= CURDATE()
            AND STR_TO_DATE(`PEIXUE_END_TIME`, '%Y-%m-%d') >= CURDATE()
            )
        </if>
        GROUP BY
        f.CORPINFO_ID
        ) s ON s.CORPINFO_ID = c.CORPINFO_ID
        WHERE
        c.ISDELETE = 0 and c.CORPINFO_ID = #{CORPINFO_ID}
    </select>
    <select id="trainingData" resultType="com.zcloud.entity.PageData">
        SELECT c.CORPINFO_ID,
               IFNULL(st.TASK_NUM, 0)      TASK_NUM,
               IFNULL(s.startTaskCount, 0) START_TASK_COUNT
        FROM bus_corp_info c
                 LEFT JOIN (SELECT f.CORPINFO_ID,
                                   COUNT(1) startTaskCount
                            FROM BUS_STUDYTASK f
                                     LEFT JOIN BUS_STAGEEXAMPAPER st ON st.STUDYTASK_ID = f.STUDYTASK_ID
                            WHERE f.ISDELETE = 0
                              AND (STR_TO_DATE(`PEIXUE_START_TIME`, '%Y-%m-%d') &lt;= CURDATE() AND
                                   STR_TO_DATE(`PEIXUE_END_TIME`, '%Y-%m-%d') >= CURDATE())
                            GROUP BY f.CORPINFO_ID) s ON s.CORPINFO_ID = c.CORPINFO_ID
                 LEFT JOIN (SELECT COUNT(1) TASK_NUM, st.CORPINFO_ID
                            FROM BUS_STUDYTASK st
                            WHERE st.ISDELETE = 0
                            GROUP BY st.CORPINFO_ID) st ON st.CORPINFO_ID = c.CORPINFO_ID
        WHERE c.ISDELETE = 0
          AND c.CORPINFO_ID = #{CORPINFO_ID}
    </select>
    <select id="postTypeIndex" resultType="com.zcloud.entity.PageData">
        SELECT d.`NAME`                     NAME,
               IFNULL(st.POSTTYPE_COUNT, 0) POSTTYPE_COUNT
        FROM sys_dictionaries d
                 LEFT JOIN (SELECT f.CORPINFO_ID,
                                   d.`NAME`          POSTTYPE_NAME,
                                   d.DICTIONARIES_ID,
                                   COUNT(f.POSTTYPE) POSTTYPE_COUNT
                            FROM BUS_STUDYTASK f
                                     LEFT JOIN sys_dictionaries d ON f.POSTTYPE = d.DICTIONARIES_ID
                            WHERE f.ISDELETE = 0
                              AND f.CORPINFO_ID = #{CORPINFO_ID}
                            GROUP BY f.POSTTYPE) st ON d.DICTIONARIES_ID = st.DICTIONARIES_ID
        WHERE d.PARENT_ID = 'f6a7c4f5602f46e291d06b1390a3f820'
    </select>
    <select id="getStudyStartIndexData" resultType="com.zcloud.entity.PageData">
        SELECT
        <if test="dept != null and dept != ''">
            d.NAME,
        </if>
        <if test="usr != null and usr != ''">
            u.NAME,
        </if>
        IFNULL(SUM(stl.COMPLETE_CLASSHOUR), 0) AS COMPLETE_CLASSHOUR,
        IFNULL(SUM(s.SUM_CLASSHOUR), 0) AS SUM_CLASSHOUR
        FROM
        sys_user u
        LEFT JOIN oa_department d on u.DEPARTMENT_ID = d.DEPARTMENT_ID
        LEFT JOIN bus_stagestudentrelation stl ON u.USER_ID = stl.USER_ID AND stl.ISDELETE = 0
        LEFT JOIN bus_studytask s ON stl.STUDYTASK_ID = s.STUDYTASK_ID AND s.ISDELETE = 0
        WHERE u.CORPINFO_ID = #{CORPINFO_ID} and u.ISMAIN = 0 and u.ISSTUDENT = 'true' and u.isdelete =0
        GROUP BY
        <if test="usr != null and usr != ''">
            u.USER_ID
        </if>
        <if test="dept != null and dept != ''">
            u.DEPARTMENT_ID
        </if>
        ORDER BY
        COMPLETE_CLASSHOUR DESC,SUM_CLASSHOUR DESC
    </select>

</mapper>
