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

    <resultMap type="com.ruoyi.zdsz.domain.ZEngineeringCivil" id="ZEngineeringCivilResult">
        <result property="id" column="id"/>
        <result property="district" column="district"/>
        <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="enginType" column="engin_type"/>
        <result property="enginClassification" column="engin_classification"/>
        <result property="enginCycle" column="engin_cycle"/>
        <result property="completionStatus" column="completion_status"/>
        <result property="version" column="version"/>
        <result property="imgUrl" column="img_url"/>
        <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="selectByDistrictStatistics" resultType="java.util.Map">
        select aas.name AS name,
               MAX(CASE aas.statu WHEN '未施工' THEN aas.num ELSE 0 END ) weishigong,
               MAX(CASE aas.statu WHEN '施工中' THEN aas.num ELSE 0 END ) shigongzhong ,
               MAX(CASE aas.statu WHEN '竣工' THEN aas.num ELSE 0 END ) jungong
        FROM (SELECT
                      (SELECT name FROM z_area WHERE id = al.zarea) AS name,
                      IF(ISNULL(al.completion_status),'未施工',al.completion_status) AS statu,
                      COUNT(ISNULL(al.completion_status)) AS num
              FROM (SELECT zh.id zid, zh.area_id zarea,zec.* FROM z_house zh
                                                                      LEFT JOIN z_engineering_civil zec on zec.house_id = zh.id
                    WHERE zh.area_id in (SELECT id FROM z_area WHERE district = #{district} AND del_flag = '0')) al GROUP BY al.zarea,al.completion_status) aas group by aas.name
    </select>

    <select id="queryPageList" resultType="com.ruoyi.zdsz.domain.vo.ZEngineeringCivilVo">
        select
        (select count(id)
        from z_engineering_civil
        where del_flag = 0
        <if test="p.district != null and p.district != ''">
            and district = #{p.district}
        </if>
        <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.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>
        <if test="p.enginType != null and p.enginType != ''">
            and engin_type = #{p.enginType}
        </if>
        <if test="p.enginClassification != null and p.enginClassification != ''">
            and engin_classification = #{p.enginClassification}
        </if>
        <if test="p.enginCycle != null and p.enginCycle != ''">
            and engin_cycle = 0
        </if>
        ) as total,
        a.*,
        if(b.name is null , '未知' ,b.NAME) AS areaName,
        if(c.name is null , '未知' ,c.NAME) AS buildingName,
        if(d.name is null , '未知' ,d.NAME) AS unitName,
        if(e.name is null , '未知' ,e.NAME) AS houseName,
        f.dict_label AS enginClassificationName
        from z_engineering_civil 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
        left join z_house e on e.id = a.house_id
        left join sys_dict_data f on f.dict_type = a.engin_type and f.dict_value = a.engin_classification
        where a.id in (SELECT at.* FROM (SELECT a.id from z_engineering_civil a
        where a.del_flag = 0
        <if test="p.district != null and p.district != ''">
            and a.district = #{p.district}
        </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.houseId != null and p.houseId != ''">
            and house_id = #{p.houseId}
        </if>
        <if test="p.enginType != null and p.enginType != ''">
            and engin_type = #{p.enginType}
        </if>
        <if test="p.enginClassification != null and p.enginClassification != ''">
            and engin_classification = #{p.enginClassification}
        </if>
        <if test="p.enginCycle != null and p.enginCycle != ''">
            and engin_cycle = 0
        </if>
        order by a.update_info_time desc
        LIMIT #{page},#{size}
        ) AS at)
        order by a.update_info_time desc
    </select>
    <select id="queryPageIdList" resultType="com.ruoyi.zdsz.domain.vo.ZEngineeringCivilVo">
       select
       #{total} as total,
        a.*,
        if(b.name is null , '未知' ,b.NAME) AS areaName,
        if(c.name is null , '未知' ,c.NAME) AS buildingName,
        if(d.name is null , '未知' ,d.NAME) AS unitName,
        if(e.name is null , '未知' ,e.NAME) AS houseName,
        f.dict_label AS enginClassificationName
        from z_engineering_civil 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
        left join z_house e on e.id = a.house_id
        left join sys_dict_data f on f.dict_type = a.engin_type and f.dict_value = a.engin_classification
        where a.id in  <foreach collection="idList" item="name" open="(" separator="," close=")">
        #{name}
    </foreach>
        order by a.update_info_time desc
    </select>
<select id="getObtainRoomcCompletionInformationList" resultType="com.ruoyi.zdsz.domain.vo.roomStatusVo">
    select
    a.area_id,
    a.building_id,
    a.unit_id ,
    CONCAT(bd.name,'-',c.name) AS unitName,
    a.NAME AS roomName,
    a.id AS roomId,
    b.engin_type AS enginCycle,
    IFNULL( b.completion_status,
    IFNULL((select '未安装' from z_engin_not_installed ni
    where ni.house_id = a.id
    <if test="enginType != null and enginType != ''">
        and ni.engin_type=#{enginType}
    </if>
    LIMIT 1),'未施工')) AS roomStatus,
    b.create_time,
    CAST(CONCAT( REPLACE ( bd.NAME, '栋', '' ),REPLACE ( c.NAME, '单元', '' )) AS SIGNED) AS unitOrder
    FROM
    z_house a
    LEFT JOIN z_engineering_civil b ON a.id = b.house_id AND b.engin_cycle=#{enginCycle}
    <if test="enginType != null and enginType != ''">
        and b.engin_type=#{enginType}
    </if>
    LEFT JOIN z_unit c ON c.id=a.unit_id
    LEFT JOIN z_building bd ON bd.id=a.building_id
    WHERE
         a.del_flag=0
    <if test="areaId != null and areaId != ''">
        and a.area_id = #{areaId}
    </if>
    <if test="buildingId != null and buildingId != ''">
        and a.building_id = #{buildingId}
    </if>
    <if test="unitId != null and unitId != ''">
        and a.unit_id = #{unitId}
    </if>
    order by
    unitOrder,
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(a.`name`,'）',''),'（',''),')',''),'(',''),'号','') ASC
</select>
    <select id="getAreaCompletionInformationList"  resultType="com.ruoyi.zdsz.domain.vo.areaWillDoneVo">
        SELECT
        za.id as areaId,
        za.NAME,
        IFNULL( za.scale, 0 ) AS scale,
        IFNULL( ze.completion_status,
        IFNULL((select '未安装' from z_engin_not_installed ni
        where ni.house_id = zh.id
        <if test="enginType != null and enginType != ''">
            and ni.engin_type=#{enginType}
        </if>
        LIMIT 1),'未施工')) AS roomStatus
        FROM
        z_house zh
        LEFT JOIN z_area za ON za.id = zh.area_id
        LEFT JOIN z_engineering_civil ze ON zh.id = ze.house_id AND ze.del_flag = '0' AND ze.engin_cycle=#{enginCycle}
        <if test="enginType != null and enginType != ''">
            and ze.engin_type=#{enginType}
        </if>
        WHERE
        zh.del_flag = 0
          AND zh.area_id in
        <foreach collection="idList" item="name" open="(" separator="," close=")">
            #{name}
        </foreach>
        ORDER BY za.id ASC
    </select>
    <select id="getAreaCompletionInformationList2"  resultType="com.ruoyi.zdsz.domain.vo.areaWillDoneVo">
        SELECT
        za.id as areaId,
        za.NAME,
        IFNULL( za.scale, 0 ) AS scale,
        IFNULL( ze.completion_status,
        IFNULL((select '未安装' from z_engin_not_installed ni
        where ni.house_id = zh.id
        <if test="enginType != null and enginType != ''">
            and ni.engin_type=#{enginType}
        </if>
        LIMIT 1),'未施工')) AS roomStatus
        FROM
        z_house zh
        LEFT JOIN z_area za ON za.id = zh.area_id
        LEFT JOIN z_engineering_civil ze ON zh.id = ze.house_id AND ze.del_flag = '0' AND ze.engin_cycle=#{enginCycle}
        <if test="enginType != null and enginType != ''">
            and ze.engin_type=#{enginType}
        </if>
        WHERE
        zh.del_flag = 0
        ORDER BY za.id ASC
    </select>
    <select id="getsumStatus" resultType="java.util.Map">
        select
            MAX(CASE aas.statu WHEN '未施工' THEN aas.num ELSE 0 END ) weishigong,
            MAX(CASE aas.statu WHEN '施工中' THEN aas.num ELSE 0 END ) shigongzhong ,
            MAX(CASE aas.statu WHEN '竣工' THEN aas.num ELSE 0 END ) jungong
        FROM (
                 SELECT

                     al.completion_status AS statu,
                     COUNT(ISNULL(al.completion_status)) AS num
                 FROM (
                          SELECT
                              IF(ISNULL(zec.completion_status),'未施工',zec.completion_status) completion_status
                          FROM z_house zh
                                   LEFT JOIN z_engineering_civil zec on zec.house_id = zh.id and zec.del_flag = '0'
                          WHERE zh.area_id IN (SELECT id FROM z_area za WHERE za.district = #{district} and za.del_flag = '0')
                      ) al GROUP BY al.completion_status) aas
    </select>
    <select id="getHouseMessage" resultType="com.ruoyi.zdsz.domain.vo.areaMessageVo">
        SELECT
            a.`name` AS houseId,
            b.`name` AS unitId,
            c.`name` AS buildingId,
            d.name AS areaId,
            e.dict_label AS district
        FROM
            z_house a
                LEFT JOIN z_unit b ON a.unit_id=b.id
                LEFT JOIN z_building c ON a.building_id=c.id
                LEFT JOIN z_area d ON a.area_id=d.id
                LEFT JOIN sys_dict_data e ON e.dict_value=d.district
        WHERE
            a.id = #{id}
 </select>
    <select id="getAreaMessage" resultType="com.ruoyi.zdsz.domain.vo.areaMessageVo">
        SELECT
            a.`name` AS buildingId,
            d.name AS areaId,
            e.dict_label AS district
        FROM
            z_building a
                LEFT JOIN z_area d ON a.area_id=d.id
                LEFT JOIN sys_dict_data e ON e.dict_value=d.district
        WHERE
            a.id = #{id}
    </select>
    <select id="getAreaName" resultType="com.ruoyi.zdsz.domain.vo.areaMessageVo">
        SELECT
            a.`name` AS areaId,
            e.dict_label AS district
        FROM
            z_area a
                LEFT JOIN sys_dict_data e ON e.dict_value = a.district
        WHERE
            a.id = #{id}
    </select>
    <select id="getTableNames" resultType="String">
        SELECT
            table_name as tableName
        FROM
            information_schema.TABLES
        WHERE
            table_name like CONCAT(#{tableName},'%') GROUP BY table_name
    </select>
    <select id="getsumId" resultType="com.ruoyi.zdsz.domain.bo.ZEngineeringInfoBo">
        SELECT
        *
        FROM
        z_engineering_info
        WHERE
            1=1
        <if test="createTime != null and createTime != ''">
            and update_info_time between #{createTime[0]} and #{createTime[1]}
        </if>
        <if test="UserName != null and UserName != ''">
        and create_by like CONCAT('%',#{UserName},'%')
        </if>
        <foreach collection="tableNames" item="name">
            union
            SELECT
            *
            FROM
            ${name}
            WHERE
            1=1
            <if test="createTime != null and createTime != ''">
              and  update_info_time between #{createTime[0]} and #{createTime[1]}
            </if>
            <if test="UserName != null and UserName != ''">
                and create_by like CONCAT('%',#{UserName},'%')
            </if>
        </foreach>

    </select>
    <select id="getexportList" resultType="com.ruoyi.zdsz.domain.vo.ZEngineeringCivilExportVo">
        select * from (
            SELECT
            a.id,
            (SELECT dict_label FROM `sys_dict_data` WHERE dict_type = 'district' AND dict_value = a.district) AS district,
            g.`name` AS area_id,
            h.`name` AS building_id,
            i.`name` AS unit_id,
            j.`name` AS house_id,
            (SELECT dict_label FROM `sys_dict_data` WHERE dict_type = 'community_source' AND dict_value = j.`source`) AS source,
            k.dict_label AS engin_classification,
            CASE
            a.engin_type
            WHEN 'new_built' THEN
            '新建' ELSE '旧改'
            END AS engin_type,
            a.completion_status,
            b.type,
            c.construct_addre,
            c.construct_according_drawings,
            c.segmented_compression_qualified,
            c.backfill_time,
            c.construct_phone,
            c.construct_time,
            c.head_name,
            c.head_phone,
            d.visit_type,
            d.corrosion_level AS corrosion_level,
            n.name as self_closing_valve_type,
            m.name as brand,
            e.`name` AS material_quality,
            f.`name` AS specifications,
            d.number,
            c.update_info_time as updateInfoTime,
            c.create_by as createBy,
            c.remark,
            d.remark as remarkd
            FROM
                z_engineering_civil a
            LEFT JOIN z_engineering_node b ON a.id = b.civli_id
            LEFT JOIN z_engineering_info c ON b.id = c.eng_info_id
            LEFT JOIN z_engineering_material d ON c.id = d.details_id
            LEFT JOIN z_engin_material_quality e ON e.id = d.material_quality
            LEFT JOIN z_engin_specifications f ON f.id = d.specifications
            LEFT JOIN z_area g ON g.id = a.area_id
            LEFT JOIN z_building h ON h.id = a.building_id
            LEFT JOIN z_unit i ON i.id = a.unit_id
            LEFT JOIN z_house j ON j.id = a.house_id
            LEFT JOIN sys_dict_data k ON k.dict_type = a.engin_type
            AND k.dict_value = a.engin_classification
            LEFT JOIN z_engin_material_quality l ON l.id = d.corrosion_level
            LEFT JOIN z_engin_material_quality m ON m.id = d.brand
            LEFT JOIN z_engin_material_quality n ON n.id = d.self_closing_valve_type
            WHERE
             a.create_time LIKE CONCAT(REPLACE(#{createTime}, '_', '-'),'%')
            <if test="p.district != null and p.district != ''">
                and a.district = #{p.district}
            </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.enginType != null and p.enginType != ''">
                and a.engin_type = #{p.enginType}
            </if>
            <if test="p.enginClassification != null and p.enginClassification != ''">
                and a.engin_classification = #{p.enginClassification}
            </if>
            <if test="p.enginCycle != null and p.enginCycle != ''">
                and a.engin_cycle = 0
            </if>
            <if test="p.beginTime != null and p.beginTime != ''">
                and  c.update_info_time between #{beginTime[0]} and #{beginTime[1]}
            </if>
            <if test="p.createBy != null and p.createBy != ''">
                and c.create_by like CONCAT(#{p.createBy},'%')
            </if>
            <foreach collection="tableNames" item="name">
                union
                SELECT
                a.id,
                (SELECT dict_label FROM `sys_dict_data` WHERE dict_type = 'district' AND dict_value = a.district) AS district,
                g.`name` AS area_id,
                h.`name` AS building_id,
                i.`name` AS unit_id,
                j.`name` AS house_id,
                (SELECT dict_label FROM `sys_dict_data` WHERE dict_type = 'community_source' AND dict_value = j.`source`) AS source,
                k.dict_label AS engin_classification,
                CASE
                a.engin_type
                WHEN 'new_built' THEN
                '新建' ELSE '旧改'
                END AS engin_type,
                a.completion_status,
                b.type,
                c.construct_addre,
                c.construct_according_drawings,
                c.segmented_compression_qualified,
                c.backfill_time,
                c.construct_phone,
                c.construct_time,
                c.head_name,
                c.head_phone,
                d.visit_type,
                d.corrosion_level AS corrosion_level,
                n.name as self_closing_valve_type,
                m.name as brand,
                e.`name` AS material_quality,
                f.`name` AS specifications,
                d.number,
                c.update_info_time as updateInfoTime,
                c.create_by as createBy,
                c.remark,
                d.remark as remarkd
                FROM
                z_engineering_civil a
                LEFT JOIN z_engineering_node_${name} b ON a.id = b.civli_id
                LEFT JOIN z_engineering_info_${name} c ON b.id = c.eng_info_id
                LEFT JOIN z_engineering_material_${name} d ON c.id = d.details_id
                LEFT JOIN z_engin_material_quality e ON e.id = d.material_quality
                LEFT JOIN z_engin_specifications f ON f.id = d.specifications
                LEFT JOIN z_area g ON g.id = a.area_id
                LEFT JOIN z_building h ON h.id = a.building_id
                LEFT JOIN z_unit i ON i.id = a.unit_id
                LEFT JOIN z_house j ON j.id = a.house_id
                LEFT JOIN sys_dict_data k ON k.dict_type = a.engin_type
                AND k.dict_value = a.engin_classification
                LEFT JOIN z_engin_specifications l ON l.id = d.corrosion_level
                LEFT JOIN z_engin_material_quality m ON m.id = d.brand
                LEFT JOIN z_engin_material_quality n ON n.id = d.self_closing_valve_type
                WHERE
                 a.create_time LIKE CONCAT(REPLACE(#{name}, '_', '-'),'%')
                <if test="p.district != null and p.district != ''">
                    and a.district = #{p.district}
                </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.enginType != null and p.enginType != ''">
                    and a.engin_type = #{p.enginType}
                </if>
                <if test="p.enginClassification != null and p.enginClassification != ''">
                    and a.engin_classification = #{p.enginClassification}
                </if>
                <if test="p.enginCycle != null and p.enginCycle != ''">
                    and a.engin_cycle = 0
                </if>
                <if test="p.beginTime != null and p.beginTime != ''">
                    and  c.update_info_time between #{beginTime[0]} and #{beginTime[1]}
                </if>
                <if test="p.createBy != null and p.createBy != ''">
                    and c.create_by like CONCAT(#{p.createBy},'%')
                </if>
            </foreach>
        ) z
        ORDER BY
        z.area_id,
        CONVERT ( REPLACE ( z.building_id, '栋', '' ), UNSIGNED ),
        CONVERT ( REPLACE ( z.unit_id, '单元', '' ), UNSIGNED ),
        CONVERT ( REPLACE ( z.house_id, '号', '' ), UNSIGNED ) ASC
    </select>
    <select id="getTypeNumber" resultType="Long">
        SELECT
        COUNT( DISTINCT c.id)
        FROM
        (
        SELECT
        a.*
        FROM
        z_engineering_civil  a
        LEFT JOIN  z_engineering_node b ON a.id=b.civli_id
        where 1=1
        <if test="p.district != null and p.district != ''">
            and a.district = #{p.district}
        </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.enginType != null and p.enginType != ''">
            and a.engin_type = #{p.enginType}
        </if>
        <if test="p.enginClassification != null and p.enginClassification != ''">
            and a.engin_classification = #{p.enginClassification}
        </if>
        <if test="p.enginCycle != null and p.enginCycle != ''">
            and a.engin_cycle = 0
        </if>
        <if test="p.beginTime != null and p.beginTime != ''">
            and  c.update_info_time between #{beginTime[0]} and #{beginTime[1]}
        </if>
        <if test="p.type != null and p.type != ''">
            and b.type =#{p.type}
        </if>
        <foreach collection="tableNames" item="name">
            union
            SELECT
            a.*
            FROM
            z_engineering_civil  a
            LEFT JOIN  z_engineering_node_${name} b ON a.id=b.civli_id
            where 1=1
            <if test="p.district != null and p.district != ''">
                and a.district = #{p.district}
            </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.enginType != null and p.enginType != ''">
                and a.engin_type = #{p.enginType}
            </if>
            <if test="p.enginClassification != null and p.enginClassification != ''">
                and a.engin_classification = #{p.enginClassification}
            </if>
            <if test="p.enginCycle != null and p.enginCycle != ''">
                and a.engin_cycle = 0
            </if>
            <if test="p.beginTime != null and p.beginTime != ''">
                and  a.update_info_time between #{beginTime[0]} and #{beginTime[1]}
            </if>
            <if test="p.type != null and p.type != ''">
                and b.type =#{p.type}
            </if>
        </foreach>
                                                     ) as c
    </select>
</mapper>
