<?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.project.training.mapper.TTrainingMapper">

  <resultMap type="TTraining" id="TTrainingResult">
    <result property="id"    column="id"    />
    <result property="plantCode"    column="plant_code"    />
    <result property="course"    column="course"    />
    <result property="courseid"    column="courseid"    />
    <result property="period"    column="period"    />
    <result property="duration"    column="duration"    />
    <result property="trainer"    column="trainer"    />
    <result property="position"    column="position"    />
    <result property="designatedPosition"    column="designated_position"    />
    <result property="designatedStaff"    column="designated_staff"    />
    <result property="year"    column="years"    />
    <result property="courseStartdate"    column="course_startdate"    />
    <result property="courseEnddate"    column="course_enddate"    />
    <result property="isfinish"    column="isfinish"    />
    <result property="confirmor"    column="confirmor"    />
    <result property="delFlag"    column="del_flag"    />
    <result property="createrCode"    column="creater_code"    />
    <result property="createdate"    column="createdate"    />
    <result property="updaterCode"    column="updater_code"    />
    <result property="updatedate"    column="updatedate"    />
    <result property="remarks"    column="remarks"    />
    <result property="deptId"    column="dept_id"    />
    <result property="deptName" column="dept_name" />
    <result property="startDate"    column="course_startdate"    />
    <result property="endDate"    column="course_startdate"    />
    <result property="nonParticipants"    column="non_participants"    />
    <result property="content"    column="content"    />
    <result property="unit"    column="unit"    />
    <result property="trainingType"    column="training_type"    />
    <result property="picUrl"    column="pic_url"    />
    <result property="regularId"    column="regular_id"    />
    <result property="positionNum"    column="positionNum"    />
    <collection property="tTrainingNons" javaType="ArrayList" ofType="TTrainingNon">
      <result property="staffId"    column="staff_id"    />
      <result property="trainingDate"    column="training_date"    />
      <result property="trainingStaff"    column="training_staff"    />
    </collection>
  </resultMap>

  <sql id="selectTTrainingVo">
      select d.id, d.regular_id, d.content, d.unit, d.training_type, d.pic_url, d.plant_code, d.course, d.courseid, d.period, d.duration, d.trainer, d.position, d.designated_position, d.designated_staff, d.years, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id, d.course_startdate, d.course_enddate, d.isfinish, d.confirmor , d.non_participants, s.dept_name from t_training d
    left join sys_dept s on s.dept_id = d.dept_id
  </sql>

  <select id="selectTTrainingList" parameterType="TTraining" resultMap="TTrainingResult">
    select
    (select count(1) from t_staffmgr st where (st.del_flag = 0 or (st.del_flag = 9 and st.LEFT_DATE > d.COURSE_STARTDATE ) ) and st.ACTUALPOST in (SELECT REGEXP_SUBSTR (d.POSITION, '[^,]+', 1,rownum) FROM DUAL CONNECT BY ROWNUM <![CDATA[<=]]> LENGTH (d.POSITION) - LENGTH (REPLACE (d.POSITION, ',', ''))+1)) as positionNum,
    d.id, d.regular_id, d.content, d.unit, d.training_type, d.pic_url, d.plant_code, d.course, d.courseid, d.period, d.duration, d.trainer, d.position, d.designated_position, d.designated_staff, d.years, d.del_flag, d.creater_code, d.createdate, d.updater_code, d.updatedate, d.remarks, d.dept_id, d.course_startdate, d.course_enddate, d.isfinish, d.confirmor , d.non_participants, s.dept_name from t_training d
    left join sys_dept s on s.dept_id = d.dept_id
    <where>
      <if test="course != null  and course != ''"> and course like concat(concat('%', #{course}), '%')</if>
      <if test="position != null  and position != ''"> and position = #{position}</if>
      <if test="year != null "> and years = #{year}</if>
      <if test="isfinish != null "> and isfinish = #{isfinish}</if>
      <if test="trainingType != null and trainingType != ''"> and training_type = #{trainingType}</if>
      <if test="startDate != null ">and course_startdate <![CDATA[>=]]> #{startDate}</if>
      <if test="endDate != null">and course_startdate <![CDATA[<=]]>#{endDate}</if>
      and d.del_flag = 0
    </where>
    <!-- 数据范围过滤 -->
    ${params.dataScope}
    order by d.years desc, d.course desc
  </select>

  <select id="selectTTrainingById" parameterType="Long" resultMap="TTrainingResult">
    <include refid="selectTTrainingVo"/>
    where id = #{id}
  </select>

  <select id="selectTTrainingByRegularId" parameterType="Long" resultMap="TTrainingResult">
    <include refid="selectTTrainingVo"/>
    where regular_id = #{regularId}
  </select>

  <insert id="insertTTraining" parameterType="TTraining" useGeneratedKeys="true" keyProperty="id">
    <selectKey keyProperty="id" resultType="long" order="BEFORE">
      SELECT seq_t_training.NEXTVAL as id FROM DUAL
    </selectKey>
    insert into t_training
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">id,</if>
      <if test="plantCode != null and plantCode != ''">plant_code,</if>
      <if test="course != null and course != ''">course,</if>
      <if test="courseid != null and courseid != ''">courseid,</if>
      <if test="period != null">period,</if>
      <if test="duration != null">duration,</if>
      <if test="trainer != null">trainer,</if>
      <if test="position != null">position,</if>
      <if test="designatedPosition != null">designated_position,</if>
      <if test="designatedStaff != null">designated_staff,</if>
      <if test="content != null">content,</if>
      <if test="unit != null">unit,</if>
      <if test="year != null">years,</if>
      <if test="courseStartdate != null">course_startdate,</if>
      <if test="courseEnddate != null">course_enddate,</if>
      <if test="isfinish != null">isfinish,</if>
      <if test="confirmor != null">confirmor,</if>
      <if test="delFlag != null">del_flag,</if>
      <if test="createrCode != null">creater_code,</if>
      <if test="createdate != null">createdate,</if>
      <if test="updaterCode != null">updater_code,</if>
      <if test="updatedate != null">updatedate,</if>
      <if test="remarks != null">remarks,</if>
      <if test="deptId != null">dept_id,</if>
      <if test="nonParticipants != null">non_participants,</if>
      <if test="trainingType != null">training_type,</if>
      <if test="picUrl != null">pic_url,</if>
      <if test="regularId != null">regular_id,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">#{id},</if>
      <if test="plantCode != null and plantCode != ''">#{plantCode},</if>
      <if test="course != null and course != ''">#{course},</if>
      <if test="courseid != null and courseid != ''">#{courseid},</if>
      <if test="period != null">#{period},</if>
      <if test="duration != null">#{duration},</if>
      <if test="trainer != null">#{trainer},</if>
      <if test="position != null">#{position},</if>
      <if test="designatedPosition != null">#{designatedPosition},</if>
      <if test="designatedStaff != null">#{designatedStaff},</if>
      <if test="content != null">#{content},</if>
      <if test="unit != null">#{unit},</if>
      <if test="year != null">#{year},</if>
      <if test="courseStartdate != null">#{courseStartdate},</if>
      <if test="courseEnddate != null">#{courseEnddate},</if>
      <if test="isfinish != null">#{isfinish},</if>
      <if test="confirmor != null">#{confirmor},</if>
      <if test="delFlag != null">#{delFlag},</if>
      <if test="createrCode != null">#{createrCode},</if>
      <if test="createdate != null">#{createdate},</if>
      <if test="updaterCode != null">#{updaterCode},</if>
      <if test="updatedate != null">#{updatedate},</if>
      <if test="remarks != null">#{remarks},</if>
      <if test="deptId != null">#{deptId},</if>
      <if test="nonParticipants != null">#{nonParticipants},</if>
      <if test="trainingType != null">#{trainingType},</if>
      <if test="picUrl != null">#{picUrl},</if>
      <if test="regularId != null">#{regularId},</if>
    </trim>
  </insert>

  <update id="updateTTraining" parameterType="TTraining">
    update t_training
    <trim prefix="SET" suffixOverrides=",">
      <if test="plantCode != null and plantCode != ''">plant_code = #{plantCode},</if>
      <if test="course != null and course != ''">course = #{course},</if>
      <if test="courseid != null and courseid != ''">courseid = #{courseid},</if>
      <if test="period != null">period = #{period},</if>
      <if test="duration != null">duration = #{duration},</if>
      <if test="trainer != null">trainer = #{trainer},</if>
      <if test="position != null">position = #{position},</if>
      <if test="position == null">position = NULL,</if>
      <if test="designatedPosition != null">designated_position = #{designatedPosition},</if>
      <if test="designatedStaff != null">designated_staff = #{designatedStaff},</if>
      <if test="designatedPosition == null">designated_position = NULL,</if>
      <if test="designatedStaff == null">designated_staff = NULL,</if>
      <if test="content != null">content = #{content},</if>
      <if test="unit != null">unit = #{unit},</if>
      <if test="year != null">years = #{year},</if>
      <if test="courseStartdate != null">course_startdate = #{courseStartdate},</if>
      <if test="courseEnddate != null">course_enddate = #{courseEnddate},</if>
      <if test="isfinish != null">isfinish = #{isfinish},</if>
      <if test="confirmor != null">confirmor = #{confirmor},</if>
      <if test="delFlag != null">del_flag = #{delFlag},</if>
      <if test="createrCode != null">creater_code = #{createrCode},</if>
      <if test="createdate != null">createdate = #{createdate},</if>
      <if test="updaterCode != null">updater_code = #{updaterCode},</if>
      <if test="updatedate != null">updatedate = #{updatedate},</if>
      <if test="remarks != null">remarks = #{remarks},</if>
      <if test="deptId != null">dept_id = #{deptId},</if>
      <if test="nonParticipants != null">non_participants = #{nonParticipants},</if>
      <if test="nonParticipants == null">non_participants = NULL,</if>
      <if test="trainingType != null">training_type = #{trainingType},</if>
      <if test="picUrl != null">pic_url = #{picUrl},</if>
    </trim>
    where id = #{id}
  </update>

  <update id="deleteTTrainingById" parameterType="Long">
        update t_training set del_flag = 2 where id = #{id}
    </update>

  <update id="deleteTTrainingByIds" parameterType="String">
    update t_training set del_flag = 2 where id in
    <foreach item="id" collection="array" open="(" separator="," close=")">
      #{id}
    </foreach>
  </update>

</mapper>
