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

    <resultMap type="com.ruoyi.zdsz.domain.ZUnit" id="ZUnitResult">
        <result property="id" column="id"/>
        <result property="areaId" column="area_id"/>
        <result property="buildingId" column="building_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.ZUnit">
        select * from z_unit
        where name = #{name} and building_id = #{buildingId}
          and del_flag = '0'
        order by id
            LIMIT 1
    </select>

    <select id="getUnitList" resultType="com.ruoyi.zdsz.domain.vo.ZUnitVo">
        select a.id as id,
        a.name as name
        from z_unit a
        where a.del_flag = '0'
        <if test="buildingId != null and buildingId != ''">
            and a.building_id = #{buildingId}
        </if>
        order by CONVERT(REPLACE(a.name,'单元',''),UNSIGNED) asc
    </select>

    <select id="getPageList" resultType="com.ruoyi.zdsz.domain.vo.ZUnitVo">
        select a.id as id,
        a.name as name,
        a.remark as remark,
        a.source,
        b.name as areaName,
        c.name as buildingName
        from z_unit a
        left join z_area b on b.id = a.area_id
        left join z_building c on c.id = a.building_id
        <where>
            a.del_flag = '0'
            and b.del_flag = '0'
            and c.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>
        </where>
        order by b.name,CONVERT(REPLACE(c.name,'栋',''),UNSIGNED),CONVERT(REPLACE(a.name,'单元',''),UNSIGNED) asc
    </select>

</mapper>
