<?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.casic.missiles.modular.system.dao.BusConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.modular.system.model.BusConfig">
        <id column="ID" property="id"/>
        <result column="DEVICE_ID" property="deviceId"/>
        <result column="FRAMECONTENT" property="framecontent"/>
        <result column="SENSORID" property="sensorid"/>
        <result column="SENSORCODE" property="sensorcode"/>
        <result column="STATUS" property="status"/>
        <result column="ATTEMPTSMAX" property="attemptsmax"/>
        <result column="ATTEMPTSCURRENT" property="attemptscurrent"/>
        <result column="SENDTIME" property="sendtime"/>
        <result column="WRITETIME" property="writetime"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID AS id, DEVICE_ID AS "deviceId", FRAMECONTENT AS framecontent, SENSORID AS sensorid, SENSORCODE AS sensorcode, STATUS AS status, ATTEMPTSMAX AS attemptsmax, ATTEMPTSCURRENT AS attemptscurrent, SENDTIME AS sendtime, WRITETIME AS writetime
    </sql>

    <sql id="Query_Column_List">
        c.ID AS "id", c.DEVICE_ID AS "deviceId", c.FRAMECONTENT AS "framecontent",
         c.SENSORID AS "sensorid", c.SENSORCODE AS "sensorcode", c.STATUS AS "status",
          c.ATTEMPTSMAX AS "attemptsmax", c.ATTEMPTSCURRENT AS "attemptscurrent",
          c.SENDTIME AS "sendtime", c.WRITETIME AS "writetime",s.id AS "deptid",
          s.full_name AS "deptName",a.WELL_CODE AS "wellCode",d.DEVCODE AS "devcode",
          d.DEVICE_NAME AS "deviceName",d.DEVICE_TYPE AS "deviceType",
          t.TYPE_NAME AS "deviceTypeName"
    </sql>

    <sql id="keywords_sql" databaseId="oracle">
        CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="mysql">
        CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="pgsql">
        CONCAT('%',#{keywords},'%')
    </sql>


    <select id="getConfigList" resultType="map"
            parameterType="com.baomidou.mybatisplus.extension.plugins.pagination.Page">
        select
        <include refid="Query_Column_List"/>
        FROM bus_config c LEFT JOIN bus_device_well b ON
        c.DEVICE_ID = b.DEVICE_ID LEFT JOIN bus_well_info a ON
        b.WELL_ID=a.ID LEFT JOIN sys_dept s ON
        a.DEPTID=s.ID LEFT JOIN bus_device d ON
        d.ID=c.DEVICE_ID LEFT JOIN base_device_type t ON
        d.DEVICE_TYPE=t.ID
        where d.VALID='1' AND b.VALID='1' AND a.VALID='1'
        AND c.sensorid =#{sensorid}
        <if test="keywords != null and keywords !=''">
            and d.DEVCODE like
            <include refid="keywords_sql"/>
        </if>
        <if test="deptid != null and deptid !=''">
            and s.ID =to_number(#{deptid}, '999999999999999999999')
        </if>
        ORDER by writetime DESC
    </select>

    <select id="getConcenCodeByDevId" resultType="String">
        select
        b.CONCENCODE as "concenCode"
        FROM bus_concentrator b RIGHT JOIN bus_device_concentrator c ON b.id=c.CONCEN_ID
        where c.ACTIVE='1' and b.VALID='1'
        and c.DEVICE_ID =#{devID}
        limit 1
    </select>

    <select id="getWellDeepByDevId" resultType="String">
        select
        w.DEEP as "depth"
        FROM bus_well_info w RIGHT JOIN bus_device_well c ON w.id=c.WELL_ID
        WHERE c.VALID='1' AND w.VALID='1'
        and c.DEVICE_ID =#{devID}
        limit 1
    </select>


</mapper>
