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

    <resultMap type="com.ruoyi.zdsz.domain.ZMaterialStatistics" id="ZMaterialStatisticsResult">
        <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="realityQuality" column="reality_quality"/>
        <result property="realitySpecifications" column="reality_specifications"/>
        <result property="realitySize" column="reality_size"/>
        <result property="enginCycle" column="engin_cycle"/>
        <result property="enginClassification" column="engin_classification"/>
        <result property="nodeType" column="node_type"/>
        <result property="enginType" column="engin_type"/>
        <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="getZMaterialStatisticsList" resultType="com.ruoyi.zdsz.domain.vo.ZMaterialStatisticsVo" >
    SELECT
        *
    FROM
        z_material_statistics
    WHERE
        del_flag = '0'
        <if test="p.unitId != null  and p.unitId != ''">
            and unit_id LIKE CONCAT('%',#{p.unitId},'%')
        </if>
        <if test="p.areaId != null  and p.areaId != ''">
            and area_id = #{p.areaId}
        </if>
        <if test="p.realityQuality != null  and p.realityQuality != ''">
            and reality_quality = #{p.realityQuality}
        </if>
        <if test="p.realitySpecifications != null  and p.realitySpecifications != ''">
            and reality_specifications = #{p.realitySpecifications}
        </if>
    GROUP BY
    unit_id,
    area_id,
    reality_quality,
    reality_specifications
</select>
    <select id="getZMaterialStatistics" resultType="com.ruoyi.zdsz.domain.vo.MaterialStatisticsListVo" >
        SELECT
        area.NAME as areaName,
        CASE
        a.engin_type
        WHEN 'old_renovation' THEN
        '旧改' ELSE '新建'
        END AS engin_type,
        dd.dict_label as engin_classification,
        mq.NAME as reality_quality,
        s.NAME as reality_specifications,
        IF
        ( ISNULL( mr.estimated_size ), 0, mr.estimated_size ) as estimated_size,
        sum( a.reality_size ) reality_size
        FROM
        z_material_statistics a
        LEFT JOIN sys_dict_data dd ON a.engin_classification = dd.dict_value
        LEFT JOIN z_engin_material_quality mq ON mq.id = a.reality_quality
        LEFT JOIN z_engin_specifications s ON s.id = a.reality_specifications
        LEFT JOIN z_engineering_materials_resets mr ON (mr.material_quality = a.reality_quality
        AND mr.estimated_specifications = a.reality_specifications AND mr.del_flag=0)
        LEFT JOIN z_area area ON area.id = a.area_id
        WHERE
        1=1
        <if test="areaId != null  and areaId != ''">
            and a.area_id = #{areaId}
        </if>
        <if test="realityQuality != null  and realityQuality != ''">
            and a.reality_quality = #{realityQuality}
        </if>
        <if test="realitySpecifications != null  and realitySpecifications != ''">
            and a.reality_specifications = #{realitySpecifications}
        </if>
        <if test="enginCycle != null  and enginCycle != ''">
            and a.engin_cycle = #{enginCycle}
        </if>
        <if test="enginClassification != null  and enginClassification != ''">
            and a.engin_classification = #{enginClassification}
        </if>
        <if test="enginType != null  and enginType != ''">
            and a.engin_type = #{enginType}
        </if>
        group by reality_quality,reality_specifications
    </select>

</mapper>
