<?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.system.mapper.ApplyMapper">
    
    <resultMap type="Apply" id="ApplyResult">
        <result property="applyId"    column="apply_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="deptName"    column="dept_name"    />
        <result property="loginName"    column="login_name"    />
        <result property="applyTime"    column="apply_time"    />
        <result property="applyType"    column="apply_type"    />
        <result property="dealStatus"    column="deal_status"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectApplyVo">
        select a.apply_id, a.user_id, a.login_name, a.apply_time, a.apply_type, a.deal_status, a.create_by, a.create_time, a.update_time,
        a.remark, a.status,u.user_name,d.dept_name
        from sys_apply a
        left join sys_user u on u.user_id = a.user_id
        left join sys_dept d on d.dept_id = u.dept_id
    </sql>

    <select id="selectApplyList" parameterType="Apply" resultMap="ApplyResult">
        <include refid="selectApplyVo"/>
        <where>  
            <if test="userId != null "> and a.user_id = #{userId}</if>
            <if test="loginName != null  and loginName != ''"> and a.login_name like concat('%', #{loginName}, '%')</if>
            <if test="applyTime != null "> and a.apply_time = #{applyTime}</if>
            <if test="applyType != null  and applyType != ''"> and a.apply_type = #{applyType}</if>
            <if test="dealStatus != null  and dealStatus != ''"> and a.deal_status = #{dealStatus}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
            <if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
                AND date_format(a.apply_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
            </if>
            <if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
                AND date_format(a.apply_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
            </if>
        </where>
    </select>
    
    <select id="selectApplyById" parameterType="Long" resultMap="ApplyResult">
        <include refid="selectApplyVo"/>
        where a.apply_id = #{applyId}
    </select>
        
    <insert id="insertApply" parameterType="Apply" useGeneratedKeys="true" keyProperty="applyId">
        insert into sys_apply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="loginName != null">login_name,</if>
            <if test="applyTime != null">apply_time,</if>
            <if test="applyType != null">apply_type,</if>
            <if test="dealStatus != null">deal_status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="loginName != null">#{loginName},</if>
            <if test="applyTime != null">#{applyTime},</if>
            <if test="applyType != null">#{applyType},</if>
            <if test="dealStatus != null">#{dealStatus},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="updateApply" parameterType="Apply">
        update sys_apply
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="loginName != null">login_name = #{loginName},</if>
            <if test="applyTime != null">apply_time = #{applyTime},</if>
            <if test="applyType != null">apply_type = #{applyType},</if>
            <if test="dealStatus != null">deal_status = #{dealStatus},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where apply_id = #{applyId}
    </update>

    <delete id="deleteApplyById" parameterType="Long">
        delete from sys_apply where apply_id = #{applyId}
    </delete>

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

</mapper>