<?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.ruoyi.oa.mapper.CmcStudyMapper">
    
    <resultMap type="CmcStudy" id="CmcStudyResult">
        <result property="studyId"    column="study_id"    />
        <result property="resourceId"    column="resource_id"    />
        <result property="userId"    column="user_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="lastPoint"    column="last_point"    />
        <result property="lastTime"    column="last_time"    />
        <result property="getHours"    column="get_hours"    />
        <association property="user"    javaType="SysUser"         resultMap="UserResult" />
        <association property="dept"    javaType="SysDept"         resultMap="SysDeptResult" />
        <association property="resource"    javaType="CmcResource"         resultMap="CmcResourceResult" />
    </resultMap>

    <resultMap type="SysUser" id="UserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <resultMap type="SysDept" id="SysDeptResult">
        <result property="deptId"    column="dept_id"    />
        <result property="deptName"    column="dept_name"    />
    </resultMap>

    <resultMap type="CmcResource" id="CmcResourceResult">
        <result property="resourceId"    column="resource_id"    />
        <result property="title"    column="title"    />
        <result property="type"    column="type"    />
        <result property="sourcePath"    column="source_path"    />
        <result property="hours"    column="hours"    />
    </resultMap>

    <sql id="selectCmcStudyVo">
        select s.study_id, s.resource_id, r.title, r.type, r.source_path, r.hours, s.user_id, u.nick_name, d.dept_name, s.last_point, s.last_time, s.get_hours from cmc_study as s
        left join sys_user as u on u.user_id = s.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        left join cmc_resource as r on r.resource_id = s.resource_id
    </sql>

    <select id="selectCmcStudyList" parameterType="CmcStudy" resultMap="CmcStudyResult">
        <include refid="selectCmcStudyVo"/>
        <where>  
            <if test="resourceId != null "> and s.resource_id = #{resourceId}</if>
            <if test="userId != null "> and s.user_id = #{userId}</if>
            <if test="lastPoint != null  and lastPoint != ''"> and s.last_point = #{lastPoint}</if>
            <if test="lastTime != null "> and YEAR(s.last_time) = YEAR(#{lastTime})</if>
            <if test="getHours != null "> and s.get_hours = #{getHours}</if>
            <if test="title != null "> and r.title like concat('%', #{title}, '%')</if>
            <if test="type != null "> and r.type like concat('%', #{type}, '%')</if>
        </where>
        order by s.last_time desc
    </select>

    <select id="selectCmcStudyStatistic" parameterType="CmcStudy" resultMap="CmcStudyResult">
        select t.user_id, t.dept_id, t.nick_name, t.dept_name, sum(t.get_hours) as get_hours, t.status, t.last_time from
        (select t1.* from (select  u.user_id, u.dept_id, u.nick_name, d.dept_name, sum(s.get_hours) as get_hours, u.status, s.last_time as last_time from sys_user as u
        left join cmc_study as s on u.user_id = s.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        <where>
            u.status = '0' and u.user_id != 1
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            <if test="lastTime != null "> and YEAR(s.last_time) = YEAR(#{lastTime})</if>
        </where>
        group by u.user_id) as t1
        union
        select t2.* from (select u.user_id, u.dept_id, u.nick_name, d.dept_name, sum(ta.hours) as get_hours, u.status, ta.dept_time as last_time from sys_user as u
        left join cmc_train_approval as ta on ta.user_id = u.user_id
        left join sys_dept as d on d.dept_id = u.dept_id
        <where>
            u.status = '0' and u.user_id != 1 and ta.dept_time is not null
            <if test="deptId != null "> and u.dept_id = #{deptId}</if>
            <if test="lastTime != null "> and YEAR(ta.dept_time) = YEAR(#{lastTime})</if>
        </where>
        group by u.user_id) as t2) as t
        group by t.user_id
        order by get_hours desc, user_id
    </select>

    <select id="selectCmcStudyByStudyId" parameterType="Integer" resultMap="CmcStudyResult">
        <include refid="selectCmcStudyVo"/>
        where study_id = #{studyId}
    </select>
        
    <insert id="insertCmcStudy" parameterType="CmcStudy" useGeneratedKeys="true" keyProperty="studyId">
        insert into cmc_study
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="resourceId != null">resource_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="lastPoint != null">last_point,</if>
            <if test="lastTime != null">last_time,</if>
            <if test="getHours != null">get_hours,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="resourceId != null">#{resourceId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="lastPoint != null">#{lastPoint},</if>
            <if test="lastTime != null">#{lastTime},</if>
            <if test="getHours != null">#{getHours},</if>
         </trim>
    </insert>

    <update id="updateCmcStudy" parameterType="CmcStudy">
        update cmc_study
        <trim prefix="SET" suffixOverrides=",">
            <if test="resourceId != null">resource_id = #{resourceId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="lastPoint != null">last_point = #{lastPoint},</if>
            <if test="lastTime != null">last_time = #{lastTime},</if>
            <if test="getHours != null">get_hours = #{getHours},</if>
        </trim>
        where study_id = #{studyId}
    </update>

    <delete id="deleteCmcStudyByStudyId" parameterType="Integer">
        delete from cmc_study where study_id = #{studyId}
    </delete>

    <delete id="deleteCmcStudyByStudyIds" parameterType="String">
        delete from cmc_study where study_id in 
        <foreach item="studyId" collection="array" open="(" separator="," close=")">
            #{studyId}
        </foreach>
    </delete>
</mapper>