<?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.dcsoft.system.mapper.SysPeopleLeaveMapper">

    <resultMap type="SysPeopleLeave" id="SysPeopleLeaveResult">
        <result property="id"    column="id"    />
        <result property="gh"    column="gh"    />
        <result property="name"    column="name"    />
        <result property="sex"    column="sex"    />
        <result property="branchId"    column="branch_id"    />
        <result property="idcard"    column="idcard"    />
        <result property="leaveTime"    column="leave_time"    />
        <result property="peopleId"    column="people_id"    />
        <result property="leaveReason"    column="leave_reason"    />
        <result property="remark"    column="remark"    />
        <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="branchName"    column="branch_name"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectSysPeopleLeaveVo">
        select spl.id, spl.gh, spl.name, spl.sex, spl.branch_id, spl.idcard, spl.leave_time, spl.people_id,
               spl.leave_reason, spl.remark, spl.create_by, spl.create_time, spl.update_by, spl.update_time,b.name branch_name,spl.status
        from sys_people_leave spl
        left join sys_branch b on spl.branch_id = b.id

    </sql>

    <select id="selectSysPeopleLeaveList" parameterType="SysPeopleLeave" resultMap="SysPeopleLeaveResult">
        <include refid="selectSysPeopleLeaveVo"/>
        <where>
            <if test="gh != null  and gh != ''"> and spl.gh = #{gh}</if>
            <if test="name != null  and name != ''"> and spl.name like concat('%', #{name}, '%')</if>
            <if test="branchId != null "> and spl.branch_id = #{branchId}</if>
            <if test="leaveTime != null "> and spl.leave_time = #{leaveTime}</if>
            <if test="type != null and type == '0'.toString()">
                and date_format(DATE_SUB(NOW(), INTERVAL 3 MONTH), '%Y-%m-%d 00:00:00') &lt;= spl.leave_time
                and spl.leave_time &lt;= date_format(NOW(), '%Y-%m-%d 00:00:00')
            </if>
            <if test="leaveReason != null  and leaveReason != ''"> and spl.leave_reason = #{leaveReason}</if>
            <if test="status != null and status != '' "> and spl.status = #{status}</if>
        </where>
    </select>

    <select id="selectSysPeopleLeaveById" parameterType="Long" resultMap="SysPeopleLeaveResult">
        <include refid="selectSysPeopleLeaveVo"/>
        where spl.id = #{id}
    </select>
    <select id="queryPeopleLeaveList" resultType="com.dcsoft.system.domain.SysPeopleLeave">
        select gh, name, sex, branch_id, leave_time, people_id, leave_reason, status, id
        from sys_people_leave
        where 1 = 1
          and gh in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.gh}
        </foreach>
    </select>

    <insert id="insertSysPeopleLeave" parameterType="SysPeopleLeave" useGeneratedKeys="true" keyProperty="id">
        insert into sys_people_leave
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="gh != null">gh,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="sex != null">sex,</if>
            <if test="branchId != null">branch_id,</if>
            <if test="idcard != null">idcard,</if>
            <if test="leaveTime != null">leave_time,</if>
            <if test="peopleId != null">people_id,</if>
            <if test="leaveReason != null">leave_reason,</if>
            <if test="remark != null">remark,</if>
            <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="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="gh != null">#{gh},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="sex != null">#{sex},</if>
            <if test="branchId != null">#{branchId},</if>
            <if test="idcard != null">#{idcard},</if>
            <if test="leaveTime != null">#{leaveTime},</if>
            <if test="peopleId != null">#{peopleId},</if>
            <if test="leaveReason != null">#{leaveReason},</if>
            <if test="remark != null">#{remark},</if>
            <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="status != null">#{status},</if>
         </trim>
    </insert>
    <insert id="savePeopleLeaveList">
        insert into sys_people_leave (gh, name, sex, branch_id, leave_time, people_id, leave_reason, status)
        VALUES
        <foreach collection="list" item="i" index="index" separator=",">
            (
            #{i.gh},
            #{i.name},
            #{i.sex},
            #{i.branchId},
            #{i.leaveTime},
            #{i.peopleId},
            #{i.leaveReason},
            #{i.status}
            )
        </foreach>
    </insert>

    <update id="updateSysPeopleLeave" parameterType="SysPeopleLeave">
        update sys_people_leave
        <trim prefix="SET" suffixOverrides=",">
            <if test="gh != null">gh = #{gh},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="branchId != null">branch_id = #{branchId},</if>
            <if test="idcard != null">idcard = #{idcard},</if>
            <if test="leaveTime != null">leave_time = #{leaveTime},</if>
            <if test="peopleId != null">people_id = #{peopleId},</if>
            <if test="leaveReason != null">leave_reason = #{leaveReason},</if>
            <if test="remark != null">remark = #{remark},</if>
            <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="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updatePeopleLeaveList">
        UPDATE sys_people_leave
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="name =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.name">
                        WHEN gh=#{item.gh} THEN #{item.name}
                    </if>
                </foreach>
            </trim>
            <trim prefix="sex =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.sex">
                        WHEN gh=#{item.gh} THEN #{item.sex}
                    </if>
                </foreach>
            </trim>
            <trim prefix="branch_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.branchId">
                        WHEN gh=#{item.gh} THEN #{item.branchId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="leave_time =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.leaveTime">
                        WHEN gh=#{item.gh} THEN #{item.leaveTime}
                    </if>
                </foreach>
            </trim>
            <trim prefix="people_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.peopleId">
                        WHEN gh=#{item.gh} THEN #{item.peopleId}
                    </if>
                </foreach>
            </trim>
            <trim prefix="leave_reason =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.leaveReason">
                        WHEN gh=#{item.gh} THEN #{item.leaveReason}
                    </if>
                </foreach>
            </trim>
            <trim prefix="status =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="null != item.status">
                        WHEN gh=#{item.gh} THEN #{item.status}
                    </if>
                </foreach>
            </trim>
        </trim>
        WHERE
        gh IN
        <foreach collection="list" index="index" item="item" separator="," open="(" close=")">
            #{item.gh}
        </foreach>
    </update>

    <delete id="deleteSysPeopleLeaveById" parameterType="Long">
        delete from sys_people_leave where id = #{id}
    </delete>

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