<?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.sundot.airport.system.mapper.SysLearningGrowthMapper">

    <resultMap type="SysLearningGrowth" id="SysLearningGrowthResult">
        <result property="id" column="id"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="userCode" column="user_code"/>
        <result property="userName" column="user_name"/>
        <result property="examCode" column="exam_code"/>
        <result property="examName" column="exam_name"/>
        <result property="examTime" column="exam_time"/>
        <result property="examScore" column="exam_score"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSysLearningGrowthVo">
        select id,
               create_by,
               create_time,
               update_by,
               update_time,
               user_code,
               user_name,
               exam_code,
               exam_name,
               exam_time,
               exam_score,
               remark
        from sys_learning_growth
    </sql>

    <select id="selectSysLearningGrowthList" parameterType="SysLearningGrowth" resultMap="SysLearningGrowthResult">
        <include refid="selectSysLearningGrowthVo"/>
        <where>
            <if test="userCode != null  and userCode != ''">and user_code = #{userCode}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="examCode != null  and examCode != ''">and exam_code = #{examCode}</if>
            <if test="examName != null  and examName != ''">and exam_name like concat('%', #{examName}, '%')</if>
            <if test="examTime != null ">and DATE(exam_time) = DATE(#{examTime})</if>
            <if test="examScore != null ">and exam_score = #{examScore}</if>
            <if test="startTime != null and endTime != null">
                and DATE(exam_time) BETWEEN DATE(#{startTime}) AND DATE(#{endTime})
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectSysLearningGrowthById" parameterType="Long" resultMap="SysLearningGrowthResult">
        <include refid="selectSysLearningGrowthVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysLearningGrowth" parameterType="SysLearningGrowth" useGeneratedKeys="true" keyProperty="id">
        insert into sys_learning_growth
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="userCode != null and userCode != ''">user_code,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="examCode != null and examCode != ''">exam_code,</if>
            <if test="examName != null and examName != ''">exam_name,</if>
            <if test="examTime != null">exam_time,</if>
            <if test="examScore != null">exam_score,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="userCode != null and userCode != ''">#{userCode},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="examCode != null and examCode != ''">#{examCode},</if>
            <if test="examName != null and examName != ''">#{examName},</if>
            <if test="examTime != null">#{examTime},</if>
            <if test="examScore != null">#{examScore},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateSysLearningGrowth" parameterType="SysLearningGrowth">
        update sys_learning_growth
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="userCode != null and userCode != ''">user_code = #{userCode},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="examCode != null and examCode != ''">exam_code = #{examCode},</if>
            <if test="examName != null and examName != ''">exam_name = #{examName},</if>
            <if test="examTime != null">exam_time = #{examTime},</if>
            <if test="examScore != null">exam_score = #{examScore},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysLearningGrowthById" parameterType="Long">
        delete
        from sys_learning_growth
        where id = #{id}
    </delete>

    <delete id="deleteSysLearningGrowthByIds" parameterType="String">
        delete from sys_learning_growth where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectSysLearningGrowthListByCondition"
            resultType="com.sundot.airport.system.domain.SysLargeScreenLearningGrowthSqlDto">
        select sd_zj.dept_id zjDeptId,
        sd_zj.dept_name zjDeptName,
        sd_dd.dept_id ddDeptId,
        sd_dd.dept_name ddDeptName,
        sd_ks.dept_id ksDeptId,
        sd_ks.dept_name ksDeptName,
        sd_bz.dept_id bzDeptId,
        sd_bz.dept_name bzDeptName,
        su.user_id userId,
        su.user_name userCode,
        su.nick_name userName,
        slg.exam_code examCode,
        slg.exam_name examName,
        slg.exam_score examScore,
        slg.exam_time examTime
        from sys_learning_growth slg
        inner join sys_user su on su.user_name = slg.user_code
        inner join sys_dept sd_bz on sd_bz.dept_id = su.dept_id
        inner join sys_dept sd_ks on sd_ks.dept_id = sd_bz.parent_id
        inner join sys_dept sd_dd on sd_dd.dept_id = sd_ks.parent_id
        inner join sys_dept sd_zj on sd_zj.dept_id = sd_dd.parent_id
        where 1 = 1
        <if test="userId != null">
            and su.user_id=#{userId}
        </if>
        <if test="teamId != null">
            and sd_bz.dept_id=#{teamId}
        </if>
        <if test="departmentId != null">
            and sd_ks.dept_id=#{departmentId}
        </if>
        <if test="brigadeId != null">
            and sd_dd.dept_id=#{brigadeId}
        </if>
        <if test="specifiedDate != null">
            and slg.exam_time=#{specifiedDate}
        </if>
        <if test="startDate != null and endDate != null">
            and (slg.exam_time >= #{startDate}
            and slg.exam_time <![CDATA[ < ]]> date_add(#{endDate} , interval 1 day))
        </if>
    </select>

    <select id="organizationalSupport"
            resultType="com.sundot.airport.system.domain.SysLargeScreenLearningGrowthOrganizationalSupportDto">
        select slg.exam_code code,
        slg.exam_name name,
        date_format(slg.exam_time, '%Y-%m-%d') date,
        round(avg(slg.exam_score), 2) number
        from sys_learning_growth slg
        inner join sys_user su on su.user_name = slg.user_code
        inner join sys_dept sd_bz on sd_bz.dept_id = su.dept_id
        inner join sys_dept sd_ks on sd_ks.dept_id = sd_bz.parent_id
        inner join sys_dept sd_dd on sd_dd.dept_id = sd_ks.parent_id
        inner join sys_dept sd_zj on sd_zj.dept_id = sd_dd.parent_id
        where 1 = 1
        <if test="userId != null">
            and su.user_id=#{userId}
        </if>
        <if test="brigadeId != null">
            and sd_dd.dept_id=#{brigadeId}
        </if>
        <if test="departmentId != null">
            and sd_ks.dept_id=#{departmentId}
        </if>
        <if test="teamId != null">
            and sd_bz.dept_id=#{teamId}
        </if>
        <if test="specifiedDate != null">
            and slg.exam_time=#{specifiedDate}
        </if>
        <if test="startDate != null and endDate != null">
            and (slg.exam_time >= #{startDate}
            and slg.exam_time <![CDATA[ < ]]> date_add(#{endDate} , interval 1 day))
        </if>
        group by slg.exam_code, slg.exam_name, date_format(slg.exam_time, '%Y-%m-%d')
        order by date_format(slg.exam_time, '%Y-%m-%d') asc
    </select>
</mapper>