<?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.zdsz.mapper.ZHouseMapper">

    <resultMap type="com.ruoyi.zdsz.domain.ZHouse" id="ZHouseResult">
        <result property="id" column="id"/>
        <result property="areaId" column="area_id"/>
        <result property="buildingId" column="building_id"/>
        <result property="unitId" column="unit_id"/>
        <result property="name" column="name"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <select id="getByName" resultType="com.ruoyi.zdsz.domain.ZHouse">
        select *
        from z_house
        where name = #{name}
          and unit_id = #{unitId}
          and del_flag = '0'
        order by id
            LIMIT 1
    </select>
    <select id="getPageList" resultType="com.ruoyi.zdsz.domain.vo.ZHouseVo">
        select a.id as id,
        a.name as name,
        a.remark as remark,
        a.source,
        b.name as areaName,
        c.name as buildingName,
        d.name as unitName,
        a.house_user_name as houseUserName,
        a.id_card as idCard,
        a.phone,
        a.new_phone as newPhone
        from z_house a
        left join z_area b on b.id = a.area_id
        left join z_building c on c.id = a.building_id
        left join z_unit d on d.id = a.unit_id
        <where>
            a.del_flag = '0'
            and b.del_flag = '0'
            and c.del_flag = '0'
            and d.del_flag = '0'
            <if test="p.name != null  and p.name != ''">
                and a.name LIKE CONCAT('%',#{p.name},'%')
            </if>
            <if test="p.areaId != null  and p.areaId != ''">
                and a.area_id = #{p.areaId}
            </if>
            <if test="p.buildingId != null  and p.buildingId != ''">
                and a.building_id = #{p.buildingId}
            </if>
            <if test="p.unitId != null  and p.unitId != ''">
                and a.unit_id = #{p.unitId}
            </if>
            <if test="p.houseUserName != null  and p.houseUserName != ''">
                and a.house_user_name LIKE CONCAT('%',#{p.houseUserName},'%')
            </if>
            <if test="p.idCard != null  and p.idCard != ''">
                and a.id_card LIKE CONCAT('%',#{p.idCard},'%')
            </if>
            <if test="p.phone != null  and p.phone != ''">
                and a.phone LIKE CONCAT('%',#{p.phone},'%')
            </if>
            <if test="p.newPhone != null  and p.newPhone != ''">
                and a.new_phone LIKE CONCAT('%',#{p.newPhone},'%')
            </if>
            <if test="p.source != null  and p.source != ''">
                and a.source = #{p.source}
            </if>
        </where>
        order by b.name,CONVERT(REPLACE(c.name,'栋',''),UNSIGNED),CONVERT(REPLACE(d.name,'单元',''),UNSIGNED),CONVERT(REPLACE(a.name,'号',''),UNSIGNED) asc
    </select>


    <select id="selectListByunitId" resultType="com.ruoyi.zdsz.domain.vo.ZHouseVo">
        select
        a.id,
        a.name,
        a.phone,
        a.id_card as idCard,
        a.house_user_name as houseUserName,
        a.new_phone as newPhone
        from z_house as a
        where a.del_flag = '0'
        <if test="unitId != null  and unitId != ''">
            and a.unit_id = #{unitId}
        </if>
        order by CONVERT(REPLACE(a.name,'号',''),UNSIGNED) asc
    </select>
    <select id="selectHouseId" resultType="com.ruoyi.zdsz.domain.ZHouse">
        select
        <if test="p.enginClassification == 'indoor_engin'">
            a.unit_id,
            a.id as id,
        </if>
        <if test="p.enginClassification == 'renovation_courtyard'  or p.enginClassification == 'renovation_courtyard' or p.enginClassification == 'built_overhead' or p.enginClassification == 'construction_records'
 or p.enginClassification == 'bottom_leg' ">
            '' as id,
            '' as unit_id,
        </if>
        a.building_id,
        a.area_id
        from z_house as a
        where a.del_flag = '0'
        <if test="p.areaId != null  and p.areaId != ''">
            and a.area_id = #{p.areaId}

        </if>

        <if test="p.enginClassification == 'renovation_courtyard'  or p.enginClassification == 'renovation_courtyard' or p.enginClassification == 'built_overhead' or p.enginClassification == 'construction_records'
 or p.enginClassification == 'bottom_leg' ">
            group by a.building_id ,
                     a.unit_id
        </if>
        <if test="p.enginClassification == 'indoor_engin'">
            group by a.building_id ,a.unit_id,a.id
        </if>
        order by a.create_time desc
    </select>

    <select id="getHousesByIdCard" resultType="com.ruoyi.zdsz.domain.vo.ZHouseVo">
        SELECT
            dd.dict_value as district,
            dd.dict_label as districtName,
            h.id,
            h.name,
            h.area_id as areaId,
            a.name as areaName,
            h.building_id as buildingId,
            b.name as buildingName,
            h.unit_id as unitId,
            u.name as unitName,
            h.phone,
            h.id_card as idCard,
            h.house_user_name as houseUserName,
            h.new_phone as newPhone
        FROM
            z_house h
                left join z_unit u on u.id=h.unit_id
                left join z_building b on b.id = h.building_id
                left join z_area a on a.id = h.area_id
                LEFT JOIN sys_dict_data dd on dd.dict_type = 'district' and a.district = dd.dict_value
        WHERE
            id_card = #{idCard}
            LIMIT 1
    </select>
    <select id="getAreaSourceString" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT
            GROUP_CONCAT( z.cou SEPARATOR ';' )
        FROM
            (
                SELECT
                    CONCAT(dict_label,':',count( b.id )) AS cou
                FROM
                    z_house b
                        LEFT JOIN sys_dict_data dd ON b.source = dd.dict_value
                WHERE
                    dd.dict_type = 'community_source'
                  AND b.del_flag = '0'
                  AND b.${table} = #{id}
                GROUP BY
                    source
            ) z
    </select>
</mapper>
