<?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.huiju.estateagents.property.dao.TaUserVerifyMapper">

    <sql id="Base_Column_List" >
    id, person_id, org_id, role_id, phase_id, phase_name, building_id, building_name,
    unit_id, unit_name, level_id, level_name, room_no_id, room_no_name, verify_status,
    create_date, verify_name, remark
  </sql>

    <select id="selectAllByPhoneAndUserNameAndIdCard" resultType="com.huiju.estateagents.property.model.TaUserVerify" >
        select
            tav.*
        from
          ta_user_verify tav
        <where>
            tav.community_id = #{communityId}
            <trim suffixOverrides="and | or">
                and tav.verify_status = 0
                <if test="phone != null and phone != ''">
                    and tav.phone like CONCAT('%',#{phone},'%')
                </if>
                <if test="userName != null and userName != ''">
                    and tav.owner_name like CONCAT('%',#{userName},'%')
                </if>
                <if test="idCard != null and idCard != ''">
                    and tav.id_card like CONCAT('%',#{idCard},'%')
                </if>
                <if test="orgId != null and orgId != ''">
                    and tav.org_id = #{orgId,jdbcType=INTEGER}
                </if>
            </trim>
        </where>
    </select>


    <select id="selectCommunityAndAddress" resultType="com.huiju.estateagents.property.model.TaUserVerify">
        select
        *
        from ta_user_verify
        where org_id = #{orgId,jdbcType=INTEGER}
        and phase_id = #{phaseId,jdbcType=INTEGER}
        and building_id = #{buildingId,jdbcType=INTEGER}
        and unit_id = #{unitId,jdbcType=INTEGER}
        and level_id = #{levelId,jdbcType=INTEGER}
        and room_no_id = #{roomNoId,jdbcType=INTEGER}
        and verify_status = 1
        and role_id = 1
    </select>

    <select id="selectCommunityAndAddressAndRoleId" resultType="com.huiju.estateagents.property.model.TaUserVerify">
        select
        *
        from ta_user_verify
        where org_id = #{orgId,jdbcType=INTEGER}
        and phase_id = #{phaseId,jdbcType=INTEGER}
        and building_id = #{buildingId,jdbcType=INTEGER}
        and unit_id = #{unitId,jdbcType=INTEGER}
        and level_id = #{levelId,jdbcType=INTEGER}
        and room_no_id = #{roomNoId,jdbcType=INTEGER}
        and verify_status = 1
        and person_id = #{userId}
        and role_id = #{roleId}
    </select>

    <select id="selectUserVerifyCommunityAndAddress" resultType="com.huiju.estateagents.property.model.TaUserVerify">
        select
          *
        from ta_user_verify
        where org_id = #{orgId,jdbcType=INTEGER}
        and person_id = #{userId,jdbcType=INTEGER}
        and phase_id = #{phaseId,jdbcType=INTEGER}
        and building_id = #{buildingId,jdbcType=INTEGER}
        and unit_id = #{unitId,jdbcType=INTEGER}
        and level_id = #{levelId,jdbcType=INTEGER}
        and room_no_id = #{roomNoId,jdbcType=INTEGER}
        <if test="verifyStatus != null and verifyStatus != ''">
            and verify_status = #{verifyStatus}
        </if>
    </select>

    <select id="selectWxCommunityAndAddress" resultType="com.huiju.estateagents.property.model.TaUserVerify">
        select
        <include refid="Base_Column_List"/>
        from ta_user_verify
        where org_id = #{orgId,jdbcType=INTEGER}
        and person_id = #{personId,jdbcType=INTEGER}
        and phase_id = #{phaseId,jdbcType=INTEGER}
        and building_id = #{buildingId,jdbcType=INTEGER}
        and unit_id = #{unitId,jdbcType=INTEGER}
        and level_id = #{levelId,jdbcType=INTEGER}
        and room_no_id = #{roomNoId,jdbcType=INTEGER}
        and verify_status = #{verifyStatus}
    </select>

    <select id="getPropUserByPaged" resultType="java.util.Map">
        SELECT DISTINCT
            t.person_id as personId,
            t.owner_name as ownerName,
            t.id_card as idCard,
            m.mp_openid as mpOpenid
        FROM
            ta_user_verify t
        INNER JOIN ta_mp_miniapp s ON t.person_id = s.miniapp_person_id
        INNER JOIN ta_mp_person m ON s.mp_person_id = m.mp_person_id
        WHERE
            t.org_id = #{orgId}
            AND t.verify_status = 1
    </select>
    <select id="getPropUserByBirday" resultType="java.util.Map">
        SELECT DISTINCT
            t.person_id as personId,
            t.owner_name as ownerName,
            t.id_card as idCard,
            m.mp_openid as mpOpenid
        FROM
            ta_user_verify t
        INNER JOIN ta_mp_miniapp s ON t.person_id = s.miniapp_person_id
        INNER JOIN ta_mp_person m ON s.mp_person_id = m.mp_person_id
        WHERE
            t.org_id = #{orgId}
            AND t.id_card like CONCAT('%', #{birthday},'%')
            AND t.verify_status = 1
    </select>
    <select id="getVerifyMapList" resultType="java.util.Map">
        SELECT
            t.create_date AS createDate,
            t.owner_name AS nickname,
            CASE
            t.role_id
            WHEN 1 THEN
            '业主'
            WHEN 3 THEN
            '家属' ELSE '租客'
            END AS content
        FROM
            ta_user_verify t
        WHERE
            t.org_id = #{orgId}
            AND t.community_id = #{communityId}
        <if test="startDate != null and startDate != ''">
            AND DATE_FORMAT(t.create_date, '%Y-%m-%d') &gt;= #{startDate}
        </if>
        <if test="endDate != null and endDate != ''">
            AND DATE_FORMAT(t.create_date, '%Y-%m-%d') &lt;= #{endDate}
        </if>
    </select>
    <select id="getNoVerifyNum" resultType="java.lang.Integer">
        SELECT count(*)
        FROM
            ta_user_verify t
        WHERE
            t.org_id = #{orgId}
        AND t.community_id = #{communityId}
        AND t.verify_status = '0'
    </select>
    <select id="getListWithPoints" resultType="com.huiju.estateagents.property.model.TaUserVerify">
        SELECT
            t.id,
            t.id_card,
            t.owner_name,
            t.phone,
            t.person_id,
            m.total_points,
            GROUP_CONCAT( CONCAT( t.phase_name, t.building_name, t.unit_name, t.room_no_name ) ) AS room_no_name
        FROM
            ta_user_verify t
            INNER JOIN  (
                SELECT
                    s.person_id,
                    sum( s.points_amount ) AS total_points
                FROM
                    ta_points_records s
                WHERE
                    s.org_id = #{params.orgId}
                GROUP BY
                    s.person_id
            ) m ON m.person_id = t.person_id
        WHERE
            t.org_id = #{params.orgId}
            AND t.community_id = #{params.communityId}
        <if test="null != params.phaseId">
            AND t.phase_id = #{params.phaseId}
        </if>
        <if test="null != params.buildingId">
            AND t.building_id = #{params.buildingId}
        </if>
        <if test="null != params.unitId">
            AND t.unit_id = #{params.unitId}
        </if>
        <if test="null != params.levelId">
            AND t.level_id = #{params.levelId}
        </if>
        <if test="null != params.roomNoId">
            AND t.room_no_id = #{params.roomNoId}
        </if>
        <if test="null != params.ownerName and '' != params.ownerName">
            AND t.owner_name LIKE CONCAT('%', #{params.ownerName}, '%')
        </if>
        <if test="null != params.phone and '' != params.phone">
            AND t.phone LIKE CONCAT('%', #{params.phone}, '%')
        </if>
        <if test="null != params.idCard and '' != params.idCard">
            AND t.id_card LIKE CONCAT('%', #{params.idCard}, '%')
        </if>
        <if test="null != params.minPoints">
            AND m.total_points &gt;= #{params.minPoints}
        </if>
        <if test="null != params.maxPoints">
            AND m.total_points &lt;= #{params.maxPoints}
        </if>
            AND t.verify_status = 1
        GROUP BY t.person_id
        ORDER BY t.create_date DESC
    </select>
</mapper>
