<?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.ZComprehensiveMapper">

    <resultMap type="com.ruoyi.zdsz.domain.ZComprehensive" id="ZComprehensiveResult">
        <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="houseId" column="house_id"/>
        <result property="district" column="district"/>
        <result property="type" column="type"/>
        <result property="isQualified" column="is_qualified"/>
        <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="queryPageList" resultType="com.ruoyi.zdsz.domain.vo.ZComprehensiveVo">
        SELECT
        (
        SELECT COUNT(id) FROM z_comprehensive WHERE del_flag = 0 AND type = #{p.type}
        <if test="p.areaId != null and p.areaId != ''">
            AND area_id = #{p.areaId}
        </if>
        <if test="p.buildingId != null and p.buildingId != ''">
            AND building_id = #{p.buildingId}
        </if>
        <if test="p.constructTime != null and p.constructTime != '' and p.constructEndTime != null and p.constructEndTime != ''">
            AND create_time BETWEEN #{p.constructTime} AND #{p.constructEndTime}
        </if>
        <if test="p.personBy != null and p.personBy != ''">
            AND create_by = #{p.personBy}
        </if>
        <if test="p.district != null and p.district != ''">
            AND district = #{p.district}
        </if>
        <if test="p.unitId != null and p.unitId != ''">
            AND unit_id = #{p.unitId}
        </if>
        <if test="p.houseId != null and p.houseId != ''">
            AND house_id = #{p.houseId}
        </if>
        ) AS total,
        zc.*,
        if(zh.NAME is null , '未知' ,zh.NAME) AS houseName,
        if(zt.NAME is null , '未知' ,zt.NAME) AS unitName,
        if(zb.NAME is null , '未知' ,zb.NAME) AS buildingName,
        if(za.NAME is null , '未知' ,za.NAME) AS areaName
        FROM
        z_comprehensive AS zc
        LEFT JOIN z_house AS zh ON zh.id = zc.house_id
        LEFT JOIN z_unit AS zt ON zh.unit_id = zt.id
        LEFT JOIN z_building AS zb ON zt.building_id = zb.id
        LEFT JOIN z_area AS za ON zb.area_id = za.id
        WHERE
        1 = 1
        AND zc.del_flag = '0'
        AND zc.id IN (
        SELECT a.id FROM (
        SELECT id FROM z_comprehensive WHERE del_flag = '0' AND type = #{p.type}
        <if test="p.areaId != null and p.areaId != ''">
            AND area_id = #{p.areaId}
        </if>
        <if test="p.buildingId != null and p.buildingId != ''">
            AND building_id = #{p.buildingId}
        </if>
        <if test="p.personBy != null and p.personBy != ''">
            AND create_by = #{p.personBy}
        </if>
        <if test="p.unitId != null and p.unitId != ''">
            AND unit_id = #{p.unitId}
        </if>
        <if test="p.constructTime != null and p.constructTime != '' and p.constructEndTime != null and p.constructEndTime != ''">
            AND create_time BETWEEN #{p.constructTime} AND #{p.constructEndTime}
        </if>
        <if test="p.district != null and p.district != ''">
            AND district = #{p.district}
        </if>
        <if test="p.houseId != null and p.houseId != ''">
            AND house_id = #{p.houseId}
        </if>
        LIMIT #{page},#{size}
        ) AS a
        )
        order by zc.create_time desc
    </select>
    <select id="getIsQualifiedString" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT
            CASE is_qualified WHEN 'N' THEN 0 ELSE 1 END as isQualified
        FROM
            z_comprehensive
        WHERE
            house_id = #{houseId}
        AND type = '1' AND del_flag =0
        order by create_time desc ,update_time desc
            LIMIT 1
    </select>
</mapper>
