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

    <resultMap type="com.ruoyi.zdsz.domain.ZArea" id="ZAreaResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="scale" column="scale"/>
        <result property="releaseIs" column="release_is"/>
        <result property="releaseBy" column="release_by"/>
        <result property="district" column="district"/>
        <result property="calculateDistrict" column="calculate_district"/>
        <result property="belongingPack" column="belonging_pack"/>
        <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.ZArea">
        select *
        from z_area
        where name = #{name}
          and del_flag = '0'
        order by create_time desc
    </select>

    <select id="getArea" resultType="com.ruoyi.zdsz.domain.vo.ZAreaVo">
        select
        a.id,
        a.name,
        a.administrative,
        a.district,
        b.dict_label as districtName
        from z_area a
        left join sys_dict_data b on a.district = b.dict_value
        where a.del_flag = '0'
        and b.dict_type = 'district'
        <if test="district != null and district != ''">
            and a.district = #{district}
        </if>
        order by a.name asc
    </select>
    <select id="selectGExportsList" resultType="com.ruoyi.zdsz.domain.vo.GImportVo">
        SELECT
            za.NAME AS areaName,
            sd.dict_label AS district,
            zb.NAME AS buildName,
            zn.NAME AS unitName,
            zh.NAME AS houseName,
            zh.id_card AS idCard,
            zh.phone AS phone,
            zh.house_user_name AS houseUserName,
            zh.new_phone AS newPhone,
            za.administrative,
            sdcs.dict_label AS source
        FROM
            z_area za
                LEFT JOIN sys_dict_data sd ON sd.dict_type = 'district'
                AND za.district = sd.dict_value
                LEFT JOIN z_building zb ON zb.area_id = za.id
                AND zb.del_flag = 0
                LEFT JOIN z_unit zn ON zn.building_id = zb.id
                AND zn.del_flag = 0
                LEFT JOIN z_house zh ON zh.unit_id = zn.id
                AND zh.del_flag = 0
                LEFT JOIN sys_dict_data sdcs ON sdcs.dict_type = 'community_source'
                AND zh.source = sdcs.dict_value
        WHERE
            za.del_flag = 0
        <if test="name != null and name != ''">
            and za.name LIKE CONCAT(#{name},'%')
        </if>
        <if test="district != null and district != ''">
            and za.district = #{district}
        </if>
        <if test="administrative != null and administrative != ''">
            and za.administrative = #{administrative}
        </if>
        ORDER BY
        za.NAME,
        CONVERT ( REPLACE ( zb.NAME, '栋', '' ), UNSIGNED ),
        CONVERT ( REPLACE ( zn.NAME, '单元', '' ), UNSIGNED ),
        CONVERT ( REPLACE ( zh.NAME, '号', '' ), UNSIGNED ) ASC
    </select>
    <select id="getExport" resultType="com.ruoyi.zdsz.domain.vo.GExportsVo">
        SELECT a.name               AS areaName,
               a.district           AS district,
               a.calculate_district AS calculateDistrict,
               a.scale              AS scale,
               a.belonging_pack     AS belongingPack,
               COUNT(b.area_id) AS count
        FROM z_area a
            LEFT JOIN z_engineering_civil b
        ON (b.area_id = a.id AND b.del_flag = '0')
        WHERE a.del_flag = '0'
          AND a.calculate_district = #{calculateDistrict}
          AND a.belonging_pack = #{belongingPack}
        GROUP BY b.area_id
        order by a.create_time desc
    </select>
    <select id="getDictNameOfAffiliatedPackage" resultType="string">
        SELECT CONCAT(a.dict_label, '-工程任务单(', b.dict_label, ')') AS name
        FROM `sys_dict_data` a
                 LEFT JOIN sys_dict_data b ON (b.dict_type = 'belonging_pack' AND b.dict_value = #{belongingPack})
        WHERE a.dict_type = 'district'
          AND a.dict_value = #{calculateDistrict}
    </select>
</mapper>
