<?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="org.springblade.bank.keypwd.mapper.KeyPwdMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="keyPwdResultMap" type="org.springblade.bank.keypwd.entity.KeyPwd">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="bank_no" property="bankNo"/>
        <result column="org_no" property="orgNo"/>
        <result column="org_name" property="orgName"/>
        <result column="category" property="category"/>
        <result column="type" property="type"/>
        <result column="number" property="number"/>
        <result column="handover_person_no" property="handoverPersonNo"/>
        <result column="handover_person_name" property="handoverPersonName"/>
        <result column="handover_date" property="handoverDate"/>
        <result column="receiver_no" property="receiverNo"/>
        <result column="receiver_name" property="receiverName"/>
        <result column="remark" property="remark"/>
        <result column="filling_date" property="fillingDate"/>
        <result column="filling_person" property="fillingPerson"/>

        <result column="is_turn_in" property="isTurnIn"/>
        <result column="receiver_id" property="receiverId"/>
        <result column="handover_person_id" property="handoverPersonId"/>
        <result column="process" property="process"/>
        <result column="sure_time" property="sureTime"/>
        <result column="pid" property="pid"/>
    </resultMap>


    <select id="selectKeyPwdPage" resultMap="keyPwdResultMap">

        SELECT c.* FROM
        (select * from zh_input_key_pwd where is_deleted = 0
        <if test="keyPwd.deptIdList!=null and keyPwd.deptIdList.size>0">
            and create_dept in
            <foreach collection="keyPwd.deptIdList" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        UNION
        select * from zh_input_key_pwd where is_deleted = 0
        and (create_user = '${keyPwd.currentuserId}' or LOCATE('${keyPwd.currentuserId}', receiver_id) > 0)
        ) c
        where  1=1


        <if test="keyPwd!=null">
            <if test="keyPwd.status!=null">
                AND c.status = '${keyPwd.status}'
            </if>
            <if test="keyPwd.createUser!=null">
                AND c.create_user like '%${keyPwd.createUser}%'
            </if>
            <if test="keyPwd.receiverId!=null">
                AND c.receiver_id = '${keyPwd.receiverId}%'
            </if>
            <if test="keyPwd.receiverName!=null">
                AND c.receiver_name like '%${keyPwd.receiverName}%'
            </if>
            <if test="keyPwd.bankNo!=null">
                AND c.bank_no = '${keyPwd.bankNo}'
            </if>
            <if test="keyPwd.isTurnIn!=null">
                AND c.is_turn_in = '${keyPwd.isTurnIn}'
            </if>
            <if test="keyPwd.process!=null">
                AND c.process = '${keyPwd.process}'
            </if>
            <if test="keyPwd.category!=null">
                AND c.category in
                <foreach item="category" collection="keyPwd.categorys" separator="," open="(" close=")" index="">
                    #{category}
                </foreach>
            </if>
            <if test="keyPwd.orgNos!=null">
                AND c.org_no in
                <foreach item="orgNo" collection="keyPwd.orgNos" separator="," open="(" close=")" index="">
                    #{orgNo}
                </foreach>
            </if>

            <if test="keyPwd.handoverDate_begin!=null and keyPwd.handoverDate_end!=null">
                and c.handover_date BETWEEN '${keyPwd.handoverDate_begin}' and '${keyPwd.handoverDate_end}'
            </if>
            <if test="keyPwd.receiverName!=null and keyPwd.receiverName!=null">
                AND c.receiver_name like '%${keyPwd.receiverName}%'
            </if>
        </if>
        ORDER BY c.handover_date DESC
    </select>

</mapper>
