<?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="org.springblade.modules.business.rtumanage.mapper.RtuManageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="rtuStatusResultMap" type="org.springblade.modules.business.rtumanage.entity.RtuStatusEntity">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
        <result column="rtu_code" property="rtuCode"/>
        <result column="run_status_id" property="runStatusId"/>
        <result column="last_up_time" property="lastUpTime"/>
    </resultMap>

    <!-- 通用查询映射结果 -->
    <resultMap id="rtuStatusVOResultMap" type="org.springblade.modules.business.rtumanage.vo.RtuStatusVO">
        <result column="id" property="id"/>
        <result column="create_user" property="createUser"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>

        <result column="run_status_id" property="runStatusId"/>
        <result column="last_up_time" property="lastUpTime"/>

        <result column="rtu_code" property="rtuCode"/>
        <result column="rtu_name" property="rtuName"/>
        <result column="lng" property="lng"/>
        <result column="lat" property="lat"/>
        <result column="org_id" property="orgId"/>
        <result column="project_id" property="projectId"/>
        <result column="sim_id" property="simId"/>
        <result column="rtu_kinds" property="rtuKinds"/>
        <result column="remark" property="remark"/>
        <result column="location_desc" property="locationDesc"/>
        <result column="remark" property="remark"/>

        <result column="project_name" property="projectName"/>
        <result column="org_name" property="orgName"/>

        <result column="is_rain" property="isRain"/>
        <result column="is_river" property="isRiver"/>
        <result column="is_res" property="isRes"/>

        <result column="drp" property="drp"/>
        <result column="rz" property="rz"/>
        <result column="z" property="z"/>
    </resultMap>


    <select id="selectPage" resultMap="rtuStatusVOResultMap">
        SELECT
        d.*, n.run_status_id,n.last_up_time,p.project_name ,d2.dept_name AS org_name,rain.drp,rsvr.rz,river.z
        FROM
        rtu_info d
        LEFT JOIN  rtu_status n ON n.rtu_code = d.rtu_code
        LEFT JOIN  project_info p ON d.project_id = p.id
        LEFT JOIN  blade_dept d2 ON d.org_id = d2.id
        LEFT JOIN  data_att_st_prec_cache rain ON rain.rtu_code = d.rtu_code
        LEFT JOIN  data_att_st_res_ware_cache rsvr ON rsvr.rtu_code = d.rtu_code
        LEFT JOIN  data_att_st_waco_ware_cache river ON river.rtu_code = d.rtu_code
        WHERE
        d.is_deleted = 0

        <if test="rtuInfoEntity.rtuName!=null">
            and d.rtu_name like concat(concat('%', #{rtuInfoEntity.rtuName}), '%')
        </if>
        <if test="rtuInfoEntity.rtuCode!=null">
            and d.rtu_code like concat(concat('%', #{rtuInfoEntity.rtuCode}), '%')
        </if>
        <if test="rtuInfoEntity.orgId!=null">
            and d.org_id = #{rtuInfoEntity.orgId}
        </if>
        <if test="rtuInfoEntity.projectId!=null ">
            and d.project_id = #{rtuInfoEntity.projectId}
        </if>

        order by n.run_status_id desc,d.rtu_code asc
    </select>
</mapper>
