<?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.spec.mapper.TStApproveMapper">
    
    <resultMap type="TStApprove" id="TStApproveResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="user_id"    />
        <result property="planId"    column="plan_id"    />
        <result property="approveType"    column="approve_type"    />
        <result property="content"    column="content"    />
        <result property="status"    column="status"    />
        <result property="creattime"    column="creattime"    />
        <result property="endtime"    column="endtime"    />
        <result property="processId"    column="process_id"    />
        <result property="apNo"    column="ap_no"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="deptId"    column="dept_id"    />
        <result property="deptName" column="dept_name" />
    </resultMap>

    <sql id="selectTStApproveVo">
        select d.id, d.user_id, d.plan_id, d.approve_type, d.content, d.status, d.creattime, d.endtime, d.process_id, d.ap_no, d.del_flag, d.dept_id ,s.dept_name from t_st_approve d
      left join sys_dept s on s.dept_id = d.dept_id
    </sql>

    <select id="selectTStApproveList" parameterType="TStApprove" resultMap="TStApproveResult">
        <include refid="selectTStApproveVo"/>
        <where>  
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="planId != null  and planId != ''"> and plan_id = #{planId}</if>
            <if test="approveType != null "> and approve_type = #{approveType}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="creattime != null "> and creattime = #{creattime}</if>
            <if test="endtime != null "> and endtime = #{endtime}</if>
            <if test="processId != null  and processId != ''"> and process_id = #{processId}</if>
            <if test="apNo != null  and apNo != ''"> and ap_no = #{apNo}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            and d.del_flag = 0
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>
    
    <select id="selectTStApproveById" parameterType="Long" resultMap="TStApproveResult">
        <include refid="selectTStApproveVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertTStApprove" parameterType="TStApprove">
        <selectKey keyProperty="id" resultType="long" order="BEFORE">
            SELECT seq_t_st_approve.NEXTVAL as id FROM DUAL
        </selectKey>
        insert into t_st_approve
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">user_id,</if>
            <if test="planId != null">plan_id,</if>
            <if test="approveType != null">approve_type,</if>
            <if test="content != null">content,</if>
            <if test="status != null">status,</if>
            <if test="creattime != null">creattime,</if>
            <if test="endtime != null">endtime,</if>
            <if test="processId != null">process_id,</if>
            <if test="apNo != null">ap_no,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="deptId != null">dept_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="planId != null">#{planId},</if>
            <if test="approveType != null">#{approveType},</if>
            <if test="content != null">#{content},</if>
            <if test="status != null">#{status},</if>
            <if test="creattime != null">#{creattime},</if>
            <if test="endtime != null">#{endtime},</if>
            <if test="processId != null">#{processId},</if>
            <if test="apNo != null">#{apNo},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="deptId != null">#{deptId},</if>
         </trim>
    </insert>

    <update id="updateTStApprove" parameterType="TStApprove">
        update t_st_approve
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="approveType != null">approve_type = #{approveType},</if>
            <if test="content != null">content = #{content},</if>
            <if test="status != null">status = #{status},</if>
            <if test="creattime != null">creattime = #{creattime},</if>
            <if test="endtime != null">endtime = #{endtime},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="apNo != null">ap_no = #{apNo},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
        </trim>
        where id = #{id}
    </update>

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

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