<?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.dgtly.system.mapper.EntryPassMapper">
    
    <resultMap type="EntryPass" id="EntryPassResult">
        <result property="userId"    column="user_id"    />
        <result property="address"    column="address"    />
        <result property="qrCode"    column="qr_code"    />
        <result property="residentNum"    column="resident_num"    />
        <result property="householdRelation"    column="household_relation"    />
        <result property="ownership"    column="ownership"    />
        <result property="auditOpinion"    column="audit_opinion"    />
        <result property="user_name"    column="username"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="isWuhan"    column="is_wuhan"    />
        <result property="sex"    column="sex"    />
        <result property="idCard"    column="id_card"    />
        <result property="chargeCommunityId"    column="charge_community_id"    />
        <result property="communityName"    column="community_name"    />

    </resultMap>

    <sql id="selectEntryPassVo">
         select ep.user_id  , ep.address, ep.qr_code , ep.resident_num, ep.household_relation, ep.ownership, ep.audit_opinion, ep.status,
         u.user_name,u.phonenumber,u.sex,u.is_wuhan,u.id_card,u.charge_community_id,com.name community_name
        from entry_pass ep
        left join sys_user u on ep.user_id = u.user_id
        left join community com on com.id = u.charge_community_id
    </sql>

    <select id="selectEntryPassList" parameterType="EntryPass" resultMap="EntryPassResult">
        <include refid="selectEntryPassVo"/>
        <where>  
            <if test="address != null  and address != ''"> and ep.address = #{address}</if>
            <if test="qrCode != null  and qrCode != ''"> and ep.qr_code = #{qrCode}</if>
            <if test="residentNum != null "> and ep.resident_num = #{residentNum}</if>
            <if test="householdRelation != null  and householdRelation != ''"> and ep.household_relation = #{householdRelation}</if>
            <if test="ownership != null  and ownership != ''"> and ep.ownership = #{ownership}</if>
            <if test="auditOpinion != null  and auditOpinion != ''"> and ep.audit_opinion = #{auditOpinion}</if>
            <if test="status != null  and status != ''"> and ep.status = #{status}</if>
            <if test="chargeCommunityId != null  and chargeCommunityId != ''"> and u.charge_community_id = #{chargeCommunityId}</if>
        </where>
    </select>
    
    <select id="selectEntryPassByUserId" parameterType="Long" resultMap="EntryPassResult">
        <include refid="selectEntryPassVo"/>
        where ep.user_id = #{userId}
    </select>
        
    <insert id="insertEntryPass" parameterType="EntryPass" useGeneratedKeys="true" keyProperty="id">
        insert into entry_pass
        <trim prefix="(" suffix=")" suffixOverrides=",">
            user_id,
            <if test="address != null  and address != ''">address,</if>
            <if test="qrCode != null  and qrCode != ''">qr_code,</if>
            <if test="residentNum != null ">resident_num,</if>
            <if test="householdRelation != null  and householdRelation != ''">household_relation,</if>
            <if test="ownership != null  and ownership != ''">ownership,</if>
            <if test="auditOpinion != null  and auditOpinion != ''">audit_opinion,</if>
            <if test="status != null  and status != ''">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{userId},
            <if test="address != null  and address != ''">#{address},</if>
            <if test="qrCode != null  and qrCode != ''">#{qrCode},</if>
            <if test="residentNum != null ">#{residentNum},</if>
            <if test="householdRelation != null  and householdRelation != ''">#{householdRelation},</if>
            <if test="ownership != null  and ownership != ''">#{ownership},</if>
            <if test="auditOpinion != null  and auditOpinion != ''">#{auditOpinion},</if>
            <if test="status != null  and status != ''">#{status},</if>
         </trim>
    </insert>

    <update id="updateEntryPass" parameterType="EntryPass">
        update entry_pass
        <trim prefix="SET" suffixOverrides=",">
            <if test="address != null  and address != ''">address = #{address},</if>
            <if test="qrCode != null  and qrCode != ''">qr_code = #{qrCode},</if>
            <if test="residentNum != null ">resident_num = #{residentNum},</if>
            <if test="householdRelation != null  and householdRelation != ''">household_relation = #{householdRelation},</if>
            <if test="ownership != null  and ownership != ''">ownership = #{ownership},</if>
            <if test="auditOpinion != null  and auditOpinion != ''">audit_opinion = #{auditOpinion},</if>
            <if test="status != null  and status != ''">status = #{status},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <delete id="deleteEntryPassById" parameterType="Long">
        delete from entry_pass where user_id = #{userId}
    </delete>

    <delete id="deleteEntryPassByIds" parameterType="String">
        delete from entry_pass where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    
</mapper>