<?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.web.modules.technology.mapper.BizPracticalTrainingMemberMapper">

    <resultMap type="com.ruoyi.web.modules.technology.domain.BizPracticalTrainingMember" id="BizPracticalTrainingMemberResult">
        <id     property="id"           column="id"/>
        <result property="trainingId"   column="training_id"/>
        <result property="memberName"   column="member_name"/>
        <result property="sysUserId"    column="sys_user_id"/>
        <result property="enrollStatus" column="enroll_status"/>
        <result property="enrollTime"   column="enroll_time"/>
        <result property="cancelTime"   column="cancel_time"/>
        <result property="delFlag"      column="del_flag"/>
        <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="remark"       column="remark"/>
    </resultMap>

    <select id="selectByTrainingIdAndUserId" resultMap="BizPracticalTrainingMemberResult">
        select id, training_id, member_name, sys_user_id, enroll_status, enroll_time, cancel_time,
               del_flag, create_by, create_time, update_by, update_time, remark
        from biz_practical_training_member
        where training_id = #{trainingId}
          and sys_user_id = #{sysUserId}
          and del_flag = '0'
        limit 1
    </select>

    <select id="countActiveByTrainingId" resultType="int">
        select count(1)
        from biz_practical_training_member
        where training_id = #{trainingId}
          and enroll_status = 0
          and del_flag = '0'
    </select>

    <insert id="insertBizPracticalTrainingMember" parameterType="com.ruoyi.web.modules.technology.domain.BizPracticalTrainingMember" useGeneratedKeys="true" keyProperty="id">
        insert into biz_practical_training_member (
            training_id, member_name, sys_user_id, enroll_status, enroll_time, cancel_time,
            del_flag, create_by, create_time, remark
        ) values (
            #{trainingId}, #{memberName}, #{sysUserId}, #{enrollStatus}, #{enrollTime}, #{cancelTime},
            #{delFlag}, #{createBy}, sysdate(), #{remark}
        )
    </insert>

    <resultMap type="com.ruoyi.web.modules.technology.domain.PracticalTrainingMyEnrollListVo" id="MyEnrollListVoResult">
        <id     property="id"                    column="id"/>
        <result property="trainingTopic"         column="training_topic"/>
        <result property="trainingTime"          column="training_time"/>
        <result property="trainingIntro"         column="training_intro"/>
        <result property="coverFileUrl"          column="cover_file_url"/>
        <result property="trainingStatus"        column="training_status"/>
        <result property="actualEnrolledCount"   column="actual_enrolled_count"/>
        <result property="plannedHeadCount"      column="planned_head_count"/>
        <result property="registrationEndTime"   column="registration_end_time"/>
        <result property="enrollTime"              column="enroll_time"/>
    </resultMap>

    <resultMap type="com.ruoyi.web.modules.technology.domain.PracticalTrainingMyEnrollDetailVo" id="MyEnrollDetailVoResult">
        <id     property="id"                    column="id"/>
        <result property="trainingTopic"         column="training_topic"/>
        <result property="trainingTime"          column="training_time"/>
        <result property="trainingIntro"         column="training_intro"/>
        <result property="plannedHeadCount"      column="planned_head_count"/>
        <result property="trainingLocation"        column="training_location"/>
        <result property="expectedOutcome"         column="expected_outcome"/>
        <result property="registrationStartTime"   column="registration_start_time"/>
        <result property="registrationEndTime"     column="registration_end_time"/>
        <result property="coverFileUrl"            column="cover_file_url"/>
        <result property="actualEnrolledCount"     column="actual_enrolled_count"/>
        <result property="trainingStatus"          column="training_status"/>
        <result property="enrollTime"              column="enroll_time"/>
    </resultMap>

    <sql id="myEnrollJoin">
        from biz_practical_training_member m
        inner join biz_practical_training t on t.id = m.training_id
        where m.sys_user_id = #{sysUserId}
          and m.enroll_status = 0
          and m.del_flag = '0'
          and t.del_flag = '0'
          and t.publish_status = 1
    </sql>

    <select id="selectMyActiveEnrollList" resultMap="MyEnrollListVoResult">
        select t.id, t.training_topic, t.training_time, t.training_intro, t.cover_file_url,
               t.training_status, t.actual_enrolled_count, t.planned_head_count,
               t.registration_end_time, m.enroll_time
        <include refid="myEnrollJoin"/>
        order by m.enroll_time desc, t.id desc
    </select>

    <select id="selectMyActiveEnrollDetail" resultMap="MyEnrollDetailVoResult">
        select t.id, t.training_topic, t.training_time, t.training_intro, t.planned_head_count,
               t.training_location, t.expected_outcome, t.registration_start_time,
               t.registration_end_time, t.cover_file_url, t.actual_enrolled_count,
               t.training_status, m.enroll_time
        <include refid="myEnrollJoin"/>
          and t.id = #{trainingId}
        limit 1
    </select>

    <update id="updateMemberEnrollStatus" parameterType="com.ruoyi.web.modules.technology.domain.BizPracticalTrainingMember">
        update biz_practical_training_member set
            member_name = #{memberName},
            enroll_status = #{enrollStatus},
            enroll_time = #{enrollTime},
            cancel_time = #{cancelTime},
            update_by = #{updateBy},
            update_time = sysdate()
        where id = #{id} and del_flag = '0'
    </update>
</mapper>
